9

I am trying to draw a series of rectangles using OpenGL but some of the ones that should appear below a rectangle appear above it.

To enable the depth function I am using

glClearDepth(1.0f);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);

I am using gluLookAt at the begining of each draw. Here is an image of the problem. The blue rectangles are positioned with higher y values than the green rectangles.

alt text alt text

As you can see in the top image some of the green rectangles appear above the blue ones. Any idea why this is happening and what I can do to fix it? Could it have something to do with the order that I'm drawing the rectangles?

Spooky
  • 2,966
  • 8
  • 27
  • 41
DHamrick
  • 8,338
  • 9
  • 45
  • 62

2 Answers2

12

When you clear your colour buffer are you also clearing the depth buffer?

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Are you sure the image isn't correct? Some of those green squares actually
do look higher than the blue ones.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
7

Did you enable the depth buffer in whatever windowing system you are using? For example, using glut you might need to: glutInitDisplayMode(GLUT_DEPTH | ....);

Christopher
  • 8,815
  • 2
  • 32
  • 41
  • I am using qt for my windowing toolkit. I am not aware of any function like that. Do you know if there is one I should be calling simmilar to glut's function. – DHamrick Jun 25 '09 at 20:49
  • 3
    In the `QGLFormat` object, you should be able to check if the depth buffer is enabled with the `depth` and `setDepth` methods. – ravuya Jun 25 '09 at 21:16