2017-04-19 55 views
0

我有一個應用程序,我想使用模具蒙版渲染紋理深度。我嘗試GL_DEPTH_STENCIL_OES:如何使用深度紋理與模具,OpenGL ES 3.0

創建質地:

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); 

創建陰影貼圖鏤空掩膜:

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(); 

當代碼爲:

// 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(); 

與模板掩模深度渲染執行,屏幕是黑色的。這是意想不到的,因爲我不會將深度渲染到默認幀緩衝區,而是渲染到紋理。由於模板值存儲在紋理中,我必須以不同的方式使用它們嗎?

使用組合深度 - 模板紋理GL_DEPTH_STENCIL_OES時,使用模板附件的正確方法是什麼?

回答

0

模板值是您正在渲染的幀緩衝區的屬性。如果您想在FBO0中使用模板掩模,那麼您需要在FBO0中請求模板(並在渲染到FBO0時填充掩模等)。