开发者

OpenGL Side Culling

开发者 https://www.devze.com 2023-03-11 12:03 出处:网络
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 s

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?


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:

PSEUDOCODE:

d = dictionary<vec3, vec3>

for each cube:
    for face 1..6:
        pos = faceCenterFor(cube, face)
        if pos in d:
            remove d[pos]
        else:
            d[pos] = normalFor(cube, face)

for each (pos, normal) in d:
    draw(vertsForFace(pos, normal))
    # or save verts to an array for drawing later


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):

gl.glNormal3f(1, 0, 0);

For back face culling use:

glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号