Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to learn opengl. I did some code for building a rectangular prism. I don't want to draw back faces so I used glCullFace(GL_BACK), glEnable(GL_CULL_FACE);. But I keep getting back faces also when viewing from front and also sometimes when rotating sides are vanishing. Can someone point me in right direction?

glPolygonMode(GL_FRONT,GL_LINE); // draw wireframe polygons
glColor3f(0,1,0); // set color green
glCullFace(GL_BACK); // don't draw back faces
glEnable(GL_CULL_FACE); // don't draw back faces
glTranslatef(-10, 1, 0); // position

     glBegin(GL_QUADS);


     // face 1
     glVertex3f(0,-1,0);
     glVertex3f(0,-1,2);
     glVertex3f(2,-1,2);
     glVertex3f(2,-1,0);

     // face 2
     glVertex3f(2,-1,2);
     glVertex3f(2,-1,0);
     glVertex3f(2,5,0);
     glVertex3f(2,5,2);

     // face 3
     glVertex3f(0,5,0);
     glVertex3f(0,5,2);
     glVertex3f(2,5,2);
     glVertex3f(2,5,0);

     // face 4
     glVertex3f(0,-1,2);
     glVertex3f(2,-1,2);
     glVertex3f(2,5,2);
     glVertex3f(0,5,2);

     // face 5
     glVertex3f(0,-1,2);
     glVertex3f(0,-1,0);
     glVertex3f(0,5,0);
     glVertex3f(0,5,2);

     // face 6
     glVertex3f(0,-1,0);
     glVertex3f(2,-1,0);
     glVertex3f(2,5,0);
     glVertex3f(0,5,0);

     glEnd();
share|improve this question
    
I'm too lazy to check all your vertices right now, but are you considering the winding order for the faces? –  melak47 Sep 25 '12 at 11:27
    
I'm not considering that. How does the order goes? Could you explain? –  BadSniper Sep 25 '12 at 17:19
add comment

2 Answers

The winding of faces 1,3,5 & 6 were clockwise in your code, so openGL rendered them the 'wrong' way around. They should have had anti-clockwise winding. OpenGL uses winding to determine which side of a face is a the front(outside facing) and which side is the back (inside facing). OpenGL doesn't know you are drawing a closed box, it treats each quad individually.

To give the correct winding, imagine you are looking at the quad (or triangle) from outside of the box you are rendering. You should list the vertices so that they form a 'circle' going anti-clockwise. In your example;

 // face 1 (clockwise)
 glVertex3f(0,-1,0);
 glVertex3f(0,-1,2);
 glVertex3f(2,-1,2);
 glVertex3f(2,-1,0);

is treated differently than;

 // face 1 (anti-clockwise)
 glVertex3f(0,-1,0);
 glVertex3f(2,-1,0);
 glVertex3f(2,-1,2);
 glVertex3f(0,-1,2);

even though the same vertices are used, the order is different. Note that vertices 2 & 4 are swapped.

to render your box correctly, your code should be; (I've fixed faces 1,3,5 & 6);

 glBegin(GL_QUADS);
 // face 1
 glVertex3f(0,-1,0);
 glVertex3f(2,-1,0);
 glVertex3f(2,-1,2);
 glVertex3f(0,-1,2);


 // face 2
 glVertex3f(2,-1,2);
 glVertex3f(2,-1,0);
 glVertex3f(2,5,0);
 glVertex3f(2,5,2);

 // face 3 
 glVertex3f(0,5,0);
 glVertex3f(0,5,2);
 glVertex3f(2,5,2);
 glVertex3f(2,5,0);

 // face 4

 glVertex3f(0,-1,2);
 glVertex3f(2,-1,2);
 glVertex3f(2,5,2);
 glVertex3f(0,5,2);

 // face 5
 glVertex3f(0,-1,2);
 glVertex3f(0,5,2);
 glVertex3f(0,5,0);
 glVertex3f(0,-1,0);

 // face 6
 glVertex3f(0,-1,0);
 glVertex3f(0,5,0);    
 glVertex3f(2,5,0);
 glVertex3f(2,-1,0);

 glEnd();
share|improve this answer
    
OpenGL also provides a API call to change the default winding order. –  danijar Apr 17 '13 at 12:51
add comment

You should double check the winding order of your vertices. You can read more about it here: http://www.opengl.org/sdk/docs/man/xhtml/glFrontFace.xml. Maybe try enabling face culling, then setting it to back. Other than that your code looks good.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.