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.
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.
The template has changed a little in what order things are done so the depth buffer size should be set in resizeFromLayer:
A clean up of the depth buffer when the view is deallocated can also be in order.
Now the depth buffer should be in place and enabling and clearing it should work.
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:
//#define USE_DEPTH_BUFFER 0 #define USE_DEPTH_BUFFER 1
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); :
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); :
Code:
: if (depthRenderbuffer) { glDeleteRenderbuffersOES(1, &depthRenderbuffer); depthRenderbuffer = 0; } :
Code:
- (void) render { : glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); :
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