Question

I'm trying to create a simple cell shader effect, and it involves back and front face culling. But so far, my culling results are rather odd.

Setup OpenGL source code:

// init matrices
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 800 / 600, 0.01f, 150);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// enable opengl caps
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
// cell shading effects
glLineWidth(3.5f);
// setup display list
pDList = glGenLists(1);
glNewList(pDList, GL_COMPILE);
    glBegin(GL_QUADS);
        // front
        glNormal3f(0, 0, 1);
        glVertex3f(-0.25f, -0.5f, -3);
        glVertex3f(0.25f, -0.5f, -3);
        glVertex3f(0.25f, -1, -3);
        glVertex3f(-0.25f, -1, -3);
        // back
        glNormal3f(0, 0, -1);
        glVertex3f(-0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -1, -3.5f);
        glVertex3f(-0.25f, -1, -3.5f);
        // top 
        glNormal3f(0, 1, 0);
        glVertex3f(-0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -0.5f, -3);
        glVertex3f(-0.25f, -0.5f, -3);
        // bottom
        glNormal3f(0, -1, 0);
        glVertex3f(-0.25f, -1, -3.5f);
        glVertex3f(0.25f, -1, -3.5f);
        glVertex3f(0.25f, -1, -3);
        glVertex3f(-0.25f, -1, -3);
        // right
        glNormal3f(1, 0, 0);
        glVertex3f(0.25f, -0.5f, -3);
        glVertex3f(0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -1, -3.5f);
        glVertex3f(0.25f, -1, -3);
        // left
        glNormal3f(-1, 0, 0);
        glVertex3f(-0.25f, -0.5f, -3);
        glVertex3f(-0.25f, -0.5f, -3.5f);
        glVertex3f(-0.25f, -1, -3.5f);
        glVertex3f(-0.25f, -1, -3);
    glEnd();
glEndList();

Render source code:

// clear
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.75f, 0.75f, 0.75f, 1);
// reset matrix
glLoadIdentity();
// draw cube
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCullFace(GL_BACK);
glColor3i(0, 0, 0);
glCallList(pDList);

This is what I got:

enter image description here

For some reason it's only drawing the right side of the cube. Weird. I changed the glCullFace(GL_BACK); to glCullFace(GL_FRONT); in my render function. I got this:

enter image description here

Now it's drawing every side? It seems to think that the right side of the cube is both back and front facing, and the rest of the cube is facing back. I also get this same result shown in the picture above when i have culling disabled.

Any other relevant info I can think of is that I'm using SDL, and that I'm sure this isn't a graphics card issue, as I've ran this on two different computers, each yielding the same result.

I just started to learn how to set normals for a primitive so chances are this is a big n00b mistake, or some dumb error. I'd be a millionaire if I got a penny every time I did the most thoughtless obvious mistake and have been stuck on it for hours.

So, in a nutshell, what have I done wrong, and how to I get the proper faced sides to cull or not cull in the proper way?

Was it helpful?

Solution

If you look at each triangle from the vector of its minus-normal you can define the order in which your vertices are specified (it is called winding): clockwise or counterclockwise.

You have to be consistent with it and specify all vertices in either CW or CCW winding to make the culling work correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top