Sign in to follow this  
bgoldbeck

Keyframe animation help

Recommended Posts

[url="https://picasaweb.google.com/lh/photo/3oR5V86Z7n3-wlSQ4mKXhOUiQfGemamgqzoBUti0-ak?feat=directlink"]Animation[/url]
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.

Share this post


Link to post
Share on other sites
Here is some of my code.[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[i][0];
float x2 = getObject(animationState, curFrame + 1, 0)->vertices[i][0];
float y1 = getObject(animationState, curFrame , 0)->vertices[i][1];
float y2 = getObject(animationState, curFrame + 1, 0)->vertices[i][1];
float z1 = getObject(animationState, curFrame , 0)->vertices[i][2];
float z2 = getObject(animationState, curFrame + 1, 0)->vertices[i][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]);
[/code]

Share this post


Link to post
Share on other sites
[quote name='Rando' timestamp='1317779608' post='4869183']
Here is some of my code.
[/quote]

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

[code]
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[i][0];
float x2 = getObject(animationState, curFrame + 1, 0)->vertices[i][0];
float y1 = getObject(animationState, curFrame , 0)->vertices[i][1];
float y2 = getObject(animationState, curFrame + 1, 0)->vertices[i][1];
float z1 = getObject(animationState, curFrame , 0)->vertices[i][2];
float z2 = getObject(animationState, curFrame + 1, 0)->vertices[i][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[i].x = newX;
newVerts[i].x = newY;
newVerts[i].x = newZ;
}

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this