Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Back-face culling not working correctly

  1. #1
    Intern Newbie
    Join Date
    Mar 2012
    Location
    Devon, England
    Posts
    39

    Back-face culling not working correctly

    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;



    Some source code:
    Code :
    glEnable(GL_DEPTH_TEST);    glDepthMask(GL_TRUE);
        glEnable(GL_CULL_FACE);

    Code :
    // 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.
    Help Me [img]<<GRAEMLIN_URL>>/smile.gif[/img]

  2. #2
    Intern Contributor
    Join Date
    Apr 2012
    Posts
    90
    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.

  3. #3
    Intern Newbie
    Join Date
    Mar 2012
    Location
    Devon, England
    Posts
    39
    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.
    Code :
    // 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);
    Help Me [img]<<GRAEMLIN_URL>>/smile.gif[/img]

  4. #4
    Intern Contributor
    Join Date
    Apr 2012
    Posts
    90
    Quote Originally Posted by Fezziwig View Post
    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.
    Last edited by Carmine; 08-15-2012 at 10:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •