Worst case, you can convert via a Python list.
> Numpy arrays now have a ctypes
> property to allow their data to be accessed from C, but i'm guessing
> it's not as simple as that as it probably doesn't contain GLFloats?
I haven't used numpy recently, but this sounds doable. a GLfloat is
the same as a c_float.
Alex.
Yes, it is easily done, and the conversion is a one-liner that does
*not* copy the array. Here's the several relevant lines of code pulled
directly from my pyglet application. This works on Python2.5, Windows
and Linux, with pyglet 1.0 and 1.1.
def PointerToNumpy(a, ptype=ctypes.c_float):
a = numpy.ascontiguousarray(a) # Probably a NO-OP, but
perhaps not
return a.ctypes.data_as(ctypes.POINTER(ptype)) # Ugly and undocumented!
glVertexPointer(3, GL_FLOAT, 0, PointerToNumpy(VertexData))
Hope that helps,
Gary Herron
> >
>
* If you are going to do depth testing, you need to clear the depth
buffer. Otherwise all draws past the first will not pass the depth
test and fail to draw anything.
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
* Your array's type is (by default) numpy.float64 (i.e., doubles),
but you tell glVertexPointer that you are passing floats. This is
what you should tell it. (Or, since doubles are way overkill,
change your array to dtype=numpy.float32).
glVertexPointer(2,GL_DOUBLE,
0,self.cverts.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))
* Your call to glDrawArrays is supposed to provide the number of
vertices not the number of quads.
glDrawArrays(GL_QUADS,0,4*50)
* Your method of building arrays is VERY inefficient -- every append
creates a new array, copies from the old, and ultimately deletes the
old. Try this instead:
self.vertarray = array([ [10+10*i,10+10*i, 10+10*i,20+10*i,
20+10*i,20+10*i, 20+10*i,10+10*i]
for i in range(50)
],
dtype=float64)
self.vertarray=self.vertarray.reshape(50*4,2) # Unneeded,
but nice
(I threw the +10*i into each coordinate to calculate 50 *different*
quads rather than one quad 50 times.)
* The call
glEnable(GL_VERTEX_ARRAY)
is not needed. In fact it's wrong, but seems to neither hurt nor
cause an error. Weird.
* The calls to glPushClientAttrib and glPopClientAttrib do nothing
useful (in this code; Don't know about your real code).
Hope that helps.
Gary Herron
--
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
If the array is contiguous, then yes. The reshape functions return
arrays that share the same underlying data in this case, so you don't
need to continually reshape it for pyglet and then for yourself, just
once during initialisation.
If you're really serious about using numpy with pyglet.graphics, your
best bet is probably to subclass
pyglet.graphics.vertexbuffer.AbstractBuffer to use a numpy array as
the underlying storage (anything else, and you will need to always
copy data into pyglet's internal array or the VBO). If done
correctly, the rest of the pyglet.graphics abstractions should fit
over this numpy array/buffer without modification (I think). Hooking
your subclassed buffer into the Batch as the default class may require
a little hacking (I can provide hooks for this in future releases if
it turns out to be useful).
Alex.