GL_LINES没有显示在多维数据集顶部吗?

时间:2019-06-17 02:44:40

标签: python opengl pygame pyopengl opengl-compat

我正在尝试使用OpenGL和Pygame创建3d Rubiks多维数据集。但是,当我尝试在创建的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 个答案:

答案 0 :(得分:3)

这些线被多边形覆盖。注意,线和多边形在数学上具有相同的深度,但是实际上,这取决于浮点算术精度,无论是由多边形绘制的片段还是由线“赢得” 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 )