Md3Player.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /////////////////////////////////////////////////////////////////////////////
00003 //
00004 // Md3Player.h -- Copyright (c) 2006 David Henry
00005 // last modification: feb. 25, 2006
00006 //
00007 // Updates by Thomas Vaughan 2010
00008 //  - added support for animation state retrieval
00009 //
00010 // This code is licenced under the MIT license.
00011 //
00012 // This software is provided "as is" without express or implied
00013 // warranties. You may freely copy and compile this source into
00014 // applications you distribute provided that the copyright text
00015 // below is included in the resulting source code.
00016 //
00017 // Definition of MD3 Player Classes.
00018 //
00019 /////////////////////////////////////////////////////////////////////////////
00020 
00021 #ifndef __MD3PLAYER_H__
00022 #define __MD3PLAYER_H__
00023 
00024 #include "Md3Model.h"
00025 
00026 
00027 // Level of details
00028 enum Md3PlayerLOD
00029   {
00030     kLodLow,
00031     kLodMedium,
00032     kLodHigh,
00033 
00034     kLodDefault = kLodHigh,
00035   };
00036 
00037 
00038 // Animation list
00039 enum Md3PlayerAnimType
00040   {
00041     // Animations for both lower and upper
00042     // parts of the player
00043     kBothDeath1,
00044     kBothDead1,
00045     kBothDeath2,
00046     kBothDead2,
00047     kBothDeath3,
00048     kBothDead3,
00049 
00050     // Animations for the upper part
00051     kTorsoGesture,
00052 
00053     kTorsoAttack,
00054     kTorsoAttack2,
00055 
00056     kTorsoDrop,
00057     kTorsoRaise,
00058 
00059     kTorsoStand,
00060     kTorsoStand2,
00061 
00062     // Animations for the lower part
00063     kLegsWalkCr,
00064     kLegsWalk,
00065     kLegsRun,
00066     kLegsBack,
00067     kLegsSwim,
00068 
00069     kLegsJump,
00070     kLegsLand,
00071 
00072     kLegsJumpB,
00073     kLegsLandB,
00074 
00075     kLegsIdle,
00076     kLegsIdleCr,
00077 
00078     kLegsTurn,
00079 
00080     kMaxAnimations,
00081   };
00082 
00083 
00084 // Animation infos
00085 struct Md3PlayerAnim_t
00086 {
00087   int index;     // which index is this?
00088   int first;     // First frame
00089   int num;       // Last frame
00090   int looping;   // Looping frames
00091   int fps;       // Frames per second
00092 };
00093 
00094 
00095 
00096 enum eAnimationArea {
00097         eAnimationArea_Upper    = 1,    // upper body
00098         eAnimationArea_Lower    = 2,    // lower body
00099 
00100         eAnimationArea_Invalid  = 0
00101 };
00102 
00103 
00104 struct Md3AnimState_t {
00105         Md3AnimState_t(void) throw() { this->clear(); }
00106         void clear(void) throw() {
00107                         animationIndex = -1;
00108                         time = -1;
00109                 }
00110 
00111         // data fields
00112         int     animationIndex; // which animation is this?
00113         float   time;           // how far into animation are we?
00114 };
00115 
00116 
00117 
00118 /////////////////////////////////////////////////////////////////////////////
00119 //
00120 // class Md3PlayerSkin -- MD3 Player Skin Class.  A player skin is
00121 // constituted of a list of mesh-texture pairs, one for each portion
00122 // of player's models (lower, upper and head).
00123 //
00124 /////////////////////////////////////////////////////////////////////////////
00125 
00126 class Md3PlayerSkin
00127 {
00128 public:
00129   // Constructor/destructor
00130   Md3PlayerSkin (const string &path, const string &name);
00131   ~Md3PlayerSkin ();
00132 
00133 public:
00134   // Public interface
00135   void setLowerTextures (Md3Model *model) {
00136     setModelTextures (model, _lowerTextures);
00137   }
00138 
00139   void setUpperTextures (Md3Model *model) {
00140     setModelTextures (model, _upperTextures);
00141   }
00142 
00143   void setHeadTextures (Md3Model *model) {
00144     setModelTextures (model, _headTextures);
00145   }
00146 
00147   // Accessors
00148   const string &path () const { return _path; }
00149   const string &name () const { return _name; }
00150 
00151 private:
00152   // Internal types
00153   typedef map<string, const Texture2D *> TexMap;
00154 
00155 private:
00156   // Internal functions
00157   void loadSkinFile (const string &filename, TexMap &tmap);
00158   void setModelTextures (Md3Model *model, const TexMap &tmap) const;
00159 
00160 private:
00161   // Member variables
00162   TexMap _lowerTextures;
00163   TexMap _upperTextures;
00164   TexMap _headTextures;
00165 
00166   string _path;
00167   string _name;
00168 };
00169 
00170 
00171 /////////////////////////////////////////////////////////////////////////////
00172 //
00173 // class Md3Weapon -- MD3 Weapon Class.  A weapon is constituted of
00174 // two models: the weapon itself and the barrel (optional).
00175 //
00176 // Weapon models use their shader strings to load their textures.
00177 //
00178 // Weapons can be linked to player's upper model, or be drawn stand-alone.
00179 //
00180 /////////////////////////////////////////////////////////////////////////////
00181 
00182 class Md3Weapon
00183 {
00184 public:
00185   // Constructor/destructor
00186   Md3Weapon (const string &path, Md3PlayerLOD lod = kLodDefault);
00187   ~Md3Weapon ();
00188 
00189 private:
00190   // Internal types
00191   typedef smart_ptr<Md3Model> Md3ModelPtr;
00192 
00193 public:
00194   // Public interface
00195   void draw () const;
00196   void scaleModels () const;
00197   void linkToModel (Md3Model *model);
00198 
00199   void setScale (float scale) { _scale = scale; }
00200 
00201   // Accessors
00202   float scale () const { return _scale; }
00203   const string &path () const { return _path; }
00204   const string &name () const { return _name; }
00205   Md3PlayerLOD lod () const { return _lod; }
00206 
00207 private:
00208   // Member variables
00209   Md3ModelPtr _weapon;
00210   Md3ModelPtr _barrel;
00211 
00212   string _path;
00213   string _name;
00214   float _scale;
00215   Md3PlayerLOD _lod;
00216 };
00217 
00218 
00219 /////////////////////////////////////////////////////////////////////////////
00220 //
00221 // class Md3Player -- MD3 Player Class.  A player is constituted of
00222 // three MD3 models: the lower portion, the upper portion and the head.
00223 //
00224 // A player can have multiple skins, a weapon and animations.  The
00225 // Md3Player loads the models, skins and animations itself at construction.
00226 //
00227 /////////////////////////////////////////////////////////////////////////////
00228 
00229 class Md3Player
00230 {
00231 public:
00232   // Constructor/destructor
00233   Md3Player (const string &path, Md3PlayerLOD lod = kLodDefault);
00234   ~Md3Player ();
00235 
00236 private:
00237   // Internal types
00238 
00239   // Model animation state
00240   struct Md3AnimState
00241   {
00242   public:
00243     // Constructor
00244     Md3AnimState ();
00245 
00246     // Member functions
00247     void setup (Md3PlayerAnim_t *a);
00248     void update (float dt);
00249 
00250     // Pointer to current animation infos
00251     Md3PlayerAnim_t *anim;
00252     float curr_time;
00253     float old_time;
00254     int curr_frame;
00255     int next_frame;
00256     float interp;
00257   };
00258 
00259   typedef smart_ptr<Md3Model> Md3ModelPtr;
00260   typedef smart_ptr<Md3PlayerSkin> Md3PlayerSkinPtr;
00261 
00262 public:
00263   typedef map<string, Md3PlayerSkinPtr> SkinMap;
00264 
00265 public:
00266   // Public interface
00267   void draw () const;
00268   void renderFrame (int upperFrame, int lowerFrame) const;
00269   void animate (float dt);
00270 
00271   void setScale (float scale) { _scale = scale; }
00272   void setAnimation (Md3PlayerAnimType type);
00273   void setSkin (const string &name);
00274 
00275   void linkWeapon (Md3Weapon *weapon);
00276   void unlinkWeapon ();
00277 
00278   // Accessors
00279   float scale () const { return _scale; }
00280   const string &path () const { return _path; }
00281   const string &name () const { return _name; }
00282   Md3PlayerLOD lod () const { return _lod; }
00283   const SkinMap &skins () const { return _skins; }
00284   const string &skinName () const { return _currentSkinName; }
00285   void getAnimationState(eAnimationArea area,
00286                         Md3AnimState_t& state);
00287   void setAnimationState(eAnimationArea area,
00288                         const Md3AnimState_t& state);
00289 
00290 private:
00291   // Internal functions
00292   void loadModels (const string &path);
00293 
00294   void loadAnimations (const string &path);
00295 
00296   void loadSkins (const string &path);
00297 
00298   static void setState(const Md3AnimState& anim,
00299                 Md3AnimState_t& state);
00300 
00301   static void updateAnim(Md3AnimState& anim,
00302                 const Md3AnimState_t& state,
00303                 Md3PlayerAnim_t * a);
00304 
00305 private:
00306   // Member variables
00307   Md3ModelPtr _lower;
00308   Md3ModelPtr _upper;
00309   Md3ModelPtr _head;
00310 
00311   Md3AnimState _lowerAnim;
00312   Md3AnimState _upperAnim;
00313 
00314   // Animation list
00315   Md3PlayerAnim_t _anims[kMaxAnimations];
00316 
00317   // Skin list
00318   SkinMap _skins;
00319   Md3PlayerSkin *_currentSkin;
00320   string _currentSkinName;
00321 
00322   Md3Weapon *_weapon;
00323 
00324   string _path;
00325   string _name;
00326   float _scale;
00327   Md3PlayerLOD _lod;
00328 };
00329 
00330 #endif  // __MD3PLAYER_H__