Depth Buffer issues.

Member
Posts: 28
Joined: 2005.04
Post: #1
I found some weird looking depth issues. When I turn on the depth buffer and set it to anything 16 or 24 I get strange aliasing artifacts. The frustrum and viewport is all set to default and the cube I am rendering in this picture to illustrate the problem is simply 1x1x1. Look at where the edges of the cube meet up and thats where you see the artifacts. It looks a lot worse when its moving because they sort of alternate in a 1D checkerboard pattern.

I am not sure if I am doing something wrong with the depth buffer, or this is an apple error, or my graphics chip error.

[Image: DepthWeirdness.png]

Also is there a way to render buffers other then the color buffer to screen so I can see what the depth buffer is seeing?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
What are the values of your near and far clipping planes? Those affect depth buffer precision. Pushing the near clipping plane out and bringing the far clipping plane in would likely help you.

- Alex Diener
Quote this message in a reply
Member
Posts: 28
Joined: 2005.04
Post: #3
ThemsAllTook Wrote:What are the values of your near and far clipping planes? Those affect depth buffer precision. Pushing the near clipping plane out and bringing the far clipping plane in would likely help you.

The viewing frustrum is at the default values. I have not altered the clipping plane from what it starts as, which I though would be able to hold a 1x1x1 cube thats only traslated -5 along the z.
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #4
Show us the code I suppose...
Quote this message in a reply
Member
Posts: 28
Joined: 2005.04
Post: #5
Code it is.. (irrelevant parts removed)

Here is my pixel format
Code:
NSOpenGLPixelFormatAttribute attrs[] =
    {
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAAccelerated,
    NSOpenGLPFAColorSize, 32,
    NSOpenGLPFADepthSize, 16,
        nil
    } ;

Here is the OpenGL stuff I turn on.
Code:
// Clear the background
    glClearColor(0.0, 0.0, 0.0, 1.0);
    
    // Enable OpenGL features
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_POLYGON_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_CULL_FACE);
    
    // Feature properties
    glShadeModel(GL_SMOOTH);              
    glCullFace(GL_BACK);
    
    // Hinting
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

And here is the very simple drawing
Code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);      
    glLoadIdentity();

    glTranslatef(0.0f, 0.0f, -5.0f);
    // Draw something of interest
    [trackball on];
    [self drawCube];
    [trackball off];
    
    glFinish();
    
    [[self openGLContext] flushBuffer];

drawCube just draws the six sides of a cube with dimensions of 1 x 1 x 1.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Relevant parts removed, too.

Where's the gluPerspective call?
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #7
NitroPye Wrote:Also is there a way to render buffers other then the color buffer to screen so I can see what the depth buffer is seeing?

Yes. You can use GL_DEPTH_COMPONENT with glReadPixels to read back the depth buffer into an array. Depending on your depth near/far planes and the geometry in your scene, it is likely that you'll want to fix up the values you get back to scale the min/max values to 0.0/1.0 for better visualization. Then you can use the array to make an e.g. luminance texture and draw a 2D quad with it.

Or, on newer hardware, you should be able to glCopyTexImage2D with GL_DEPTH_COMPONENT into a depth texture and draw it directly (no slow readback.) Use a fragment program to scale the values.

However, the easiest thing to do is to use OpenGL Profiler. Attach to your app, set a breakpoint on CGLFlushDrawable, then Views>Buffers>Depth Buffer.



NitroPye Wrote:
Code:
    NSOpenGLPFADepthSize, 16,

    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

What's GL_DEPTH_BITS? Remember that you don't necessarily get the precision you ask for in the pixel format; some hardware doesn't support 32 bit color and 16 bit depth.

You're trying to use polygon smoothing but haven't enabled blending. And polygon smoothing is incompatible with the depth buffer, so it won't do what you expect.
Quote this message in a reply
Member
Posts: 28
Joined: 2005.04
Post: #8
arekkusu Wrote:You're trying to use polygon smoothing but haven't enabled blending. And polygon smoothing is incompatible with the depth buffer, so it won't do what you expect.

Thank you this solved it. I turned off polygon smoothing, which really was not needed in the first place and now it looks great. I did not know that polygon smoothing was incompatible with the depth buffer. Makes sence now thinking about it though.

Thanks again.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Depth Buffer Question AnotherJake 10 4,712 Sep 23, 2008 01:43 PM
Last Post: AnotherJake
  problems getting the depth buffer akb825 3 4,181 Jun 2, 2006 12:51 PM
Last Post: akb825
  Drawing to the Depth Buffer unknown 12 4,290 Dec 9, 2005 02:49 PM
Last Post: OneSadCookie
  Depth Buffer / Testing basic question... WhatMeWorry 5 6,378 Nov 18, 2005 12:50 AM
Last Post: arekkusu
  Reading from the Depth Buffer Joseph Duchesne 2 3,043 Jun 16, 2005 04:51 PM
Last Post: OneSadCookie