106.07.05 使用python3執行PyOpenGL

恩....高中同學最近自學python
想使用python3執行 PyOpenGL
卻一直吐錯誤訊息

錯誤訊息:
Traceback (most recent call last):
File "D:\cygwin\home\user\pro\test\pyOpenGLWindow.py", line 46, in <module>
window = OpenGLWindow()
File "D:\cygwin\home\user\pro\test\pyOpenGLWindow.py", line 17, in __init__
glutInit(sys.argv)
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\GLUT\special.py", line 333, in glutInit
_base_glutInit( ctypes.byref(count), holder )
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\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


然後我也是查超久
赫然發現竟然不是 python3、pip、pyopengl 的錯
而是系統缺少對應的 .dll

解法
這裡下載glutdlls.zip

解壓縮後將 glut.dll 和 glut32.dll 複製(也是可以剪下)
貼到
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\OpenGL\DLLS
(前面路徑可能不一樣,請依照自己的安裝路徑動作)

再來就可以直接執行了
見證奇蹟的一刻OuO

不過呢 Cygwin 似乎尚未支援,要的話可能要下載原始碼自行編譯
所以只能開 M$ 的 cmd 使用囉



參考資料:
Attempt to call an undefined function glutInit
Starting off with OpenGL under Cygwin



提供測試用程式碼:
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

class OpenGLWindow:
    def __init__(self, width=640, height=480, title='PyOpenGL'):
        # Initialization
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(width, height)
        self.window = glutCreateWindow(title)
        glutDisplayFunc(self.Draw)
        self.InitGL(width, height)
    def Draw(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        glTranslatef(0.0, 0.0, -1.0)
        glColor3f(0.0, 1.0, 0.0)
        glRasterPos2f(0.0, 0.0)
        self.DrawText('PyOpenGL')
        glutSwapBuffers()
    def DrawText(self, string):
        for c in string:
            glutBitmapCharacter(GLUT_BITMAP_8_BY_13, ord(c))
    def InitGL(self, width, height):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClearDepth(1.0)
        glDepthFunc(GL_LESS)
        glEnable(GL_DEPTH_TEST)
        glShadeModel(GL_SMOOTH)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0, float(width)/float(height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)
    def MainLoop(self):
        glutMainLoop()
window = OpenGLWindow()
window.MainLoop()

沒有留言:

張貼留言

^ Top