All graphics with pyglet are drawn using OpenGL. One way to draw rectangles is:
from pyglet.gl import *
glColor3f(1, 0, 0) # red
glRectf(x, y, x+w, y+h)
A more efficient way to draw the entire array is to copy it into a
texture. Use the pyglet.image.ImageData to create and update the data
in a texture, which is then drawn using the blit() method. If your
numpy array is not in a format that ImageData can use, you can use the
glTexSubImage2D call directly, with a little more effort.
If you've never used OpenGL before this task is going to give you a
steep learning curve -- you may want to skim some of the many
tutorials around on the net first.
You could also take a look at
http://groups.google.com/group/pyglet-users/web/2d-drawing-primitives-module,
which provides some simple drawing primitives without needing to know
OpenGL.
Cheers
Alex.
Can you give some hints as to what would be necessary to either A)
modify ImageData to take a buffer object (or a view of numpy data,
although this is more specific and less general) or B) create a
ImageData-like class that would do something similar?
Thanks,
Andrew
The _ensure_string_data() path is skipped if the data is already in a
format supported by your video card. Off the top of my head, this
means:
* Positive pitch (image rows are ordered bottom-up)
* Not having the subimage workaround enabled
* A format such as 'RGBA', 'RGB' or perhaps 'BGR', 'BGRA' if you have
the relevant extension.
Alex.
I succeeded in displaying numpy arrays without copying the data (via a
slightly different means -- the array's ctypes attribute). I have sent a
patch, including example code to
http://code.google.com/p/pyglet/issues/detail?id=202
(I don't know how to change the type of this report to "enhancement"
rather than "defect".)
I would be happy if you included this in pyglet.
Cheers!
Andrew
Another annoying problem was the data member returned each individual
pixel value back as a string which looks like '\x00' or '\xff'. It'd
be nice if these were integers instead.
--Patrick.