Memention Blog

Think Deep - get some depth in the 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.

//#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.

- (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:

- (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.

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

Now the depth buffer should be in place and enabling and clearing it should work.

- (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 link

I have put all changes in easy searchable comments.

// ADDED DEPTH BEGIN
:
:
// ADDED DEPTH END
By Edward Patel, 08 Oct 2009




Show comments

Archive

Throwing pebbles12 Jul 2013
Provision this!30 Aug 2012
Break what?18 Aug 2011
That old thing04 May 2011
Removing a Step15 May 2010
Make It Work22 Feb 2010
KISS server31 Jan 2010
tig on Mac12 Dec 2009
The XML Runner31 Oct 2009
Get some software from Memention
Powered by Jekyll and Disqus.
Copyright © 2010-2013 Memention AB