PyOpenGL 彩色多边形

2019-08-14 17:31:44 浏览数 (1)

一直想在PyQt中找一个能显示有限元2D云图的部件,可惜QLinearGradient类只能使颜色在两个点之间插值变化,3个或以上点时则无能为力。还好我们可以使用PyOpenGL。

PyOpenGL下载链接如下:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl

不推荐使用Pip下载,因为Pip下载的64位版本缺少某些库文件,会报错。

本程序将OpenGL渲染的图形窗口嵌入到PyQt5窗口部件中,显示了两个彩色多边形,多边形内部颜色由顶点颜色插值而来。

代码如下:

代码语言:javascript复制
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtOpenGL import QGLWidget
from OpenGL import GL


class GLWidget(QGLWidget):
    def __init__(self, parent =None):
        super(GLWidget, self).__init__(parent)
    def initializeGL(self):
        self.qglClearColor(QColor(0,0,0))
        #GL.glClearDepth(1.0)# Enables Clearing Of The Depth Buffer
        GL.glShadeModel(GL.GL_SMOOTH) #!颜色平滑渲染
        #GL.glDepthFunc(GL.GL_LESS)      # The Type Of Depth Test To Do
        #GL.glEnable(GL.GL_DEPTH_TEST)
        self.object = self.makeObject()
       
    def paintGL(self):
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glLoadIdentity()# Reset The Projection Matrix
        #GL.glTranslatef(0, 0.0, 0)#平移
        #GL.glRotated(0.5, 1.0, 0.0, 0.0)#旋转
        GL.glCallList(self.object)
        
    def resizeGL(self, width, height):
        side = min(width, height)
        GL.glViewport((width - side) // 2, (height - side) // 2, side, side)#保持图形的长宽比
        #GL.glViewport(50,50,500,500)
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glLoadIdentity()# Reset The Projection Matrix
        #GL.glOrtho(-0.5,  0.5,  0.5, -0.5, 4.0, 15.0)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        
    def makeObject(self):
        genList = GL.glGenLists(1)
        GL.glNewList(genList, GL.GL_COMPILE)
        # Since we have smooth color mode on, this will be great for the Phish Heads :-).
        '''
        # Draw a triangle in 3D space
        GL.glBegin(GL.GL_POLYGON)
        # Start drawing a polygon
        GL.glColor3f(1.0, 0.0, 0.0)            # Red
        GL.glVertex3f(0.0, 1.0, 0)           # Top
       
        GL.glColor3f(0.0, 1.0, 0.0)            # Green
        GL.glVertex3f(1.0, -1.0, 0.0)          # Bottom Right
       
        GL.glColor3f(0.0, 0.0, 1.0)            # Blue
        GL.glVertex3f(-1.0, -1.0, 0.0)         # Bottom Left
       
        GL.glEnd()
        '''
         # Draw a 2d triangle
        GL.glBegin(GL.GL_POLYGON)# Start drawing a polygon
        GL.glColor3f(1.0, 0.0, 0.0) #R,G,B color
        GL.glVertex2f(0, 0.5)
       
        GL.glColor3f(0.0, 1.0, 0.0)
        GL.glVertex2f(-0.5, 0)
       
        GL.glColor3f(0.0, 0.0, 1.0)
        GL.glVertex2f(0.5,0)
        GL.glEnd()
         # Draw a 2d triangle
        GL.glBegin(GL.GL_POLYGON)# Start drawing a polygon
        GL.glColor3f(1.0, 0.0, 0.0)
        GL.glVertex2f(0, 0.5)
       
        GL.glColor3f(0.0, 0.0, 1.0)
        GL.glVertex2f(0.5,0)
       
        GL.glColor3f(0.5, 0.0, 0.5)
        GL.glVertex2f(0.9, 0.2)
       
        GL.glColor3f(0.0, 1.0, 0.0)
        GL.glVertex2f(1.0, 1.0) #1.0, 1.0代表视口的左上角点,0,0代表中心点
        GL.glEnd()
       
        GL.glEndList()
        return genList
 
    def minimumSizeHint(self):
        return QSize(100, 100)
    #def sizeHint(self):
        #return QSize(800, 800)
   
class MainWindow(QMainWindow):
    def __init__(self):       
        super().__init__()
        self.setCentralWidget(GLWidget())
        self.setWindowTitle("OpenGL example")
        self.resize(800, 800)
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())   

0 人点赞