4

我正在尝试使用 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()
4

1 回答 1

3

这些线被多边形覆盖。请注意,线和多边形在数学上具有相同的深度,但实际上这取决于浮点算术精度,或者由多边形或线绘制的片段“赢得”深度测试。这也可能导致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 )
于 2019-06-17T05:02:49.500 回答