Reputation: 1211
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?
Upvotes: 1
Views: 1024
Reputation: 3067
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);
Upvotes: 0
Reputation: 13007
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
Upvotes: 2