关于C#:简单情况下的模板缓冲区行为(GL_ALWAYS,GL_LEQUAL)

Stencil buffer behaviour in simple case (GL_ALWAYS, GL_LEQUAL)

我不明白为什么这两个代码不能产生相同的结果。我已启用深度测试和模板测试,并且我的主循环如下所示:

1
2
3
4
5
6
7
8
9
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);

很明显,在这种情况下,我的多维数据集会呈现。

但是,如果我使用此代码,则根本不会呈现任何内容:

1
2
3
4
5
6
7
8
9
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。

顺便说一下,使用此代码,多维数据集也将呈现:

1
2
3
4
5
6
7
8
9
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);

我不知道发生了什么。


glStencilFunc(GL_LEQUAL, 1, 0xFF)表示if 1 <= 0,因为模板缓冲区包含0,因为您将其清除了。参考值是1,因为您将此值传递给了函数。这总是失败。

请参阅Khronos参考页(glStencilFunc):

GL_LEQUAL Passes if ( ref & mask ) <= ( stencil & mask ).