[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] is fast opengl a possibility in pygame?



On Wednesday 24 August 2005 16:45, Kris Schnee fired a shotgun at the keyboard 
and the following appeared:
> Sami Hangaslammi wrote:
> > On 8/24/05, Drewcore <drewsph@xxxxxxxxx> wrote:
> >>is creating a playable game (speed-wise) a possibility with
> >>pygame and pyopengl?
> >
> > Absolutely. It's just that function calls have such a high overhead in
> > Python, that you have to minimize the frequency and number of OpenGL
> > calls you do from your code. This is pretty easily achieved by liberal
> > use of opengl display lists and vertex/color/texture-arrays. With
> > display lists, you can render a complex scene with a single function
> > call (glCallList), so the overhead of Python stays minimal.
>
> I've played with display lists and had trouble with them. It seems that
> any variables the list descriptions use are fixed at that time, so that
> if a list uses "player.position.x" and it changes, the position to which
> the player is drawn doesn't change.
<snip>

You need to approach displaylists a little differently. As Andrew pointed out, 
anything you pass to a list is indeed fixed. Think of any object in a 
displaylist as being in its own little universe, the center of which is the 
center of your object. For any movement of geometry that's in a displaylist, 
you need to work with the matrix itself.........ie, move it with glTranslate, 
rotate it with glRotate, etc.

With multiple objects, I keep the matrices "pure" by using glPushMatrix/
glPopMatrix. I do initial translations/rotations on init to get my "base" 
matrix, which all other matrices will be copies of. The actual loop goes 
something like this:

1. Call glPushMatrix(), giving me a new matrix that's a duplicate of the 
previous one

2. Do any movement/scaling with glScale, glTranslate, and glRotate

3. Bind the appropriate texture, if applicable

4. Call the appropriate displaylist(s)

5. Call glPopMatrix, to go back to the old matrix

This sequence is repeated for each object I need to render. Of course, there 
are times when I might want to, after calling the displaylist, do some more 
moving/scaling and then calling another displaylist, BEFORE calling 
glPopMatrix. This would happen when one object is "sync'd" with another, such 
as when rendering buildings on a landscape that scrolls by.

There are ways other than glPushMatrix/glPopMatrix to handle multiple matrices 
of course, such as glLoadIdentity as Andrew mentioned, or glLoadMatrix.

You can still use Pygame'ish coords, obviously. In fact in my current game 
(which renders a ship based on screen coords) I have the player's ship as a 
full Pygame sprite, but instead of drawing it normally, I have OpenGL code in 
my update() function, which converts the sprite's screen coords (actually, 
rect position) to units compatible with my OpenGL setup (actually you could 
move the viewpoint far enough away that screen coords match OpenGL coords, 
but I chose not to), and passes these coords to glTranslate.

	-Matt Bailey