Pyopengl dynamic buffer read from numpy array

I am trying to write a module in python that would draw a multidimensional array of color data (rgb) for a screen. At the moment, I am using a 3-dimensional color array as follows:

numpy.ones((10,10,3),dtype=np.float32,order='F') # (for 10x10 pure white tiles) 

associating it with a buffer and using glVertexAttribArray to transfer data to an array of fragments (point sprites) (in this case, a 10x10 array), and this works fine for a static image.

But I want to be able to modify the data in the array and have a buffer reflecting this change, without having to rebuild it from scratch.

I have currently created a buffer using

 glBufferData(GL_ARRAY_BUFFER, buffer_data.nbytes, buffer_data, GL_DYNAMIC_DRAW) 

where buffer_data is a numpy array. What (if you like) could I pass instead (maybe some kind of memory pointer?)

+7
source share
2 answers

If you want to quickly visualize a rapidly changing numpy array, you might consider glumpy . If you go with a clean pyopengl solution, I will also be interested to know how this works.

Edit: see my answer here for an example of how to use Glumpy to view a constantly updated numpy array

+2
source

glBufferData is designed to update the entire buffer, because each time it will create a new buffer.

You also want:

glMapBuffer / glUnmapBuffer .

glMapBuffer copies the buffer to client memory and locally changes values, and then returns the changes to the GPU using glUnmapBuffer.

glBufferSubData

This allows you to update small sections of the buffer instead of everything.

It looks like you also need some kind of class that automatically picks up these changes. I cannot confirm if this is a good idea, but you can wrap or extend numpy.array and overload the built-in setitem method.

0
source

All Articles