LoginSignup
17
20

More than 1 year has passed since last update.

PyOpenGLしてみる

Last updated at Posted at 2016-03-18

最近のWindowsでPythonを使ってOpenGLしてみる。

環境

Windows10(64bit)で作業した。

Pythonは、

で配布しているAnacondaディストリビューションのPython3.5(64bit)を選択。
Anacondaだと最初からnumpy等が入っているのでWindowsならこれがいいのではないか。

PyOpenGLのインストール

最初、下記のようにpipしたのだが、

> C:\Anaconda3\Scripts\pip.exe install PyOpenGL PyOpenGL-Demo

Anacondaディストリビューションではこうするらしい。

> C:\Anaconda3\Scripts\conda.exe install pyopengl

PyOpenGL-Demoで動作確認

からアーカイブをダウンロードして解凍。

> cd PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo
PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo> dir

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2016/03/19      2:27                da
d-----       2016/03/19      2:27                dek
d-----       2016/03/19      2:27                GLE
d-----       2016/03/19      2:27                GLUT
d-----       2016/03/19      2:27                NeHe
d-----       2016/03/19      2:27                proesch
d-----       2016/03/19      2:27                redbook
d-----       2016/03/19      2:27                tom
-a----       2008/12/08     12:25              0 readme.txt
-a----       2008/12/08     12:25              0 __init__.py

NeHe行ってみよか。

PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo> cd NeHe
PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo\Nehe> C:\Anaconda3\python.exe .\lesson1.py
  File ".\lesson1.py", line 141
    print "Hit ESC key to quit."
                               ^
SyntaxError: Missing parentheses in call to 'print'

Oh, python2仕様だ。しかし、

PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo\Nehe> C:\Anaconda3\Scripts\2to3.exe -w .
PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo\Nehe> C:\Anaconda3\python.exe .\lesson1.py
Traceback (most recent call last):
  File ".\lesson1.py", line 8, in <module>
    __version__ = string.split('$Revision: 1.1.1.1 $')[1]
AttributeError: module 'string' has no attribute 'split'

コメントアウト

#__version__ = string.split('$Revision: 1.1.1.1 $')[1]
#__date__ = string.join(string.split('$Date: 2007/02/15 19:25:19 $')[1:3], ' ')
#__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'

再度。

PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo\Nehe> C:\Anaconda3\python.exe .\lesson1.py
Hit ESC key to quit.
Traceback (most recent call last):
  File ".\lesson1.py", line 142, in <module>
    main()
  File ".\lesson1.py", line 97, in main
    glutInit(sys.argv)
  File "C:\Anaconda3\lib\site-packages\OpenGL\GLUT\special.py", line 333, in glutInit
    _base_glutInit( ctypes.byref(count), holder )
  File "C:\Anaconda3\lib\site-packages\OpenGL\platform\baseplatform.py", line 407, in __call__
    self.__name__, self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling

glutが無い。

Glut入れる。

なるほど。

http://ktm11.eng.shizuoka.ac.jp/lesson/modeling.html
からglut32.dll(64bit)を頂いてきて、C:\Windows\System32にコピーした。

エラー変わらず。
コードを追ってみた。

# C:\Anaconda3\Lib\site-packages\OpenGL\platform\win32.py
def GLUT( self ):
   for possible in ('freeglut%s'%(size,),'freeglut', 'glut%s'%(size,)):
       # Prefer FreeGLUT if the user has installed it, fallback to the included 
       # GLUT if it is installed
       try:
           return ctypesloader.loadLibrary(
               ctypes.windll, possible, mode = ctypes.RTLD_GLOBAL
           )
       except WindowsError as err:
           pass
   return None

dllを探しているけどglut64を探しにいってますな。
C:\Windows\System32\glut64.dllにしといた。
再度。

PyOpenGL-Demo-3.0.1b1\PyOpenGL-Demo\Nehe> C:\Anaconda3\python.exe .\lesson1.py
Hit ESC key to quit.
Traceback (most recent call last):
  File ".\lesson1.py", line 142, in <module>
    main()
  File ".\lesson1.py", line 115, in main
    window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
  File "C:\Anaconda3\lib\site-packages\OpenGL\GLUT\special.py", line 73, in glutCreateWindow
    return __glutCreateWindowWithExit(title, _exitfunc)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

エラーが変った。

Python3のUnicode文字列をglutCreateWindowに渡す

115
window = glutCreateWindow(b"Jeff Molofee's GL Code Tutorial ... NeHe '99")

b付けてbyte列にする。
通った。黒いWindowが出た。

teapotのサンプルがあったのでss。
teapots.png

手直しはNeHeと同じ。こういう方法でも可。

glutCreateWindow(sys.argv[0].encode('ascii'))

久しぶりのPyOpenGLだったがとりあえず環境はできた。
次回で、拙作のglglue(不親切で使い方がさっぱりわからん)をpython3向けにメンテナンスしてから、その次で、ShaderとかVertexBufferあたりの手順を確認してみる予定。

17
20
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
20