首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在openGL立方叠中的物体顶部打个洞?

如何在openGL立方叠中的物体顶部打个洞?
EN

Stack Overflow用户
提问于 2014-03-20 07:45:04
回答 1查看 869关注 0票数 0

我想在openGL立方体上做一个洞,我已经尝试过一些方法,比如使用模板和αblending.but问题,用模板来划分和显示,只有一半的part.My要求我必须堆叠立方体,并且应该使用户指定数量的孔(矩形/椭圆形状)到顶部对象,我能够堆叠对象,但如果需要的话,我不能造一个洞。我是openGL的新手,我没有为this.can找到任何直接的解决方案,有人给出了这个需求的示例程序吗?

模板代码:

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2014-03-20 08:06:58

在你的样本中,你为什么要在立方体之后呈现洞呢?如果你想要你的模具做一些事情,你应该做相反的事情。

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

代码语言:javascript
复制
-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通道的新纹理。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22526534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档