コード例 #1
0
void InterpolationHelper::interpolateMatrix(){


    if (bMerge){

        if (mergeFactor<=1.0)
            mergeFactor+=2.0 * renderer->deltaTime * 0.001 * mergeDirection;

        if (mergeFactor>1.0 ){
            mergeFactor=1.0;
        }

        if (mergeFactor<0.0 ){
            mergeFactor=1.0;
            bFinishedMatrix=true;
            return;
        }
    }


    Matrix4f matrixOne;
    Matrix4f matrixTwo;

    Matrix4f resultingMatrix;

    Matrix3f rotationOne;
    Matrix3f rotationTwo;

    Matrix3f resultingRotation;


    double timeKeyOne;
    double timeKeyTwo;

    currentTime=(renderer->currentTime-startTime) + inPoint;

        //currentTime=currentTime*100.0;
        currentTime=(int)currentTime/100;
        currentTime=currentTime*100;

    currentTime*=timeScale;


    //currentTime=startTime+1;
    //currentTime++;
    //move forward if we are at end of key

    while (currentTime> keyFrames[currentKeyMatrix+1]->timeKey ){
        currentKeyMatrix++;
        if ((uint)currentKeyMatrix+1 >= keyFrames.size()){
            if (bLooping){
                currentKeyMatrix=0;
                startTime=renderer->currentTime;
                currentTime=0.0;
                //cout << "looped keyframes size: " << keyFrames.size() << endl;
                //return;
            }else if (bMerge){
                if (mergeDirection > 0.0){
                    mergeDirection=-1.0 * mergeDirection;
                    mergeFactor=1.0f;
                }
                currentKeyMatrix=keyFrames.size()-2;
                fadeOut(keyFrames.size()-1);
                return;
            }else{
                bFinishedMatrix=true;
                return;
            }
        }
    }

    //cout << "key: " << currentKeyMatrix << " max Keys: " << (*timeKeys).size() << endl;
    //if we are at the end of all keys, we're finished!

    //get the two closest interpolation points
    timeKeyOne=keyFrames[currentKeyMatrix]->timeKey;
    timeKeyTwo=keyFrames[currentKeyMatrix+1]->timeKey;

        //get the time difference
        double timeDifference=timeKeyTwo-timeKeyOne;

        //map time difference to 0.0 - 1.0
        double keyTime=currentTime-timeKeyOne; //-> current Position, in the beginning 0.0;
        float relativeTime=(float) (keyTime/timeDifference); //-> will go from 0.0 to 1.0 between the keys


    SkeletalActor* skel=(SkeletalActor*)moveActor;

    for (uint i=0;i<skel->bones.size();i++){

        matrixOne=keyFrames[currentKeyMatrix]->boneMatrices[skel->bones[i]->name];
        matrixTwo=keyFrames[currentKeyMatrix+1]->boneMatrices[skel->bones[i]->name];

        //ease in - ease out
        float x=relativeTime*2.0f;
        float y=0.0f;

        if (x< 1)
            y=x*x;
        else
            y=2-( (x-2) * (x-2) );

        y=y* 0.5;

        if (bLinear)
            y=relativeTime;
        //interpolate between them
        //resultingMatrix=matrixOne.lerp(relativeTime,matrixTwo); //calculate resulting position


        rotationOne=getRotationMatrix(matrixOne);
        rotationTwo=getRotationMatrix(matrixTwo);
        resultingRotation=rotationOne.lerp(y,rotationTwo);


        resultingMatrix=matrixOne.lerp(y,matrixTwo); //calculate resulting position

        if (bMerge && mergeFactor < 1.0 && mergeFactor > 0.0){
            Matrix3f sourceMatrix=getRotationMatrix(skel->bones[i]->transformMatrix);
            resultingRotation = sourceMatrix.lerp(mergeFactor,resultingRotation);
        }

        //normalize rotations!
        resultingRotation=normalizeRotations(resultingRotation);

        //apply rotation
        skel->bones[i]->transformMatrix.setRotation(resultingRotation);
    }

}
コード例 #2
0
void InterpolationHelper::interpolateTransform(){

    Matrix4f transformOne, transformTwo;

    Matrix4f resultingTransform;

	Matrix3f rotationOne;
    Matrix3f rotationTwo;

    Matrix3f resultingRotation;

    double timeKeyOne;
    double timeKeyTwo;

    currentTime=renderer->currentTime-startTime;

    //move forward if we are at end of key
    while (currentTime> keyFrames[currentKeyTransform+1]->timeKey ){
    //if (currentTime> keyFrames[currentKey+1]->timeKey ){
          currentKeyTransform++;
          if (currentKeyTransform+1 >= (int)keyFrames.size()){
            bFinishedTransform=true;
            return;
            }
          }

    timeKeyOne=keyFrames[currentKeyTransform]->timeKey;
    timeKeyTwo=keyFrames[currentKeyTransform+1]->timeKey;

    //get the time difference
    double timeDifference=timeKeyTwo-timeKeyOne;

    //map time difference to 0.0 - 1.0
    double keyTime=currentTime-timeKeyOne; //-> current Position, in the beginning 0.0;
    float relativeTime=(float) (keyTime/timeDifference); //-> will go from 0.0 to 1.0 between the keys

/*
        relativeTime=relativeTime*100.0;
        relativeTime=(int)relativeTime/30;
        relativeTime=relativeTime*0.3;
*/
    if (bAdditive){
        transformOne=moveActor->transformMatrix;                //
        relativeTime*=relativeTime;      //
    }else{
        transformOne=keyFrames[currentKeyTransform]->transformKey;
    }


    transformTwo=keyFrames[currentKeyTransform+1]->transformKey;

       float x=relativeTime*2.0f;
        float y=0.0f;

        if (x< 1)
            y=x*x;
        else
            y=2-( (x-2) * (x-2) );

        y=y* 0.5;

        if (bLinear)
            y=relativeTime;


	rotationOne=getRotationMatrix(transformOne);
	rotationTwo=getRotationMatrix(transformTwo);
	resultingRotation=rotationOne.lerp(y,rotationTwo);


    //interpolate between them
    resultingTransform=transformOne.lerp(y,transformTwo); //calculate resulting position


	//normalize rotations!
	resultingRotation=normalizeRotations(resultingRotation);
	resultingTransform.setRotation(resultingRotation);

    //apply resulting position
    if (bRelative){
		//apply rotation
		moveActor->transformMatrix=baseTransform* resultingTransform;
        }
    else{
		//apply rotation
		moveActor->transformMatrix=resultingTransform;
	}
}