00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "KeosAnimatedMeshKF.h"
00023 #include "KeosException.h"
00024 #include "KeosRoot.h"
00025 #include "KeosRenderSystem.h"
00026 #include "KeosUtil.h"
00027
00028 namespace Keos
00029 {
00030 #define Renderer CRoot::Instance().GetRenderSystem()
00031
00032
00033
00034
00035
00036
00037
00038 CAnimatedMeshKF::CAnimatedMeshKF(std::vector<CMeshGeom::TVertex> Frames)
00039 {
00040 m_Frames = Frames;
00041
00042 m_nCurrFrame = 0;
00043 m_nNextFrame = 0;
00044 m_fInterp = 0.0f;
00045 m_fPercent = 0.0f;
00046
00047 m_nCurrentAnimation = 0;
00048 }
00049
00050
00051 CAnimatedMeshKF::~CAnimatedMeshKF()
00052 {}
00053
00054
00055 void CAnimatedMeshKF::SetAnimation(uint nAnimIndex)
00056 {
00057 Assert(nAnimIndex < GetNumberOfAnimations());
00058
00059 m_nCurrentAnimation = nAnimIndex;
00060
00061 CAnimationInfoKF AnimationInfo = m_AnimationVector[m_nCurrentAnimation];
00062
00063 if ( m_nCurrFrame < AnimationInfo.Begin )
00064 m_nCurrFrame = AnimationInfo.Begin;
00065
00066 if ( m_nCurrFrame > AnimationInfo.End )
00067 m_nCurrFrame = AnimationInfo.Begin;
00068
00069 m_nNextFrame = m_nCurrFrame + 1;
00070
00071 if ( m_nNextFrame >= AnimationInfo.End )
00072 m_nNextFrame = AnimationInfo.Begin;
00073 }
00074
00075
00076 void CAnimatedMeshKF::LinearInterpolation()
00077 {
00078 CMeshGeom* pMeshGeom = GetMeshGeom(0);
00079 int nOffsetFrame1 = m_nCurrFrame * pMeshGeom->m_VertexBuffer.GetCount();
00080 int nOffsetFrame2 = m_nNextFrame * pMeshGeom->m_VertexBuffer.GetCount();
00081
00082 CMeshGeom::TVertex* Vertices = pMeshGeom->m_VertexBuffer.Lock(0, 0, LOCK_WRITEONLY);
00083 for (uint i = 0; i < pMeshGeom->m_VertexBuffer.GetCount(); i++)
00084 {
00085
00086 Vertices[i].Position = m_Frames[nOffsetFrame1 + i].Position + m_fInterp *
00087 (m_Frames[nOffsetFrame2 + i].Position - m_Frames[nOffsetFrame1 + i].Position);
00088 }
00089 pMeshGeom->m_VertexBuffer.Unlock();
00090
00091 m_fInterp += m_fPercent;
00092 }
00093
00094
00095 void CAnimatedMeshKF::Animate( float fPercent )
00096 {
00097 CAnimationInfoKF AnimationInfo = m_AnimationVector[m_nCurrentAnimation];
00098
00099 if ( m_nCurrFrame > AnimationInfo.End )
00100 m_nCurrFrame = AnimationInfo.Begin;
00101
00102 m_fPercent = fPercent;
00103 if ( m_fInterp >= 1.0 )
00104 {
00105 m_fInterp = 0.0f;
00106 m_nCurrFrame++;
00107 if ( m_nCurrFrame >= AnimationInfo.End )
00108 m_nCurrFrame = AnimationInfo.Begin;
00109
00110 m_nNextFrame = m_nCurrFrame + 1;
00111
00112 if ( m_nNextFrame >= AnimationInfo.End )
00113 m_nNextFrame = AnimationInfo.Begin;
00114 }
00115
00116 LinearInterpolation();
00117 Render();
00118 }
00119
00120
00121 void CAnimatedMeshKF::AddAnimation(const CAnimationInfoKF& AnimationInfo)
00122 {
00123 m_AnimationVector.push_back(AnimationInfo);
00124 }
00125
00126
00127 }