I noticed that if I bind my depth buffer before the color buffer, the application works as intended:
glGenRenderbuffers(1, &_depthbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _depthbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, _sw, _sh);
glGenRenderbuffers(1, &_renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];
However, binding the depth buffer afterwards causes nothing to render, even my glClearColor setting is ignored:
glGenRenderbuffers(1, &_renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];
glGenRenderbuffers(1, &_depthbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _depthbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, _sw, _sh);
I've gotten to understand some of the flow of how OpenGL ES 2.0 works by researching the individual components thoroughly, but this seems as if it's the only thing that everyone just does in their tutorials/books, but doesn't explain why. Any ideas? Is this even an issue, or possibly something wrong in the rest of my setup? (if so I'll include all the code)
EDIT
@cli_hlt - the depth buffer is already being added to the framebuffer:
glGenFramebuffers(1, &_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderbuffer);
EDIT
Depth bound before:
Depth bound after:
I may be totally wrong—I'm just getting a handle on this stuff myself—but as I understand it, glBind commands only tell OpenGL which renderbuffer/texture/whatever to use for subsequent functions. It's a weird model if you're used to object-oriented programming. In the boilerplate setup code you have to bind the buffer you created to the GL_RENDERBUFFER "slot" so the next glRenderbufferStorage() or -[EAGLContext renderbufferStorage:fromDrawable:] call knows what buffer to use. I think the problem is just that you're not binding the active GL_RENDERBUFFER back to your color buffer before you're calling -[EAGLContext presentRenderBuffer:], so you're actually showing the depth buffer. Adding
glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
before the presentRenderBuffer: call should fix this. …I think.