2017-08-28 37 views
1

我不明白为什么这两个代码不会产生相同的结果。我已经启用了深度测试和模板测试和我的主循环是这样的:模板缓存行为(GL_ALWAYS,GL_LEQUAL)

myShader.Use() // Use shader program 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
// define model, projection, view, .... 
glBindVertexArray(VAO[0]); 
glStencilMask(0xFF); // Enable stencil buffer writing 
glStencilFunc(GL_ALWAYS, 1, 0xFF); 
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); 
drawCube(&myShader, model, projection, view); 
glBindVertexArray(0); 

很显然,我的立方体呈现在这种情况下。

但是,如果我用这个代码,没有呈现在所有:

myShader.Use() // Use shader program 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
// define model, projection, view, .... 
glBindVertexArray(VAO[0]); 
glStencilMask(0xFF); // Enable stencil buffer writing 
glStencilFunc(GL_LEQUAL, 1, 0xFF); 
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); 
drawCube(&myShader, model, projection, view); 
glBindVertexArray(0); 

只有一个对象在我的场景,无论是代码的行为应该以同样的方式。然而,事实并非如此。

我认为它有一些东西需要与模板缓冲区的默认值。然而,因为我在做这个之前清除模板缓存,默认值应为0

顺便说一句,使用此代码,立方体呈现,以及:

myShader.Use() // Use shader program 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
// define model, projection, view, .... 
glBindVertexArray(VAO[0]); 
glStencilMask(0xFF); // Enable stencil buffer writing 
glStencilFunc(GL_LEQUAL, 0, 0xFF); 
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); 
drawCube(&myShader, model, projection, view); 
glBindVertexArray(0); 

我不了解正在发生的事情。

回答

2

glStencilFunc(GL_LEQUAL, 1, 0xFF)意味着if 1 <= 0,因为模板缓存中包含0,因为你清除它。该参考值是1,因为你通过这个值的功能。这总是失败。

参见Khronos的参考页(glStencilFunc):

GL_LEQUAL通行证if (ref & mask) <= (stencil & mask)