# Make sprite animation

**URL:** https://discourse.libcinder.org/t/make-sprite-animation/1411
**Category:** Using Cinder
**Created:** [January 25, 2019, 10:55am UTC](https://discourse.libcinder.org/t/make-sprite-animation/1411 "2019-01-25T10:55:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DukeMan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/dukeman/32/712_2.png) [@DukeMan](https://discourse.libcinder.org/u/DukeMan)
#### Post date: [January 25, 2019, 10:55am UTC](https://discourse.libcinder.org/t/make-sprite-animation/1411/1 "2019-01-25T10:55:13Z")

</div>

Hello guys,  
I’m wondering what it’s the best way to display an animation of sprites  
For now, I load all my sprites and I store them in a vector

But I think that will be better if I would load them in the GPU ?

Is that possible ?

---

<div class="post-metadata">

### Author: ![balachandran\_c](https://avatars.discourse-cdn.com/v4/letter/b/f17d59/32.png) [@balachandran\_c](https://discourse.libcinder.org/u/balachandran_c)
#### Post date: [January 25, 2019, 6:51pm UTC](https://discourse.libcinder.org/t/make-sprite-animation/1411/2 "2019-01-25T18:51:51Z")

</div>

Hi,

You might want to take a look [here](https://github.com/Potion/Cinder-poSpritesheet).

---

<div class="post-metadata">

### Author: ![sharkbox](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/sharkbox/32/39_2.png) [@sharkbox](https://discourse.libcinder.org/u/sharkbox)
#### Post date: [January 28, 2019, 9:14pm UTC](https://discourse.libcinder.org/t/make-sprite-animation/1411/3 "2019-01-28T21:14:13Z")

</div>

There are a bunch of great ways to do it. Using a spritesheet is definitely one of them. An advantage of something like a spritesheet is that when openGL binds a texture, it only has to do it once per frame, and not multiple times for different textures. This is especially helpful if you have a lot of instances of the same sprite on screen at once.

In the past when I’ve needed to bind a lot of textures and needed great performance, I’ve used 2D sampler arrays. These will let you pass in “3D” arrays of textures, and I think on my system I can pass up to 2048. `GL_TEXTURE_2D_ARRAY` is the thing to google. You’ll need to write a custom shader, and then in the texture lookup, you pass in a vec3 instead of a vec2, where the `z` is an index for which texture you’re looking up.

Lastly, a while back I made a simple block for animated gifs. [New block - ciAnimatedGif](https://discourse.libcinder.org/t/new-block-cianimatedgif/301) Depending on your app structure and performance needs, this can also work in a pinch.

TLDR: Try spritesheets, and gifs in that order. If you need performance, check out `GL_TEXTURE_2D_ARRAY`.

---

<div class="post-metadata">

### Author: ![DukeMan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/dukeman/32/712_2.png) [@DukeMan](https://discourse.libcinder.org/u/DukeMan)
#### Post date: [January 29, 2019, 2:11pm UTC](https://discourse.libcinder.org/t/make-sprite-animation/1411/4 "2019-01-29T14:11:46Z")

</div>

Thanks for your answer,  
I try to create a shader by myself, with the cinder’s guide but i don’t understand how to use shaders with my images, so do you have any tips ?

---

<div class="post-metadata">

### Author: ![xumo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/xumo/32/75_2.png) [@xumo](https://discourse.libcinder.org/u/xumo)
#### Post date: [January 30, 2019, 10:31am UTC](https://discourse.libcinder.org/t/make-sprite-animation/1411/5 "2019-01-30T10:31:45Z")

</div>

Take a look at the shader guide:  
[https://libcinder.org/docs/guides/opengl/part5.html](https://libcinder.org/docs/guides/opengl/part5.html)

In the section of texture loading you can add more textures to your fragment shader.

```auto
		uniform sampler2D	uTex0;
		uniform sampler2D	uTex1;
                ....
		uniform sampler2D	uTexN;

```

The pass it like this

```auto
mGlsl->uniform( "uTex0", texture0->getId() );
mGlsl->uniform( "uTex1", texture1->getId() );
...
mGlsl->uniform( "uTexN", textureN->getId() );

```

I think you can upload the textures just once, but not sure.  
You can also control wich texture to draw if you already have a vector of textures:

```auto
mGlsl->uniform( "uTex0", textures[id]->getId());

```

---

<div class="post-metadata">

### Author: ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)
#### Post date: [January 31, 2019, 3:55am UTC](https://discourse.libcinder.org/t/make-sprite-animation/1411/6 "2019-01-31T03:55:27Z")

</div>

That’s incorrect. The uniform should be set to the texture unit the texture is bound to. So for instance:

```auto
texture0->bind( 10 ); // unit can be chosen freely up to a maximum
texture1->bind( 11 );

mGlsl->uniform( "uTex0", 10 );
mGlsl->uniform( "uTex1", 11 );

```

It’s better to do the following, though:

```auto
gl::ScopedTextureBind scpTex0( texture0, 10 );
gl::ScopedGlslProg scpGlsl( mGlsl );

mGlsl->uniform( "uTex0", 10 );

```

That way, the OpenGL state will be restored when you’re done drawing. It’s good to get into the habit of managing your state like that.

As @Xumo correctly pointed out, you only need to bind your textures once (if you’re not going to reuse that texture unit for something else), for example in your `setup()`. And if you do, you also only need to set the uniform once.

-Paul
