如何用另一个四边形遮罩OpenGL四边形

时间:2019-10-27 16:32:30

标签: opengl mask

我试图显示一个四边形,但是仅当它在我知道位置的另一个四边形上时才显示。我曾考虑过使用该四边形作为另一个四边形的遮罩,但是我不确定该如何做(我已经发现this帖子谈到了遮罩,但是在我的情况下,我没有遮罩纹理;我只知道要遮罩的区域的X,Y,宽度和高度)。我发现的当前解决方案是使用glBlendFunc,并且只有在我没有在其后面渲染任何东西时,该方法才有效,以后将不再是这种情况。

glBlendFunc(GL_ONE, GL_ZERO);
// draw the background quad, that is acting as the mask...
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ZERO);
// draw the background quad again, this time it will act as a mask...
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
// draw the quads that will be masked...
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // this is the blend func used for the rest of the rendering

在绘制每一帧之前,我还有一个清除屏幕的功能:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,0);

我怎么做,这样无论我画的是什么,它只会掩盖上一个四边形?

1 个答案:

答案 0 :(得分:1)

如果要将渲染限制为矩形区域,则可以使用Scissor Test
必须启用剪刀测试(GL_SCISSOR_TEST),并可以通过glScissor设置第一个矩形区域。例如:

glEnable(GL_SCISSOR_TEST);
glScissor(x, w, width, height);   
相关问题