11
votes

I need stencil buffer on 3GS to render planar shadow, and polygon offset won't work prefect, still has z-fighting problem. So I use stencil buffer to make the shadow correct, it works on win32 gles2 emulator, but not on iPhone. After I added a post effect to the whole scene. The stencil buffer won't work even on win32 gles2 emulator.

And I tried to attach a stencil buffer to FBO, buf the screen turns to black. Here's my code,

   glGenRenderbuffers(1, &dbo); // depth buffer
   glBindRenderbuffer(GL_RENDERBUFFER, dbo);
   glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES,
widthGL, heightGL);

   glGenRenderbuffers(1, &sbo); // stencil buffer
   glBindRenderbuffer(GL_RENDERBUFFER, sbo);
   glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, widthGL,
heightGL);

   glGenFramebuffers(1, &fbo);
   glBindFramebuffer(GL_FRAMEBUFFER, fbo);

   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, tex, 0);
   glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, dbo);
   glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, sbo); // this make the whole screen black.

The eglContext is created with STENCIL_SIZE=8, it works without a RTT.

I tried to change the RenderbufferStorage for both depth buffer and stencil buffer, but none of them works.

Is there anything I have missed? Does the stencil buffer pack with depth buffer? (I cannot find things like GL_DEPTH24_STENCIL8 ...)

2
You could check for the return value from glCheckFramebufferStatus() to get more information - Ricky Lung
It's COMPLETE, no error reported. - Xiaobin
I found GL_OES_packed_depth_stencil defined in OpenGLES.framework. But I didn't find it in PVR OpenGLES2 emulator on Win32 :( I'll try to make it work on device tomorrow. - Xiaobin
When I generated a render buffer, and set its storage to GL_DEPTH24_STENCIL8_OES, glCheckFramebufferStatus reported GL_FRAMEBUFFER_UNSUPPORT. I was confused. GLES2 on iPhone should support stencil buffer. - Xiaobin
It's solved... Stencil buffer works only on iPhone device, never on simulators on both Win32 and Mac. Errrr.. - Xiaobin

2 Answers

14
votes

In OpenGL ES 2.0 on iOS, you have to create a combined depth and stencil renderbuffer using GL_DEPTH24_STENCIL8_OES, and then attach it to the bound framebuffer as both GL_DEPTH_ATTACHMENT and GL_STENCIL_ATTACHMENT.

glGenRenderbuffers(1, &depthStencilRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthStencilRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthStencilRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilRenderbuffer);
3
votes

Just a note, if you are using GLKView, it is as simple as

view.drawableStencilFormat = GLKViewDrawableStencilFormat8;

where view is: GLKView*