Python OpenGL基本配置

2023-01-31 03:01:53 python opengl 基本配置

参考:Http://pyopengl.sourceforge.net

一、pip 安装 PyOpenGL

pip install PyOpenGL PyOpenGL_accelerate



二、测试代码

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

def init():
    glClearColor(1,1,1,1)
    gluOrtho2D(-1,1,-1,1)

def triangle():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1,0,0)
    glBegin(GL_TRIANGLES)

    glColor3f(1,0,0)
    glVertex2f(-1, -1)
    
    glColor3f(0,1,0)
    glVertex2f(1, -1)
    
    glColor3f(0,0,1)
    glVertex2f(0, 1)
    
    glEnd()
    glFlush()

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
    glutInitwindowsize(800,600)
    glutInitWindowPosition(50,50)
    glutCreateWindow("Triangle")
    glutDisplayFunc(triangle)
    init()
    glutMainLoop()

if __name__ == '__main__':
    main()


运行结果,绘制出一个彩色三角形:



相关文章