gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:48:10 25 4
gpt4 key购买 nike

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 的答案

最佳答案

这个答案可能不会赢得任何奖品,它在凌晨04:00匆忙完成。😣

我无法从代码中得到您想要做什么,但据我所知,问题是如何在 QOpenGLBuffer 的上下文中使用分配/读/写来处理 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()


读方法和写方法一样,需要 buffer.read(offset, data, count) . C++ 签名是 read(int offset, void *data, int count),但 PyQt 实现将其抽象为 read(offset, data, count)。

它确实假设缓冲区绑定(bind)到当前的 OpenGL 上下文。

关于python - 如何在 PyQt5 中正确使用 QOpenGLBuffer 的分配/读/写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61202990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com