KeosAnimatedMeshKF.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
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   // CAnimatedMeshKF implementation
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       // Linear interpolation
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 } // namespace Keos

Generated on Fri Mar 9 14:29:01 2007 for Keos by  doxygen 1.5.1-p1