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     throw (Md3Exception);
00132   ~Md3PlayerSkin ();
00133 
00134 public:
00135   // Public interface
00136   void setLowerTextures (Md3Model *model) {
00137     setModelTextures (model, _lowerTextures);
00138   }
00139 
00140   void setUpperTextures (Md3Model *model) {
00141     setModelTextures (model, _upperTextures);
00142   }
00143 
00144   void setHeadTextures (Md3Model *model) {
00145     setModelTextures (model, _headTextures);
00146   }
00147 
00148   // Accessors
00149   const string &path () const { return _path; }
00150   const string &name () const { return _name; }
00151 
00152 private:
00153   // Internal types
00154   typedef map<string, const Texture2D *> TexMap;
00155 
00156 private:
00157   // Internal functions
00158   void loadSkinFile (const string &filename, TexMap &tmap)
00159     throw (Md3Exception);
00160 
00161   void setModelTextures (Md3Model *model, const TexMap &tmap) const;
00162 
00163 private:
00164   // Member variables
00165   TexMap _lowerTextures;
00166   TexMap _upperTextures;
00167   TexMap _headTextures;
00168 
00169   string _path;
00170   string _name;
00171 };
00172 
00173 
00174 /////////////////////////////////////////////////////////////////////////////
00175 //
00176 // class Md3Weapon -- MD3 Weapon Class.  A weapon is constituted of
00177 // two models: the weapon itself and the barrel (optional).
00178 //
00179 // Weapon models use their shader strings to load their textures.
00180 //
00181 // Weapons can be linked to player's upper model, or be drawn stand-alone.
00182 //
00183 /////////////////////////////////////////////////////////////////////////////
00184 
00185 class Md3Weapon
00186 {
00187 public:
00188   // Constructor/destructor
00189   Md3Weapon (const string &path, Md3PlayerLOD lod = kLodDefault)
00190     throw (Md3Exception);
00191   ~Md3Weapon ();
00192 
00193 private:
00194   // Internal types
00195   typedef shared_ptr<Md3Model> Md3ModelPtr;
00196 
00197 public:
00198   // Public interface
00199   void draw () const;
00200   void scaleModels () const;
00201   void linkToModel (Md3Model *model);
00202 
00203   void setScale (float scale) { _scale = scale; }
00204 
00205   // Accessors
00206   float scale () const { return _scale; }
00207   const string &path () const { return _path; }
00208   const string &name () const { return _name; }
00209   Md3PlayerLOD lod () const { return _lod; }
00210 
00211 private:
00212   // Member variables
00213   Md3ModelPtr _weapon;
00214   Md3ModelPtr _barrel;
00215 
00216   string _path;
00217   string _name;
00218   float _scale;
00219   Md3PlayerLOD _lod;
00220 };
00221 
00222 
00223 /////////////////////////////////////////////////////////////////////////////
00224 //
00225 // class Md3Player -- MD3 Player Class.  A player is constituted of
00226 // three MD3 models: the lower portion, the upper portion and the head.
00227 //
00228 // A player can have multiple skins, a weapon and animations.  The
00229 // Md3Player loads the models, skins and animations itself at construction.
00230 //
00231 /////////////////////////////////////////////////////////////////////////////
00232 
00233 class Md3Player
00234 {
00235 public:
00236   // Constructor/destructor
00237   Md3Player (const string &path, Md3PlayerLOD lod = kLodDefault)
00238     throw (Md3Exception);
00239   ~Md3Player ();
00240 
00241 private:
00242   // Internal types
00243 
00244   // Model animation state
00245   struct Md3AnimState
00246   {
00247   public:
00248     // Constructor
00249     Md3AnimState ();
00250 
00251     // Member functions
00252     void setup (Md3PlayerAnim_t *a);
00253     void update (float dt);
00254 
00255     // Pointer to current animation infos
00256     Md3PlayerAnim_t *anim;
00257     float curr_time;
00258     float old_time;
00259     int curr_frame;
00260     int next_frame;
00261     float interp;
00262   };
00263 
00264   typedef shared_ptr<Md3Model> Md3ModelPtr;
00265   typedef shared_ptr<Md3PlayerSkin> Md3PlayerSkinPtr;
00266 
00267 public:
00268   typedef map<string, Md3PlayerSkinPtr> SkinMap;
00269 
00270 public:
00271   // Public interface
00272   void draw () const;
00273   void renderFrame (int upperFrame, int lowerFrame) const;
00274   void animate (float dt);
00275 
00276   void setScale (float scale) { _scale = scale; }
00277   void setAnimation (Md3PlayerAnimType type);
00278   void setSkin (const string &name);
00279 
00280   void linkWeapon (Md3Weapon *weapon);
00281   void unlinkWeapon ();
00282 
00283   // Accessors
00284   float scale () const { return _scale; }
00285   const string &path () const { return _path; }
00286   const string &name () const { return _name; }
00287   Md3PlayerLOD lod () const { return _lod; }
00288   const SkinMap &skins () const { return _skins; }
00289   const string &skinName () const { return _currentSkinName; }
00290   void getAnimationState(eAnimationArea area,
00291                         Md3AnimState_t& state);
00292   void setAnimationState(eAnimationArea area,
00293                         const Md3AnimState_t& state);
00294 
00295 private:
00296   // Internal functions
00297   void loadModels (const string &path)
00298     throw (Md3Exception);
00299 
00300   void loadAnimations (const string &path)
00301     throw (Md3Exception);
00302 
00303   void loadSkins (const string &path)
00304     throw (Md3Exception);
00305 
00306   static void setState(const Md3AnimState& anim,
00307                 Md3AnimState_t& state);
00308 
00309   static void updateAnim(Md3AnimState& anim,
00310                 const Md3AnimState_t& state,
00311                 Md3PlayerAnim_t * a);
00312 
00313 private:
00314   // Member variables
00315   Md3ModelPtr _lower;
00316   Md3ModelPtr _upper;
00317   Md3ModelPtr _head;
00318 
00319   Md3AnimState _lowerAnim;
00320   Md3AnimState _upperAnim;
00321 
00322   // Animation list
00323   Md3PlayerAnim_t _anims[kMaxAnimations];
00324 
00325   // Skin list
00326   SkinMap _skins;
00327   Md3PlayerSkin *_currentSkin;
00328   string _currentSkinName;
00329 
00330   Md3Weapon *_weapon;
00331 
00332   string _path;
00333   string _name;
00334   float _scale;
00335   Md3PlayerLOD _lod;
00336 };
00337 
00338 #endif  // __MD3PLAYER_H__