如果我运行这段代码

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

def on_wheel(wheel, direction, x, y):
    print(wheel, direction, x, y)

glutInit()
glutInitWindowSize(100, 100)
glutCreateWindow("glutMouseWheelFunc test")
glutMouseWheelFunc( on_wheel ) # Error here
glutMainLoop()


我得到这个错误

Traceback (most recent call last):
  File "...", line 28, in <module>
    glutMouseWheelFunc( on_wheel )
  File ".../OpenGL/GLUT/special.py", line 148, in __call__
    self.wrappedOperation( cCallback, *args )
  File ".../OpenGL/GLUT/special.py", line 116, in failFunction
    typeName, 'glut%sFunc'%(typeName),
OpenGL.error.NullFunctionError: Undefined GLUT callback function MouseWheel,
check for bool(glutMouseWheelFunc) before calling


这是Windows的解决方案:

How to use FreeGLUT glutMouseWheelFunc in PyOpenGL program?

但是如何在Mac或Linux上的pyopengl中调用glutMouseWheelFunc()

或者更好:是否有便携式解决方案?

最佳答案

函数glutMouseFunc()应该在glutMouseWheelFunc()之前定义,该示例现在可以在Linux上使用以下代码从代码开始工作:

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

def mouse(button, state, x, y):
    print(button, state, x, y)

def on_wheel(wheel, direction, x, y):
    print(wheel, direction, x, y)

glutInit()
glutInitWindowSize(100, 100)
glutCreateWindow("glutMouseWheelFunc test")
glutMouseFunc( mouse )
glutMouseWheelFunc( on_wheel )
glutMainLoop()

10-05 19:19