環境は OS:Windows8 Pro 64 bit,Python: Python2.7 (64bit)
> pip install "pyopengl"
pip が使えなければ,PyPI の pyOpenGL のページ
https://pypi.python.org/pypi/PyOpenGL
から PyOpenGL-3.0.2.win-amd64.exe をダウンロードし,インストールウィザードに従う.もしくは,ソース PyOpenGL-3.0.2.zip をダウンロードして解凍した後,当該フォルダでコマンドプロンプトから
> python setup.py install
とする.これで python から import OpenGL できるようになる.
しかし,このままでは適当なサンプルスクリプト(後述)を動かそうとすると以下のエラーが出た.
1 2 3 4 5 6 7 8 |
Traceback (most recent call last): File "C:\*\***.py", line xx, in setup() File "C:\*\***.py", line xx, in setup glutInit(sys.argv) File "C:\*\lib\site-packages\OpenGL\GLUT\special.py", line 324, in glutInit _base_glutInit( ctypes.byref(count), holder ) TypeError: 'NoneType' object is not callable |
実行時に引数は指定していないので,sys.argv の中身はこのスクリプト teapot.py のパスのはず.どうも DLL が読み込まれていないことが原因らしい.解凍したソースの C:\***\Downloads\PyOpenGL-3.0.2\OpenGL\DLLS にある glut32.dll と glut64.dll を C:\Windows\System32 フォルダへコピーしたらこのエラーはでなくなった.
テストに使ったサンプルスクリプトは以下(こちら ”Pythonでゲーム作りますが何か?” を参考にした).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#!/usr/bin/env python #coding:utf-8 from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * import sys def main(): glutInit(sys.argv) glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH) glutInitWindowSize(300, 300) # window size glutInitWindowPosition(100, 100) # window position glutCreateWindow("teapot") # show window glutDisplayFunc(display) # draw callback function glutReshapeFunc(reshape) # resize callback function init(300, 300) glutMainLoop() def init(width, height): """ initialize """ glClearColor(0.0, 0.0, 0.0, 1.0) glEnable(GL_DEPTH_TEST) # enable shading glMatrixMode(GL_PROJECTION) glLoadIdentity() ##set perspective gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) def display(): """ display """ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() ##set camera gluLookAt(0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) ##draw a teapot glColor3f(1.0, 0.0, 0.0) glutWireTeapot(1.0) # wireframe # glutSolidTeapot(1.0) # solid glFlush() # enforce OpenGL command def reshape(width, height): """callback function resize window""" glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) if __name__ == "__main__": main() |
> python teapot.py
http://sourceforge.net/projects/pyopengl/files/
にある PyOpenGL-Demo をインストールすると色々なデモが見られます.例えば
> python .\PyOpenGL-Demo\GLUT\gears.py
1 2 3 4 5 6 |
25828 frames in 5.0 seconds = 5165.600 FPS 26693 frames in 5.0 seconds = 5338.600 FPS 26626 frames in 5.0 seconds = 5325.200 FPS 26851 frames in 5.0 seconds = 5370.200 FPS 26447 frames in 5.0 seconds = 5289.400 FPS ... |