所以这真的很奇怪,我在 ubuntu 上,当我尝试运行这个示例程序时,我无法让窗口正确显示。如果我将它切换到 glFlush 并使用 GLUT_SINGLE 而不是 GLUT_DOUBLE 那么它工作得很好。发生这种情况的原因是什么?
#include <iostream>
#include <GL/glut.h>
using namespace std;
// Initialization routine.
void setup(void)
{
glEnableClientState(GL_VERTEX_ARRAY);
glClearColor(1.0, 1.0, 1.0, 0.0);
}
// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 0.0);
glLoadIdentity();
gluLookAt(0,0,0,0,0,0,0,1,0);
glColor3f(1,0,1);
glutSolidCube(1);
glutSwapBuffers();
//glFlush();
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (float)w/(float)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
//glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Test");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}
请您参考如下方法:
不要试探命运,每次通过显示回调重置矩阵:
#include <GL/glut.h>
void drawScene()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(60, w / h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1,1,1,0,0,0,0,1,0);
glColor3f(1,0,1);
glutSolidCube(1);
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Test");
glutDisplayFunc(drawScene);
glutMainLoop();
return 0;
}
此外,在 gluLookAt()
中设置 eye
和 center
点到相同的位置是Bad (参见 F
和 f
向量的计算)。