How do I draw pyglet OpenGL primitives together with Rabbyt sprites?

134 views
Skip to first unread message

Raden Mu'az

unread,
Oct 31, 2008, 3:16:12 AM10/31/08
to Rabbyt
Is there some kind of way to draw lines using pyglet.gl while
rendering rabbyt.Sprite on the same window?

Wolverine

unread,
Oct 31, 2008, 3:44:31 AM10/31/08
to rab...@googlegroups.com
Raden Mu'az pisze:

> Is there some kind of way to draw lines using pyglet.gl while
> rendering rabbyt.Sprite on the same window?
>
You should use PyOpenGL. You must setup PyOpenGL context first for
drawing, then draw your primitives using:
glBegin(GL_LINES)
glVertex3f(x1, y1, z1)
glVertex3f(x2, y2, z2)
glEnd()

You can draw more lines simply specifying more pairs of glVertex3f(...).
Remember though that you need to make matrix setup and transforms first.
AFAIR you then have to issue rabbyt.set_default_attribs(), but I may be
wrong.

Matthew should clarify this a bit for you.

Regards,
Karol Tomala

Raden Mu'az

unread,
Oct 31, 2008, 4:43:42 AM10/31/08
to Rabbyt
Thanks but I put it at the rabbyt/pyglet gameloop, but it won't work!

Code:
while not window.has_exit:
pyglet.clock.tick(1/world.dt)
rabbyt.clear((1,1,1))

window.dispatch_events()

glBegin(GL_LINES)
glVertex3f(20, 40, 0)
glVertex3f(40, 50, 0)
glEnd()

world.update()
world.render()

window.flip()

Anyway, why I can't use pyglet.gl?
Pyglet is somehow faster than PyOpenGL and has easy-to-use wrappers to
draw simple 2d opengl primitives.

Wolverine

unread,
Oct 31, 2008, 7:29:25 AM10/31/08
to rab...@googlegroups.com
Raden Mu'az pisze:

> Thanks but I put it at the rabbyt/pyglet gameloop, but it won't work!
>
> Code:
> while not window.has_exit:
> pyglet.clock.tick(1/world.dt)
> rabbyt.clear((1,1,1))
>
> window.dispatch_events()
>
> glBegin(GL_LINES)
> glVertex3f(20, 40, 0)
> glVertex3f(40, 50, 0)
> glEnd()
>
> world.update()
> world.render()
>
> window.flip()
>
It won't work like this. First you have to set GL_PROJECTION matrix and
perform at least glTranslatef(x, y, z). Before the loop you have to set
this.

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90.0, width/float(height), 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_DEPTH_TEST)

This setting probably overrides rabbyt.set_default_attributes(), so it
may be wrong setting for rabbyt drawing. If unsure look at rabbyt source
code of the set_default_attributes function.

Within the loop, before you issue glBegin try to put in this (-2.5 is Z
distance):

glLoadIdentity()
glTranslate(0.0, 0.0, -2.5)

Z distance has to be negative, because you won't see anything. This
distance is probably not correct, so you'll have to experiment with this
value.


> Anyway, why I can't use pyglet.gl?
> Pyglet is somehow faster than PyOpenGL and has easy-to-use wrappers to
> draw simple 2d opengl primitives.
>

I haven't used pyglet. If pyglet has opengl functions you could probably
use them instead. But I won't help you in this matter.

Regards,
Karol Tomala

Wolverine

unread,
Oct 31, 2008, 7:36:23 AM10/31/08
to rab...@googlegroups.com
Sorry, the formatting went bad. It should be:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90.0, width/float(height), 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_DEPTH_TEST)

But first, try to omit putting in these before the loop and see if only
putting in this before glBegin:

glLoadIdentity()
glTranslate(0.0, 0.0, -2.5)

works for you.

Regards,
Karol Tomala

Raden Mu'az

unread,
Oct 31, 2008, 10:03:32 AM10/31/08
to Rabbyt
Didn't work! :(
PyOpenGl messing around with Pyglet?

Wolverine

unread,
Oct 31, 2008, 12:28:55 PM10/31/08
to rab...@googlegroups.com
Raden Mu'az pisze:

