如何摆脱glClear(GL_STENCIL_BUFFER_BIT)

How to get rid of glClear(GL_STENCIL_BUFFER_BIT)

本文关键字:STENCIL BUFFER BIT GL 何摆脱 glClear      更新时间:2023-10-16

我试图将OpenGL绘制限制在窗口的一部分。为了做到这一点,我尝试使用GL_STENCIL_TEST选项。这是我的代码:

// Enable stencil and clear the stencil buffer
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
// Enable func and Op so drawing will effect the stencil buffer
glStencilFunc(GL_NEVER, 0x0, 0x0);
glStencilOp(GL_INCR, GL_INCR, GL_INCR);
// Draw a rect to the stencil
DrawSolidRect(rectDrawArea);
// Enable func and Op so drawing will not effect the stencil buffer
// but will only effect places where the stencil was drawn to in the previous step.
glStencilFunc(GL_EQUAL, 0x1, 0x1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

问题是glClear(GL_STENCIL_BUFFER_BIT)函数非常耗时,而且由于我以每秒25帧的速度绘制,它确实会减慢应用程序的速度。

我试着删除这个函数-这解决了速度慢的问题,但当我初始化应用程序时,它会导致闪烁-大约10秒。然后它消失了,工作正常。

有人能告诉我为什么闪烁,或者我可以使用其他什么不那么耗时的功能吗?

如果您使用矩形剪辑区域,使用glScissor可能会更容易、更快。http://www.opengl.org/sdk/docs/man/xhtml/glScissor.xml

如上面链接中所述,也可以调用glEnable(GL_SCISSOR_TEST)来启用剪刀测试。

感谢@datenwolf