OpenGL 侧面剔除
我正在绘制一系列彼此相邻的立方体,并且我想剔除所有接触的面,因为它们是无用的。问题是我不知道如何剔除侧面。有人可以解释如何剔除通过 glBegin()/glEnd() 调用调用的立方体的任何边吗?
I am drawing a series of cubes that are beside each other and I want to cull all the sides that are touching because they are useless. The problem is that I don't know how to cull the sides. Can someone explain how to cull any sides of a cube that is being called with glBegin()/glEnd() calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OpenGL 不会为你做这件事,你必须自己剔除它们。
我在最近的一个项目中执行了以下操作:
从立方体位置的集合中,我将每个面的条目添加到字典中。这将面中心位置映射到面法线。在添加新的(位置,正常)对之前,我会测试该位置是否存在。如果找到:删除现有条目并扔掉新条目。否则添加新的(位置,正常)。
从这本字典中,您可以构建顶点和三角形列表,并且不会有任何接触面。
可能有更有效的方法来做到这一点,但这对于我的应用程序来说已经足够好了并且易于实现。
编辑:
OpenGL won't do this for you, you'll have to cull them yourself.
I did the following on a recent project:
From a collection of cube positions I added entries for each face into a dictionary. This mapped face centre position to face normal. Before adding a new (position, normal) pair, I would test for existence of that position. If found: remove the existing entry and throw away the new one. Otherwise add the new (position, normal).
From this dictionary you can build vertex and triangle lists and you won't have any touching faces.
There may be more efficient ways to do this, but this was good enough for my application and simple to implement.
Edit:
如果您确保指定立方体的面法线,则至少可以启用背面剔除 (
glEnable()
),但对于立方体的侧面,您可能需要手动剔除。但是,您可以将侧面的法线也设置为背面,以便通过背面剔除来拾取。
使用(例如)指定每个面的法线:
对于背面剔除使用:
If you make sure to specify the face normals for your cubes you can then enable back face culling at least (
glEnable()
), but it is likely that for your cube sides you will need to manually cull.However, you could set the normals for the side faces to also be back facing so as to get picked up by the back face culling.
Specify the normals for each face using (eg):
For back face culling use: