★ TouchArcade needs your help. Click here to support us on Patreon.

Enabling depth buffer in new Xcode template

10-10-2009, 05:46 AM
#1
Joined: Apr 2009
Location: Stockholm, Sweden
Posts: 19
Enabling depth buffer in new Xcode template

The project template for creating a simple iPhone OpenGL ES app was very nice in the iPhone SDK up to OS 2.2.1. One just clicked the OpenGL ES template and almost always enabled the depth buffer by changing a 0 to 1 in the EAGLView.m file.

Code:
//#define USE_DEPTH_BUFFER 0
#define USE_DEPTH_BUFFER 1
After the OS 3.0 was introduced the OpenGL ES template was changed to support OpenGL ES 2.0 and the easy way to enable the depth buffer was removed. Pitty.

There are a couple of things that has to be done to enable a depth buffer. When creating the framebuffer one need to create a depth buffer and attach it to the framebuffer. Here is the new part in the init method in ES1Renderer.m.

Code:
- (id) init
{
    :
    glGenFramebuffersOES(1, &defaultFramebuffer);
    glGenRenderbuffersOES(1, &colorRenderbuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, 
                                 GL_COLOR_ATTACHMENT0_OES, 
                                 GL_RENDERBUFFER_OES, 
                                 colorRenderbuffer);

    // New part, remember to add GLuint depthRenderbuffer to the class
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, 
                                 GL_DEPTH_ATTACHMENT_OES, 
                                 GL_RENDERBUFFER_OES, 
                                 depthRenderbuffer);
    :
The template has changed a little in what order things are done so the depth buffer size should be set in resizeFromLayer:

Code:
- (BOOL) resizeFromLayer:(CAEAGLLayer *)layer
{ 
    // Allocate color buffer backing based on the current layer size
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer];
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, 
                                    GL_RENDERBUFFER_WIDTH_OES, 
                                    &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, 
                                    GL_RENDERBUFFER_HEIGHT_OES, 
                                    &backingHeight);
    
    // New part
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, 
                             GL_DEPTH_COMPONENT16_OES, 
                             backingWidth, backingHeight);
    :
A clean up of the depth buffer when the view is deallocated can also be in order.

Code:
:
if (depthRenderbuffer) 
{
    glDeleteRenderbuffersOES(1, &depthRenderbuffer);
    depthRenderbuffer = 0;
}
:
Now the depth buffer should be in place and enabling and clearing it should work.

Code:
- (void) render
{

    :
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    :
It took a little while to figure all this out, and doing the same for the ES2Renderer.m file. Somehow I really liked the prepared solution from the previous template.

Feel free to grab a prepared project here http://www.memention.com/blog/bag/DepthBuffers.zip

I have put all changes in easy searchable comments.

Code:
// ADDED DEPTH BEGIN
:
:
// ADDED DEPTH END

Last edited by epatel; 10-10-2009 at 05:56 AM. Reason: read the rules
02-25-2010, 10:14 PM
#2
Hi,

Thank you for this tutorial. I've started playing around with an OpenGL ES 2.0 project. At the moment I'm loading models and displaying them but I forgot to add a depth buffer because Xcode no longer adds one, as you said.

I followed your guide and added the depth buffer but now it renders out completely white.

Before adding the depth buffer it looks like this:


And, afterwards it is completely white. Do you have an ideas of what might cause this?
03-30-2010, 08:06 PM
#3
Minor correction (?)

@Macmenace:

I was using an OpenGL ES1.1 project and had a similar problem when trying to add depth buffering (the screen turned solid purple.)

The solution for me was

1) in init, don't change anything from the original OGL template.
2) in resizeLayer, rather than do what the posting shows, add:

glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);


3) I also added this to dealloc:

if (depthRenderbuffer) {
glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}



I did this, and my depth buffering starting working properly.

I got this information from Apple's OpenGLES_ProgrammingGuide.pdf, p25


Hope that helps!