首页 > 解决方案 > 如何在mac os中修复“尝试调用未定义的函数glutInit,在调用之前检查bool(glutInit)”

问题描述

当我尝试在 Mac 中使用 pyopengl 时出现此错误。我用windows找到了解决方案。但我不知道如何在 Mac 中修复它。

这是我的代码:

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

import os

def initFunc():
    glDisable(GL_DEPTH_TEST)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0.0, 400.0, 0.0, 400.0)

def displayFunc():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0, 1.0, 1.0)
    glBegin(GL_TRIANGLES)
    glVertex2f(10.0, 10.0)
    glVertex2f(10.0, 100.0)
    glVertex2f(100.0, 100.0)
    glEnd()
    glFlush()

if __name__ == '__main__':
    glutInit()
    glutInitWindowSize(400,400)
    glutCreateWindow("GL test")
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
    glutDisplayFunc(displayFunc)
    initFunc()
    os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process 
    "Python" to true' ''')
    # prevent GL window from appearing behind other applications
    glutMainLoop()

这是错误

    Traceback (most recent call last):
      File "/Users/wupeihan/Project/learning/python/opengl/traingle.py", line 25, in <module>
        glutInit()
      File "/Users/wupeihan/miniconda3/envs/opengl/lib/python3.7/site- 
    packages/OpenGL/GLUT/special.py", line 333, in glutInit
        _base_glutInit( ctypes.byref(count), holder )
      File "/Users/wupeihan/miniconda3/envs/opengl/lib/python3.7/site- 
    packages/OpenGL/platform/baseplatform.py", line 425, in __call__
        self.__name__, self.__name__,
    OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for 
    bool(glutInit) before calling

我想知道如何在 Mac 中解决它。

标签: pythonmacospyopengl

解决方案


推荐阅读