gpt4 book ai didi

python - GL_LINES 没有出现在立方体的顶部?

转载 作者:行者123 更新时间:2023-11-28 16:57:55 25 4
gpt4 key购买 nike

我正在尝试使用 OpenGL 和 Pygame 创建一个 3d Rubiks Cube。但是,当我尝试在我创建的 GL_QUADS 表面上绘制线条时,它们没有显示出来。

我已经尝试将 GL_LINES 放在 GL_QUADS 行之后,但它们仍然没有显示为黑线。

有人有解决办法吗?另外,我该如何调整线条的粗细?

代码如下:

def draw(self):
glEnable(GL_DEPTH_TEST)

glBegin(GL_LINES)
glColor3fv((0, 0, 0))
glVertex3fv((self.x - self.len, self.y + self.len, self.z - self.len))
glVertex3fv((self.x - self.len, self.y + self.len, self.z - self.len))

glColor3fv((0, 0, 0))
glVertex3fv((self.x + self.len, self.y + self.len, self.z + self.len))
glVertex3fv((self.x + self.len, self.y - self.len, self.z - self.len))

glColor3fv((0, 0, 0))
glVertex3fv((self.x - self.len, self.y + self.len, self.z - self.len))
glVertex3fv((self.x - self.len, self.y + self.len, self.z - self.len))

glColor3fv((0, 0, 0))
glVertex3fv((self.x - self.len, self.y - self.len, self.z + self.len))
glVertex3fv((self.x - self.len, self.y - self.len, self.z - self.len))
glEnd()

glBegin(GL_QUADS)

glColor3fv(colors[0])
# fix z
glVertex3fv((self.x - self.len, self.y - self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y - self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y + self.len, self.z - self.len))
glVertex3fv((self.x - self.len, self.y + self.len, self.z - self.len))

glColor3fv(colors[1])
glVertex3fv((self.x - self.len, self.y - self.len, self.z + self.len))
glVertex3fv((self.x + self.len, self.y - self.len, self.z + self.len))
glVertex3fv((self.x + self.len, self.y + self.len, self.z + self.len))
glVertex3fv((self.x - self.len, self.y + self.len, self.z + self.len))

# fix x
glColor3fv(colors[2])
glVertex3fv((self.x - self.len, self.y - self.len, self.z - self.len))
glVertex3fv((self.x - self.len, self.y + self.len, self.z - self.len))
glVertex3fv((self.x - self.len, self.y + self.len, self.z + self.len))
glVertex3fv((self.x - self.len, self.y - self.len, self.z + self.len))

glColor3fv(colors[3])
glVertex3fv((self.x + self.len, self.y - self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y + self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y + self.len, self.z + self.len))
glVertex3fv((self.x + self.len, self.y - self.len, self.z + self.len))

# fix y
glColor3fv(colors[4])
glVertex3fv((self.x - self.len, self.y - self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y - self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y - self.len, self.z + self.len))
glVertex3fv((self.x - self.len, self.y - self.len, self.z + self.len))

glColor3fv(colors[5])
glVertex3fv((self.x - self.len, self.y + self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y + self.len, self.z - self.len))
glVertex3fv((self.x + self.len, self.y + self.len, self.z + self.len))
glVertex3fv((self.x - self.len, self.y + self.len, self.z + self.len))

glEnd()

最佳答案

线条被多边形覆盖。请注意,线条和多边形在数学上具有相同的深度,但实际上这取决于浮点运算精度,由多边形或线条绘制的片段“赢得”depth test。 .这也可能导致 Z-fighting .

通过设置深度偏移(参见 glPolygonOffset),将多边形稍微向后推,因此线条将位于多边形的前面:

 def draw(self):
glEnable(GL_DEPTH_TEST)

glBegin(GL_LINES)
# [...]
glEnd()

glEnable( GL_POLYGON_OFFSET_FILL )
glPolygonOffset( 1.0, 1.0 )

glBegin(GL_QUADS)
# [...]
glEnd()

glDisable( GL_POLYGON_OFFSET_FILL )

如果您仍然看不到直线,则必须增加多边形偏移量(第一个参数)。请注意,这些线条很细并且是用黑色绘制的 (glColor3fv((0, 0, 0))),可能很难看到。


线的粗细可以通过glLineWidth设置


请注意,一个立方体由 8 个顶点、6 条边和 12 条边组成。在您的代码中,您只绘制了 4 条边。
我建议定义一个包含立方体 8 个角点的列表,并定义一个包含 6 个边四边形索引和 12 个边的列表。使用此列表绘制立方体:

repl.it/@Rabbid76/PyGame-opengl-cube-wireframe

class Cube:

def __init__(self, x, y, z, len):
self.x = x
self.y = y
self.z = z
self.len = len

self.v = [
(self.x - self.len, self.y - self.len, self.z - self.len),
(self.x + self.len, self.y - self.len, self.z - self.len),
(self.x + self.len, self.y + self.len, self.z - self.len),
(self.x - self.len, self.y + self.len, self.z - self.len),
(self.x - self.len, self.y - self.len, self.z + self.len),
(self.x + self.len, self.y - self.len, self.z + self.len),
(self.x + self.len, self.y + self.len, self.z + self.len),
(self.x - self.len, self.y + self.len, self.z + self.len),
]
self.edges = [(0,1), (1,2), (2,3), (3,0), (4,5), (5,6),
(6,7), (7,4), (0,4), (1,5), (2,6), (3,7)]
self.surfaces = [(0,1,2,3), (5,4,7,6), (4,0,3,7),(1,5,6,2), (4,5,1,0), (3,2,6,7)]

self.colors = [(1,0,0), (0,1,0), (0,0,1), (1,1,0), (1,0,1), (1,0.5,0)]

def draw(self):
glEnable(GL_DEPTH_TEST)

glLineWidth(5)
glColor3fv((0, 0, 0))
glBegin(GL_LINES)
for e in self.edges:
glVertex3fv(self.v[e[0]])
glVertex3fv(self.v[e[1]])
glEnd()

glEnable( GL_POLYGON_OFFSET_FILL )
glPolygonOffset( 1.0, 1.0 )

glBegin(GL_QUADS)
for i, quad in enumerate(self.surfaces):
glColor3fv(self.colors[i])
for iv in quad:
glVertex3fv(self.v[iv])
glEnd()

glDisable( GL_POLYGON_OFFSET_FILL )

关于python - GL_LINES 没有出现在立方体的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com