Depth Test(?) help
I've written a basic OpenGL app that has a camera and 2 quads drawn. My problem is that I don't think depth testing is working properly, or perhaps I have something else flawed.
In my scene, I have a red quad and a green quad. The red quad is in front of the green quad (when facing down Z from 0,0,0).
(Default View - Looking Down Z)
![[Image: idg_01.jpg]](http://www.hunterinteractive.com/personal/idg_01.jpg)
As you can see in this image, the green quad appears to be in front of the red quad. This shouldn't be so.
(Perspective View)
![[Image: idg_02.jpg]](http://www.hunterinteractive.com/personal/idg_02.jpg)
Again, the green quad appears in front of the red quad.
(Opposite View - Looking Up Z)
![[Image: idg_03.jpg]](http://www.hunterinteractive.com/personal/idg_03.jpg)
Now that I look at everything from the opposite side, the red quad appears behind the green quad as it should.
Below is related code:
Can someone please tell me what I'm doing wrong?
Thanks for any help you can offer.
In my scene, I have a red quad and a green quad. The red quad is in front of the green quad (when facing down Z from 0,0,0).
(Default View - Looking Down Z)
![[Image: idg_01.jpg]](http://www.hunterinteractive.com/personal/idg_01.jpg)
As you can see in this image, the green quad appears to be in front of the red quad. This shouldn't be so.
(Perspective View)
![[Image: idg_02.jpg]](http://www.hunterinteractive.com/personal/idg_02.jpg)
Again, the green quad appears in front of the red quad.
(Opposite View - Looking Up Z)
![[Image: idg_03.jpg]](http://www.hunterinteractive.com/personal/idg_03.jpg)
Now that I look at everything from the opposite side, the red quad appears behind the green quad as it should.
Below is related code:
Code:
-(void)prepareOpenGL
{
long swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; // set to vbl sync
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); //ive tried GL_LESS, and GL_GREATER also
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glPolygonOffset(1.0f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0);
}
-(void)drawScene
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//...setup view...
glPushMatrix();
glBegin(GL_QUADS);
//fr
glColor3f(1.0,0.0,0.0);
glVertex3d(-1.0,-1.0,1.0);
glVertex3d(1.0,-1.0,1.0);
glVertex3d(1.0,1.0,1.0);
glVertex3d(-1.0,1.0,1.0);
//ba
glColor3f(0.0,1.0,0.0);
glVertex3d(-1.0,-1.0,-1.0);
glVertex3d(1.0,-1.0,-1.0);
glVertex3d(1.0,1.0,-1.0);
glVertex3d(-1.0,1.0,-1.0);
glEnd();
glPopMatrix();
glFlush();
}Can someone please tell me what I'm doing wrong?
Thanks for any help you can offer.
You need to specify NSOpenGLPFADepthSize, 16 in your pixel format.
Oops I forgot to include my pixel format in my first post:
16 is a valid value for depth size right?
Code:
NSOpenGLPixelFormatAttribute attributes [] = {
//NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer, // double buffered
NSOpenGLPFAAccelerated,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)16, // 16 bit color buffer
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16, // 16 bit depth buffer
(NSOpenGLPixelFormatAttribute)nil
};16 is a valid value for depth size right?
Well, I think I may know what the problem is. I'm using Apple's CustomOpenGLView (verbatim) and my own subclass of the view. Oddly, CustomOpenGLView is a subclass of NSView (as opposed to NSOpenGLView). Perhaps someone could take a look at it to see if everything is up to par?
Project File:
ftp://ftp.apple.com/developer/Sample_Cod...OpenGL.sit
Project Page:
http://developer.apple.com/samplecode/Sa...OpenGL.htm
Project File:
ftp://ftp.apple.com/developer/Sample_Cod...OpenGL.sit
Project Page:
http://developer.apple.com/samplecode/Sa...OpenGL.htm
That seems like it should work.
As a quick sanity check, can you get the depth buffer size back from the context just before you draw to it?
As a quick sanity check, can you get the depth buffer size back from the context just before you draw to it?
Ahh yes. GL_DEPTH_RANGE appears to be 0 before I draw my scene...so this is the problem. Apple's code in BasicOpenGLView (subclass of CustomOpenGLView [subclass of NSView]: this is what I modeled my custom view after) appears to be messed. For some reason, it's not getting/asking for the pixel format. Hmm.
How come you're using glPolygonOffset? I've never used it myself, I'm curious about it.
AnotherJake: I've never used it either, I just figured I'd throw it in there since it's in Apple's code. I have no clue what it does either.
Anyway, it turns out I didn't have my view set up properly in InterfaceBuilder and as a result, the pixelFormat function wasn't getting called. Now that I got it set up the right, I get nothing by white in the view (using the same pixelFormat above). So from this, I can conclude that when I pass an EMPTY pixelFormat to my context, everything is normal except that there is no depth testing. But if I pass a PROPER pixelFormat to my context, I get nothing but white, even though the clear color is black. Any ideas?
OSC: When I call glGetIntegerv on GL_DEPTH_BITS (before I fixed the IB thing: when the context was getting an empty pixelFormat), I would get 0. Now that I am passing a proper pixelFormat to the context, I get 16 (as I should), but again, everything is white.
Anyway, it turns out I didn't have my view set up properly in InterfaceBuilder and as a result, the pixelFormat function wasn't getting called. Now that I got it set up the right, I get nothing by white in the view (using the same pixelFormat above). So from this, I can conclude that when I pass an EMPTY pixelFormat to my context, everything is normal except that there is no depth testing. But if I pass a PROPER pixelFormat to my context, I get nothing but white, even though the clear color is black. Any ideas?
OSC: When I call glGetIntegerv on GL_DEPTH_BITS (before I fixed the IB thing: when the context was getting an empty pixelFormat), I would get 0. Now that I am passing a proper pixelFormat to the context, I get 16 (as I should), but again, everything is white.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| depth test feedback | honkFactory | 6 | 3,098 |
Feb 20, 2003 08:12 PM Last Post: OneSadCookie |
|

