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

GL_LINES not showing up on top of cube?

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

线条被多边形覆盖。请注意,线条和多边形在数学上具有相同的深度,但实际上这取决于 floating-point 算术精度,由多边形绘制的片段或由线条绘制的片段“赢得”depth test. This may also cause 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 )