下面的代码主要供OpenGL入门使用,有不足或者错误请指正
主函数
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("MyProgaram");
init();
glutReshapeFunc(reshape);
//绘制
glutDisplayFunc(draw);
glutIdleFunc(draw);
//
//交互
...
//
glutMainLoop();
return 0;
}
Reshape
void reshape(GLsizei w,GLsizei h)
{
width = w; height = h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLfloat)w/(GLfloat)h,0.01,50.0);
}
设置相机
float pos[3];//eye(x,y,z)
float target[3];//focal(x,y,z)
void setCamera()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(pos[0], pos[1], pos[2], target[0], target[1], target[2], 0, 1, 0);
}
纹理贴图
文字显示
2D绘制