Problems with rotation and drawing order


I am just starting with OpenGL, but have a fair bit of programming knowledge in other fields.

To get used to the API, I started drawing a simple rotating cube onto the screen.

My demo program draws the cube and rotates it, but there is a very odd problem, to which I have thus been unable to find an answer:

The cube is composed by individual quad faces. As soon as the rotation goes over a certain point, it seems, that OpenGL is drawing the back face on top of the front face, resulting in some weird escheresk geometry.

I added two screenshots to this post, to further illustrate the problem.

Also, the drawing function looks like this:

<div class=“ubbcode-block”><div class=“ubbcode-header”>Click to reveal… <input type=“button” class=“form-button” value=“Show me!” onclick=“toggle_spoiler(this, ‘Yikes, my eyes!’, ‘Show me!’)” />]<div style=“display: none;”>

  • (void) draw:(int) delta_t{
    glClearColor(0, 0, 0, 0);
    glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
    glVertex3f(s,-1s, s); // Top Right Of The Quad (Bottom)
    s,-1s, s); // Top Left Of The Quad (Bottom)
    s,-1s,-1s); // Bottom Left Of The Quad (Bottom)
    glVertex3f( s,-1s,-1s); // Bottom Right Of The Quad (Bottom)

      glColor3f(0.0f,s,0.0f);  // Set The Color To Green
      glVertex3f( s, s,-1*s);    // Top Right Of The Quad (Top)
      glVertex3f(-1*s, s,-1*s);  // Top Left Of The Quad (Top)
      glVertex3f(-1*s, s, s);    // Bottom Left Of The Quad (Top)
      glVertex3f( s, s, s);      // Bottom Right Of The Quad (Top)
      glColor3f(s,0.0f,0.0f);          // Set The Color To Red
      glVertex3f(s,s,s);          // Top Right Of The Quad (Front)
      glVertex3f(-1*s,s,s);          // Top Left Of The Quad (Front)
      glVertex3f(-1*s,-1*s,s);          // Bottom Left Of The Quad (Front)
      glVertex3f(s,-1*s,s);          // Bottom Right Of The Quad (Front)

    glRotatef(5, 1, 1, 1);


What am I doing wrong here? Do I really need to change the order of the faces halfway through an animation? Is there maybe a better attempt to rotation than this?

Thanks for the help and kind regards,

The Raven

It looks like the depth test is not enabled. Have u enabled the depth test?


Indead, that was the problem.

At least when I code this, using GLUT, that gets rid of the problem.


Now I just have to find out, what Apple does different to GLUT, when using Cocoa; because for some reason, setting the depth test with only the glEnable call does not work inside a Cocoa view.

If anybody has an idea about that, I would be delighted to hear it.

The Raven

Are you sure you request a depth buffer at the windows creation ?
Otherwise some GL implementations can provide one even if not asked, but not the case everywhere.
Be sure to ask for everything you need, ie RGBA 8 bits per component, depth buffer 24 bits, double buffering, and maybe stencil 8 bits.