Hello,

I'm running a pyglet demo:

http://tartley.com/files/stretching_pyglets_wings/   (try demo #3, I also 
attached the files)

Problem is, I'm seeing about about 17 fps on a fairly powerful desktop 
machine (i7, radeon 5800 series).  The author got about 40  on a much older 
laptop.  (http://tartley.com/files/stretching_pyglets_wings/presentation/)

Knowing what other users are seeing FPS wise would be helpful and any 
explanation as to what I can do to speed things up would be very 
appreciated.  Python version is 32 bit 3.3

Thanks!


-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


#! python -O
"""
Demo3
A single vertlist can be drawn in more than one place at once.
Entities reference a vertlist, and maintain their own x,y,angle.
"""
from __future__ import division
from math import cos, pi, sin, sqrt
from random import gauss, uniform
from sys import stdout
from pyglet import app, clock, image
from pyglet.window import key, Window
from pyglet.window.key import symbol_string
from pyglet.gl import *
from camera import Camera
from entity import Entity

rad2deg = 180 / pi

verts = [
    ((000, 255, 000), (+0, -5)),
    ((000, 000, 255), (-8, +5)),
    ((255, 000, 000), (+8, +5)),
]

def create_flock(num_ents):
    entities = []
    max_radius = 400
    min_radius = 40
    for idx in range(num_ents):
        length = sqrt(uniform(0, max_radius - min_radius) ** 2) + min_radius
        angle = uniform(-pi, +pi)
        x = length * sin(angle)
        y = length * cos(angle)
        ent = Entity(verts, (x, y), uniform(-pi, +pi))
        entities.append(ent)
    ent = Entity(verts, (0, 0))
    entities.append(ent)
    return entities

entities = create_flock(800)

win = Window(fullscreen=True, visible=False, vsync=0)
clockDisplay = clock.ClockDisplay()
glClearColor(0.4, 0.5, 0.3, 0)
camera = Camera((0, 0), 5)

@win.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT)
    camera.update()
    camera.focus(win.width, win.height)

    for entity in entities:

        glPushMatrix()
        glTranslatef(entity.x, entity.y, 0)
        glRotatef(entity.angle * rad2deg, 0, 0, 1)

        glBegin(GL_TRIANGLES)
        for color, position in entity.verts:
            glColor3ub(*color)
            glVertex2f(*position)
        glEnd()

        glPopMatrix()

    camera.hud_mode(win.width, win.height)
    clockDisplay.draw()

def update(dt):
    for idx, entity in enumerate(entities):
        entity.angle += ((entity.x) + abs(entity.y)) / 10000
        entity.x += sin(entity.angle) / (1+idx) * 10
        entity.y -= cos(entity.angle) / (1+idx) * 10
    camera.zoom(1.008)

clock.schedule(update) # triggers on_draw event when done

key_handlers = {
    key.ESCAPE: lambda: win.close(),
    key.PAGEUP: lambda: camera.zoom(2),
    key.PAGEDOWN: lambda: camera.zoom(0.5),
    key.LEFT: lambda: camera.pan(camera.scale, -pi/2),
    key.RIGHT: lambda: camera.pan(camera.scale, +pi/2),
    key.DOWN: lambda: camera.pan(camera.scale, pi),
    key.UP: lambda: camera.pan(camera.scale, 0),
    key.COMMA: lambda: camera.tilt(-1),
    key.PERIOD: lambda: camera.tilt(+1),
}

@win.event
def on_key_press(symbol, modifiers):
    handler = key_handlers.get(symbol, lambda: None)
    handler()

print("keys to try:", [symbol_string(k) for k in key_handlers.keys()])
stdout.flush()
win.set_visible()
app.run()

class Entity(object):

    def __init__(self, verts, position=None, angle=0):
        self.verts = verts
        if position is None:
            position = (0, 0)
        self.x, self.y = position
        self.angle = angle

"""
Camera tracks a position, orientation and zoom level, and applies openGL
transforms so that subsequent renders are drawn at the correct place, size
and orientation on screen
"""
from __future__ import division
from math import sin, cos

from pyglet.gl import (
    glLoadIdentity, glMatrixMode, gluLookAt, gluOrtho2D,
    GL_MODELVIEW, GL_PROJECTION,
)


class Target(object):

    def __init__(self, camera):
        self.x, self.y = camera.x, camera.y
        self.scale = camera.scale
        self.angle = camera.angle


class Camera(object):

    def __init__(self, position=None, scale=None, angle=None):
        if position is None:
            position = (0, 0)
        self.x, self.y = position
        if scale is None:
            scale = 1
        self.scale = scale
        if angle is None:
            angle = 0
        self.angle = angle
        self.target = Target(self)


    def zoom(self, factor):
        self.target.scale *= factor

    def pan(self, length, angle):
        self.target.x += length * sin(angle + self.angle)
        self.target.y += length * cos(angle + self.angle)

    def tilt(self, angle):
        self.target.angle += angle


    def update(self):
        self.x += (self.target.x - self.x) * 0.1
        self.y += (self.target.y - self.y) * 0.1
        self.scale += (self.target.scale - self.scale) * 0.1
        self.angle += (self.target.angle - self.angle) * 0.1


    def focus(self, width, height):
        "Set projection and modelview matrices ready for rendering"

        # Set projection matrix suitable for 2D rendering"
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        aspect = width / height
        gluOrtho2D(
            -self.scale * aspect,
            +self.scale * aspect,
            -self.scale,
            +self.scale)

        # Set modelview matrix to move, scale & rotate to camera position"
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(
            self.x, self.y, +1.0,
            self.x, self.y, -1.0,
            sin(self.angle), cos(self.angle), 0.0)


    def hud_mode(self, width, height):
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluOrtho2D(0, width, 0, height)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

Reply via email to