绘制多维数据集时出现奇怪的OpenGL行为

时间:2012-06-13 05:09:43

标签: c opengl 3d glut depth-testing

当我使用此代码绘制多维数据集时

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();

我得到一张奇怪的画作:

img1

img2

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

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

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

1 个答案:

答案 0 :(得分:3)

通过添加:

解决了我自己的问题
glEnable(GL_DEPTH_TEST); 

到我的代码。