On Wed, Mar 5, 2008 at 11:23 AM, Serg Kovrov <[EMAIL PROTECTED]> wrote:
>
> Here is my silly particle system code:
>
> class Sparks:
> def __init__(self, pos):
> self.pos = pos
> self.particles = tuple({
> 'life': 1.0,
> 'decay': random.uniform(0.5, 0.8),
> 'r': 1.0, 'g': 1.0, 'b': 1.0,
> 'x': 0.0, 'y': 0.0,
> 'vect': utils.normalizeVector2((random.uniform(-1.0,
> 1.0), random.uniform(-1.0, 1.0))),
> 'speed': random.uniform(0.0, 64.0)
> } for i in xrange(64))
> def draw(self):
> glDisable(GL_DEPTH_TEST)
> glPushMatrix()
> glTranslatef(*self.pos)
> glPointSize(5.0)
> glEnable(GL_POINT_SMOOTH)
> glBegin(GL_POINTS)
> for p in self.particles:
> if p['life'] > 0.0:
> glColor4f(p['r'], p['g'], p['b'], p['life'])
> glVertex2f(p['x'], p['y'])
> glEnd()
> glDisable(GL_POINT_SMOOTH)
> glPopMatrix()
> def update_task(self):
> done = False
> while not done:
> time = yield
> done = True
> for p in self.particles:
> if p['life'] > 0.0:
> done = False
> p['life'] -= p['decay'] * time
> p['x'] += p['vect'][0] * p['speed'] * time
> p['y'] += p['vect'][1] * p['speed'] * time
>
>
> `update_task` is a generator, and meant to work with simple task
> queue:
>
> tasks = list()
> reset_time = False
> def append(task):
> global reset_time
> if not tasks:
> pyglet.clock.schedule(update)
> reset_time = False
> task.next() # init generator
> tasks.append(task)
> def update(frame_time):
> global reset_time
> if not reset_time:
> reset_time = True
> frame_time = 0.0
> for task in tasks[:]:
> try:
> task.send(frame_time)
> except StopIteration:
> tasks.remove(task)
> if not tasks:
> pyglet.clock.unschedule(update)
>
>
> I have simple controller which put everything together like this:
>
> sparks = Sparks((x, y, z))
> scene.models.append(sparks) # to draw particles
> tasks.append(sparks.update_task()) # to update particles
>
>
> If someone interested I can post complete code, but don't know
> convenient way to publish 10 files (that is the way i organize my
> project, sorry).
>
>
Weird ... I didn't receive this email until now. Why are you using
the new iterator features of Python 2.5 to make your update an
iterator? ... It's a rather a mind-bending approach for this use
case, and it certainly won't help you with performance (remember that
send() is just a function call). I suggest you make `update' a
normal normal method - not an iterator - and take the time as a
argument. Or just set the time delta on the instance of Sparks and
use it in `draw' to update positions and draw in one loop. Also, If
you have multiple particle systems, you might want to group them in a
single state - the excess glEnable(), glDisable() calls can hurt
performance in the pipeline.
As noted already, though, you're going to run into performance
problems if you have lots of particles, so ultimately you'll
appreciate a C extension or a vertex shader to do anything
CPU-intensive.
Good luck,
--
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
d.p.s
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---