Opengl es 如何在模具中使用深度纹理,OpenGL ES 3.0

Opengl es 如何在模具中使用深度纹理,OpenGL ES 3.0,opengl-es,depth-buffer,stencil-buffer,Opengl Es,Depth Buffer,Stencil Buffer,我有一个应用程序,我想渲染深度的纹理使用模具蒙版。我尝试使用GL_DEPTH_STENCIL_OES: 创建纹理: glGenFramebuffers(1, fbo); glBindFramebuffer(GL_FRAMEBUFFER, *fbo); glGenTextures(1, depthTexture); glBindTexture(GL_TEXTURE_2D, *depthTexture); // using combined format: depth + stencil glTe

我有一个应用程序,我想渲染深度的纹理使用模具蒙版。我尝试使用GL_DEPTH_STENCIL_OES:

创建纹理:

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

glGenTextures(1, depthTexture);
glBindTexture(GL_TEXTURE_2D, *depthTexture);
// using combined format: depth + stencil
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL_OES, w, h, 0, GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);

// attaching both depth and stencil
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, *depthTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, *depthTexture, 0);

// No color output in the bound framebuffer, only depth.
GLenum drawbuffers[] = { GL_NONE };
glDrawBuffers(1, drawbuffers);

GL_CHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE);
正在为阴影贴图创建模具遮罩:

// bind to depth fbo
glBindFramebuffer(GL_FRAMEBUFFER, *fbo);

glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glClear(GL_STENCIL_BUFFER_BIT);

// write to stencil when objects are rendered
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilMask(0xFF);

drawVisibleObjects();
使用模板遮罩渲染深度:

glBindFramebuffer(GL_FRAMEBUFFER, *fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1
glStencilMask(0x00); // Don't write anything to stencil buffer
glDepthMask(GL_TRUE); // Write to depth buffer

drawObjects();
执行代码时,屏幕为黑色。这是意外的,因为我没有将深度渲染为默认帧缓冲区,而是渲染为纹理。由于模具值存储在纹理中,我是否必须以不同的方式使用它们


使用组合深度模具纹理GL_depth_stencil_OES时,使用模具附件的正确方法是什么?

模具值是要渲染到的帧缓冲区的属性。如果要在FBO0中使用模具遮罩,则需要在FBO0中请求模具(并在渲染到FBO0时填充遮罩等)