Friday, July 22, 2016

textures - OpenGL ES 2.0. Sprite Sheet Animation


I've found a bunch of tutorials on how to make this work on Open GL 1 & 1.1 but I can't find it for 2.0.


I would work it out by loading the texture and use a matrix on the vertex shader to move through the sprite sheet.


I'm looking for the most efficient way to do it. I've read that when you do the thing I'm proposing you are constantly changing the VBO's and that that is not good.


Edit: Been doing some research myself. Came upon this two Updating Texture and referring to the one before PBO's. I can't use PBO's since i'm using ES version of OpenGL so I suppose the best way is to make FBO's but, what I still don't get, is if I should create a Sprite atlas/batch and make a FBO/loadtexture for each frame of if I should load every frame into the buffer and change just de texture directions.



Answer



One way to do sprite animation with OpenGL ES shaders is achieved by using uniforms and changing texture coordinates in the vertex shader. To elaborate, suppose you have a 1024x128 sprite sheet with 8 frames.



  1. Set up a quad whose vertices (clockwise) are (-1, -1), (-1, 1), (1, 1), (1, -1).


  2. Assign texture coordinates to the vertices with (0, 0), (0, 1), (1, 1), (1, 0), respectively.

  3. In the vertex shader, define a uniform iSpriteFrame which is set to the frame of the sprite animation you wish to draw (between zero and seven for this example).


  4. When you draw the sprite at frame n, for each vertex with texture coordinate tc and position p, calculate the final texture coordinate ftc with


    ftc.x = (iSpriteFrame + tc.x) * 0.125
    ftc.y = tc.y


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...