由于自己的创新项目中需要用OpenGL完成一些材质的模拟效果,于是看了一些关于OpenGL的材料,加之最近比较喜欢Python这个语言,于是准备在Python中写点OpenGL的东西。
自己在装的时候遇到了一些问题,希望在这个告诉大家,以便其他朋友使用。
安装步骤:
STEP 1:安装Python 目录为xxx/Python2.7
STEP 2: 下载其安装pyOpenGL 模块(http://pypi.python.org/pypi/PyOpenGL),在Window下,可以选择
PyOpenGL-3.0.1.win32.exe (md5)安装包。在安装过程中,会要求选择Python的目录,选择xxx/Python2.7即可。
如果此时运行包含OpenGL.Tk模块的程序,会显示
TclError: can't find package Togl之类的错误。于是有
STEP 3:在联网状态下,运行xxx/Python27/Lib/site-packages/OpenGL中的togl.py
然后应该就可以了,下面是一个示例(From “Python How to program”, H.M.Deitel, P.J.Deitel...)
#Colored, rotating box from Tkinter import * from OpenGL.GL import * from OpenGL.Tk import * class ColorBox(Frame): def __init__(self): Frame.__init__(self) self.master.title("Color Box") self.master.geometry("300x300") self.pack(expand = YES, fill = BOTH) self.openGL = Opengl(self, double = 1) self.openGL.pack(expand = YES, fill = BOTH) self.openGL.redraw = self.redraw self.openGL.set_eyepoint(20) self.amountRotated = 0 self.increment = 2 self.update() def redraw(self, openGL): glClearColor(1.0, 1.0, 1.0, 0.0) glClear(GL_COLOR_BUFFER_BIT) glDisable(GL_LIGHTING) red = (1.0, 0.0, 0.0) green = (0.0, 1.0, 0.0) blue = (0.0, 0.0, 1.0) purple = (1.0, 0.0, 1.0) vertices = / [((-3.0, 3.0, -3.0), red), ((-3.0, -3.0, -3.0), green), ((3.0, 3.0, -3.0), blue), ((3.0, -3.0, -3.0), purple), ((3.0, 3.0, 3.0), red), ((3.0, -3.0, 3.0), green), ((-3.0, 3.0, 3.0), blue), ((-3.0, -3.0, 3.0), purple), ((-3.0, 3.0, -3.0), red), ((-3.0, -3.0, -3.0), green)] glBegin(GL_QUAD_STRIP) for vertex in vertices: location, color = vertex apply(glColor3f, color) apply(glVertex3f, location) glEnd() def update(self): if self.amountRotated >= 500: self.increment = -2 elif self.amountRotated <= 0: self.increment = 2 glRotate(self.increment, 1.0, 1.0, 1.0) self.amountRotated += self.increment self.openGL.tkRedraw() self.openGL.after(10, self.update) def main(): ColorBox().mainloop() if __name__ == "__main__": main()
推荐
其它的关于pyOpenGL的问题可以关注一下网站:
http://pyopengl.sourceforge.net/
http://pyopengl.sourceforge.net/documentation/installation.html
鸣谢
Deitel的Python编程书籍
http://pyopengl.sourceforge.net/上的相关文章
Python技术交流群中的Quan┃的群友