使用 glLoadMatrix 和 glm.value_ptr() 时出错

Error when using glLoadMatrix and glm.value_ptr()

我目前正在 python 使用 OpenGL 和 GLM 开发 FPS 风格的相机模块。使用 glm,我生成了一个 gluLookAt() 的视图矩阵。当我尝试使用 glm.value_ptr() 将它加载到 OpenGL 时,它 returns 出现错误:

AttributeError: ("'CtypesPointerHandler' object has no attribute 'arrayByteCount'", <function asArrayTypeSize.<locals>.asArraySize at 0x0000021D3BD0CD08>)

这是我的代码:

viewMatrix = glm.lookAt(self.position, self.position + self.front, self.upVector)
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf(glm.value_ptr(viewMatrix))

请告诉我哪里做错了,也许可以解释一下 glm.value_ptr() returns 到底是什么?提前致谢!

当你使用 PyOpenGL 时,你必须将矩阵转换为列表。见 PyOpenGL - glLoadMatrix:

viewMatrix = glm.lookAt(self.position, self.position + self.front, self.upVector)
matList = [viewMatrix[i][j] for i in range(4) for j in range(4)]

glMatrixMode(GL_MODELVIEW)
glLoadMatrixf(matList)

或者 glLoadMatrix 接受 ctypes.c_float array. See ctypes - Data types 例如:

matArray = (ctypes.c_float *16).from_buffer(viewMatrix)

glMatrixMode(GL_MODELVIEW)
glLoadMatrixf(matArray)

甚至可以使用 numpy.array

matArray = np.array(viewMatrix, dtype=np.float32)

glMatrixMode(GL_MODELVIEW)
glLoadMatrixf(matArray)