Thread: OpenGL cube's faces mess up

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    OpenGL cube's faces mess up

    Can someone give me the code that draws a cube in OpenGL with each face a different colour because I did it with rectangles not polygons and the faces go through each other. Thanks alot!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Post the code you have and we may correct it. Could be the z-buffer, is it enabled?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Code:
    glPushMatrix();
                
                
                glColor3f(1.0f, 0.0f, 0.0f);
                glBegin(GL_TRIANGLE_STRIP);                            //back
                     glVertex3f(-1.0f, 0.0f, -1.0f);
                     glVertex3f(-1.0f, -1.0f, -1.0f);
                     glVertex3f(0.0f, 0.0f, -1.0f);
                     glVertex3f(0.0f, -1.0f, -1.0f);
                glEnd();
                glColor3f(1.0f, 1.0f, 0.0f);
                glBegin(GL_TRIANGLE_STRIP);                            //left
                     glVertex3f(-1.0f, 0.0f, -1.0f);
                     glVertex3f(-1.0f, -1.0f, -1.0f);
                     glVertex3f(-1.0f, 0.0f, 0.0f);
                     glVertex3f(-1.0f, -1.0f, 0.0f);
                glEnd();
                glColor3f(1.0f, 0.0f, 1.0f);
                glBegin(GL_TRIANGLE_STRIP);                            //right
                     glVertex3f(0.0f, 0.0f, -1.0f);
                     glVertex3f(0.0f, -1.0f, -1.0f);
                     glVertex3f(0.0f, 0.0f, 0.0f);
                     glVertex3f(0.0f, -1.0f, 0.0f);
                glEnd();
                glColor3f(0.0f, 1.0f, 1.0f);
                glBegin(GL_TRIANGLE_STRIP);                            //bottom
                     glVertex3f(-1.0f, -1.0f, -1.0f);
                     glVertex3f(-1.0f, -1.0f, 0.0f);
                     glVertex3f(0.0f, -1.0f, -1.0f);
                     glVertex3f(0.0f, -1.0f, 0.0f);
                glEnd();
                glColor3f(0.0f, 0.0f, 1.0f);
                glBegin(GL_TRIANGLE_STRIP);                            //top
                     glVertex3f(-1.0f, 0.0f, -1.0f);
                     glVertex3f(-1.0f, 0.0f, 0.0f);
                     glVertex3f(0.0f, 0.0f, -1.0f);
                     glVertex3f(0.0f, 0.0f, 0.0f);
                glEnd();
                glColor3f(1.0f, 0.5f, 0.5f);
                glBegin(GL_TRIANGLE_STRIP);                            //front
                     glVertex3f(-1.0f, 0.0f, 0.0f);
                     glVertex3f(-1.0f, -1.0f, 0.0f);
                     glVertex3f(0.0f, 0.0f, 0.0f);
                     glVertex3f(0.0f, -1.0f, 0.0f);
                glEnd();
                glPopMatrix();
    That's my cube. If you add some rotatef's then you'll see what I mean. And about the z-buffer, could you expand, I'm learning OpenGL and the more explaining the better!

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Add the following to your initialization routine.

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    It might not matter that much at this point, but for future reference, try calling you're glBegin once at the beginning of ALL the triangles, and glEnd and the end of ALL the triangles. Calling them for each face will eventually slow you down.

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  6. #6
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Thanks guys, I'll try the commands at initialization. I'm not at my computer right now but I'll post back if it works or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM