exposing underlying storage from a Vec2Array

8 views
Skip to first unread message

Tartley

unread,
Nov 12, 2010, 8:32:31 AM11/12/10
to Grease Users
Hey,

I was hoping that under the covers, Vec2Array owns a contiguous C
arrays of floats, which I could somehow get access to and, for
example, pass directly to OpenGL as a vertex array.

Is this sort of thing possible? I've been browing attributes of
Vec2Array instances from the python command-line, and looking at
cvector.c, but it's been many years since I used C, and apparently I'm
a bear of too little brain to figure it out.

Jonathan

Casey Duncan

unread,
Nov 12, 2010, 11:56:15 AM11/12/10
to grease...@googlegroups.com
Hey there,

Vec2Array has an array of doubles underlying it, which is good for
precision, but not so good for using directly with OpenGL. My
intention is to add an api to let you use a Vec2Array (actually any
Seq2 subclass, which Vec2Array is one) to populate an array of floats
using the Python buffer api. This should allow you to directly
populate a ctypes float array, a stdlib float array, or a numpy float
array. There's a bit more overhead there converting doubles to floats,
but it lets the canonical data have a much higher precision, which imo
is worth the tradeoff.

If that's something you could use now, I can try and add it in in the
next few days.

-Casey

Tartley

unread,
Nov 18, 2010, 1:54:23 PM11/18/10
to Grease Users
On Nov 12, 4:56 pm, Casey Duncan <casey.dun...@gmail.com> wrote:
> Hey there,
>
> Vec2Array has an array of doubles underlying it, which is good for
> precision, but not so good for using directly with OpenGL. My
> intention is to add an api to let you use a Vec2Array (actually any
> Seq2 subclass, which Vec2Array is one) to populate an array of floats
> using the Python buffer api. This should allow you to directly
> populate a ctypes float array, a stdlib float array, or a numpy float
> array. There's a bit more overhead there converting doubles to floats,
> but it lets the canonical data have a much higher precision, which imo
> is worth the tradeoff.
>
> If that's something you could use now, I can try and add it in in the
> next few days.
>
> -Casey
>
> On Fri, Nov 12, 2010 at 5:32 AM, Tartley <tart...@tartley.com> wrote:
> > Hey,
>
> > I was hoping that under the covers, Vec2Array owns a contiguous C
> > arrays of floats, which I could somehow get access to and, for
> > example, pass directly to OpenGL as a vertex array.
>
> > Is this sort of thing possible? I've been browing attributes of
> > Vec2Array instances from the python command-line, and looking at
> > cvector.c, but it's been many years since I used C, and apparently I'm
> > a bear of too little brain to figure it out.
>
> >  Jonathan
>
>

I see, very interesting. Thanks for the insight.

I guess for I'm trying to do, I don't need the double precision. And
ultimately I'm looking to store arrays of all kinds of opengl-y
things, including arrays of vectors of floats, arrays of matrices of
floats, and arrays of color vectors of unsigned bytes. So perhaps
numpy is a better fit for what I'm trying to do?

Specifically, I want to have a contiguous array of data I can pass
directly to OpenGL (e.g. for vertex arrays), but retain fast access to
individual members within the array from Python. I'm currently using
ctypes arrays, and my understanding is that crossing the ctypes
boundary to update a single member of the array is relatively
expensive.

I'll give numpy a try, see whether I can figure that out.

Casey Duncan

unread,
Nov 18, 2010, 5:24:57 PM11/18/10
to grease...@googlegroups.com

Yes numpy is probably a reasonable place to start. I typically find
storing canonical data in an opengl-friendly format less than ideal,
so I typically pay the price of a copy each time I want to update the
vertex array data. If it changes a lot (like nearly every vertex,
nearly every frame), the vertex array is basically disposable anyway.
Though it's perfectly possible to have some parts of the array static
(like colors and texture coordinates) while others are dynamic. Then
you can just update/re-create the dynamic parts each time.

Planar is really designed to be the "model" in an MVC architecture.
The vertex array data would be part of the view, and may or may not
persist between frames, that would be up to the view to decide.

-Casey

Jonathan Hartley

unread,
Nov 18, 2010, 8:04:02 PM11/18/10
to grease...@googlegroups.com


Right. That makes sense, I was thinking that my application was a bit of
an abuse of Planar, but was clutching all kinds of straws looking for
inspiration. Thanks for the guidance.

Jonathan

--
Jonathan Hartley Made of meat. http://tartley.com
tar...@tartley.com +44 7737 062 225 twitter/skype: tartley


Reply all
Reply to author
Forward
0 new messages