[4615] | 1 | /*! |
---|
[5039] | 2 | * @file md2Model.h |
---|
[4836] | 3 | * Definition of an MD2 Model, a model format invented by ID Software. |
---|
[4615] | 4 | |
---|
[4245] | 5 | We are deeply thankfull for all the wunderfull things id software made for us gamers! |
---|
[4615] | 6 | |
---|
[4245] | 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). |
---|
[4615] | 10 | |
---|
| 11 | A typical model has about 200 frames, the maximum frame count is fixed by MD2_MAX_FRAMES to currently |
---|
[4245] | 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... |
---|
[4615] | 14 | |
---|
[4245] | 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" |
---|
[9869] | 22 | #include "count_pointer.h" |
---|
[4245] | 23 | |
---|
[8439] | 24 | #include "interactive_model.h" |
---|
[7123] | 25 | #include "material.h" |
---|
[4245] | 26 | |
---|
[8344] | 27 | #include "md_model_structure.h" |
---|
| 28 | |
---|
[4245] | 29 | //! These are the needed defines for the max values when loading .MD2 files |
---|
[4459] | 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 |
---|
[4615] | 32 | #define MD2_MAX_TRIANGLES 4096 //!< maximal triangles count |
---|
[9235] | 33 | #define MD2_MAX_VERTICES 3048 //!< maximal vertices count |
---|
| 34 | #define MD2_MAX_TEXCOORDS 3048 //!< maximal tex coordinates |
---|
[4615] | 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 |
---|
[4245] | 38 | |
---|
[4461] | 39 | #define NUM_VERTEX_NORMALS 162 //!< number of vertex normals |
---|
| 40 | #define SHADEDOT_QUANT 16 //!< shade dot quantity - no idea what it is |
---|
[4245] | 41 | |
---|
| 42 | //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework |
---|
[4615] | 43 | #define kAnimationSpeed 12.0f //!< animation speed |
---|
[4245] | 44 | |
---|
| 45 | //! This holds the header information that is read in at the beginning of the file: id software definition |
---|
[4279] | 46 | struct MD2Header |
---|
[4615] | 47 | { |
---|
[9869] | 48 | int ident; //!< This is used to identify the file |
---|
| 49 | int version; //!< The version number of the file (Must be 8) |
---|
[4615] | 50 | |
---|
[9869] | 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 |
---|
[4615] | 54 | |
---|
[9869] | 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 |
---|
[4615] | 61 | |
---|
[9869] | 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 |
---|
[4245] | 68 | }; |
---|
| 69 | |
---|
| 70 | |
---|
[4459] | 71 | //! animation names enumeration |
---|
[8894] | 72 | typedef enum MD2animType |
---|
[9869] | 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 |
---|
[9885] | 85 | MD2_POINT, //11 |
---|
[9869] | 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, |
---|
[4615] | 96 | |
---|
[9869] | 97 | MAX_ANIMATIONS |
---|
| 98 | }; |
---|
[4245] | 99 | |
---|
| 100 | |
---|
[7071] | 101 | typedef enum animPlayback |
---|
| 102 | { |
---|
| 103 | MD2_ANIM_LOOP = 0, |
---|
| 104 | MD2_ANIM_ONCE, |
---|
[4245] | 105 | |
---|
[7071] | 106 | MD2_ANIM_NUM |
---|
| 107 | }; |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
[4245] | 111 | /* forward definitions */ |
---|
| 112 | class Material; |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
[4277] | 116 | //! class to store the md2 data in |
---|
[5304] | 117 | class MD2Data : public BaseObject |
---|
[4277] | 118 | { |
---|
[9869] | 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); |
---|
[4277] | 124 | virtual ~MD2Data(); |
---|
[4245] | 125 | |
---|
[9869] | 126 | private: |
---|
| 127 | void init(); |
---|
| 128 | bool loadModel(const std::string& fileName); |
---|
| 129 | bool loadSkin(const std::string& fileName = ""); |
---|
[4277] | 130 | |
---|
[9869] | 131 | public: |
---|
[4615] | 132 | int numFrames; //!< number of frames |
---|
[4461] | 133 | int numVertices; //!< number of vertices |
---|
| 134 | int numTriangles; //!< number of triangles |
---|
[5085] | 135 | int numTexCoor; //!< number of texture coordinates |
---|
[4461] | 136 | int numGLCommands; //!< number of gl commands in the glList (tells how to draw) |
---|
[7221] | 137 | std::string fileName; //!< file name of the model File |
---|
| 138 | std::string skinFileName; //!< file name of the skin file |
---|
[4461] | 139 | MD2Header* header; //!< the header file |
---|
[4277] | 140 | |
---|
[4461] | 141 | sVec3D* pVertices; //!< pointer to the vertices data block |
---|
[4787] | 142 | sTriangle* pTriangles; //!< pointer to the triangles data |
---|
[5085] | 143 | sTexCoor* pTexCoor; //!< pointer to the texture coordinate data |
---|
[4461] | 144 | int* pGLCommands; //!< pointer to the gllist data block |
---|
| 145 | int* pLightNormals; //!< pointer to the light normals |
---|
[4277] | 146 | |
---|
[7123] | 147 | Material material; //!< pointer to the material |
---|
[4461] | 148 | float scaleFactor; //!< the scale factor of the model, (global) |
---|
[4277] | 149 | }; |
---|
| 150 | |
---|
| 151 | |
---|
[5085] | 152 | |
---|
| 153 | |
---|
[4245] | 154 | //! This is a MD2 Model class |
---|
[9869] | 155 | class MD2Model : public InteractiveModel |
---|
| 156 | { |
---|
| 157 | ObjectListDeclaration(MD2Model); |
---|
[4245] | 158 | public: |
---|
[9869] | 159 | MD2Model(); |
---|
[7221] | 160 | MD2Model(const std::string& modelFileName, const std::string& skinFileName = "", float scale = 1.0f); |
---|
[9869] | 161 | MD2Model(const MD2Model& model); |
---|
[4276] | 162 | virtual ~MD2Model(); |
---|
[4615] | 163 | |
---|
[9869] | 164 | MD2Model& operator=(const MD2Model& md2model); |
---|
| 165 | |
---|
| 166 | bool load(const std::string& modelFileName, const std::string& skinFileName = "", float scale = 1.0f); |
---|
| 167 | |
---|
[6222] | 168 | virtual void draw() const; |
---|
| 169 | void renderFrameTriangles() const; |
---|
[4615] | 170 | |
---|
[8439] | 171 | virtual void setAnimation(int type, int animPlayback = MD2_ANIM_LOOP); |
---|
[8894] | 172 | virtual void setAnimation(int firstFrame, int lastFrame, int fps, int bStoppable, int animPlayback); |
---|
[6222] | 173 | /** returns the current animation @returns animation type */ |
---|
[9869] | 174 | inline int getAnimation() const { return this->animationState.type; } |
---|
[8894] | 175 | virtual void setAnimationSpeed(float speed) { this->animationSpeed = speed; } |
---|
[9869] | 176 | virtual bool isAnimationFinished() const { return (this->animationState.currentFrame == this->animationState.endFrame )?true:false; } |
---|
[6222] | 177 | /** scales the current model @param scaleFactor: the factor [0..1] to use for scaling */ |
---|
[4245] | 178 | void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;} |
---|
| 179 | |
---|
[9869] | 180 | |
---|
| 181 | void acquireData(const MD2Data::Pointer& data) { this->data = data; rebuildInfo(); }; |
---|
| 182 | const MD2Data::Pointer& dataPointer() const { return this->data; }; |
---|
[6222] | 183 | virtual void tick(float dtS); |
---|
[4245] | 184 | void debug(); |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | private: |
---|
[9869] | 188 | void init(); |
---|
| 189 | void rebuildInfo(); |
---|
| 190 | |
---|
[6222] | 191 | void animate(float time); |
---|
[4245] | 192 | void processLighting(); |
---|
[6222] | 193 | void interpolate(/*sVec3D* verticesList*/); |
---|
| 194 | void renderFrame() const ; |
---|
[4245] | 195 | |
---|
[5087] | 196 | |
---|
[9869] | 197 | public: |
---|
[4245] | 198 | /* these variables are static, because they are all the same for every model */ |
---|
[9869] | 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; |
---|
[4245] | 204 | |
---|
[9869] | 205 | CountPointer<MD2Data> data; //!< the md2 data pointer |
---|
[4245] | 206 | |
---|
[9869] | 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 |
---|
[4245] | 212 | }; |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | #endif /* _MD2MODEL_H */ |
---|