[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" |
---|
| 22 | |
---|
[8439] | 23 | #include "interactive_model.h" |
---|
[7123] | 24 | #include "material.h" |
---|
[4245] | 25 | |
---|
[8344] | 26 | #include "md_model_structure.h" |
---|
| 27 | |
---|
[4245] | 28 | //! These are the needed defines for the max values when loading .MD2 files |
---|
[4459] | 29 | #define MD2_IDENT (('2'<<24) + ('P'<<16) + ('D'<<8) + 'I') //!< the md2 identifier tag in the bin file |
---|
| 30 | #define MD2_VERSION 8 //!< the md2 version in the header |
---|
[4615] | 31 | #define MD2_MAX_TRIANGLES 4096 //!< maximal triangles count |
---|
| 32 | #define MD2_MAX_VERTICES 2048 //!< maximal vertices count |
---|
| 33 | #define MD2_MAX_TEXCOORDS 2048 //!< maximal tex coordinates |
---|
| 34 | #define MD2_MAX_FRAMES 512 //!< maximal frames |
---|
| 35 | #define MD2_MAX_SKINS 32 //!< maximal skins |
---|
| 36 | #define MD2_MAX_FRAMESIZE (MD2_MAX_VERTICES * 4 + 128) //!< maximal framesize |
---|
[4245] | 37 | |
---|
[4461] | 38 | #define NUM_VERTEX_NORMALS 162 //!< number of vertex normals |
---|
| 39 | #define SHADEDOT_QUANT 16 //!< shade dot quantity - no idea what it is |
---|
[4245] | 40 | |
---|
| 41 | //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework |
---|
[4615] | 42 | #define kAnimationSpeed 12.0f //!< animation speed |
---|
[4245] | 43 | |
---|
| 44 | //! This holds the header information that is read in at the beginning of the file: id software definition |
---|
[4279] | 45 | struct MD2Header |
---|
[4615] | 46 | { |
---|
| 47 | int ident; //!< This is used to identify the file |
---|
| 48 | int version; //!< The version number of the file (Must be 8) |
---|
| 49 | |
---|
| 50 | int skinWidth; //!< The skin width in pixels |
---|
| 51 | int skinHeight; //!< The skin height in pixels |
---|
| 52 | int frameSize; //!< The size in bytes the frames are |
---|
| 53 | |
---|
| 54 | int numSkins; //!< The number of skins associated with the model |
---|
| 55 | int numVertices; //!< The number of vertices (constant for each frame) |
---|
| 56 | int numTexCoords; //!< The number of texture coordinates |
---|
| 57 | int numTriangles; //!< The number of faces (polygons) |
---|
| 58 | int numGlCommands; //!< The number of gl commands |
---|
| 59 | int numFrames; //!< The number of animation frames |
---|
| 60 | |
---|
| 61 | int offsetSkins; //!< The offset in the file for the skin data |
---|
| 62 | int offsetTexCoords; //!< The offset in the file for the texture data |
---|
| 63 | int offsetTriangles; //!< The offset in the file for the face data |
---|
| 64 | int offsetFrames; //!< The offset in the file for the frames data |
---|
| 65 | int offsetGlCommands; //!< The offset in the file for the gl commands data |
---|
| 66 | int offsetEnd; //!< The end of the file offset |
---|
[4245] | 67 | }; |
---|
| 68 | |
---|
| 69 | |
---|
[4459] | 70 | //! animation names enumeration |
---|
[8894] | 71 | typedef enum MD2animType |
---|
[4245] | 72 | { |
---|
[7114] | 73 | STAND, //0 |
---|
| 74 | RUN, //1 |
---|
| 75 | ATTACK, //2 |
---|
| 76 | PAIN_A, //3 |
---|
| 77 | PAIN_B, //4 |
---|
| 78 | PAIN_C, //5 |
---|
| 79 | JUMP, //6 |
---|
| 80 | FLIP, //7 |
---|
| 81 | SALUTE, //8 |
---|
| 82 | FALLBACK, //9 |
---|
| 83 | WAVE, //10 |
---|
| 84 | POINT, //11 |
---|
[4245] | 85 | CROUCH_STAND, |
---|
| 86 | CROUCH_WALK, |
---|
| 87 | CROUCH_ATTACK, |
---|
| 88 | CROUCH_PAIN, |
---|
[4615] | 89 | CROUCH_DEATH, |
---|
[4245] | 90 | DEATH_FALLBACK, |
---|
| 91 | DEATH_FALLFORWARD, |
---|
| 92 | DEATH_FALLBACKSLOW, |
---|
| 93 | BOOM, |
---|
[4615] | 94 | |
---|
[4245] | 95 | MAX_ANIMATIONS |
---|
[4488] | 96 | }; |
---|
[4245] | 97 | |
---|
| 98 | |
---|
[7071] | 99 | typedef enum animPlayback |
---|
| 100 | { |
---|
| 101 | MD2_ANIM_LOOP = 0, |
---|
| 102 | MD2_ANIM_ONCE, |
---|
[4245] | 103 | |
---|
[7071] | 104 | MD2_ANIM_NUM |
---|
| 105 | }; |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
[4245] | 109 | /* forward definitions */ |
---|
| 110 | class Material; |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
[4277] | 114 | //! class to store the md2 data in |
---|
[5304] | 115 | class MD2Data : public BaseObject |
---|
[4277] | 116 | { |
---|
| 117 | public: |
---|
[7221] | 118 | MD2Data(const std::string& modelFileName, const std::string& skinFileName, float scale = 1.0f); |
---|
[4277] | 119 | virtual ~MD2Data(); |
---|
[4245] | 120 | |
---|
[4459] | 121 | private: |
---|
[7221] | 122 | bool loadModel(const std::string& fileName); |
---|
| 123 | bool loadSkin(const std::string& fileName = ""); |
---|
[4277] | 124 | |
---|
[4459] | 125 | public: |
---|
[4615] | 126 | int numFrames; //!< number of frames |
---|
[4461] | 127 | int numVertices; //!< number of vertices |
---|
| 128 | int numTriangles; //!< number of triangles |
---|
[5085] | 129 | int numTexCoor; //!< number of texture coordinates |
---|
[4461] | 130 | int numGLCommands; //!< number of gl commands in the glList (tells how to draw) |
---|
[7221] | 131 | std::string fileName; //!< file name of the model File |
---|
| 132 | std::string skinFileName; //!< file name of the skin file |
---|
[4461] | 133 | MD2Header* header; //!< the header file |
---|
[4277] | 134 | |
---|
[4461] | 135 | sVec3D* pVertices; //!< pointer to the vertices data block |
---|
[4787] | 136 | sTriangle* pTriangles; //!< pointer to the triangles data |
---|
[5085] | 137 | sTexCoor* pTexCoor; //!< pointer to the texture coordinate data |
---|
[4461] | 138 | int* pGLCommands; //!< pointer to the gllist data block |
---|
| 139 | int* pLightNormals; //!< pointer to the light normals |
---|
[4277] | 140 | |
---|
[7123] | 141 | Material material; //!< pointer to the material |
---|
[4461] | 142 | float scaleFactor; //!< the scale factor of the model, (global) |
---|
[4277] | 143 | }; |
---|
| 144 | |
---|
| 145 | |
---|
[5085] | 146 | |
---|
| 147 | |
---|
[4245] | 148 | //! This is a MD2 Model class |
---|
[8439] | 149 | class MD2Model : public InteractiveModel { |
---|
[4245] | 150 | |
---|
| 151 | public: |
---|
[7221] | 152 | MD2Model(const std::string& modelFileName, const std::string& skinFileName = "", float scale = 1.0f); |
---|
[4276] | 153 | virtual ~MD2Model(); |
---|
[4615] | 154 | |
---|
[6222] | 155 | virtual void draw() const; |
---|
| 156 | void renderFrameTriangles() const; |
---|
[4615] | 157 | |
---|
[8439] | 158 | virtual void setAnimation(int type, int animPlayback = MD2_ANIM_LOOP); |
---|
[8894] | 159 | virtual void setAnimation(int firstFrame, int lastFrame, int fps, int bStoppable, int animPlayback); |
---|
[6222] | 160 | /** returns the current animation @returns animation type */ |
---|
[8724] | 161 | inline int MD2Model::getAnimation() { return this->animationState.type; } |
---|
[8894] | 162 | virtual void setAnimationSpeed(float speed) { this->animationSpeed = speed; } |
---|
[6222] | 163 | /** scales the current model @param scaleFactor: the factor [0..1] to use for scaling */ |
---|
[4245] | 164 | void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;} |
---|
| 165 | |
---|
[6222] | 166 | virtual void tick(float dtS); |
---|
[4245] | 167 | void debug(); |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | private: |
---|
[6222] | 171 | void animate(float time); |
---|
[4245] | 172 | void processLighting(); |
---|
[6222] | 173 | void interpolate(/*sVec3D* verticesList*/); |
---|
| 174 | void renderFrame() const ; |
---|
[4245] | 175 | |
---|
[5087] | 176 | |
---|
[4245] | 177 | public: |
---|
| 178 | /* these variables are static, because they are all the same for every model */ |
---|
[4461] | 179 | static sVec3D anorms[NUM_VERTEX_NORMALS]; //!< the anormals |
---|
| 180 | static float anormsDots[SHADEDOT_QUANT][256]; //!< the anormals dot products |
---|
| 181 | static sAnim animationList[21]; //!< the anomation list |
---|
[6222] | 182 | //! again one of these strange id software parts |
---|
| 183 | float* shadeDots; |
---|
[4245] | 184 | |
---|
[4461] | 185 | MD2Data* data; //!< the md2 data pointer |
---|
[4245] | 186 | |
---|
[4615] | 187 | private: |
---|
[4461] | 188 | float scaleFactor; //!< the scale factor (individual) |
---|
[8894] | 189 | float animationSpeed; //!< the speed of the animation (factor for the time) |
---|
[4461] | 190 | sAnimState animationState; //!< animation state of the model |
---|
[6222] | 191 | sVec3D verticesList[MD2_MAX_VERTICES]; //!< place to temp sav the vert |
---|
[4245] | 192 | }; |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | #endif /* _MD2MODEL_H */ |
---|