我正在使用自己的算法来剔除永远不会显示的被遮挡的正面。当我不剔除脸部时,我有一套完美的积木。当我启用脸部剔除时,我会有意想不到的结果。我检查了我的剔除代码,我相信它正在剔除适当的面孔。
为了简单起见,我将输出减少到了9箱的平面。例如,平面中间的盒子只需要有一个顶面和一个底面。所有其他面孔都可以丢弃,因为它们根本无法在任何地方被人看到。
我检查了代码中的每个方框,以确定他们是否有正确的面孔被淘汰,我相信他们这样做了。这让我觉得我的顶点,索引或法线都有问题。
我正在使用python的Pyglet,它可以自动创建和管理VAO和VBO。
OpenGL设置:
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
OpenGL On Draw:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
顶点代码:
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)
)
法线代码:
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)
)
指数代码:
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)
)
剔除代码:
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)
在其中一个方框上剔除前面和左面后的示例输出:
<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]>
答案 0 :(得分:0)
请参阅here了解如何实施剔除算法。在你的剔除代码中,你正在做一些完全无关紧要的事情。要确定是否应该剔除面部,您只需要检查它的缠绕顺序(假设您提供与其法线相关的CW或CCW顺序的所有三角形)。如果您的三角形没有排序,那么为了确定剔除,您必须检查三角形顶点的z顺序,这是图形卡自动执行的操作。如果你决定自己实现它,你将有效地实现软件渲染(尽管渲染为三角形片段,而不是单个像素)。
答案 1 :(得分:0)
我没有正确计算指数。新方法应该是:
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]))
)
inds = []
faces = [f for f, v in self.faces if v]
for triangle, face in zip(triangles, faces):
f, tdata = triangle
inds.extend(tdata)