xfscrypt
xfscrypt

Reputation: 276

how to use vertex list in pyglet?

I am making a script which can generate multiple objects in Pyglet. In this example (see link below) there are two pyramids in 3d space, but every triangle is recalculated in every frame. My aim is to make a swarm with a large number of pyramids flying around, but i cant seem to figure out how to implement vertex lists in a batch. (assuming this is the fastest way to do it).

Do they need to be indexed for example? (batch.add_indexed(...) )

A standard seems to be:

batch1  = pyglet.graphics.Batch()

then add vertices to batch1. and finally:

def on_draw():
   batch1.draw()

So how to do the intermediate step, where pyramids are added to vertex lists? A final question: when would you suggest to use multiple batches?

Thank you! apfz

http://www.2shared.com/file/iXq7AOvg/pyramid_move.html

Upvotes: 2

Views: 2947

Answers (1)

Sebastian
Sebastian

Reputation: 6057

Just have a look at pyglet.sprite.Sprite._create_vertex_list for inspiration. There, the vertices for simple sprites (QUADS) are generated and added to a batch.

def _create_vertex_list(self):
        if self._subpixel:
            vertex_format = 'v2f/%s' % self._usage
        else:
            vertex_format = 'v2i/%s' % self._usage
        if self._batch is None:
            self._vertex_list = graphics.vertex_list(4,
                vertex_format,
                'c4B', ('t3f', self._texture.tex_coords))
        else:
            self._vertex_list = self._batch.add(4, GL_QUADS, self._group,
                vertex_format,
                'c4B', ('t3f', self._texture.tex_coords))
        self._update_position()
        self._update_color()

So the required function is Batch.add(vertex_list). Your vertices should only be recalculated if your pyramid changes it's position and not at every draw call. Instead of v2f you need to use v3f for 3D-coordinates and of course you need GL_TRIANGLES instead of GL_QUADS. Here is an example of a torus rendered with pyglet.

Upvotes: 1

Related Questions