OpenGL's odd cube behavior

When I draw a cube with this code

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

I get an odd cude drawing:

img1

and

img2

Even without gluLookAt (), I still get an odd pattern.

It is strange that I used the same code in the OpenGL project on python and did not experience any problems with it. So what is the error C?

It seems that the first 2 boxes (green and orange) are not drawn at all.

+5
source share
1 answer

Just solved my own problem by adding:

glEnable(GL_DEPTH_TEST); 

into my code.

+3
source

All Articles