| 1 | /*! |
|---|
| 2 | * @file md2Model.h |
|---|
| 3 | * Definition of an MD2 Model, a model format invented by ID Software. |
|---|
| 4 | |
|---|
| 5 | We are deeply thankfull for all the wunderfull things id software made for us gamers! |
|---|
| 6 | |
|---|
| 7 | The md2 file format is structured in a very simple way: it contains animations which are made out of |
|---|
| 8 | frames (so called keyframes). Each frame is a complete draweable model in a specific position. |
|---|
| 9 | A frame is a collection of vertex and its compagnions (normals, texture coordinates). |
|---|
| 10 | |
|---|
| 11 | A typical model has about 200 frames, the maximum frame count is fixed by MD2_MAX_FRAMES to currently |
|---|
| 12 | 512 frames. The maximal vetices count is set to 2048 verteces, not enough? |
|---|
| 13 | You just have to change the MD2_MAX_* values if it doesn't fit your purposes... |
|---|
| 14 | |
|---|
| 15 | Surface Culling is fully implemented in md2 models: quake2 uses front face culling. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #ifndef _MD2MODEL_H |
|---|
| 19 | #define _MD2MODEL_H |
|---|
| 20 | |
|---|
| 21 | #include "base_object.h" |
|---|
| 22 | #include "count_pointer.h" |
|---|
| 23 | |
|---|
| 24 | #include "interactive_model.h" |
|---|
| 25 | #include "material.h" |
|---|
| 26 | |
|---|
| 27 | #include "md_model_structure.h" |
|---|
| 28 | |
|---|
| 29 | //! These are the needed defines for the max values when loading .MD2 files |
|---|
| 30 | #define MD2_IDENT (('2'<<24) + ('P'<<16) + ('D'<<8) + 'I') //!< the md2 identifier tag in the bin file |
|---|
| 31 | #define MD2_VERSION 8 //!< the md2 version in the header |
|---|
| 32 | #define MD2_MAX_TRIANGLES 4096 //!< maximal triangles count |
|---|
| 33 | #define MD2_MAX_VERTICES 3048 //!< maximal vertices count |
|---|
| 34 | #define MD2_MAX_TEXCOORDS 3048 //!< maximal tex coordinates |
|---|
| 35 | #define MD2_MAX_FRAMES 512 //!< maximal frames |
|---|
| 36 | #define MD2_MAX_SKINS 32 //!< maximal skins |
|---|
| 37 | #define MD2_MAX_FRAMESIZE (MD2_MAX_VERTICES * 4 + 128) //!< maximal framesize |
|---|
| 38 | |
|---|
| 39 | #define NUM_VERTEX_NORMALS 162 //!< number of vertex normals |
|---|
| 40 | #define SHADEDOT_QUANT 16 //!< shade dot quantity - no idea what it is |
|---|
| 41 | |
|---|
| 42 | //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework |
|---|
| 43 | #define kAnimationSpeed 12.0f //!< animation speed |
|---|
| 44 | |
|---|
| 45 | //! This holds the header information that is read in at the beginning of the file: id software definition |
|---|
| 46 | struct MD2Header |
|---|
| 47 | { |
|---|
| 48 | int ident; //!< This is used to identify the file |
|---|
| 49 | int version; //!< The version number of the file (Must be 8) |
|---|
| 50 | |
|---|
| 51 | int skinWidth; //!< The skin width in pixels |
|---|
| 52 | int skinHeight; //!< The skin height in pixels |
|---|
| 53 | int frameSize; //!< The size in bytes the frames are |
|---|
| 54 | |
|---|
| 55 | int numSkins; //!< The number of skins associated with the model |
|---|
| 56 | int numVertices; //!< The number of vertices (constant for each frame) |
|---|
| 57 | int numTexCoords; //!< The number of texture coordinates |
|---|
| 58 | int numTriangles; //!< The number of faces (polygons) |
|---|
| 59 | int numGlCommands; //!< The number of gl commands |
|---|
| 60 | int numFrames; //!< The number of animation frames |
|---|
| 61 | |
|---|
| 62 | int offsetSkins; //!< The offset in the file for the skin data |
|---|
| 63 | int offsetTexCoords; //!< The offset in the file for the texture data |
|---|
| 64 | int offsetTriangles; //!< The offset in the file for the face data |
|---|
| 65 | int offsetFrames; //!< The offset in the file for the frames data |
|---|
| 66 | int offsetGlCommands; //!< The offset in the file for the gl commands data |
|---|
| 67 | int offsetEnd; //!< The end of the file offset |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | //! animation names enumeration |
|---|
| 72 | typedef enum MD2animType |
|---|
| 73 | { |
|---|
| 74 | STAND, //0 |
|---|
| 75 | RUN, //1 |
|---|
| 76 | ATTACK, //2 |
|---|
| 77 | PAIN_A, //3 |
|---|
| 78 | PAIN_B, //4 |
|---|
| 79 | PAIN_C, //5 |
|---|
| 80 | JUMP, //6 |
|---|
| 81 | FLIP, //7 |
|---|
| 82 | SALUTE, //8 |
|---|
| 83 | FALLBACK, //9 |
|---|
| 84 | WAVE, //10 |
|---|
| 85 | MD2_POINT, //11 |
|---|
| 86 | CROUCH_STAND, |
|---|
| 87 | CROUCH_WALK, |
|---|
| 88 | CROUCH_ATTACK, |
|---|
| 89 | CROUCH_PAIN, |
|---|
| 90 | CROUCH_DEATH, |
|---|
| 91 | DEATH_FALLBACK, |
|---|
| 92 | DEATH_FALLFORWARD, |
|---|
| 93 | DEATH_FALLBACKSLOW, |
|---|
| 94 | BOOM, |
|---|
| 95 | WALK, |
|---|
| 96 | |
|---|
| 97 | MAX_ANIMATIONS |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | typedef enum animPlayback |
|---|
| 102 | { |
|---|
| 103 | MD2_ANIM_LOOP = 0, |
|---|
| 104 | MD2_ANIM_ONCE, |
|---|
| 105 | |
|---|
| 106 | MD2_ANIM_NUM |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | /* forward definitions */ |
|---|
| 112 | class Material; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | //! class to store the md2 data in |
|---|
| 117 | class MD2Data : public BaseObject |
|---|
| 118 | { |
|---|
| 119 | public: |
|---|
| 120 | typedef CountPointer<MD2Data> Pointer; |
|---|
| 121 | public: |
|---|
| 122 | MD2Data(); |
|---|
| 123 | MD2Data(const std::string& modelFileName, const std::string& skinFileName, float scale = 1.0f); |
|---|
| 124 | virtual ~MD2Data(); |
|---|
| 125 | |
|---|
| 126 | private: |
|---|
| 127 | void init(); |
|---|
| 128 | bool loadModel(const std::string& fileName); |
|---|
| 129 | bool loadSkin(const std::string& fileName = ""); |
|---|
| 130 | |
|---|
| 131 | public: |
|---|
| 132 | int numFrames; //!< number of frames |
|---|
| 133 | int numVertices; //!< number of vertices |
|---|
| 134 | int numTriangles; //!< number of triangles |
|---|
| 135 | int numTexCoor; //!< number of texture coordinates |
|---|
| 136 | int numGLCommands; //!< number of gl commands in the glList (tells how to draw) |
|---|
| 137 | std::string fileName; //!< file name of the model File |
|---|
| 138 | std::string skinFileName; //!< file name of the skin file |
|---|
| 139 | MD2Header* header; //!< the header file |
|---|
| 140 | |
|---|
| 141 | sVec3D* pVertices; //!< pointer to the vertices data block |
|---|
| 142 | sTriangle* pTriangles; //!< pointer to the triangles data |
|---|
| 143 | sTexCoor* pTexCoor; //!< pointer to the texture coordinate data |
|---|
| 144 | int* pGLCommands; //!< pointer to the gllist data block |
|---|
| 145 | int* pLightNormals; //!< pointer to the light normals |
|---|
| 146 | |
|---|
| 147 | Material material; //!< pointer to the material |
|---|
| 148 | float scaleFactor; //!< the scale factor of the model, (global) |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | //! This is a MD2 Model class |
|---|
| 155 | class MD2Model : public InteractiveModel |
|---|
| 156 | { |
|---|
| 157 | ObjectListDeclaration(MD2Model); |
|---|
| 158 | public: |
|---|
| 159 | MD2Model(); |
|---|
| 160 | MD2Model(const std::string& modelFileName, const std::string& skinFileName = "", float scale = 1.0f); |
|---|
| 161 | MD2Model(const MD2Model& model); |
|---|
| 162 | virtual ~MD2Model(); |
|---|
| 163 | |
|---|
| 164 | MD2Model& operator=(const MD2Model& md2model); |
|---|
| 165 | |
|---|
| 166 | bool load(const std::string& modelFileName, const std::string& skinFileName = "", float scale = 1.0f); |
|---|
| 167 | |
|---|
| 168 | virtual void draw() const; |
|---|
| 169 | void renderFrameTriangles() const; |
|---|
| 170 | |
|---|
| 171 | virtual void setAnimation(int type, int animPlayback = MD2_ANIM_LOOP); |
|---|
| 172 | virtual void setAnimation(int firstFrame, int lastFrame, int fps, int bStoppable, int animPlayback); |
|---|
| 173 | /** returns the current animation @returns animation type */ |
|---|
| 174 | inline int getAnimation() const { return this->animationState.type; } |
|---|
| 175 | virtual void setAnimationSpeed(float speed) { this->animationSpeed = speed; } |
|---|
| 176 | virtual bool isAnimationFinished() const { return (this->animationState.currentFrame == this->animationState.endFrame )?true:false; } |
|---|
| 177 | /** scales the current model @param scaleFactor: the factor [0..1] to use for scaling */ |
|---|
| 178 | void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;} |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | void acquireData(const MD2Data::Pointer& data) { this->data = data; rebuildInfo(); }; |
|---|
| 182 | const MD2Data::Pointer& dataPointer() const { return this->data; }; |
|---|
| 183 | virtual void tick(float dtS); |
|---|
| 184 | void debug(); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | private: |
|---|
| 188 | void init(); |
|---|
| 189 | void rebuildInfo(); |
|---|
| 190 | |
|---|
| 191 | void animate(float time); |
|---|
| 192 | void processLighting(); |
|---|
| 193 | void interpolate(/*sVec3D* verticesList*/); |
|---|
| 194 | void renderFrame() const ; |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | public: |
|---|
| 198 | /* these variables are static, because they are all the same for every model */ |
|---|
| 199 | static sVec3D anorms[NUM_VERTEX_NORMALS]; //!< the anormals |
|---|
| 200 | static float anormsDots[SHADEDOT_QUANT][256]; //!< the anormals dot products |
|---|
| 201 | static sAnim animationList[22]; //!< the anomation list |
|---|
| 202 | //! again one of these strange id software parts |
|---|
| 203 | float* shadeDots; |
|---|
| 204 | |
|---|
| 205 | CountPointer<MD2Data> data; //!< the md2 data pointer |
|---|
| 206 | |
|---|
| 207 | private: |
|---|
| 208 | float scaleFactor; //!< the scale factor (individual) |
|---|
| 209 | float animationSpeed; //!< the speed of the animation (factor for the time) |
|---|
| 210 | sAnimState animationState; //!< animation state of the model |
|---|
| 211 | sVec3D verticesList[MD2_MAX_VERTICES]; //!< place to temp sav the vert |
|---|
| 212 | }; |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | #endif /* _MD2MODEL_H */ |
|---|