KFo*_*Fox 2 c++ opengl stencil-buffer glfw
在使用Mac上的OpenGL中的模板测试绘图时遇到问题.当我第一次绘制场景时,模板工作正常.我在中间绘制一个半透明的黑色矩形,启用了对模板缓冲区的写入,然后禁用了写入模板缓冲区的更大的蓝色矩形.当窗口首次弹出时,我得到了正确的结果,如下所示:
但是,当我调整窗口大小,再次调用渲染函数时,我得到的结果如下:
有时奇怪的白色空间跟随中间的矩形,有时候白色在看似随意的排列之间捕捉,但是当你回到那个窗口大小时保持这些安排.我在网上找不到解决方案.这是我的渲染函数,在调整窗口大小时调用它:
glClearColor(0, 0, 0, 1);
glClearStencil(0x00);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Replace data in the stencil buffer with 1s if it passes the test
// which should be GL_ALWAYS
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// Allow data to be written to the stencil buffer.
glStencilMask(0xFF);
p->fillSquare(1, 1, 1, 0.3, -0.25, 0.25, 0.25, 0.25, -0.25, -0.25, 0.25, -0.25); // Write a semi transparent black rectangle
glStencilMask(0x00); // Disable writing to the stencil buffer.
glStencilFunc(GL_NOTEQUAL, 1, 0xFF); // Only draw if stencil value is 1.
// Draw blue rectangle
p->fillSquare(0.60, 0.60, 0.80, 1, -0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5);
glfwSwapBuffers(w);
Run Code Online (Sandbox Code Playgroud)
如果你想知道,p->fillRect()为矩形的RGBA颜色和每个顶点的x和y坐标取四个浮点数.
在清除模板时似乎可能存在某种问题,但我真的不能确定.我在OpenGL初始化函数中打开了模板测试.如果您需要了解有关我的代码或系统其他方面的任何其他信息,请随时发表评论.
注意:我没有使用OpenGL的深度,所以可以不清除深度缓冲区(我已经测试了这个).
清除模板缓冲区确实不起作用(除非是第一次调用函数),正如您所期望的那样.
你错过了什么glStencilMask也会影响glClear(... | GL_STENCIL_BUFFER_BIT).glStencilMask(0xFF)在进行清除呼叫之前,您应该向上移动一点.