Page principale | Liste des namespaces | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Membres des namespaces | Composants | Déclarations

# ExCAnimation.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002  * ExNihilo 3D Engine
    00003  *
    00004  *  This program is free software; you can redistribute it and/or modify
    00005  *  it under the terms of the GNU General Public License as published by
    00006  *  the Free Software Foundation; either version 2 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  This program is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU General Public License
    00015  *  along with this program; if not, write to the Free Software
    00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  *
    00018  * Please read AUTHORS file !!!
    00019  *
    00020
    00021  *
    00022  */
    00023
    00024 #include "ExCAnimation.h"
    00025
    00026 ExCAnimation::ExCAnimation()
    00027 {
    00028 Guard(ExCAnimation::ExCAnimation())
    00029         ExCObject3D::ExCObject3D();
    00030         SetName("ExCAnimation");
    00031         SetType(typeid(this).name());
    00032         m_Time=0;
    00033         m_CurrentAnimation=0;
    00034         m_Pause=false;
    00035 UnGuard
    00036 }
    00037
    00038 ExCAnimation::~ExCAnimation()
    00039 {
    00040 Guard(ExCAnimation::~ExCAnimation())
    00041 UnGuard
    00042 }
    00043
    00044
    00045 void ExCAnimation::Draw(void)
    00046 {
    00047 Guard(void ExCAnimation::Draw(void))
    00048         //Draw gizmo line
    00049         for(unsigned int i=0;i<m_VecAnimation.size();i++)
    00050         {
    00051                 m_VecAnimation.at(i).m_Line.Draw();
    00052                 //Draw Angle interpol point
    00053                 if(m_ShowPath)
    00054                 {
    00055                         ExCVec3D PositionAngle;
    00056                         glColor3f(0,0,1);
    00057                         glPushMatrix();
    00058                         PositionAngle=m_VecAnimation.at(i).m_Line.PointOnCurve(0.25);
    00059                         glTranslatef(PositionAngle.GetX(),PositionAngle.GetY(),PositionAngle.GetZ());
    00060                         glutWireSphere(5,8,8);
    00061                         glPopMatrix();
    00062
    00063                         glPushMatrix();
    00064                         PositionAngle=m_VecAnimation.at(i).m_Line.PointOnCurve(0.5);
    00065                         glTranslatef(PositionAngle.GetX(),PositionAngle.GetY(),PositionAngle.GetZ());
    00066                         glutWireSphere(5,8,8);
    00067                         glPopMatrix();
    00068
    00069                         glPushMatrix();
    00070                         PositionAngle=m_VecAnimation.at(i).m_Line.PointOnCurve(0.75);
    00071                         glTranslatef(PositionAngle.GetX(),PositionAngle.GetY(),PositionAngle.GetZ());
    00072                         glutWireSphere(5,8,8);
    00073                         glPopMatrix();
    00074                 }
    00075         }
    00076         float AgX,AgY,AgZ;
    00077         //Get posistion on curve
    00078         m_Object->m_Position=m_VecAnimation.at(m_CurrentAnimation).m_Line.PointOnCurve(m_Time);
    00079
    00080         if(!m_Pause)
    00081         {
    00082                 //Interpol angle
    00083                 float nbriterprc;
    00084                 nbriterprc=m_VecAnimation.at(m_CurrentAnimation).m_NumberIter/4;
    00085                 if(m_Time==0)
    00086                 {
    00087                         m_Object->m_Angle.SetValue(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetX(),
    00088                                 m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetY(),
    00089                                 m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetZ());
    00090
    00091                 }else
    00092                 {
    00093                         if(m_Time<=0.25)
    00094                         {
    00095                                 AgX=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetX()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint1.GetX())/nbriterprc;
    00096                                 AgY=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetY()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint1.GetY())/nbriterprc;
    00097                                 AgZ=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetZ()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint1.GetZ())/nbriterprc;
    00098                                 m_Object->m_Angle.DecX(AgX);
    00099                                 m_Object->m_Angle.DecY(AgY);
    00100                                 m_Object->m_Angle.DecZ(AgZ);
    00101
    00102                         }
    00103                         if(m_Time>0.25&&m_Time<0.5)
    00104                         {
    00105
    00106                                 AgX=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint1.GetX()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint2.GetX())/nbriterprc;
    00107                                 AgY=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint1.GetY()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint2.GetY())/nbriterprc;
    00108                                 AgZ=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint1.GetZ()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint2.GetZ())/nbriterprc;
    00109                                 m_Object->m_Angle.DecX(AgX);
    00110                                 m_Object->m_Angle.DecY(AgY);
    00111                                 m_Object->m_Angle.DecZ(AgZ);
    00112                         }
    00113                         if(m_Time>0.5&&m_Time<0.75)
    00114                         {
    00115                                 AgX=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint2.GetX()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint3.GetX())/nbriterprc;
    00116                                 AgY=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint2.GetY()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint3.GetY())/nbriterprc;
    00117                                 AgZ=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint2.GetZ()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint3.GetZ())/nbriterprc;
    00118                                 m_Object->m_Angle.DecX(AgX);
    00119                                 m_Object->m_Angle.DecY(AgY);
    00120                                 m_Object->m_Angle.DecZ(AgZ);
    00121                         }
    00122                         if(m_Time>0.75&&m_Time<1)
    00123                         {
    00124                                 AgX=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint3.GetX()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint4.GetX())/nbriterprc;
    00125                                 AgY=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint3.GetY()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint4.GetY())/nbriterprc;
    00126                                 AgZ=(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint3.GetZ()-m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint4.GetZ())/nbriterprc;
    00127                                 m_Object->m_Angle.DecX(AgX);
    00128                                 m_Object->m_Angle.DecY(AgY);
    00129                                 m_Object->m_Angle.DecZ(AgZ);
    00130                         }
    00131
    00132
    00133                 }
    00134
    00135
    00136                 if(m_Loop)m_Time=m_Time+m_VecAnimation.at(m_CurrentAnimation).m_Speed;
    00137                 if (m_Time>1)
    00138                 {
    00139                         //std::cout<<"NExt anim "<<m_VecAnimation.at(m_CurrentAnimation).m_EndAction<<std::endl;
    00140                         if(m_VecAnimation.at(m_CurrentAnimation).m_EndAction.m_Action!=0)
    00141                         {
    00142                                 FluxAction->Push(m_VecAnimation.at(m_CurrentAnimation).m_EndAction);
    00143                         }
    00144                         m_Time=0;
    00145                         m_CurrentAnimation++;
    00146                         if(m_CurrentAnimation>=m_VecAnimation.size())
    00147                         {
    00148                                 if(m_Loop)
    00149                                 {
    00150                                         m_CurrentAnimation=0;
    00151                                 }
    00152                         }
    00153                 }
    00154         }
    00155 UnGuard
    00156 }
    00157
    00158 void ExCAnimation::SetShowPathState(bool state)
    00159 {
    00160         m_ShowPath=state;
    00161         for(unsigned int i=0;i<m_VecAnimation.size();i++)
    00162         {
    00163                  m_VecAnimation.at(i).m_Line.SetVisibleState(m_ShowPath);
    00164         }
    00165 }
    00166
    00167 bool ExCAnimation::LoadFile(std::string FileName)
    00168 {
    00169 Guard(bool ExCAnimation::LoadFile(std::string FileName))
    00170         m_VecAnimation.clear();
    00171         SetFileName(FileName);
    00172         SetName(FileName);
    00173         char                    buffer[255];
    00174         sprintf(buffer, "../Data/Animation/%s", FileName.data());
    00175         std::ifstream fin;
    00176         std::string buffstring;
    00177         char b[256];
    00178         fin.open(buffer,std::ios::in);
    00179
    00180         AnimationStruct Anim;
    00181         int tmpNumber;
    00182         if(fin.is_open())
    00183         {
    00184                 try
    00185                 {
    00186                         //----Read Type
    00187                         memset(b,0,255);
    00188                         fin.getline(b,256,'\n');
    00189                         //----Read Loop
    00190                         memset(b,0,255);
    00191                         fin.getline(b,256,'\n');
    00192                         m_Loop=ExNihilo::ExtractIntValueFromSring(b,"<Loop>","<#Loop>");
    00193                         //----Read TargetObjectType
    00194                         memset(b,0,255);
    00195                         fin.getline(b,256,'\n');
    00196                         m_TargetObjectType=ExNihilo::ExtracValueFromSring(b,"<TargetObjectType>","<#TargetObjectType>");
    00197                         //----Read TargetObjectName
    00198                         memset(b,0,255);
    00199                         fin.getline(b,256,'\n');
    00200                         m_TargetObjectName=ExNihilo::ExtracValueFromSring(b,"<TargetObjectName>","<#TargetObjectName>");
    00201                         //----Read ShowPath
    00202                         memset(b,0,255);
    00203                         fin.getline(b,256,'\n');
    00204                         m_ShowPath=(bool)ExNihilo::ExtractIntValueFromSring(b,"<ShowPath>","<#ShowPath>");
    00205                         //----Read Number of Bezier
    00206                         memset(b,0,255);
    00207                         fin.getline(b,256,'\n');
    00208                         tmpNumber=ExNihilo::ExtractIntValueFromSring(b,"<NumberOfBezier>","<#NumberOfBezier>");
    00209
    00210                         for(int i=0;i<tmpNumber;i++)
    00211                         {
    00212                                 //----Read Bezier file
    00213                                 memset(b,0,255);
    00214                                 fin.getline(b,256,'\n');
    00215                                 Anim.m_Line.LoadFile(ExNihilo::ExtracValueFromSring(b,"<BezierFile>","<#BezierFile>"));
    00216                                 //----Read speed
    00217                                 memset(b,0,255);
    00218                                 fin.getline(b,256,'\n');
    00219                                 Anim.m_Speed=ExNihilo::ExtractFloatValueFromSring(b,"<Speed>","<#Speed>");
    00220                                 //----Read end action
    00221                                 memset(b,0,255);
    00222                                 fin.getline(b,256,'\n');
    00223
    00224                                 buffstring=ExNihilo::ExtracValueFromSring(b,"<Exec_Action>","<#Exec_Action>");
    00225                                 Anim.m_EndAction.m_Action=ExNihilo::ExtractIntValueFromSring(buffstring,"<Action>","<#Action>");
    00226                                 Anim.m_EndAction.m_Param=ExNihilo::ExtracValueFromSring(buffstring,"<Param>","<#Param>");
    00227                                 Anim.m_EndAction.m_Param1=ExNihilo::ExtracValueFromSring(buffstring,"<Param1>","<#Param1>");
    00228                                 Anim.m_EndAction.m_Param2=ExNihilo::ExtracValueFromSring(buffstring,"<Param2>","<#Param2>");
    00229                                 Anim.m_EndAction.m_ExecutingLife=ExNihilo::ExtractIntValueFromSring(buffstring,"<Timer>","<#Timer>");
    00230                                 Anim.m_EndAction.m_StartingLife=(double)((double)glutGet(GLUT_ELAPSED_TIME)/1000);
    00231
    00232                                 //---- Read CtrtPoint0Angle
    00233                                 memset(b,0,255);
    00234                                 fin.getline(b,256,'\n');
    00235                                 buffstring=ExNihilo::ExtracValueFromSring(b,"<CtrlPoint0Angle>","<#CtrlPoint0Angle>");
    00236                                 Anim.m_AngleCtrlPoint0.SetValue(ExNihilo::ExtractFloatValueFromSring(buffstring,"<X>","<#X>"),
    00237                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Y>","<#Y>"),
    00238                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Z>","<#Z>"));
    00239                                 //---- Read CtrtPoint1Angle
    00240                                 memset(b,0,255);
    00241                                 fin.getline(b,256,'\n');
    00242                                 buffstring=ExNihilo::ExtracValueFromSring(b,"<CtrlPoint1Angle>","<#CtrtPoint1Angle>");
    00243                                 Anim.m_AngleCtrlPoint1.SetValue(ExNihilo::ExtractFloatValueFromSring(buffstring,"<X>","<#X>"),
    00244                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Y>","<#Y>"),
    00245                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Z>","<#Z>"));
    00246                                 //---- Read CtrtPoint2Angle
    00247                                 memset(b,0,255);
    00248                                 fin.getline(b,256,'\n');
    00249                                 buffstring=ExNihilo::ExtracValueFromSring(b,"<CtrlPoint2Angle>","<#CtrlPoint2Angle>");
    00250                                 Anim.m_AngleCtrlPoint2.SetValue(ExNihilo::ExtractFloatValueFromSring(buffstring,"<X>","<#X>"),
    00251                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Y>","<#Y>"),
    00252                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Z>","<#Z>"));
    00253                                 //---- Read CtrtPoint3Angle
    00254                                 memset(b,0,255);
    00255                                 fin.getline(b,256,'\n');
    00256                                 buffstring=ExNihilo::ExtracValueFromSring(b,"<CtrlPoint3Angle>","<#CtrlPoint3Angle>");
    00257                                 Anim.m_AngleCtrlPoint3.SetValue(ExNihilo::ExtractFloatValueFromSring(buffstring,"<X>","<#X>"),
    00258                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Y>","<#Y>"),
    00259                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Z>","<#Z>"));
    00260                                 //---- Read CtrtPoint4Angle
    00261                                 memset(b,0,255);
    00262                                 fin.getline(b,256,'\n');
    00263                                 buffstring=ExNihilo::ExtracValueFromSring(b,"<CtrlPoint4Angle>","<#CtrlPoint4Angle>");
    00264                                 Anim.m_AngleCtrlPoint4.SetValue(ExNihilo::ExtractFloatValueFromSring(buffstring,"<X>","<#X>"),
    00265                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Y>","<#Y>"),
    00266                                         ExNihilo::ExtractFloatValueFromSring(buffstring,"<Z>","<#Z>"));
    00267
    00268                                 Anim.m_NumberIter=1/Anim.m_Speed;
    00269                                 m_VecAnimation.push_back(Anim);
    00270                         }
    00271                         fin.close();
    00272                 }
    00273                 catch(ExCExpStringNotFound)
    00274                 {
    00275                         std::cout<<"ExCExpStringNotFound"<<std::endl;
    00276                 }
    00277                 catch(...)
    00278                 {
    00279                         std::cout<<"ExCExpFileReadError"<<std::endl;
    00280                         throw ExCExpFileReadError();
    00281                 }
    00282         }else throw   ExCExpFileNotFound();
    00283         SetShowPathState(m_ShowPath);
    00284
    00285         //Find manager where is the object and get pointer to object
    00286         try
    00287         {
    00288
    00289                 if(strcmpi(ManagerId->GetObjectManagerType(m_TargetObjectType).data(),"ExManagerCamera")==0)
    00290                 {
    00291                         m_Object=ManagerCamera->GetObject(ManagerId->GetObjectId(m_TargetObjectName,m_TargetObjectType));
    00292
    00293                 }
    00294                 if(strcmpi(ManagerId->GetObjectManagerType(m_TargetObjectType).data(),"ExManagerModel")==0)
    00295                 {
    00296                         m_Object=ManagerModel->GetObject(ManagerId->GetObjectId(m_TargetObjectName,m_TargetObjectType));
    00297
    00298                 }
    00299                 if(strcmpi(ManagerId->GetObjectManagerType(m_TargetObjectType).data(),"ExManagerEntity")==0)
    00300                 {
    00301                         m_Object=ManagerEntity->GetObject(ManagerId->GetObjectId(m_TargetObjectName,m_TargetObjectType));
    00302
    00303                 }
    00304                 m_Object->m_Angle.SetValue(m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetX(),
    00305                         m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetY(),
    00306                         m_VecAnimation.at(m_CurrentAnimation).m_AngleCtrlPoint0.GetZ());
    00307                 return true;
    00308         }catch (...)
    00309         {
    00310                 std::cout<<"Wrong animation object chek name or object type in your anim file"<<std::endl;
    00311                 return false;
    00312         }
    00313         return false;
    00314 UnGuard
    00315 }
    00316
    00317