Jump to content
  • Advertisement
Sign in to follow this  
Peter Conn

OpenGL Cube sides being drawn over

This topic is 4163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya I am learning OpenGL - I used the template from a book called "Beginning OpenGL Programming" and have been using NeHe's tutorials to play around with the drawing part. At the moment I have made a rotating 3d cube, the problem is however that the quads that are being drawn later are being drawn over anything else (including the sides of the cube that are infront of them), this is what it looks like: OpenGL Cube Error In that picture the faces of the cube closest to you is yellow and is being drawn over by the other face - so you can't actually see it. Here is my drawing code, if you need any other code please say.
void OpenGLHeader::Render(){
    // Clear the screen and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    
    // This is where you edit the code to draw various things
    glTranslatef(0.0f, 0.0f, -6.0f);
    glRotatef(m_angle, 0.0f, 1.0f, 0.0f);
    
    glBegin(GL_QUADS);
        // The front face
        glColor3f(1.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        // The top face
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        // The right face
        glColor3f(1.0f, 0.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);
        // The back face
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        // The bottom face
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        // The left face
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
    glEnd();
}

Share this post


Link to post
Share on other sites
Advertisement
there is a depth buffer, which cares about drawing
when pixel is going to be drawn, it is decided whether it is not behind some previosly drawn pixel
if it is, it is ignored

to enable WRITING to depth buffer, use glDepthMask(GL_TRUE);
to enable testing (whether pixel is behind or not) use glEnable(GL_DEPTH_TEST);

Share this post


Link to post
Share on other sites
So would I put those in the main loop or in the setup part?
Actually, never mind I put it in the "Init()", thanks a lot, problem solved.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!