带有OpenGL的嵌套剪刀盒

mwd*_*mwd 2 c opengl

我正在研究用C编写的基于OpenGL的UI.我有一个控件层次结构 - 在较大控件的可滚动区域内的小方形2D框.在小盒子内部绘制3D场景.必须在小的2D框内剪切3D场景,并且必须在较大的区域内剪切小的2D框.

glScissor()是确保绘图在所需区域内进行的明显候选者.问题是我无法同时调用glScissor()大区域,然后glScissor()再次调用较大的剪切区域内的子区域.

用于演示我想要的示例代码:

void drawBigBox() {
    glScissor( ... ); //set the bounds of the larger region
    for each little box {
        //these might be anywhere, but they should only be visible if inside the glScissor bounds
        drawLittleBoxes(); 
    }
}

void drawLittleBoxes() {
    //draw only inside little box -- Whoops! This replaces the big box bounds!
    //How can I meet this constraint while retaining the outer scissor region?
    glScissor( ... );
    draw3dScene();
}
Run Code Online (Sandbox Code Playgroud)

有一种简单的方法可以做我想要的吗?显然我也可以计算小型和大型剪刀盒的交叉点,或者我可以更仔细地剪辑/投影我的3D场景,但这比OpenGL调用更复杂.也许还有另一种方法可以做到这一点,我不知道.

谢谢!

小智 6

为此,使用模板缓冲区可能更容易.在向其写入四边形时,可以使用INCR增加模板缓冲区值,并在绘制时将测试设置为EQUAL或GEQUAL,以便只写入模板缓冲区具有最高值的位置,因此它只会写入受限制的区域所有当前剪辑区域.要在绘制层次结构时删除它们,请使用DECR再次删除它们.这样,您还可以使用非矩形剪切区域.