|
|
|
@ -14,6 +14,37 @@ namespace Oyster
|
|
|
|
|
{
|
|
|
|
|
Definitions::Pointlight pl;
|
|
|
|
|
|
|
|
|
|
/********************************************************
|
|
|
|
|
* Private Prototype Methods
|
|
|
|
|
********************************************************/
|
|
|
|
|
void AnimateRelativeBones( const Model::ModelInfo &info, Model::AnimationData &anim, Math::Matrix relativeBuffer[] );
|
|
|
|
|
void MergeAnimatedBones
|
|
|
|
|
(
|
|
|
|
|
const Model::Bone raw[], int numBones, Math::Float interpolation, // comparable raw bone data and interpolation value
|
|
|
|
|
const Math::Matrix animatedBoneSourceA[], // relative bone animations
|
|
|
|
|
Math::Matrix animatedBoneSourceB_Target[] // relative bone animations and targetbuffer
|
|
|
|
|
);
|
|
|
|
|
int AnimateAbsoluteBones
|
|
|
|
|
(
|
|
|
|
|
const Model::ModelInfo &info, Math::Float deltaTime,
|
|
|
|
|
Model::AnimationData &anim,
|
|
|
|
|
Math::Matrix SkinTransformBuffer[],
|
|
|
|
|
Math::Matrix BoneAnimationBuffer_Relative[],
|
|
|
|
|
Math::Matrix BoneAnimationBuffer_Absolute[]
|
|
|
|
|
);
|
|
|
|
|
int AnimateAbsoluteBones
|
|
|
|
|
(
|
|
|
|
|
const Model::ModelInfo &info, Math::Float deltaTime,
|
|
|
|
|
Model::AnimationData anim[], int numAnimations,
|
|
|
|
|
Math::Matrix SkinTransformBuffer[],
|
|
|
|
|
Math::Matrix BoneAnimationBuffer_Relative[],
|
|
|
|
|
Math::Matrix BoneAnimationBuffer_Absolute[]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/********************************************************
|
|
|
|
|
* Public Method Implementations
|
|
|
|
|
********************************************************/
|
|
|
|
|
|
|
|
|
|
void DefaultRenderer::NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4x4 Projection, Definitions::Pointlight* Lights, int numLights)
|
|
|
|
|
{
|
|
|
|
|
Preparations::Basic::ClearBackBuffer(Oyster::Math::Float4(0,0,0,0));
|
|
|
|
@ -82,77 +113,18 @@ namespace Oyster
|
|
|
|
|
|
|
|
|
|
Model::ModelInfo* info = models[i].info;
|
|
|
|
|
|
|
|
|
|
Definitions::AnimationData am; //final
|
|
|
|
|
if(info->Animated && models[i].Animation.AnimationPlaying != NULL)
|
|
|
|
|
{
|
|
|
|
|
models[i].Animation.AnimationTime += deltaTime;
|
|
|
|
|
////store inverse absolut transform
|
|
|
|
|
// Bone animation buffers
|
|
|
|
|
Math::Matrix SkinTransform[100];
|
|
|
|
|
Math::Matrix BoneAnimated[100];
|
|
|
|
|
Math::Matrix BoneAbsAnimated[100];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int b = 0; b <info->BoneCount; ++b)
|
|
|
|
|
{
|
|
|
|
|
Model::Bone Bone = info->bones[b];
|
|
|
|
|
SkinTransform[b] = Bone.Absolute.GetInverse();
|
|
|
|
|
BoneAnimated[b] = Bone.Relative;
|
|
|
|
|
BoneAbsAnimated[b] = Bone.Absolute;
|
|
|
|
|
}
|
|
|
|
|
int b = 0;
|
|
|
|
|
Model::Animation A = *models[i].Animation.AnimationPlaying;
|
|
|
|
|
while(models[i].Animation.AnimationTime>A.duration && models[i].Animation.LoopAnimation)
|
|
|
|
|
models[i].Animation.AnimationTime -= (float)A.duration;
|
|
|
|
|
|
|
|
|
|
float position = models[i].Animation.AnimationTime;
|
|
|
|
|
for(int b = 0; b < A.Bones;++b)
|
|
|
|
|
{
|
|
|
|
|
//find current frame
|
|
|
|
|
int nrOfFrames = A.Frames[b];
|
|
|
|
|
Model::Frame PFrame = A.Keyframes[b][nrOfFrames-1];
|
|
|
|
|
Model::Frame NFrame = A.Keyframes[b][nrOfFrames-1];
|
|
|
|
|
bool FrameFound = false;
|
|
|
|
|
for (int i = 0; i < nrOfFrames; i++)
|
|
|
|
|
{
|
|
|
|
|
if(position < A.Keyframes[b][i].time)
|
|
|
|
|
{
|
|
|
|
|
PFrame = A.Keyframes[b][i-1];
|
|
|
|
|
NFrame = A.Keyframes[b][i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
float denominator = (float)(NFrame.time - PFrame.time);
|
|
|
|
|
if(denominator == 0)
|
|
|
|
|
{
|
|
|
|
|
BoneAnimated[PFrame.bone.Parent] = PFrame.bone.Relative;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
float inter = (float)((position - PFrame.time) / denominator);
|
|
|
|
|
Math3D::InterpolateOrientation_UsingNonRigidNlerp(PFrame.bone.Relative,NFrame.bone.Relative,inter, BoneAnimated[PFrame.bone.Parent]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////calculate Absolute Animation Transform
|
|
|
|
|
for(int b = 0; b < info->BoneCount; ++b)
|
|
|
|
|
{
|
|
|
|
|
BoneAbsAnimated[b] = BoneAbsAnimated[info->bones[b].Parent] * BoneAnimated[b];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//write data to am
|
|
|
|
|
for(int b = 0; b < info->BoneCount; ++b)
|
|
|
|
|
{
|
|
|
|
|
am.AnimatedData[b] = (BoneAbsAnimated[b] * SkinTransform[b]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void *data = Resources::Gather::AnimationData.Map();
|
|
|
|
|
memcpy(data,&am,sizeof(Definitions::AnimationData));
|
|
|
|
|
Resources::Gather::AnimationData.Unmap();
|
|
|
|
|
|
|
|
|
|
pm.Animated = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
pm.Animated = 0;
|
|
|
|
|
pm.Animated = AnimateAbsoluteBones
|
|
|
|
|
(
|
|
|
|
|
*info, deltaTime,
|
|
|
|
|
models[i].Animation,
|
|
|
|
|
::Utility::StaticArray::NumElementsOf( models[i].Animation ),
|
|
|
|
|
SkinTransform, BoneAnimated, BoneAbsAnimated
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
void* data = Resources::Gather::ModelData.Map();
|
|
|
|
|
memcpy(data,&(pm),sizeof(pm));
|
|
|
|
@ -284,6 +256,183 @@ namespace Oyster
|
|
|
|
|
|
|
|
|
|
Core::swapChain->Present(0,0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/********************************************************
|
|
|
|
|
* Private Prototype Method Implementations
|
|
|
|
|
********************************************************/
|
|
|
|
|
|
|
|
|
|
void AnimateRelativeBones( const Model::ModelInfo &info, Model::AnimationData &anim, Math::Matrix relativeBuffer[] )
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0; i < info.BoneCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
Model::Bone Bone = info.bones[i];
|
|
|
|
|
relativeBuffer[i] = Bone.Relative;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Model::Animation &A = *anim.AnimationPlaying;
|
|
|
|
|
|
|
|
|
|
while( anim.AnimationTime > A.duration && anim.LoopAnimation )
|
|
|
|
|
anim.AnimationTime -= (float)A.duration;
|
|
|
|
|
|
|
|
|
|
float position = anim.AnimationTime;
|
|
|
|
|
for( int i = 0; i < A.Bones; ++i )
|
|
|
|
|
{
|
|
|
|
|
//find current frame
|
|
|
|
|
int nrOfFrames = A.Frames[i];
|
|
|
|
|
Model::Frame PFrame = A.Keyframes[i][nrOfFrames-1];
|
|
|
|
|
Model::Frame NFrame = A.Keyframes[i][nrOfFrames-1];
|
|
|
|
|
bool FrameFound = false;
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < nrOfFrames; j++)
|
|
|
|
|
{
|
|
|
|
|
if(position < A.Keyframes[i][j].time)
|
|
|
|
|
{
|
|
|
|
|
PFrame = A.Keyframes[i][j-1];
|
|
|
|
|
NFrame = A.Keyframes[i][j];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float denominator = (float)(NFrame.time - PFrame.time);
|
|
|
|
|
|
|
|
|
|
if( denominator != 0.0f )
|
|
|
|
|
{
|
|
|
|
|
float inter = (float)((position - PFrame.time) / denominator);
|
|
|
|
|
Math3D::InterpolateOrientation_UsingNonRigidNlerp( PFrame.bone.Relative,NFrame.bone.Relative, inter, relativeBuffer[PFrame.bone.Parent] );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
relativeBuffer[PFrame.bone.Parent] = PFrame.bone.Relative;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum Conflict
|
|
|
|
|
{
|
|
|
|
|
Conflict_detected,
|
|
|
|
|
Conflict_useA,
|
|
|
|
|
Conflict_useB
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Conflict DetectBoneAnimationConflict( const Model::Bone &raw, const Math::Matrix &animBoneA, const Math::Matrix &animBoneB );
|
|
|
|
|
|
|
|
|
|
void MergeAnimatedBones( const Model::Bone raw[], int numBones, Math::Float interpolation, const Math::Matrix *animatedBoneSourceA, Math::Matrix animatedBoneSourceB_Target[] )
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0; i < numBones; ++i )
|
|
|
|
|
{
|
|
|
|
|
switch( DetectBoneAnimationConflict(raw[i], animatedBoneSourceA[i], animatedBoneSourceB_Target[i]) )
|
|
|
|
|
{
|
|
|
|
|
case Conflict_detected:
|
|
|
|
|
Math3D::InterpolateOrientation_UsingNonRigidNlerp( animatedBoneSourceA[i], animatedBoneSourceB_Target[i], interpolation, animatedBoneSourceB_Target[i] );
|
|
|
|
|
break;
|
|
|
|
|
case Conflict_useA:
|
|
|
|
|
animatedBoneSourceB_Target[i] = animatedBoneSourceA[i];
|
|
|
|
|
break;
|
|
|
|
|
default: case Conflict_useB: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Conflict DetectBoneAnimationConflict( const Model::Bone &raw, const Math::Matrix &animBoneA, const Math::Matrix &animBoneB )
|
|
|
|
|
{
|
|
|
|
|
if( animBoneA == raw.Relative )
|
|
|
|
|
return Conflict_useB;
|
|
|
|
|
|
|
|
|
|
if( animBoneB == raw.Relative )
|
|
|
|
|
return Conflict_useA;
|
|
|
|
|
|
|
|
|
|
return Conflict_detected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AnimateAbsoluteBones( const Model::ModelInfo &info, Math::Float deltaTime, Model::AnimationData &anim, Math::Matrix SkinTransformBuffer[], Math::Matrix BoneAnimationBuffer_Relative[], Math::Matrix BoneAnimationBuffer_Absolute[] )
|
|
|
|
|
{
|
|
|
|
|
if( !info.Animated || (anim.AnimationPlaying == nullptr) )
|
|
|
|
|
{ // no animation
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
anim.AnimationTime += deltaTime;
|
|
|
|
|
AnimateRelativeBones( info, anim, BoneAnimationBuffer_Relative );
|
|
|
|
|
|
|
|
|
|
for( int i = 0; i < info.BoneCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
Model::Bone Bone = info.bones[i];
|
|
|
|
|
SkinTransformBuffer[i] = Bone.Absolute.GetInverse();
|
|
|
|
|
BoneAnimationBuffer_Absolute[i] = Bone.Absolute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Definitions::AnimationData am;
|
|
|
|
|
|
|
|
|
|
for( int i = 0; i < info.BoneCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
//calculate Absolute Animation Transform
|
|
|
|
|
BoneAnimationBuffer_Absolute[i] = BoneAnimationBuffer_Absolute[info.bones[i].Parent] * BoneAnimationBuffer_Relative[i];
|
|
|
|
|
|
|
|
|
|
//write data to am
|
|
|
|
|
am.AnimatedData[i] = (BoneAnimationBuffer_Absolute[i] * SkinTransformBuffer[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *data = Resources::Gather::AnimationData.Map();
|
|
|
|
|
memcpy( data, &am, sizeof(Definitions::AnimationData) );
|
|
|
|
|
Resources::Gather::AnimationData.Unmap();
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AnimateAbsoluteBones( const Model::ModelInfo &info, Math::Float deltaTime, Model::AnimationData anim[], int numAnimations, Math::Matrix SkinTransformBuffer[], Math::Matrix BoneAnimationBuffer_Relative[], Math::Matrix BoneAnimationBuffer_Absolute[] )
|
|
|
|
|
{
|
|
|
|
|
if( !info.Animated || (numAnimations < 1) )
|
|
|
|
|
{ // no animation
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int isAnimated = 0;
|
|
|
|
|
|
|
|
|
|
// for each animation
|
|
|
|
|
for( int i = 0; i < numAnimations; ++i )
|
|
|
|
|
{
|
|
|
|
|
if( anim[i].AnimationPlaying != nullptr )
|
|
|
|
|
{
|
|
|
|
|
anim[i].AnimationTime += deltaTime;
|
|
|
|
|
if( isAnimated )
|
|
|
|
|
{
|
|
|
|
|
AnimateRelativeBones( info, anim[i], BoneAnimationBuffer_Absolute ); // Borrowing BoneAnimationBuffer_Absolute as interim buffer
|
|
|
|
|
MergeAnimatedBones( info.bones, info.BoneCount, 0.5f, BoneAnimationBuffer_Absolute, BoneAnimationBuffer_Relative );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
isAnimated = 1;
|
|
|
|
|
AnimateRelativeBones( info, anim[i], BoneAnimationBuffer_Relative );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( isAnimated )
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0; i < info.BoneCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
Model::Bone Bone = info.bones[i];
|
|
|
|
|
SkinTransformBuffer[i] = Bone.Absolute.GetInverse();
|
|
|
|
|
BoneAnimationBuffer_Absolute[i] = Bone.Absolute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Definitions::AnimationData am;
|
|
|
|
|
|
|
|
|
|
for( int i = 0; i < info.BoneCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
//calculate Absolute Animation Transform
|
|
|
|
|
BoneAnimationBuffer_Absolute[i] = BoneAnimationBuffer_Absolute[info.bones[i].Parent] * BoneAnimationBuffer_Relative[i];
|
|
|
|
|
|
|
|
|
|
//write data to am
|
|
|
|
|
am.AnimatedData[i] = (BoneAnimationBuffer_Absolute[i] * SkinTransformBuffer[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *data = Resources::Gather::AnimationData.Map();
|
|
|
|
|
memcpy( data, &am, sizeof(Definitions::AnimationData) );
|
|
|
|
|
Resources::Gather::AnimationData.Unmap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isAnimated;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|