Keyframe animation help

Started by
2 comments, last by bgoldbeck 12 years, 6 months ago
Animation
Hello, I am currently trying to learn keyframe animation and I am currently running into trouble. The link about shows where I am trying to animate a "T" shaped structure made of multiple cubes at three separate frames. I interpolate between each frame and the motion appears to come out correctly, but the triangular polygons lose there bearing and detach from the shape.
[size="2"]More sprinkles!
Advertisement
Here is some of my code.
static long begin = Timer::timerInitialize();
long dif = Timer::timerDifference(begin);

if(dif > 1000)
{
begin = Timer::timerInitialize();
dif = 1000;
if((curFrame + 1) != endFrame)
{
curFrame++;
}
else
{
curFrame = startFrame;
}
}


float weight = dif * .001; //Divide by 1000.
vector<vec3> newVerts;

for(int i = 0; i < getObject(animationState, curFrame, 0)->vertices.size(); i++)
{

float x1 = getObject(animationState, curFrame , 0)->vertices[0];
float x2 = getObject(animationState, curFrame + 1, 0)->vertices[0];
float y1 = getObject(animationState, curFrame , 0)->vertices[1];
float y2 = getObject(animationState, curFrame + 1, 0)->vertices[1];
float z1 = getObject(animationState, curFrame , 0)->vertices[2];
float z2 = getObject(animationState, curFrame + 1, 0)->vertices[2];
float newX = (1.0 - weight) * x1 + (weight * x2);
float newY = (1.0 - weight) * y1 + (weight * y2);
float newZ = (1.0 - weight) * z1 + (weight * z2);
vec3 newVector(newX, newY, newZ);
newVerts.push_back(newVector);
}
vbo->modifyVertices(&newVerts[0]);
[size="2"]More sprinkles!

Here is some of my code.


And it's quite hideous! (no offence!)


vector<vec3> newVerts;

// [1a] At this point, do you not know the number of vertices already? So why not resize the vec3 array here?


// [2] Why are you calling getObject() all the time? Why not determine the number of verts as an int (instead of adding a hideous amount of fn call overhead?)
for(int i = 0; i < getObject(animationState, curFrame, 0)->vertices.size(); i++)
{
// [3] curFrame doesn't change at all during the entire loop, so why call getObject so frequently?
// You are making NUM_VERTS * 6 calls to getObject when you could have simply called it *twice* before the loop.
//
float x1 = getObject(animationState, curFrame , 0)->vertices[0];
float x2 = getObject(animationState, curFrame + 1, 0)->vertices[0];
float y1 = getObject(animationState, curFrame , 0)->vertices[1];
float y2 = getObject(animationState, curFrame + 1, 0)->vertices[1];
float z1 = getObject(animationState, curFrame , 0)->vertices[2];
float z2 = getObject(animationState, curFrame + 1, 0)->vertices[2];

// [4] (1.0 - weight) ??????
// 3 times per iteration ?????
// That's 3 x NUM_VERTS times you are performing a calculation that could have be done exactly once.....
float newX = (1.0 - weight) * x1 + (weight * x2);
float newY = (1.0 - weight) * y1 + (weight * y2);
float newZ = (1.0 - weight) * z1 + (weight * z2);

// ..... and to make matters worse, it's not even needed:

float newX = x1 + weight * (x2 - x1);
float newY = y1 + weight * (y2 - y1);
float newZ = z1 + weight * (z2 - z1);

vec3 newVector(newX, newY, newZ); //< [1b] generate temporary.
newVerts.push_back(newVector); //< duplicate temporary, reallocate the verts array (including the additional copying that entails), and assign ?

// alternatively..... you could just make a call to resize before the loop, and just assign normally?
newVerts.x = newX;
newVerts.x = newY;
newVerts.x = newZ;
}

// *************** !! EVIL CODE ALERT !! ***********
vbo->modifyVertices(&newVerts[0]); //< never access [0] unless you know it has at least 1 element!
// *************** !! EVIL CODE ALERT !! ***********

I didn't ask for help refactoring my code. The last thing i'm worried about is that while i'm trying to learn a new subject. I also have tried multiple variations of an interpolation algorithm.
[size="2"]More sprinkles!

This topic is closed to new replies.

Advertisement