Jump to content

  • Log In with Google      Sign In   
  • Create Account


2D Animation in OpenGL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 storm33229   Members   -  Reputation: 125

Like
0Likes
Like

Posted 06 August 2011 - 02:23 PM

I've done a little searching on the forums here, but couldn't quite find any one post that outlined how to animate 2d sprites in OpenGL. I have figured out how to clip a part of a texture and assign it to a quad. This would give me a single frame of a spritesheet, and obviously knowing how to do this would make it rather simple to make sprites animate. The trouble is that because I am using TexCoords I do not know how to select certain pixel rects of a sprite sheet to draw. For example:



glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2f(0.5, 0.5);
glVertex3f(0.25, 0.25, 0.0);
glTexCoord2f(1.0, 0.5);
glVertex3f(0.5, 0.25, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(0.5, 0.5, 0.0);
glTexCoord2f(0.5, 1.0);
glVertex3f(0.25, 0.5, 0.0);

glEnd();

This code will draw a texture that starts halfway through and displays the other half so:
OOOO
OOOO
OOXX
OOXX

(or at least that is how I imagine it)

Anyway using TexCoords like this does not seem to be a good way to select which part of a sprite sheet i would like to draw. Are there better ways of doing this?

Sponsor:

#2 capricorn   Members   -  Reputation: 139

Like
1Likes
Like

Posted 06 August 2011 - 03:55 PM

Anyway using TexCoords like this does not seem to be a good way to select which part of a sprite sheet i would like to draw. Are there better ways of doing this?


There may be another way through the use of texture matrix, but it is impractical.

You can easiliy convert from your pixel coordinates to texture coordinates and back: you know actual texture dimensions in pixels and your sprite locations/dimensions.
For a sprite with pixel geometry (x,y,width,height) texture coords would be (x/TextureWidth,y/TextureHeight,(x+w)/TextureWidth,(y+h)/TextureHeight). I'm sure you can quickly come up with a backwards conversion should you need one.
WBW, capricorn

#3 JTippetts   Moderators   -  Reputation: 8351

Like
1Likes
Like

Posted 06 August 2011 - 03:59 PM

If you know the pixel locations and widths of the sprite, and the dimensions of the texture, you can easily calculate the texture coordinates. ie, given a sprite sheet sized 1024x1024, the sprite at location (256,256) of size 64x64 would be defined by the texture coordinate rectangle (0.25,0.25) - (0.3125,0.3125). It's just a matter of simple division. Each pixel in a 1024x1024 image is equalt to 1/1024 or 0.0009765625 and (1/1024)*256=0.25.

#4 GothSeiDank   Members   -  Reputation: 156

Like
1Likes
Like

Posted 06 August 2011 - 06:04 PM

SpriteSheets are the way to go. In my 2D Engine, I use this Tool:
Very nice and easy to use, even has a commandline tool coming with it.
Implementation for a parser of the format:
http://code.google.com/p/nightlight2d/source/browse/NightLightDLL/NLSpriteSheet.cpp

And animating is just flipping the texture-coords.
In my code, I just keep all frames in a vector, which are described in a xml document and all frames live on the same spritesheet.
Then every X seconds or frames I flip the texture-coords of the sprite and voilá, decent animation :).

http://code.google.c...NLAnimation.cpp
Thats the code without drawing the sprite, that is done in another class since this is a derived class only. But could give a clue about how to do it.
The code should be easy to understand.
Believe me, Spritesheets really speed you up and flipping the texture-coords is the most efficient way you can have when doing pic-by-pic animations.
Thats at least what I researched.
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/

#5 storm33229   Members   -  Reputation: 125

Like
0Likes
Like

Posted 07 August 2011 - 12:02 PM

Awesome, thank you for your help. Now I am on to learning how to NOT use Immediate mode. Posted Image

#6 johnchapman   Members   -  Reputation: 532

Like
1Likes
Like

Posted 07 August 2011 - 11:38 PM

Another way to do it would be to load each frame of animation into layers of a 3D texture. Then to 'play' the animation you just interpolate the w texture coordinate. I'm not sure how efficient this is vs. jumping around a 2d texture but it means you can take advantage of texture filtering between frames.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS