Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyopengl buffer dynamic read from numpy array

I am trying to write a module in python which will draw a numpy array of color data (rgb) to screen. At the moment I am currently using a 3 dimensional color array like this:

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

binding it to a buffer and using a glVertexAttribArray to broadcast the data to an array of tiles (point sprites) (in this case a 10x10 array) and this works fine for a static image.

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

Currently I've built the buffer with:

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

where buffer_data is the numpy array. What (if anything) could I pass instead (some pointer into memory perhaps?)

like image 283
user1483596 Avatar asked Jul 01 '12 16:07

user1483596


2 Answers

If you want to quickly render a rapidly changing numpy array, you might consider taking a look at glumpy. If you do go with a pure pyopengl solution I'd also be curious to see how it works.

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

like image 189
ali_m Avatar answered Nov 16 '22 15:11

ali_m


glBufferData is for updating the entire buffer as it will create a new buffer each time.

What you want is either:

glMapBuffer / glUnmapBuffer.

glMapBuffer copies the buffer to client memory and alter the values locally, then push the changes back to the GPU with glUnmapBuffer.

glBufferSubData

This allows you to update small sections of a buffer, instead of the entire thing.

It sounds like you also want some class that automatically picks up these changes. I cannot confirm if this is a good idea, but you could wrap or extend numpy.array and over-ride the built in method setitem.

like image 1
Rebs Avatar answered Nov 16 '22 16:11

Rebs