1

Environment:

Python: 3.6.6
pyglet version: 1.3.2

Code example:

Adding element into pyglet.graphics.Batch() where I understand how to set z axis

# e.g. z = -2
texture_group = pyglet.graphics.TextureGroup(
    pyglet.image.load(os.path.join(GROUND_DIR, "t1.png")).texture
)

self.vertices = self.batch.add(
    4, pyglet.gl.GL_QUADS,
    texture_group,
    ('v3f', (x, y, z,
             x_, y, z,
             x_, y_, z,
             x, y_, z)),
    ('t2f', (0, 0, 1, 0, 1, 1, 0, 1)))

But I can't find way to set z axis value during usage pyglet.sprite.Sprite()

image = pyglet.image.load(os.path.join(MOVEMENT_DIR, "t1_1.png"))
image2 = pyglet.image.load(os.path.join(MOVEMENT_DIR, "t1_2.png"))
self.sprite = pyglet.sprite.Sprite(
    img=pyglet.image.Animation(
        frames=[
            pyglet.image.AnimationFrame(image, 1),
            pyglet.image.AnimationFrame(image2, 1)
        ]
    ),
    x=x,
    y=y,
    batch=self.batch
)

Sprite is drawn on z=0. e.g. I need to move in on z=-1

What i was trying to do

I tried to set z directly to image texture

texture = image.texture
texture.z = -1

But anyway sprite was drawn on z=0 again.

Question

Is it possible to "say" pyglet.sprite.Sprite() where on z axis it should be drawn?

I would be very appreciated for any advice/link/etc, because I can't find this aspect on documentation and can't find examples with same situation.

Yuriy Leonov
  • 536
  • 1
  • 9
  • 33
  • Either you manipulate individual vertices by iterating over `self.sprite._vertex_list.vertices` or you use `glTranslated(0, 0, 100)` followed by `self.batch.draw()` *(as a poor example)*. – Torxed Oct 16 '18 at 07:39
  • @Torxed I'm using `glTranslated(x, y, z)` function. And you gave me some idea to keep several batches which will be drawn separately with different `z` value in `glTranslated` – Yuriy Leonov Oct 16 '18 at 12:02
  • I see your issue, and I can honestly say I've never thought about it. Because I usually stop using sprites when I start working with 3D spaces. Sprites are extremely good and versatile for 2D applications, but other than that I quickly find myself manipulating vertices and shaders manually instead. – Torxed Oct 16 '18 at 12:04
  • @Torxed Shaders. Hm. I've heard it several times, but never try to figure out what it is. If it could be replacement for animated `Sprite` you help me alot. If you have any links about shaders and how to animate them - I would be very appreciated. – Yuriy Leonov Oct 16 '18 at 12:15
  • 1
    Animations are essentially a sequence of vertices in "frames". Meaning you iterate over a static model, jump one frame, render it, jump one more, render that. Shaders is a complicated language to define different effects, but for basic purposes you won't need to worry about that yet. Focus on generating your models into a static vertices cloud and iterate over it efficiently. I'd skip the usage of Sprites for this. For instance https://medium.com/@yvanscher/opengl-and-pyglet-basics-1bd9f1721cc6 and for images https://bryceboe.com/2011/10/08/moving-images-in-3d-space-with-pyglet/ – Torxed Oct 16 '18 at 12:19

1 Answers1

0

Perhaps you may use a different batch:

batch_down.draw() # background
batch.draw()

but we can't move sprite between batch.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459