Jump to content
  • Advertisement
Sign in to follow this  
Giedrius

Texture coordinates and animation

This topic is 4370 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

I'm making a 2D game. For animating tiles/sprites, I have made my own format for saving graphics. All frames are put onto one image and when drawing that sprite/tile the correct frame gets selected by glTexCoord2f. Is there a way to specify direct position in pixels, instead of [0;1] range? If my images have odd frame number (like 9), the image is unstable when animating, because of incorrect precision when dividing numbers. This is the code that I have written:
void DrawSpr(sprite *spr)
{
	static int _frame = 0;
	int frame = _frame/100;
	double w = (double)spr->w / (spr->w*spr->num_frames);
	glBindTexture(GL_TEXTURE_2D,spr->id);
	glBegin(GL_QUADS);
	glTexCoord2d(w*frame,0);
	glVertex2i(0,0);
	glTexCoord2d(w*frame+w,0);
	glVertex2i(spr->w,0);
	glTexCoord2d(w*frame+w,1);
	glVertex2i(spr->w,spr->h);
	glTexCoord2d(w*frame,1);
	glVertex2i(0,spr->h);
	glEnd();
	_frame++;
	if (_frame>spr->num_frames*100) _frame = 0;
}


It currently is very unclean. I'll clean it after I solve this problem. [Edited by - Giedrius on July 4, 2006 3:18:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Giedrius
I'm making a 2D game. For animating tiles/sprites, I have made my own format for saving graphics. All frames are put onto one image and when drawing that sprite/tile the correct frame gets selected by glTexCoord2f. Is there a way to specify direct position in pixels, instead of [0;1] range? If my images have odd frame number (like 9), the image is unstable when animating, because of incorrect precision when dividing numbers. This is the code that I have written:
*** Source Snippet Removed ***It currently is very unclean. I'll clean it after I solve this problem.


Need a bit more info. Which division is causing the problem? How is the animation unstable? I would think that the double's you are using would have more than enough precision...

The only way (AFAIK) to specify coordinates in pixels would be to use the ARB_TEXTURE_RECTANGLE extension.

Are you currently using a timer to update the animations or just the counter variable?

Share this post


Link to post
Share on other sites
I will use a timer later, this is a temporary solution to slow the animation down. I'm just playing with it, trying to get the animation work. I haven't made no OpenGL animations yet. One divided by 9 is 0.(1). That's what is making it unprecise. And I believe that's why the objects image is "jumpy". If you can't understand, I probably can upload a demo for it. I'm going to check that extension out.

Share this post


Link to post
Share on other sites
Hmm, that extension seems to be only supported with new graphics cards. Although my video card has that extension, the requirements would be too big for my game. How should I do animation in OpenGL? I don't want to take a lot of texture ids for one animation.

Share this post


Link to post
Share on other sites
What sort of frame dimensions are you using? Are they all the same?

I have done this in the past without issue, while using the same frame sizes for all of a sprites frames.

Share this post


Link to post
Share on other sites
I think that jumpiness was caused because my image width wasn't power of two. When it's power of two, it is normal.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!