2d drawing

232 views
Skip to first unread message

dima...@gmail.com

unread,
May 27, 2008, 12:26:33 AM5/27/08
to pyglet-users
Looked at this code example

http://ethanmiller.name/notes/pyglet_pycairo/

seems like the way to bring cairo into the equation is to render out a
context and then re-import it into pyglet as a GLtexture?

I am trying to see if I can use SVG formated images and the above
solution works but seems rather inefficient :(

Any ideas as to how to bring vector graphics into the equation.


d

Ben Sizer

unread,
May 27, 2008, 4:46:07 AM5/27/08
to pyglet-users
If you created a pyglet image of the size you wanted, I imagine it
shouldn't be too difficult to set that as the target for the
ImageSurface you create, and therefore to have Cairo draw directly to
there. But a lot of that will depend on the low level details of how
Python is wrapping both these libraries, eg. whether you can easily
get that raw data in the format you need and whether PyCairo exposes
the functions to create ImageSurfaces from it.

--
Ben Sizer

Dunk Fordyce

unread,
May 27, 2008, 5:42:43 AM5/27/08
to pyglet...@googlegroups.com
This doesnt help with the svg issue but Ive used code like below for
cairo which I think is better than the code pasted in the blog post.

(This method should work for SVG too but Im not sure how to use
'create_for_data' along with an svg surface - you can probably just
draw the svg surface to the other surface )

# do the following once at init...
data = (ctypes.c_ubyte * w * h * 4)()
stride = w * 4
surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32,
w, h, stride);

texture = pyglet.image.Texture.create_for_size(GL_TEXTURE_2D, w, h, GL_RGBA)

# when drawing....
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture.id)

if needs_redraw:
ctx = cairo.Context(surface)
# .... do you cairo draw/svg calls here ....
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA,
GL_UNSIGNED_BYTE,
data)

# ... draw a quad here

Richard Jones

unread,
May 27, 2008, 7:14:36 AM5/27/08
to pyglet...@googlegroups.com
On Tue, 27 May 2008, Dunk Fordyce wrote:
> This doesnt help with the svg issue but Ive used code like below for
> cairo which I think is better than the code pasted in the blog post.

Looks much nicer indeed! That's precisely the sort of thing it'd be good to
have in the pyglet-users web space.

Hey, it looks like the web pages are back!

Could you please add something?


Thanks,

Richard

Dunk Fordyce

unread,
May 27, 2008, 7:22:36 AM5/27/08
to pyglet...@googlegroups.com
> Looks much nicer indeed! That's precisely the sort of thing it'd be good to
> have in the pyglet-users web space.
>
> Hey, it looks like the web pages are back!
>
> Could you please add something?

ill write up a simple example when i get time unless someone beats me to it

Alex Holkner

unread,
May 27, 2008, 7:22:42 AM5/27/08
to pyglet...@googlegroups.com
On 5/27/08, Dunk Fordyce <dunkf...@googlemail.com> wrote:

> glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA,
> GL_UNSIGNED_BYTE,
> data)

glTexSubImage2D (equivalently, pyglet.image.Texture.blit_into) is much
more efficient than glTexImage2D for updating a texture (it is how
video frames are rendered, for example).

Alex.

dima...@gmail.com

unread,
May 28, 2008, 10:55:48 PM5/28/08
to pyglet-users
Thanks everyone. Will keep you posted on cairo.svg experiment.

Basically I'm thinking of doing something Processing-like, using
pyglet + cairo + a few serial and comm libraries (possibly OpenCV, if
I feel really brave). I am a pretty horrible programmer but I know I
don't like Java :)


On May 27, 7:22 am, "Alex Holkner" <alex.holk...@gmail.com> wrote:

Leonardo Santagada

unread,
May 31, 2008, 8:32:18 PM5/31/08
to pyglet-users


On May 27, 6:42 am, "Dunk Fordyce" <dunkford...@googlemail.com> wrote:
> This doesnt help with the svg issue but Ive used code like below for
> cairo which I think is better than the code pasted in the blog post.
>
> (This method should work for SVG too but Im not sure how to use
> 'create_for_data' along with an svg surface - you can probably just
> draw the svg surface to the other surface )
[snip]

isn't there a way to make cairo draw directly to a opengl context
(using the opengl backend of cairo)? Then it would be much faster than
this and maybe you could mix cairo drawing with regular pyglet
drawing.
Reply all
Reply to author
Forward
0 new messages