簡體   English   中英

如何在 PyQt5 中正確使用 QOpenGLBuffer 的分配/讀/寫?

[英]How to correctly use QOpenGLBuffer's allocate/read/write in PyQt5?

需要一個 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()

read 方法類似於 write 方法,它需要buffer.read(offset, data, count) 讀取 C++ 簽名(int offset, void *data, int count),但 PyQt 實現將其抽象為 read(offset, data, count)。

它確實假設緩沖區綁定到當前 OpenGL 上下文。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM