如何在PyQt5中正确使用QOpenGLBuffer的分配/读取/写入?

2024-04-24 22:15:42 发布

您现在位置:Python中文网/ 问答频道 /正文

This需要一个void*数据输入。所以,我让它和这样的东西一起工作

vbo = QtGui.QOpenGLBuffer(QtGui.QOpenGLBuffer.VertexBuffer)
vrtxAttrs = [1., 1., 1., 0., 0.1, 0.1, 1.0,
             -1., -1., -1., 1.0, 0.9, 0.9, 1.0] # a list of interleaved positions xyz and colors rgba
data = numpy.array(vrtxAttrs, dtype=numpy.float32)

老实说,我不知道我是怎么来的。我尝试了很多东西,却无意中使用了data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)).contents。请告诉我这很糟糕,还有更好的办法

# How I allocate..
vbo.allocate(data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)).contents,
                          sys.getsizeof(data))
# How I write.. (I believe this calls glBufferSubData internally)
# I intend to change the second vertex's position and color.
newdata = [2., 2., 2., 0.5, 0.4, 0.4, 1.]
toWrite = numpy.array(newdata, dtype=numpy.float32)
offset = 1 * 7 * 4 # this has to be in bytes. (1 is the index, 7 the stride, 4 the bytesize.)
count = len(newdata) * 4 # this has to be in bytes.
vbo.write(offset, data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)).contents, count)
# How I read,
# I don't read, since I don't know what weird cast I need to do to read.
# The usage is documented as bool QOpenGLBuffer::read(int offset, void *data, int count)

这些Python文档是荒谬的,是的,我们知道如何在C++中使用它们,Python呢?首先,我在寻找解释sip.voidptr的答案


Tags: thetonumpyreaddataascontentsthis
1条回答
网友
1楼 · 发布于 2024-04-24 22:15:42

这个答案可能不会赢得任何奖品,它是在凌晨4点匆忙完成的。😣

我无法从代码中确切了解您想要做什么,但据我所知,问题是如何在QOpenGLBuffer的上下文中使用allocate/read/write来处理NumPy数组

# Your imports / initializer code here...

buffer = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer)
buffer.create() #Creates the buffer object in the OpenGL server
buffer.bind()   # Binds object to the current OpenGL context
buffer.allocate(120) # How many bytes to allocate

data = numpy.array([2., 2., 2., 0.5, 0.4, 0.4, 1.], dtype = numpy.float32).toString()

# Write
buffer.write(0, data, len(data))
# Release buffer
buffer.release()

read方法与write方法类似,它需要buffer.read(offset, data, count)。C++签名被读取(int偏移,空隙*数据,int计数),但是PyQT实现将其抽象为读取(偏移、数据、计数)。p>

它确实假设缓冲区绑定到当前OpenGL上下文

相关问题 更多 >