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

时间:2014-03-20 07:45:05

标签: c opengl glut freeglut

我想在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);

1 个答案:

答案 0 :(得分:0)

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

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

-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通道的新纹理。