PDA

View Full Version : Back-face culling not working correctly



Fezziwig
08-14-2012, 02:10 PM
I'm new to culling and I've followed a tutorial but in my program it doesn't seem to be culling correctly.

What's happening;
http://i.imgur.com/bIz8F.png
http://i.imgur.com/Tqlu1.png

Some source code:

glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE);
glEnable(GL_CULL_FACE);


// Top Face glColor3f(0.6, 0.6, 0.6);
glVertex3f(x, y + sz, z);
glVertex3f(x, y + sz, z + sz);
glVertex3f(x + sz, y + sz, z + sz);
glVertex3f(x + sz, y + sz, z);


// Bottom Face
glColor3f(0.4, 0.4, 0.4);
glVertex3f(x, y, z);
glVertex3f(x, y, z + sz);
glVertex3f(x + sz, y, z + sz);
glVertex3f(x + sz, y, z);


// Left Face
glColor3f(0.2, 0.2, 0.2);
glVertex3f(x, y, z);
glVertex3f(x, y + sz, z);
glVertex3f(x + sz, y + sz, z);
glVertex3f(x + sz, y, z);


// Right Face
glColor3f(1.0, 1.0, 1.0);
glVertex3f(x, y, z + sz);
glVertex3f(x, y + sz, z + sz);
glVertex3f(x + sz, y + sz, z + sz);
glVertex3f(x + sz, y, z + sz);

// Front
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x, y, z);
glVertex3f(x, y, z + sz);
glVertex3f(x, y + sz, z + sz);
glVertex3f(x, y + sz, z);


// Back
glColor3f(0.8, 0.8, 0.8);
glVertex3f(x + sz, y, z);
glVertex3f(x + sz, y, z + sz);
glVertex3f(x + sz, y + sz, z + sz);
glVertex3f(x + sz, y + sz, z);

Any help is much appreciated
Thanks,
Rowan.

Carmine
08-14-2012, 02:53 PM
Without looking at your code in detail, I'm guessing that you're not paying attention to the winding of your polygons. When viewed from the outside of the cube, the vertices of a face must be connected in counter-clockwise order. Also, try ONE cube with numbers first. Get it working. Then one cube with variables. When you have that working, you're ready to move on to hundreds and thousands of cubes.

Fezziwig
08-15-2012, 12:15 AM
Thanks, that's solved it! For future reference this is the new quads list. I worked it out by making one face at a time have red, yellow, green and blue in each corner (in that order), and then making sure they were going anti clock wise.

// Top Face glColor3f(0.7, 0.7, 0.7);
glVertex3f(x, y + sz, z);
glVertex3f(x, y + sz, z + sz);
glVertex3f(x + sz, y + sz, z + sz);
glVertex3f(x + sz, y + sz, z);


// Bottom Face
glColor3f(0.5, 0.5, 0.5);
glVertex3f(x + sz, y, z);
glVertex3f(x + sz, y, z + sz);
glVertex3f(x, y, z + sz);
glVertex3f(x, y, z);


// Left Face
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x, y, z);
glVertex3f(x, y + sz, z);
glVertex3f(x + sz, y + sz, z);
glVertex3f(x + sz, y, z);


// Right Face
glColor3f(1.0, 1.0, 1.0);
glVertex3f(x + sz, y, z + sz);
glVertex3f(x + sz, y + sz, z + sz);
glVertex3f(x, y + sz, z + sz);
glVertex3f(x, y, z + sz);


// Front
glColor3f(0.4, 0.4, 0.4);
glVertex3f(x, y, z);
glVertex3f(x, y, z + sz);
glVertex3f(x, y + sz, z + sz);
glVertex3f(x, y + sz, z);


// Back
glColor3f(0.8, 0.8, 0.8);
glVertex3f(x + sz, y + sz, z);
glVertex3f(x + sz, y + sz, z + sz);
glVertex3f(x + sz, y, z + sz);
glVertex3f(x + sz, y, z);

Carmine
08-15-2012, 09:52 AM
Thanks, that's solved it! You're welcome. BTW - there's a command called 'glFrontface' you might find interesting. It allows you to define what is meant by the 'front face' of a polygon. You can set the vertex winding to be clockwise or counter-clockwise. The default is counter-clockwise. Either way, you have to pay attention to winding.