如何在openGL cubestack中的对象顶部打个洞?

我想在openGL cube.i尝试某些方法,如使用模板和alpha混合。但模板的问题是它是划分和显示只有一半的部分。我的要求是我必须堆叠立方体,应该让用户指定的孔数(矩形/椭圆形)仅限于顶部对象。我能够堆叠对象,但如果需要则无法创建孔。 我是openGL的新手,我找不到任何直接的解决方案。有人给出了这个要求的示例程序吗?

模具代码:

glClear(GL_STENCIL_BUFFER_BIT); glColorMask(false, false, false, false); glEnable(GL_STENCIL_TEST); glStencilFunc(GL_EQUAL, 0, 1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glDisable(GL_DEPTH_TEST); glColor4f(0,0,1,1.0f); //code for cube glEnable(GL_DEPTH_TEST); glColorMask(true, true, true, true); glStencilFunc(GL_ALWAYS,0, 1); glStencilOp(GL_REPLACE,GL_KEEP, GL_ZERO); //code for cylinder glDisable(GL_STENCIL_TEST); 

在你的样本中,你为什么要在立方体之后渲染洞? 如果你想让模板做某事你应该做相反的事情。

使用这个伪代码,孔应该是平面网格或近似。

 -enable depth test if not already done; (depth test should not be disabled, if the point of view place hole on back faces, you dont want to have it reflected in front...) -Enable stencil test; -set colormask to false; -set stencil op replace; (if your hole are rendered and maybe moving each frame, use replace to update the stencil) -set stencil func always 1 1; (if you wants your hole to be rendered into the stencil buffer you have to pass always) -render your hole mesh with your current model view matrices; -restore colormask to true; -set stencilfunc notequal 1 1; (dont draw if something appear in stencil at this place) -set stencil op keep:; (keep in any case) -draw your scene: holes will be masked; 
  1. 使用FBO创建具有用于创建孔的Alpha通道的新纹理。