Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texture coordinates and animation

Started by Giedrius Jul 4, 2006 at 11:48 AM 5 replies 900+ views
Original Post
Giedrius
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:
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]
nts
nts
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?

Giedrius
Giedrius
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.
Giedrius
Giedrius
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.
kordova
kordova
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.
Giedrius
Giedrius
My image for animation is 288x32. Each frame size is 32x32.
Giedrius
Giedrius
I think that jumpiness was caused because my image width wasn't power of two. When it's power of two, it is normal.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.