Commit e3d76c04 authored by hybrid's avatar hybrid

Minor cleanup and type usage clarifications.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@906 dfc29bdd-3216-0410-991c-e03cc46cb475
parent bad0b246
......@@ -30,8 +30,8 @@ CAnimatedMeshSceneNode::CAnimatedMeshSceneNode(IAnimatedMesh* mesh, ISceneNode*
const core::vector3df& position, const core::vector3df& rotation, const core::vector3df& scale)
: IAnimatedMeshSceneNode(parent, mgr, id, position, rotation, scale), Mesh(0),
BeginFrameTime(0), StartFrame(0), EndFrame(0), FramesPerSecond(25.f / 1000.f ),
CurrentFrameNr(0.f), JointMode(0), JointsUsed(0),
TransitionTime(0), Transiting(0), TransitingBlend(0),
CurrentFrameNr(0.f), JointMode(0), JointsUsed(false),
TransitionTime(0), Transiting(0.f), TransitingBlend(0.f),
Looping(true), ReadOnlyMaterials(false),
LoopCallBack(0), PassCount(0), Shadow(0)
{
......@@ -45,7 +45,6 @@ CAnimatedMeshSceneNode::CAnimatedMeshSceneNode(IAnimatedMesh* mesh, ISceneNode*
}
//! destructor
CAnimatedMeshSceneNode::~CAnimatedMeshSceneNode()
{
......@@ -64,15 +63,6 @@ CAnimatedMeshSceneNode::~CAnimatedMeshSceneNode()
}
//! Sets the current frame. From now on the animation is played from this frame.
void CAnimatedMeshSceneNode::setCurrentFrame(f32 frame)
{
......@@ -81,8 +71,7 @@ void CAnimatedMeshSceneNode::setCurrentFrame(f32 frame)
BeginFrameTime = os::Timer::getTime() - (s32)((CurrentFrameNr - StartFrame) / FramesPerSecond);
beginTransition(); //transite to this frame if enabled
beginTransition(); //transit to this frame if enabled
}
......@@ -95,28 +84,26 @@ f32 CAnimatedMeshSceneNode::getFrameNr() const
f32 CAnimatedMeshSceneNode::buildFrameNr(u32 timeMs)
{
if (Transiting!=0)
if (Transiting!=0.f)
{
TransitingBlend=f32(timeMs-BeginFrameTime) * Transiting;
if (TransitingBlend>1)
TransitingBlend = (f32)(timeMs-BeginFrameTime) * Transiting;
if (TransitingBlend > 1.f)
{
Transiting=0;
TransitingBlend=0;
Transiting=0.f;
TransitingBlend=0.f;
}
}
if (StartFrame==EndFrame)
return (f32)StartFrame; //Support for non animated meshes
if (FramesPerSecond==0)
if (FramesPerSecond==0.f)
return (f32)StartFrame;
if (Looping)
{
// play animation looped
if (FramesPerSecond>0) //forwards...
if (FramesPerSecond > 0.f) //forwards...
{
const s32 lenInTime = s32( f32(EndFrame - StartFrame) / FramesPerSecond);
return StartFrame + ( (timeMs - BeginFrameTime) % lenInTime) *FramesPerSecond;
......@@ -133,7 +120,7 @@ f32 CAnimatedMeshSceneNode::buildFrameNr(u32 timeMs)
f32 frame;
if (FramesPerSecond>0) //forwards...
if (FramesPerSecond > 0.f) //forwards...
{
const f32 deltaFrame = floorf( f32 ( timeMs - BeginFrameTime ) * FramesPerSecond );
......@@ -874,9 +861,7 @@ void CAnimatedMeshSceneNode::updateAbsolutePosition()
MD3Special.AbsoluteTagList[i].position = parent.position + (*taglist)[i].position + relative.position;
MD3Special.AbsoluteTagList[i].rotation = parent.rotation * (*taglist)[i].rotation * relative.rotation;
}
}
}
//! Set the joint update mode (0-unused, 1-get joints only, 2-set joints only, 3-move and set)
......@@ -893,11 +878,13 @@ void CAnimatedMeshSceneNode::setJointMode(s32 mode)
//! Sets the transition time in seconds (note: This needs to enable joints, and setJointmode maybe set to 2)
//! you must call animateJoints(), or the mesh will not animate
void CAnimatedMeshSceneNode::setTransitionTime(f32 Time)
void CAnimatedMeshSceneNode::setTransitionTime(f32 time)
{
if (Time!=0) checkJoints();
if (!(JointMode &2)) setJointMode(2);
TransitionTime=u32(Time*1000.0f);
if (time != 0.f)
checkJoints();
if (!(JointMode & 0x2))
setJointMode(2);
TransitionTime = (u32)core::floor32(time*1000.0f);
}
//! updates the joint positions of this mesh
......@@ -928,60 +915,51 @@ void CAnimatedMeshSceneNode::animateJoints()
// Transition
//-----------------------------------------
if (Transiting!=0)
if (Transiting != 0.f)
{
u32 n;
//Check the array is big enough (not really needed)
if (PretransitingSave.size()<JointChildSceneNodes.size())
{
for(n=PretransitingSave.size();n<JointChildSceneNodes.size();++n)
for(u32 n=PretransitingSave.size(); n<JointChildSceneNodes.size(); ++n)
PretransitingSave.push_back(core::matrix4());
}
f32 InvTransitingBlend=1-TransitingBlend;
for (n=0;n<JointChildSceneNodes.size();++n)
for (u32 n=0; n<JointChildSceneNodes.size(); ++n)
{
//------Position------
JointChildSceneNodes[n]->setPosition(PretransitingSave[n].getTranslation()*InvTransitingBlend+
JointChildSceneNodes[n]->getPosition()*TransitingBlend);
JointChildSceneNodes[n]->setPosition(
core::lerp(
PretransitingSave[n].getTranslation(),
JointChildSceneNodes[n]->getPosition(),
TransitingBlend));
//------Rotation------
//Code is slow, needs to be fixed up
core::quaternion RotationStart, RotationEnd;
core::quaternion QRotation;
core::vector3df tmpVector;
tmpVector=PretransitingSave[n].getRotationDegrees();
RotationStart.set(tmpVector.X*core::DEGTORAD ,tmpVector.Y*core::DEGTORAD,tmpVector.Z*core::DEGTORAD);
tmpVector=JointChildSceneNodes[n]->getRotation();
RotationEnd.set(tmpVector.X*core::DEGTORAD ,tmpVector.Y*core::DEGTORAD,tmpVector.Z*core::DEGTORAD);
const core::quaternion RotationStart(PretransitingSave[n].getRotationDegrees()*core::DEGTORAD);
const core::quaternion RotationEnd(JointChildSceneNodes[n]->getRotation()*core::DEGTORAD);
core::quaternion QRotation;
QRotation.slerp(RotationStart, RotationEnd, TransitingBlend);
QRotation.toEuler (tmpVector);
tmpVector.X*=core::RADTODEG; tmpVector.Y*=core::RADTODEG; tmpVector.Z*=core::RADTODEG; //convert from radians back to degrees
core::vector3df tmpVector;
QRotation.toEuler(tmpVector);
tmpVector*=core::RADTODEG; //convert from radians back to degrees
JointChildSceneNodes[n]->setRotation( tmpVector );
//------Scale------
//JointChildSceneNodes[n]->setScale(PretransitingSave[n].getScale()*InvTransitingBlend+
// JointChildSceneNodes[n]->getScale()*TransitingBlend);
//JointChildSceneNodes[n]->setScale(
// core::lerp(
// PretransitingSave[n].getScale(),
// JointChildSceneNodes[n]->getScale(),
// TransitingBlend));
}
}
}
}
}
......@@ -994,52 +972,40 @@ void CAnimatedMeshSceneNode::checkJoints()
if (!JointsUsed)
{
//Create joints for SkinnedMesh
((ISkinnedMesh*)Mesh)->createJoints( JointChildSceneNodes,this,SceneManager);
((ISkinnedMesh*)Mesh)->recoverJointsFromMesh( JointChildSceneNodes);
((ISkinnedMesh*)Mesh)->createJoints(JointChildSceneNodes, this, SceneManager);
((ISkinnedMesh*)Mesh)->recoverJointsFromMesh(JointChildSceneNodes);
JointsUsed=true;
JointMode=1;
}
}
void CAnimatedMeshSceneNode::beginTransition()
{
if (!JointsUsed) return;
u32 n;
if (!JointsUsed)
return;
if (TransitionTime!=0)
if (TransitionTime != 0)
{
//Check the array is big enough
if (PretransitingSave.size()<JointChildSceneNodes.size())
{
for(n=PretransitingSave.size();n<JointChildSceneNodes.size();++n)
for(u32 n=PretransitingSave.size(); n<JointChildSceneNodes.size(); ++n)
PretransitingSave.push_back(core::matrix4());
}
//Copy the position of joints
for (n=0;n<JointChildSceneNodes.size();++n)
for (u32 n=0;n<JointChildSceneNodes.size();++n)
PretransitingSave[n]=JointChildSceneNodes[n]->getRelativeTransformation();
Transiting=1.0f/f32(TransitionTime);
Transiting = core::reciprocal((f32)TransitionTime);
}
}
} // end namespace scene
} // end namespace irr
......@@ -150,9 +150,7 @@ namespace scene
private:
f32 buildFrameNr( u32 timeMs);
void checkJoints();
void beginTransition();
core::array<video::SMaterial> Materials;
......@@ -163,7 +161,6 @@ namespace scene
s32 StartFrame;
s32 EndFrame;
f32 FramesPerSecond;
f32 CurrentFrameNr;
s32 JointMode; //0-unused, 1-get joints only, 2-set joints only, 3-move and set
......
......@@ -34,26 +34,22 @@ CSkinnedMesh::CSkinnedMesh()
//! destructor
CSkinnedMesh::~CSkinnedMesh()
{
u32 n;
for (n=0;n<AllJoints.size();++n)
{
if (AllJoints[n])
delete AllJoints[n];
}
for (u32 i=0; i<AllJoints.size(); ++i)
delete AllJoints[i];
for (n=0;n<LocalBuffers.size();++n)
for (u32 j=0; j<LocalBuffers.size(); ++j)
{
if (LocalBuffers[n])
LocalBuffers[n]->drop();
if (LocalBuffers[j])
LocalBuffers[j]->drop();
}
}
//! returns the amount of frames in milliseconds. If the amount is 1, it is a static (=non animated) mesh.
//! returns the amount of frames in milliseconds.
//! If the amount is 1, it is a static (=non animated) mesh.
s32 CSkinnedMesh::getFrameCount()
{
return (s32)AnimationFrames;
return core::floor32(AnimationFrames);
}
......@@ -72,7 +68,6 @@ IMesh* CSkinnedMesh::getMesh(s32 frame, s32 detailLevel, s32 startFrameLoop, s32
}
//--------------------------------------------------------------------------
// Keyframe Animation
//--------------------------------------------------------------------------
......@@ -87,22 +82,22 @@ void CSkinnedMesh::animateMesh(f32 frame, f32 blend)
lastAnimatedFrame=frame;
if (blend<=0)
if (blend<=0.f)
return; //No need to animate
for (u32 i=0; i<AllJoints.size(); ++i)
{
//To Bitplane: The joints can be animated here with no input from there parents, but for setAnimationMode extra check are needed to their parents
//To Bitplane: The joints can be animated here with no input from their parents, but for setAnimationMode extra checks are needed to their parents
SJoint *Joint = AllJoints[i];
core::vector3df oldPosition = Joint->Animatedposition;
core::vector3df oldScale = Joint->Animatedscale;
core::quaternion oldRotation = Joint->Animatedrotation;
const core::vector3df oldPosition = Joint->Animatedposition;
const core::vector3df oldScale = Joint->Animatedscale;
const core::quaternion oldRotation = Joint->Animatedrotation;
core::vector3df position =oldPosition;
core::vector3df scale =oldScale;
core::quaternion rotation =oldRotation;
core::vector3df position = oldPosition;
core::vector3df scale = oldScale;
core::quaternion rotation = oldRotation;
if (!BoneControlUsed)
{
......@@ -118,25 +113,21 @@ void CSkinnedMesh::animateMesh(f32 frame, f32 blend)
if (blend==1.0f)
{
//No blending need:
//No blending needed
Joint->Animatedposition = position;
Joint->Animatedscale = scale;
Joint->Animatedrotation = rotation;
}
else
{
//Blend animation:
f32 invBlend=1-blend;
Joint->Animatedposition = (position * blend) + (oldPosition* invBlend );
Joint->Animatedscale = (scale * blend) + (oldScale* invBlend );
//Blend animation
Joint->Animatedposition = core::lerp(oldPosition, position, blend);
Joint->Animatedscale = core::lerp(oldScale, scale, blend);
Joint->Animatedrotation.slerp(oldRotation, rotation, blend);
}
//Node:
//_LocalAnimatedMatrix needs to be built at some point, but this function maybe called lots of times for
//Note:
//_LocalAnimatedMatrix needs to be built at some point, but this function may be called lots of times for
//one render (to play two animations at the same time) _LocalAnimatedMatrix only needs to be built once.
//a call to buildAllLocalAnimatedMatrices is needed before skinning the mesh, and before the user gets the joints to move
......@@ -144,7 +135,6 @@ void CSkinnedMesh::animateMesh(f32 frame, f32 blend)
// Temp!
buildAll_LocalAnimatedMatrices();
//-----------------
}
BoneControlUsed=false;
......@@ -179,13 +169,12 @@ void CSkinnedMesh::buildAll_LocalAnimatedMatrices()
{
Joint->LocalAnimatedMatrix=Joint->LocalMatrix;
}
}
}
void CSkinnedMesh::buildAll_GlobalAnimatedMatrices(SJoint *Joint, SJoint *ParentJoint)
{
if (!Joint)
{
for (u32 i=0; i<RootJoints.size(); ++i)
......@@ -869,64 +858,75 @@ void CSkinnedMesh::finalize()
core::array<SScaleKey> &ScaleKeys = AllJoints[i]->ScaleKeys;
core::array<SRotationKey> &RotationKeys = AllJoints[i]->RotationKeys;
if (PositionKeys.size()>2)
{
for(j=0;j<PositionKeys.size()-2;++j)
{
if (PositionKeys[j].position == PositionKeys[j+1].position && PositionKeys[j+1].position == PositionKeys[j+2].position)
{
PositionKeys.erase(j+1); //the middle key is unneeded
j--;
--j;
}
}
}
if (PositionKeys.size()>1)
{
for(j=0;j<PositionKeys.size()-1;++j)
{
if (PositionKeys[j].frame >= PositionKeys[j+1].frame) //bad frame, unneed and may cause problems
{
PositionKeys.erase(j+1);
j--;
--j;
}
}
}
if (ScaleKeys.size()>2)
{
for(j=0;j<ScaleKeys.size()-2;++j)
{
if (ScaleKeys[j].scale == ScaleKeys[j+1].scale && ScaleKeys[j+1].scale == ScaleKeys[j+2].scale)
{
ScaleKeys.erase(j+1); //the middle key is unneeded
j--;
--j;
}
}
}
if (ScaleKeys.size()>1)
{
for(j=0;j<ScaleKeys.size()-1;++j)
{
if (ScaleKeys[j].frame >= ScaleKeys[j+1].frame) //bad frame, unneed and may cause problems
{
ScaleKeys.erase(j+1);
j--;
--j;
}
}
}
if (RotationKeys.size()>2)
{
for(j=0;j<RotationKeys.size()-2;++j)
{
if (RotationKeys[j].rotation == RotationKeys[j+1].rotation && RotationKeys[j+1].rotation == RotationKeys[j+2].rotation)
{
RotationKeys.erase(j+1); //the middle key is unneeded
j--;
--j;
}
}
}
if (RotationKeys.size()>1)
{
for(j=0;j<RotationKeys.size()-1;++j)
{
if (RotationKeys[j].frame >= RotationKeys[j+1].frame) //bad frame, unneed and may cause problems
{
RotationKeys.erase(j+1);
j--;
--j;
}
}
}
......@@ -1088,7 +1088,7 @@ CSkinnedMesh::SWeight *CSkinnedMesh::createWeight(SJoint *joint)
void CSkinnedMesh::normalizeWeights()
{
// node: unsure if weights ids are going to be used.
// note: unsure if weights ids are going to be used.
// Normalise the weights on bones....
......@@ -1179,6 +1179,7 @@ void CSkinnedMesh::tranferJointsToMesh(core::array<IBoneSceneNode*> &JointChildS
BoneControlUsed=true;
}
void CSkinnedMesh::createJoints(core::array<IBoneSceneNode*> &JointChildSceneNodes,
IAnimatedMeshSceneNode* AnimatedMeshSceneNode, ISceneManager* SceneManager)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment