Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd OpenGL behavior when drawing a cube

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 end up with an odd drawing.

The weird thing is I used the exact same code in a python OpenGL project and had no trouble with it. So it seems to be a C error?

It also seems like the first 2 quads (green and orange) are not being drawn at all.

like image 435
WIll Cobb Avatar asked Oct 07 '22 13:10

WIll Cobb


1 Answers

Just solved my own problem by adding:

glEnable(GL_DEPTH_TEST); 

to my code.

like image 169
WIll Cobb Avatar answered Oct 13 '22 00:10

WIll Cobb