运行pyOpenGL时出现NameError
我想搭建一个环境,用来编写一个粒子模拟器,使用的是pyOpenGL。我已经安装了pyOpenGL。
这是测试代码,目的是确保OpenGL能正常工作。
from OpenGL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
window = 0
width, height = 500,400
def draw():
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glutDwapBuffers()
#initialization
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width,height)
glutInitWindowPosition(0,0)
window = glutCreateWindow(b"noobtute")
glutDisplayFunc(draw)
glutIdleFunc(draw)
glutMainLoop()
但是,当我从命令行运行这个脚本时,出现了以下错误:
NameError: name 'glClear' is not defined
GLUT Display callback <function draw at 0x0000000002F8CB70> with (),{} failed:
returning None name 'glClear' is not defined
我尝试重新安装pyOpenGL,但没有成功。
我在Windows 8.1上运行的是Python 3.4。
如果你觉得需要更多信息,请随时问我。谢谢!
1 个回答
5
把
from OpenGL import *
改成
from OpenGL.GL import *
不过,我更喜欢用这种格式:
import OpenGL.GL as GL
import OpenGL.GLU as GLU
import OpenGL.GLUT as GLUT
window = 0
width, height = 500,400
def draw():
GL.glClear(GL.GL_COLOR_BUFFER_BIT |GL.GL_DEPTH_BUFFER_BIT)
GL.glLoadIdentity()
GLUT.glutSwapBuffers()
#initialization
GLUT.glutInit()
GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA | GLUT.GLUT_DOUBLE | GLUT.GLUT_ALPHA |
GLUT.GLUT_DEPTH)
GLUT.glutInitWindowSize(width,height)
GLUT.glutInitWindowPosition(0,0)
window = GLUT.glutCreateWindow(b"noobtute")
GLUT.glutDisplayFunc(draw)
GLUT.glutIdleFunc(draw)
GLUT.glutMainLoop()
虽然没有那么简洁,但它能帮助我理清每个人在做什么。