Re: any guide to drawing primitives

562 views
Skip to first unread message

Martin Balmaceda

unread,
Mar 5, 2013, 3:12:36 AM3/5/13
to pyglet...@googlegroups.com
Hi

Pyglet lacks a surface like pygame has so it doesn't support 2D primitives out of the box (unless you draw the polygons directly in openGL). One alternative I quite like is to use a pycairo surface to draw primitives and then use that as a texture in pyglet.
See below for an example.

Regards
Martin

import pyglet
import ctypes
import cairo

WIDTH = 800
HEIGHT = 600

window = pyglet.window.Window(width=WIDTH, height=HEIGHT)

#cairo
data = (ctypes.c_ubyte * WIDTH * HEIGHT * 4)()
stride = WIDTH * 4
surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_RGB24, WIDTH, HEIGHT, stride);
ctx = cairo.Context(surface)
ctx.translate(200, 200)

#pyglet
texture = pyglet.image.Texture.create_for_size(pyglet.gl.GL_TEXTURE_2D, WIDTH, HEIGHT, pyglet.gl.GL_RGB)

@window.event
def on_draw():
    ctx.set_source_rgb(0, 1, 0)
    ctx.rectangle(5, 5, 50, 50)
    ctx.fill()
   
    window.clear()
   
    pyglet.gl.glEnable(pyglet.gl.GL_TEXTURE_2D)
    pyglet.gl.glBindTexture(pyglet.gl.GL_TEXTURE_2D, texture.id)
   
    pyglet.gl.glTexImage2D(pyglet.gl.GL_TEXTURE_2D, 0, pyglet.gl.GL_RGBA, WIDTH, HEIGHT, 1, pyglet.gl.GL_BGRA, pyglet.gl.GL_UNSIGNED_BYTE, data)
   
    pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
    pyglet.gl.glTexCoord2f(0.0, 1.0)
    pyglet.gl.glVertex2i(0, 0)
    pyglet.gl.glTexCoord2f(1.0, 1.0)
    pyglet.gl.glVertex2i(WIDTH, 0)
    pyglet.gl.glTexCoord2f(1.0, 0.0)
    pyglet.gl.glVertex2i(WIDTH, HEIGHT)
    pyglet.gl.glTexCoord2f(0.0, 0.0)
    pyglet.gl.glVertex2i(0, HEIGHT)
    pyglet.gl.glEnd()
   
    ctx.set_source_rgb(0, 0, 0)
    ctx.paint()

pyglet.app.run()


On Tue, Mar 5, 2013 at 1:44 AM, Joseph Clark <joecl...@gmail.com> wrote:
I know this is a newbie question, but can anybody shed light on how to use Pyglet draw a simple primitive like a filled rectangle?  I can't believe that a web search didn't turn up any examples, but it didn't.  And the OpenGL documentation is impenetrable to me.
 
One article I found (http://www.akeric.com/blog/?p=1510) points to a '2d drawing primitives module' apparently uploaded to this Google group some years ago, but the link is dead now.  That's as close as I've got.

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users...@googlegroups.com.
To post to this group, send email to pyglet...@googlegroups.com.
Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
to do is to be. dobedobedo

Greg Ewing

unread,
Mar 6, 2013, 12:33:17 AM3/6/13
to pyglet...@googlegroups.com
Joseph Clark wrote:
> Perhaps what I should really be asking for is some kind of
> introduction or tutorial to help me understand how OpenGL works.
> Anybody know of such a resource?

There's a famous series of tutorials by NeHe Productions:

http://nehe.gamedev.net/

(Look for "Legacy Tutorials" on the right.) Accompanying
example code is provided in a large variety of languages,
including Python.

But if you're really serious about OpenGL, IMO you need
the OpenGL Programming Guide by Shreiner, Woo, Neider
and Davis (the "red book"). It's dense -- no hand
holding -- but it explains all of the principles very
clearly and completely. It's where I got almost all
of my OpenGL knowledge from.

--
Greg


Peter Enerccio

unread,
Mar 6, 2013, 12:59:18 AM3/6/13
to pyglet...@googlegroups.com


2013/3/6 Greg Ewing <greg....@canterbury.ac.nz>
--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users+unsubscribe@googlegroups.com.

To post to this group, send email to pyglet...@googlegroups.com.
Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.





--
Bc. Peter Vaňušanik
http://www.bishojo.tk
Reply all
Reply to author
Forward
0 new messages