Jump to content
  • Advertisement
Sign in to follow this  
Qa303asmGuru

OpenGL 2D animation w/ OGL

This topic is 5052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This could probably be considered a noob question, but I felt that it was more relevant to OpenGL specifically than just a general question about 2D animation. I'm trying to figure out a better way to do my 2D animation for the space-shooter I've been working on. Right now I save each individual frame of an animation as a separate .bmp file (i.e. shield_1.bmp, shield_2.bmp, shield_3.bmp...). I know all these frames can be saved in a single file, the question is, how do you render individual frames using OpenGL if the frames are contained in a single file? I've browsed around the web of course, but I've found in the past that people on here generally offer the best solutions and I get an answer directly related to what I'm asking instead of 'in the ball park' ;) I'm using the NeHe method of loading bitmaps (auxDIBImageLoad, coupled with BindTexture...etc). I'm guessing the solution might have to do with glTexImage2D, setting up the parameters and such. Any advice is greatly appreciated! -Q

Share this post


Link to post
Share on other sites
Advertisement
You probably want to use glMatrixMode(GL_TEXTURE). Then you can set the texture area using glTranslate and glScale. eg. say you wanted to draw a 64x64 subimage at (128,0) in a 256x256 texture:
glMatrixMode(GL_TEXTURE);
glTranslatef(128/256, 0, 0);
glScalef(64/256, 64/256, 0);

(you divide by 256 because texture coordinates are from (0,0) to (1,1)).

Share this post


Link to post
Share on other sites
The general way to do it is to create a "tile sheet", which is a single texture that contains all the animation frames for an item. When you load this tile sheet, you use glTexSubImage2D to seperate each frame into seperate textures. For each frame you want to load, you need to know the exact position and dimensions of that frame within the tile sheet. When you want to render using a different frame, you just use a different texture like you would before, but all those textures are now coming from the single image. You can read the description of glTexSubImage2D from MSDN.

Share this post


Link to post
Share on other sites
I'll give you yet a 3rd way. Put all the frames in one texture and then change the texture coords of the sprite you are drawing to change the frame being displayed. In some cases this may reduce the number of textures you need per frame (if multiple sprites use the animation at different points). This will also let you batch these sprites since you can change the texture coords for each quad drawn in a single batch while you only get one texture matrix per batch.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nemesis2k2
The general way to do it is to create a "tile sheet", which is a single texture that contains all the animation frames for an item. When you load this tile sheet, you use glTexSubImage2D to seperate each frame into seperate textures. For each frame you want to load, you need to know the exact position and dimensions of that frame within the tile sheet. When you want to render using a different frame, you just use a different texture like you would before, but all those textures are now coming from the single image. You can read the description of glTexSubImage2D from MSDN.

I'm attempting to use this method, yet I'm getting stuck with glTexSubImage2D. I've read every bit of documentation I've found doing web searches and using MSDN, but I can't figure out how to use it correctly. I understand how to set the function up, but I've been unable to find a good example of how to use it in the program.

Anybody care to shed some light?

Share this post


Link to post
Share on other sites
Okay yeah it's definetly not working. I've followed every example I've come across and I can't seem to get it to work. This is getting really frustrating...

I've even gone through the example on this page (Texture Sub-Loading example), and looking at the source I'd think the sub-image would only show part of the tex64 bitmap, but it displays the entire thing??

Please help!

Share this post


Link to post
Share on other sites
Quote:
Original post by Solias
I'll give you yet a 3rd way. Put all the frames in one texture and then change the texture coords of the sprite you are drawing to change the frame being displayed. In some cases this may reduce the number of textures you need per frame (if multiple sprites use the animation at different points). This will also let you batch these sprites since you can change the texture coords for each quad drawn in a single batch while you only get one texture matrix per batch.

Heh, I asked myself, "could it really be that simple?"

Yup. Works like a charm. I don't know if this is the most efficient way to do it but it's a heck of a good start.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!