> Didn't work! :(
> PyOpenGl messing around with Pyglet?
>
I'm not familiar with Pyglet. However. Once you setup OpenGL context it
should be possible to use both. PyOpenGL is a low-level API wrapper. In
fact rabbyt uses OpenGL instructions directly within the code. You have
to issue rabbyt.set_default_attribs() before you start messing with
low-level API.

I'm posting here some old mails exchanged with Matthew regarding similar
question. Maybe you'll find it helpful.

Regards,
Karol Tomala

--- cut ---

> First of all I'd like to know what OpenGL functions does
> the OpenGL context for Rabbyt need to work properly. I'd like to try
> integrate truly 3D object and rotate it when Rabbyt is running. What are
> the restrictions? For example as I can deduce sprite quad rendering is
> performed on Sprite.render() am I right? Then I shouldn't interfere with
> it when for example I'm running the 3D rendering in a thread right? This
> can be quite hard to implement because I have to somehow check the
> status of OpenGL context each time when 3D rendering is about to occur.
> Are there any functions exposing Rabbyt/OpenGL rendering status?


I suppose that the biggest thing is the transform matrix. If you change
transformations, make sure to call glLoadIdentity before rendering a rabbyt
sprite. (Unless you *want* to render the sprite with other transformations.)

Also, some people enable back face culling. This doesn't make much sense in a
2d world, so I didn't take it into account. The direction that quads are
drawn in will probably be inconsistent. If you use it for your 3d stuff just
call glDisable(GL_CULL_FACE) before rendering with rabbyt.

> AFAIR glLoadIdentity() resets all transform matrices, but what about
> rendering options?

I can't really give you an itemized list... there is just so much stuff that
will affect it. Mostly, you'll want blending enabled, and the blend function
and texture environment functions set up right, unless of course if you want
something different :-/ Take a look at the set_default_attribs function
inside of rabbyt._rabbyt.pyx.

There really is nothing fancy going on here -- it's just rendering basic
quads.

> Oh this is not good. If I'm about to render complex 3D objects I'd
> certainly NEED hardware culling. It can save a lot FPS. I know it's not
> VERY important for you and I'm not going to use 3D rendering before I
> have proto-engine ready, but after this I'd like to experiment with
> mixing Rabbyt with 3D objects. This could be fun and I could achieve
> great special effects. ;) So may I ask you to put sorting out this
> culling problem somewhere in the future on your TODO list? I'm sure
> people who'd like to use Rabbyt in conjunction with somehow more
> advanced rendering would be pleased too.

That's no problem, you just need to enable culling before rendering a 3d
object, and then disable it before rendering your sprites. 3d engines do
that sort of thing all the time. (You'll also have to enable/disable depth
testing, but you need to do that anyways when using transparencies.)

--- cut ---

Matthew Marshall

unread,
Oct 31, 2008, 2:46:14 PM10/31/08
to rab...@googlegroups.com
Raden Mu'az wrote:
> Thanks but I put it at the rabbyt/pyglet gameloop, but it won't work!
>
> Code:
> while not window.has_exit:
> pyglet.clock.tick(1/world.dt)
> rabbyt.clear((1,1,1))
>
> window.dispatch_events()
>
> glBegin(GL_LINES)
> glVertex3f(20, 40, 0)
> glVertex3f(40, 50, 0)
> glEnd()
>
> world.update()
> world.render()
>
> window.flip()

You probably want to call glDisable(GL_TEXTURE_2D) and
glColor4f(1,1,1,1) before rendering your lines.

The *only* state changes that rabbyt makes are for texture and color.
Take a look at rabbyt._sprites.pyx line 379. Rabbyt keeps things very
simple as far as opengl goes.

> Anyway, why I can't use pyglet.gl?
> Pyglet is somehow faster than PyOpenGL and has easy-to-use wrappers to
> draw simple 2d opengl primitives.

Either one will work. You can use both in the same application.
PyOpenGL just provides a nicer interface for some operations. (Like
glGenTexture)

MWM

Raden Mu'az

unread,
Oct 31, 2008, 10:55:51 PM10/31/08
to Rabbyt
I did this, the game flickers for a second (saw some sprites), and
then blank.
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90.0, 640/float(480), 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_DEPTH_TEST)

while not window.has_exit:
pyglet.clock.tick()
rabbyt.clear((1,1,1))

window.dispatch_events()

glBegin(GL_LINES)
glVertex3f(20, 40, 0)
glVertex3f(40, 50, 0)
glEnd()

world.update()
world.render()

window.flip()

If I add glDisable(GL_TEXTURE_2D) and glColor4f(1,1,1,1), I see only a
blank white screen. :(

I just need to use pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
('v2i', (a, b, c, d)) together with rabbyt.Sprite().render().

Can someone give some example code on how to do this?
Message has been deleted
Message has been deleted

Raden Mu'az

unread,
Oct 31, 2008, 11:41:40 PM10/31/08
to Rabbyt
Okay, after messing around with Pyglet, I found that you don't need
custom main loop to have Rabbyt working with Pyglet.

http://rabbyt.googlegroups.com/web/pyglet-fps.png?gsc=q_pHQBYAAADe-YJHo6-eyuJ3E6yL9e3lAJwcXtjUFRYNEgqVs1KnTQ

Somehow I successfully merged rabbyt and pyglet with this code, which
draws rabbyt.Sprite and pyglet.clock.ClockDisplay(), on the same
window. I don't know wheather this code is optimized or not; But as
far as I know, using ready-made Pyglet mainloop is way much efficient
than custom main loop.

---------------------------------

import pyglet,rabbyt
from pyglet.window import Window

window = Window()
rabbyt.set_default_attribs()

pyglet.clock.schedule(rabbyt.add_time)
pyglet.clock.set_fps_limit(50)
fps_display = pyglet.clock.ClockDisplay()

s = rabbyt.Sprite()
s.xy = 320,240

@window.event
def on_draw():
rabbyt.clear((0,0,0))
s.render()
fps_display.draw()

pyglet.app.run()

------------------------------

Matthew, could you update the examples in Rabbyt source code? I got
lots of confusion in integrating Rabbyt/Pymunk and Rabbyt/Pyglet.
Recently I wasted a lot of time finding out ways to do this rather
than working on my game. Thanks in advance.

Matthew Marshall

unread,
Nov 1, 2008, 2:07:52 PM11/1/08
to rab...@googlegroups.com
Raden Mu'az wrote:
> Matthew, could you update the examples in Rabbyt source code? I got
> lots of confusion in integrating Rabbyt/Pymunk and Rabbyt/Pyglet.
> Recently I wasted a lot of time finding out ways to do this rather
> than working on my game. Thanks in advance.

Yes, the examples really could use an update. I wrote them before the
pyglet loop existed. Writing your own loop was how it was done at that
time.

MWM

Raden Mu'az

unread,
Nov 2, 2008, 1:59:38 AM11/2/08
to Rabbyt
Now I have figured it out on how to draw pyglet-OpenGL primitives
while drawing rabbyt sprites.

This code draws a line on the screen

-----------------------------
import pyglet,rabbyt
from pyglet.window import Window

window = Window()
rabbyt.set_default_attribs()

pyglet.clock.schedule(rabbyt.add_time)

s = rabbyt.Sprite()
s.xy = 320,240
s.rgb = 0,0,0

@window.event
def on_draw():
rabbyt.clear((1,1,1))
s.render()
pyglet.gl.glColor3f(0.0, 0.0, 0.0) # set color to black
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
('v2i', (10, 15, 30, 35)) )

pyglet.app.run()
-----------------------------

Thanks Matthew and Wolverine

Keeyai

unread,
Nov 23, 2008, 12:15:19 PM11/23/08
to Rabbyt
Have you tried this with different colored sprites and/or sprites that
use textures? I've had trouble before rendering real sprites (not just
empty black ones like you have here) and gl primitives before. I
solved it as Matthew mentions above - changing the settings I need,
rendering all my non rabbyt stuff, then changing them back and doing
the sprites.
Reply all
Reply to author
Forward
0 new messages