猿问

PyOpenGL如何选择颜色来绘制每个形状?

我正在一个PyQt5 PyOpenGL项目进行中。我正在尝试用一堆彩色的实心立方体绘制一个白色线框立方体。线框立方体是从元组点列表和对这些点的元组引用列表中绘制的。实心立方体是从对点的元组引用列表中绘制的。下面是多维数据集代码:


class cube():

    render = True

    solid = False

    color = (1, 1, 1)


    def config(self, x, y, z, size = 0.1, solid = False, color = (1, 1, 1)):

        self.solid = solid

        self.color = color

        self.size = size / 2

        s = self.size

        self.vertices = [

                         (-s + x, s + y, -s + z),

                         (s + x, s + y, -s + z),

                         (s + x, -s + y, -s + z),

                         (-s + x, -s + y, -s + z),

                         (-s + x, s + y, s + z),

                         (s + x, s + y, s + z),

                         (s + x, -s + y, s + z),

                         (-s + x, -s + y, s + z)

                       ]

        self.edges = [

                      (0,1), (0,3), (0,4), (2,1),

                      (2,3), (2,6), (7,3), (7,4),

                      (7,6), (5,1), (5,4), (5,6)

                     ]

        self.facets = [

                       (0, 1, 2, 3), (0, 1, 6, 5),

                       (0, 3, 7, 4), (6, 5, 1, 2),

                       (6, 7, 4, 5), (6, 7, 3, 2)

                      ]

    def show(self):

        self.render = True

    def hide(self):

        self.render = False

为了呈现多维数据集,我获取类中保存的列表的大小,然后将多维数据集类的实例追加到该列表中。然后,我可以在追加之前使用大小来引用该实例。下面是渲染函数的代码:mainWindow


def paintGL(self):

    glLoadIdentity()

    gluPerspective(45, self.width / self.height, 0.1, 110.0)    #set perspective?

    glTranslatef(0, 0, self.zoomLevel)    #I used -10 instead of -2 in the PyGame version.

    glRotatef(self.rotateDegreeV, 1, 0, 0)    #I used 2 instead of 1 in the PyGame version.

    glRotatef(self.rotateDegreeH, 0, 0, 1)

结果:

Smart猫小萌
浏览 186回答 1
1回答

一只名叫tom的猫

OpenGL 是一个状态引擎。设置状态后,将一直保留该状态,直到再次更改状态,甚至在帧之外。当前颜色是全局状态。调用 glColor* 时,将设置当前颜色。当调用 glVertex* 时,当前的颜色、法线和纹理坐标将与顶点相关联。这意味着,在指定顶点之前,必须设置正确的颜色。在绘制线框立方体之前,您错过了设置颜色属性:class mainWindow(QMainWindow):&nbsp; &nbsp; #Main class.&nbsp; &nbsp; # [...]&nbsp; &nbsp; def paintGL(self):&nbsp; &nbsp; &nbsp; &nbsp; # [...]&nbsp; &nbsp; &nbsp; &nbsp; if len(self.shapes) != 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glBegin(GL_LINES)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for s in self.shapes:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glColor3fv(s.color)&nbsp; # <------------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if s.render and not s.solid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for e in s.edges:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for v in e:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glVertex3fv(s.vertices[v])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glEnd()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glBegin(GL_QUADS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for s in self.shapes:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glColor3fv(s.color)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if s.render and s.solid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for f in s.facets:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for v in f:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glVertex3fv(s.vertices[v])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glEnd()请注意,在每次调用 之前不必设置当前颜色。更改当前颜色时,只需设置一次即可。新颜色与以下所有顶点相关联。glVertex3fv
随时随地看视频慕课网APP

相关分类

Python
我要回答