2

我对 pyopengl 及其导入有一个奇怪的问题,下面是一个从一个更大的测试程序中剪下来的小测试程序,但这足以说明问题。您需要安装 PyOpenGL,如果您按原样运行它,它会显示一个空白窗口,如果您单击并拖动它会打印出一些 0。如果您取消注释下面的行HANDLE.final = True,则它会停止工作,请参阅下面的回溯。

from OpenGL.GL import *
from OpenGL.GLUT import *
from ctypes import POINTER
import sys

HANDLE = POINTER(None)
#HANDLE.final = True

def display():
    glutSwapBuffers()

glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(400,400)
glutCreateWindow("GLUT Window")

glutDisplayFunc(display)

def readPx(x,y):
    data_all = glReadPixels(x, y, 1, 1, GL_RGB, GL_BYTE)
    print data_all

glutMotionFunc(readPx)

glutMainLoop()

未注释该行时,它会执行以下操作:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\OpenGL\GLUT\special.py", line 130, in safeCall
    return function( *args, **named )
  File "C:\Users\Robin\dev\ogl_bug\ogl_bug.py", line 19, in readPx
    data_all = glReadPixels(x, y, 1, 1, GL_RGB, GL_BYTE)
  File "C:\Python27\lib\site-packages\OpenGL\GL\images.py", line 371, in glReadPixels
    imageData
  File "C:\Python27\lib\site-packages\OpenGL\platform\baseplatform.py", line 402, in __call__
    return self( *args, **named )
ArgumentError: argument 7: <type 'exceptions.TypeError'>: wrong type
GLUT Motion callback <function readPx at 0x02CC5830> with (238, 190),{} failed: returning None argument 7: <type 'exceptions.TypeError'>: wrong type

在我更大的程序中,当尝试导入 OpenGL.raw.WGL._types(由任何 WGL Opengl 扩展导入但这是仍然导致错误的最小片段时)时,此 HANDLE 代码被深埋 5 或 6 个导入。

我不明白为什么这条线的存在会对看似不相关的 gl 调用产生影响——如何在不破坏 PyOpenGL 其他部分的情况下使用加载这个扩展?

4

1 回答 1

1

我找到了原因 - Give POINTER(None)back ctypes.c_void_p,它是一个单例,并且意味着该.final属性在此导入后可见,可能会在稍后扰乱某些类型检查机制。

它在 PyopenGL 的 github 版本中得到修复。这是提交:

https://github.com/mcfletch/pyopengl/commit/f087200406a37fc4b99eaad701d18bc64ded2d71

您可以在当前版本的 PyOpenGL 中修复它,方法是在任何 OpenGL 扩展之前导入这个小模块:

import OpenGL.raw.WGL._types as t
from ctypes import _SimpleCData, _check_size

delattr(t.HANDLE, "final")

class HANDLE(_SimpleCData):
    _type_ = "P"

_check_size(HANDLE)

HANDLE.final = True

t.HANDLE = HANDLE
t.HGLRC = HANDLE
t.HDC = HANDLE

t.HPBUFFERARB = HANDLE
t.HPBUFFEREXT = HANDLE

这会导入导致问题的 PyOpenGL 模块,撤消对 的损坏c_void_p,然后从上面的 github 链接将HANDLE变量及其别名重新分配给解决方案。_SimpleCData

于 2018-11-07T01:16:10.353 回答