Depth Buffer woes in OpenGL ES 2.0
Hi,
I recently started a little graphics project in OpenGL ES 2.0 for the iPad.
I began with Xcode's OpenGL ES template with Xcode 3.2. I noticed that they do not have a depth buffer by default, so I added one.
At the moment I'm loading models and displaying them. Without the depth buffer the models display, just with triangles out of place(as expected). Like so:
![[Image: screenshot20100225at707.png]](http://img195.imageshack.us/img195/8868/screenshot20100225at707.png)
But, if I add the depth buffer my screen renders completely white (And, my clear color is NOT white). Does anyone have an ideas of what might cause this?
Here is what I've added for my depth buffer.
In the ES2Renderer.m init function:
IIn the ES2Renderer.m resizeFromLayer function:
And, I making sure to enable depth testing, and front face culling and clearing the depth buffer:
I recently started a little graphics project in OpenGL ES 2.0 for the iPad.
I began with Xcode's OpenGL ES template with Xcode 3.2. I noticed that they do not have a depth buffer by default, so I added one.
At the moment I'm loading models and displaying them. Without the depth buffer the models display, just with triangles out of place(as expected). Like so:
![[Image: screenshot20100225at707.png]](http://img195.imageshack.us/img195/8868/screenshot20100225at707.png)
But, if I add the depth buffer my screen renders completely white (And, my clear color is NOT white). Does anyone have an ideas of what might cause this?
Here is what I've added for my depth buffer.
In the ES2Renderer.m init function:
Code:
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);IIn the ES2Renderer.m resizeFromLayer function:
Code:
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);And, I making sure to enable depth testing, and front face culling and clearing the depth buffer:
Code:
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
...
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
When you call presentRenderbuffer: which renderbuffer is bound?
I might be off here but at a glance that image looks like maybe the culling is reversed??
Sorry I have not responded sooner. I thought I had e-mail for replies enabled, but apparently not.
I thought about your response arekkusu and you were right. I forgot to rebind my "colorRenderbuffer," so I was rendering my depth buffer. That is why everything was white.
So, I added this call after binding my depth buffer in resizeFromLayer:
Thank you so much for the help. I'll be sure to continue to browse idevgames, I really like the forums here.
I thought about your response arekkusu and you were right. I forgot to rebind my "colorRenderbuffer," so I was rendering my depth buffer. That is why everything was white.
So, I added this call after binding my depth buffer in resizeFromLayer:
Code:
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);Thank you so much for the help. I'll be sure to continue to browse idevgames, I really like the forums here.
Macmenace Wrote:I forgot to rebind my "colorRenderbuffer," so I was rendering my depth buffer. That is why everything was white.
Good to hear your problem is solved.
Remember that GL is a state machine, and you can query the current state at any time.
In this case, with only two renderbuffers it was simple to find the problem. What if your app was really complicated, and had 100 renderbuffers? You can ask GL about the current renderbuffer:
Code:
{
GLint rb_binding, rb_wide, rb_high, rb_format;
glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, &rb_binding);
glGetRenderbufferParameterivOES(GL RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &rb_wide);
glGetRenderbufferParameterivOES(GL RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &rb_high);
glGetRenderbufferParameterivOES(GL RENDERBUFFER_OES, GL_RENDERBUFFER_INTERNAL_FORMAT_OES, &rb_format);
printf("Current renderbuffer %d is %dx%d, format %04x\n", rb_binding, rb_wide, rb_high, rb_format);
}Then you can easily compare the data to your expectations, seeing (for example) that the internal format is depth instead of rgba.
Yes, I could see how things could get complicated with many render buffers. Thank you for the code sample. I could see something like that being a great debugging tool in the future.
And, if anyone wants to see the final result with the depth buffer working. Here it is:
And, if anyone wants to see the final result with the depth buffer working. Here it is:
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| opengl es 2.0 and the stencil buffer | NelsonMandella | 1 | 4,076 |
Sep 30, 2009 06:09 AM Last Post: arekkusu |
|
| Do you activate depth buffer when doing 2D games and opengl es? | riruilo | 13 | 7,113 |
Apr 3, 2009 11:02 AM Last Post: miq01 |
|
| Reading the depth buffer? Plus gluUnProject | Bachus | 7 | 5,683 |
Mar 5, 2009 04:29 AM Last Post: ShadoFlameX |
|
| Vertex buffer objects in OpenGL ES on iPhone | ZooMan | 5 | 6,724 |
Feb 4, 2009 07:00 PM Last Post: ZooMan |
|
| OpenGL Flip buffer sometimes fail | SunHpp | 4 | 6,098 |
Jul 25, 2008 04:03 PM Last Post: Frogblast |
|

