说明
python的opengl库叫PyOpenGL,安装这个库的时候要注意位数版本要正确,比如我的python37是32位的,安装PyOpenGL-3.1.5-cp37-cp37m-win32.whl才行,默认pip安装的运行时可能会有问题,所以需要手动下载安装对应版本的whl。
demo
这是一个直接运行的demo# !/usr/bin/python
# -*- coding: utf-8 -*-
# -------------------------------------------
# quidam_01.py 三维空间的世界坐标系和三角形
# -------------------------------------------
from OpenGL.GL import *
from OpenGL.GLUT import *
def draw():
# ---------------------------------------------------------------
glBegin(GL_LINES) # 开始绘制线段(世界坐标系)
# 以红色绘制x轴
glColor4f(1.0, 0.0, 0.0, 1.0) # 设置当前颜色为红色不透明
glVertex3f(-0.8, 0.0, 0.0) # 设置x轴顶点(x轴负方向)
glVertex3f(0.8, 0.0, 0.0) # 设置x轴顶点(x轴正方向)
# 以绿色绘制y轴
glColor4f(0.0, 1.0, 0.0, 1.0) # 设置当前颜色为绿色不透明
glVertex3f(0.0, -0.8, 0.0) # 设置y轴顶点(y轴负方向)
glVertex3f(0.0, 0.8, 0.0)