pyopengl:OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit

File "D:\ProgramData\Anaconda3\envs\pytorch_cpu\lib\site-packages\OpenGL\GLUT\special.py", line 333, in glutInit
    _base_glutInit( ctypes.byref(count), holder )
  File "D:\ProgramData\Anaconda3\envs\pytorch_cpu\lib\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

原因是:pip install pyopengl安装的版本不对
下载whl文件
我的是python3.7+windows+64位: PyOpenGL-3.1.5-cp37-cp37m-win_amd64.whl

测试代码:

#coding:utf-8
import sys
from math import pi as PI
from math import sin, cos
 
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

def drawFunc():
    #清除之前画面
    glClear(GL_COLOR_BUFFER_BIT)
    glRotatef(0.1, 10,5,0)   #(角度,x,y,z)
    glutWireTeapot(0.5)      #画线框茶壶
    #glutSolidTeapot(0.5)      #画实心茶壶
    #刷新显示
    glFlush()
    	    
#使用glut初始化OpenGL
glutInit()
#显示模式:GLUT_SINGLE无缓冲直接显示|GLUT_RGBA采用RGB(A非alpha)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
#窗口位置及大小-生成
glutInitWindowPosition(0,0)
glutInitWindowSize(400,400)
glutCreateWindow(b"first")
 
#调用函数绘制图像
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)      #产生动画函数
#主循环
glutMainLoop()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40437821/article/details/114942161