如何在 PyOpenGL 中使用 glDebugMessageCallback

How can I use glDebugMessageCallback in PyOpenGL

我是 Python 和 PyOpenGL 的新手。尝试将我的 C++ 代码移植到 PyOpenGL,我尝试使用调试消息传递功能并定义了以下代码。

def onDebugMessage(*args, **kwargs):
    println('args = {0}, kwargs = {1}'.format(args, kwargs))

def initializeGL(self):
    super().initializeGL()
    glEnable(GL_DEBUG_OUTPUT)
    glDebugMessageCallback(onDebugMessage, None)
    glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, None, GL_TRUE)
    glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG, GL_DEBUG_TYPE_MARKER, 0, GL_DEBUG_SEVERITY_NOTIFICATION, -1, "Starting debug messaging service")

不幸的是,它在调用 glDebugMessageCallback 时崩溃了。

错误信息如下:

Traceback (most recent call last):
File "debug.py", line 12, in initializeGL
  glDebugMessageCallback(onDebugMessage, None)
File "C:\Python34\lib\site-packages\OpenGL\platform\baseplatform.py", line 402, in __call__
    return self( *args, **named )
ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected WinFunctionType instance instead of function

这是否意味着我需要提供本机函数并且无法使用 Python 回调代码?

感谢您的建议。

我刚刚 运行 陷入同样的​​错误。这是我提出的解决方案的独立示例:

from OpenGL import GL as gl
import cyglfw3 as glfw

def cb_dbg_msg(source, msg_type, msg_id, severity, length, raw, user):
    msg = raw[0:length]
    print 'debug', source, msg_type, msg_id, severity, msg

glfw.Init()
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 2)
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.WindowHint(glfw.OPENGL_DEBUG_CONTEXT, True)

window = glfw.CreateWindow(640, 480, "test")
glfw.MakeContextCurrent(window)

# Install our debug message callback
gl.glDebugMessageCallback(gl.GLDEBUGPROC(cb_dbg_msg), None)

# This shader program doesn't exist so you can expect the debug
# message callback to receive an error message
gl.glUseProgram(1234)

还有一个例子运行它:

$ virtualenv venv
$ . venv/bin/activate
$ pip install cyglfw3==3.1.0.2 PyOpenGL==3.1.0
$ python gldbg.py 
debug 33350 33356 2 37190 GL_INVALID_VALUE in glUseProgram
Traceback (most recent call last):
  File "gldbg.py", line 22, in <module>
    gl.glUseProgram(1234)
  File "/home/nicholasbishop/pygldbg/venv/lib/python2.7/site-packages/OpenGL/platform/baseplatform.py", line 402, in __call__
    return self( *args, **named )
  File "/home/nicholasbishop/pygldbg/venv/lib/python2.7/site-packages/OpenGL/error.py", line 232, in glCheckError
    baseOperation = baseOperation,
OpenGL.error.GLError: GLError(
    err = 1281,
    description = 'invalid value',
    baseOperation = glUseProgram,
    cArguments = (1234,)
)

您应该将函数转换为 GLDEBUGPROC:

def onDebugMessage(*args, **kwargs):
    println('args = {0}, kwargs = {1}'.format(args, kwargs))

glDebugMessageCallback(GLDEBUGPROC(onDebugMessage), None)

另外不要忘记 glGetDebugMessageLog 仅适用于 OpenGL 4.3+