I am using my own algorithm to cull occluded front-faces that should never be displayed. When I don't cull the faces, I have a perfect set of blocks. When I enable my face culling, I have unexpected results. I have checked my culling code, and I believe it is properly culling the appropriate faces.
For simplicity's sake, I have reduced my output to a plane of 9 boxes. As an example, the box in the middle of the plane need only have a top face and a bottom face. All other faces can be discarded because they simply won't ever be seen anywhere.
I checked each box within the code to determine if they had the correct faces culled, and I believe they do. That makes me think it's a problem with my vertices, indices or normals.
I'm using python's Pyglet, which auto-creates and manages the VAOs and VBOs.
OpenGL settings:
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
OpenGL On Draw:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Vertex code:
v0 = [1, 1, 1]
v1 = [-1, 1, 1]
v2 = [-1, -1, 1]
v3 = [1, -1, 1]
v4 = [1, -1, -1]
v5 = [1, 1, -1]
v6 = [-1, 1, -1]
v7 = [-1, -1, -1]
pt = self.point
s = getattr(self, 'scale', 0.5)
faces = ((k, v) for k, v in (
('front', [v * s + p for vert in [v0, v1, v2, v3]
for v, p in zip(vert, pt)]),
('right', [v * s + p for vert in [v0, v3, v4, v5]
for v, p in zip(vert, pt)]),
('top', [v * s + p for vert in [v0, v5, v6, v1]
for v, p in zip(vert, pt)]),
('left', [v * s + p for vert in [v1, v6, v7, v2]
for v, p in zip(vert, pt)]),
('bottom', [v * s + p for vert in [v7, v4, v3, v2]
for v, p in zip(vert, pt)]),
('back', [v * s + p for vert in [v4, v7, v6, v5]
for v, p in zip(vert, pt)]))
if getattr(self, k)
)
Normals code:
verts_per_face = 4
faces = ((k, v) for k, v in (
('front', [0, 0, 1] * verts_per_face),
('right', [1, 0, 0] * verts_per_face),
('top', [0, 1, 0] * verts_per_face),
('left', [-1, 0, 0] * verts_per_face),
('bottom', [0, -1, 0] * verts_per_face),
('back', [0, 0, -1] * verts_per_face))
if getattr(self, k)
)
Indices code:
t0 = [0, 1, 2]
t1 = [2, 3, 0]
t2 = [4, 5, 6]
t3 = [6, 7, 4]
t4 = [8, 9, 10]
t5 = [10, 11, 8]
t6 = [12, 13, 14]
t7 = [14, 15, 12]
t8 = [16, 17, 18]
t9 = [18, 19, 16]
t10 = [20, 21, 22]
t11 = [22, 23, 20]
triangles = ((k, v) for k, v in (
('front', [t for triangle in [t0, t1] for t in triangle]),
('right', [t for triangle in [t2, t3] for t in triangle]),
('top', [t for triangle in [t4, t5] for t in triangle]),
('left', [t for triangle in [t6, t7] for t in triangle]),
('bottom', [t for triangle in [t8, t9] for t in triangle]),
('back', [t for triangle in [t10, t11] for t in triangle]))
if getattr(self, k)
)
Culling code:
for face, neighbor_point in block.neighbors():
# import pdb; pdb.set_trace()
if neighbor_point in pts:
neighbor = self.blocks.get(neighbor_point)
if neighbor:
setattr(block, face, False)
else:
setattr(block, face, True)
else:
setattr(block, face, True)
Example output after culling the front and left faces on one of the boxes:
<Voxel (1,0,1) # top right corner box in images /w center point = (1, 0, 1)
[f r t l o a] # front, right, top, left, bottom, back
[ |+|+| |+|+] # + = face available, ' ' = face culled
(1, 0, 0) (1, 0, 0) (1, 0, 0) (1, 0, 0) # right normals
(0, 1, 0) (0, 1, 0) (0, 1, 0) (0, 1, 0) # top normals
(0, -1, 0) (0, -1, 0) (0, -1, 0) (0, -1, 0) # bottom normals
(0, 0, -1) (0, 0, -1) (0, 0, -1) (0, 0, -1) # back normals
[ 1.50| 0.50| 1.50| # right verts
1.50|-0.50| 1.50|
1.50|-0.50| 0.50|
1.50| 0.50| 0.50|
1.50| 0.50| 1.50| # top verts
1.50| 0.50| 0.50|
0.50| 0.50| 0.50|
0.50| 0.50| 1.50|
0.50|-0.50| 0.50| # bottom verts
1.50|-0.50| 0.50|
1.50|-0.50| 1.50|
0.50|-0.50| 1.50|
1.50|-0.50| 0.50| # back verts
0.50|-0.50| 0.50|
0.50| 0.50| 0.50|
1.50| 0.50| 0.50]>