GL_LINES 没有显示在立方体顶部?
Posted
技术标签:
【中文标题】GL_LINES 没有显示在立方体顶部?【英文标题】:GL_LINES not showing up on top of cube? 【发布时间】:2019-06-17 02:44:40 【问题描述】:我正在尝试使用 OpenGL 和 Pygame 创建一个 3d 魔方。但是,当我尝试在我创建的 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()
【问题讨论】:
【参考方案1】:线条被多边形覆盖。请注意,线和多边形在数学上具有相同的深度,但实际上这取决于浮点算术精度,或者由多边形或线绘制的片段“赢得”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 个边的列表。使用此列表绘制立方体:
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 )
【讨论】:
以上是关于GL_LINES 没有显示在立方体顶部?的主要内容,如果未能解决你的问题,请参考以下文章
我的 opengl 代码应该显示多个立方体,但只显示一个正方形