views:

298

answers:

3

I remember running into this problem when I started using OpenGL in OS X. Eventually I solved it, but I think that was just by using glut and c++ instead of Objective-C...

The lines of code I have in init for the ES1Renderer are as follows:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

Then in the render method, I have this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

I assume I'm missing something specific to either the iPhone or ES. What other steps are required to enable the depth test?

Thanks

A: 

#define USE_DEPTH_BUFFER 1 if you're using the OpenGL ES project template. This sets up a depth buffer somewhere in EAGLView.m.

codewarrior
I think this only applies to the old setup. With the new default project, USE_DEPTH_BUFFER is not used anywhere.
Chris Cooper
A: 

You need to allocate the depth buffer itself. Allocate a new renderbuffer with the internal format DEPTH_COMPONENT16 or DEPTH_COMPONENT24, and attach it to the framebuffer object.

Frogblast
Do you mean with something like this?glGenRenderbuffersOES(DEPTH_COMPONENT24, glBindRenderbufferOES(GL_DEPTHBUFFER_OES, depthRenderbuffer);I tried to do this where the other gen and bind calls were, but neither DEPTH_COMPONENT24 (or 16) nor GL_DEPTHBUFFER_OES seem to be defined. How do you allocate and attach it?
Chris Cooper
+1  A: 

The instructions are here, if anyone else has this problem. The code is also below:

glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, 320, 480);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) ;
if(status != GL_FRAMEBUFFER_COMPLETE_OES) {
    NSLog(@"failed to make complete framebuffer object %x", status);
}
Chris Cooper
Any reason you're passing floats to glRenderbufferStorage ?
Bahbar
Most likely because I don't know what it does, and have not looked at it's documentation. =PThanks for pointing it out.
Chris Cooper