1

我正在尝试运行 OpenGL Core Profile Qt 演示的 PySide 版本 - How_to_use_OpenGL_Core_Profile_with_Qt

它使用 QGLBuffer.allocate() 方法(C++ 代码)

float points[] = { -0.5f, -0.5f, 0.0f, 1.0f,
                    0.5f, -0.5f, 0.0f, 1.0f,
                    0.0f,  0.5f, 0.0f, 1.0f };
m_vertexBuffer.allocate( points, 3 * 4 * sizeof( float ) );

Pythonic方式将是:

points = [-0.5, -0.5, 0.0, 1.0,
           0.5, -0.5, 0.0, 1.0,
           0.0,  0.5, 0.0, 1.0]
m_vertexBuffer.allocate(points)

但是当我运行此代码时,出现以下错误

TypeError: 'PySide.QtOpenGL.QGLBuffer.allocate' called with wrong argument types:
  PySide.QtOpenGL.QGLBuffer.allocate(list)
Supported signatures:
  PySide.QtOpenGL.QGLBuffer.allocate(void, int = -1)
  PySide.QtOpenGL.QGLBuffer.allocate(int) 

我发现用于此功能的单元测试- 它使用 QByteArray。

data = QByteArray("12345")
b.allocate(data)

但我不明白如何将 Python 列表转换为 QByteArray 或将分配与列表一起使用。还是 PySide 包装函数中的错误?

4

1 回答 1

2

看来我找到了解决方案。我们应该使用 struct 或 array 之类的 python 模块。例如:

from array import *

points = [0.5, 1, -0.5]
data = array('f', points)
# data.tostring() - returns packed data with size of len(data.tostring())
于 2012-11-10T12:30:36.203 回答