5

当我用这段代码画一个立方体时

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(
x,    y,    z, 
x+xp, y+yp, z+zp,
0.0f, 1.0f, 0.0f);   
glBegin(GL_QUADS);            

glColor3f(0.0f,1.0f,0.0f);            
glVertex3f( 1.0f, 1.0f,-1.0f);        
glVertex3f(-1.0f, 1.0f,-1.0f);       
glVertex3f(-1.0f, 1.0f, 1.0f);       
glVertex3f( 1.0f, 1.0f, 1.0f);        

glColor3f(1.0f,0.5,0.0f);           
glVertex3f( 1.0f,-1.0f, 1.0f);        
glVertex3f(-1.0f,-1.0f, 1.0f);        
glVertex3f(-1.0f,-1.0f,-1.0f);       
glVertex3f( 1.0f,-1.0f,-1.0f);        

glColor3f(1.0f,0.0f,0.0f);            
glVertex3f( 1.0f, 1.0f, 1.0f);        
glVertex3f(-1.0f, 1.0f, 1.0f);       
glVertex3f(-1.0f,-1.0f, 1.0f);        
glVertex3f( 1.0f,-1.0f, 1.0f);        

glColor3f(1.0f,1.0f,0.0f);            
glVertex3f( 1.0f,-1.0f,-1.0f);        
glVertex3f(-1.0f,-1.0f,-1.0f);        
glVertex3f(-1.0f, 1.0f,-1.0f);        
glVertex3f( 1.0f, 1.0f,-1.0f);        

glColor3f(0.0f,0.0f,1.0f);            
glVertex3f(-1.0f, 1.0f, 1.0f);       
glVertex3f(-1.0f, 1.0f,-1.0f);        
glVertex3f(-1.0f,-1.0f,-1.0f);       
glVertex3f(-1.0f,-1.0f, 1.0f);        

glColor3f(1.0f,0.0f,1.0f);            
glVertex3f( 1.0f, 1.0f,-1.0f);        
glVertex3f( 1.0f, 1.0f, 1.0f);        
glVertex3f( 1.0f,-1.0f, 1.0f);       
glVertex3f( 1.0f,-1.0f,-1.0f);   
glEnd();

我得到一个奇怪的 cude 图:

图像1

img2

即使没有 gluLookAt(),我仍然会得到一张奇怪的图画。

奇怪的是我在 python OpenGL 项目中使用了完全相同的代码并且没有遇到任何问题。所以这似乎是一个C错误?

似乎根本没有绘制前 2 个四边形(绿色和橙色)。

4

1 回答 1

4

刚刚通过添加解决了我自己的问题:

glEnable(GL_DEPTH_TEST); 

到我的代码。

于 2012-06-13T21:05:26.137 回答