Original Post
Hey,
I build a display list from images in 1 texture. Now when I update the list sometimes the last image does not get rendered. Also the animation skips frames many times and is noticeable. I wait for 11 ms, if I wait longer it takes forever to do the animation. If I go below it moves too fast. I can''t figure out where I am going wrong. Here is the animation code
Hope someone can figure this out. I am struggling with this for days. Thank you.
#include "animation.h"
void Load_Animation_File(ANIMATION *anim, const char *filename)
{
anim->imageTexture = Load_TextureBMP(filename, GL_RGBA, GL_RGBA);
}
void Setup_Animation(ANIMATION *anim, int numAnims, int numX, int numY)
{
float texX = 0, texY = 1;
int x = 0, y = 0;
anim->numAnims = numAnims;
anim->xNum = numX;
anim->yNum = numY;
anim->startFrame = 0;
anim->texCoords = new GLfloat[anim->xNum * anim->yNum * 8];
for(x = 0; x < anim->xNum * anim->yNum * 8; x ++)
anim->texCoords[x] = 0;
for(x = 0; x < anim->xNum * anim->yNum * 8; x += 8) {
anim->texCoords[x]= texX/anim->xNum; // x1
anim->texCoords[x+1] = 1 - (texY/anim->yNum);// y1
anim->texCoords[x+2] =(texX + 1)/anim->xNum; // x2
anim->texCoords[x+3] = anim->texCoords[x+1]; // y2
anim->texCoords[x+4] = anim->texCoords[x + 2];// x3
anim->texCoords[x+5] = anim->texCoords[x + 1] +
(float)1/anim->yNum; // y3
anim->texCoords[x+6] = anim->texCoords[x]; // x4
anim->texCoords[x+7] = anim->texCoords[x+5];// y4
if((texX/anim->xNum) == 1) {
texX = 0;
texY += 1;
} else
texX += 1;
}
anim->isAlive = false;
}
void Setup_Animation_Vertices(ANIMATION &anim, GLfloat x1, GLfloat y1,GLfloat x2, GLfloat y2)
{
anim.vertexCoords[0] = x1;
anim.vertexCoords[1] = y1;
anim.vertexCoords[2] = x2;
anim.vertexCoords[3] = y1;
anim.vertexCoords[4] = x2;
anim.vertexCoords[5] = y2;
anim.vertexCoords[6] = x1;
anim.vertexCoords[7] = y2;
}
void Build_Animation_List(ANIMATION &anim)
{
int coordsIndex = 0;
anim.animLists = glGenLists(anim.numAnims);
glBindTexture(GL_TEXTURE_2D, anim.imageTexture);
for(int i = 0; i <= anim.numAnims; i++, coordsIndex+=8)
{
glNewList(anim.animLists + anim.startFrame, GL_COMPILE);
glEnable(GL_ALPHA_TEST);
glBegin(GL_QUADS);
glTexCoord2fv(&anim.texCoords[coordsIndex]);
glVertex2fv(&anim.vertexCoords[0]);
glTexCoord2fv(&anim.texCoords[coordsIndex+2]);
glVertex2fv(&anim.vertexCoords[2]);
glTexCoord2fv(&anim.texCoords[coordsIndex+4]);
glVertex2fv(&anim.vertexCoords[4]);
glTexCoord2fv(&anim.texCoords[coordsIndex+6]);
glVertex2fv(&anim.vertexCoords[6]);
glEnd();
glDisable(GL_ALPHA_TEST);
glTranslated(10, 0, 0);
glEndList();
anim.startFrame++;
}
anim.startFrame = 0;
}
void Start_Animation(ANIMATION &anim, DWORD waitTime)
{
anim.isAlive = true;
anim.waitTime = waitTime;
anim.startTime = GetTickCount();
}
void Render_Animation(ANIMATION &anim)
{
glPushMatrix();
glTranslatef(20, 150, 0);
glCallList(anim.animLists + anim.startFrame);
glPopMatrix();
}
void Update_Animation(ANIMATION &anim)
{
if((GetTickCount() - anim.startTime) >= anim.waitTime)
anim.startFrame++;
if(anim.startFrame >= anim.numAnims)
anim.startFrame = 0;
anim.startTime = GetTickCount();
}
void Destroy_Animation(ANIMATION &anim)
{
glDeleteTextures(1, &anim.imageTexture);
delete [] anim.texCoords;
}