| 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 "model.h" |
|---|
| 22 | #include "base_object.h" |
|---|
| 23 | #include "stdincl.h" |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | //! These are the needed defines for the max values when loading .MD2 files |
|---|
| 27 | #define MD2_IDENT (('2'<<24) + ('P'<<16) + ('D'<<8) + 'I') //!< the md2 identifier tag in the bin file |
|---|
| 28 | #define MD2_VERSION 8 //!< the md2 version in the header |
|---|
| 29 | #define MD2_MAX_TRIANGLES 4096 //!< maximal triangles count |
|---|
| 30 | #define MD2_MAX_VERTICES 2048 //!< maximal vertices count |
|---|
| 31 | #define MD2_MAX_TEXCOORDS 2048 //!< maximal tex coordinates |
|---|
| 32 | #define MD2_MAX_FRAMES 512 //!< maximal frames |
|---|
| 33 | #define MD2_MAX_SKINS 32 //!< maximal skins |
|---|
| 34 | #define MD2_MAX_FRAMESIZE (MD2_MAX_VERTICES * 4 + 128) //!< maximal framesize |
|---|
| 35 | |
|---|
| 36 | #define NUM_VERTEX_NORMALS 162 //!< number of vertex normals |
|---|
| 37 | #define SHADEDOT_QUANT 16 //!< shade dot quantity - no idea what it is |
|---|
| 38 | |
|---|
| 39 | //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework |
|---|
| 40 | #define kAnimationSpeed 12.0f //!< animation speed |
|---|
| 41 | |
|---|
| 42 | //! This holds the header information that is read in at the beginning of the file: id software definition |
|---|
| 43 | struct MD2Header |
|---|
| 44 | { |
|---|
| 45 | int ident; //!< This is used to identify the file |
|---|
| 46 | int version; //!< The version number of the file (Must be 8) |
|---|
| 47 | |
|---|
| 48 | int skinWidth; //!< The skin width in pixels |
|---|
| 49 | int skinHeight; //!< The skin height in pixels |
|---|
| 50 | int frameSize; //!< The size in bytes the frames are |
|---|
| 51 | |
|---|
| 52 | int numSkins; //!< The number of skins associated with the model |
|---|
| 53 | int numVertices; //!< The number of vertices (constant for each frame) |
|---|
| 54 | int numTexCoords; //!< The number of texture coordinates |
|---|
| 55 | int numTriangles; //!< The number of faces (polygons) |
|---|
| 56 | int numGlCommands; //!< The number of gl commands |
|---|
| 57 | int numFrames; //!< The number of animation frames |
|---|
| 58 | |
|---|
| 59 | int offsetSkins; //!< The offset in the file for the skin data |
|---|
| 60 | int offsetTexCoords; //!< The offset in the file for the texture data |
|---|
| 61 | int offsetTriangles; //!< The offset in the file for the face data |
|---|
| 62 | int offsetFrames; //!< The offset in the file for the frames data |
|---|
| 63 | int offsetGlCommands; //!< The offset in the file for the gl commands data |
|---|
| 64 | int offsetEnd; //!< The end of the file offset |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | //! compressed vertex data: char insetead of float, the value will be expanded by the scale value. only for loading |
|---|
| 69 | typedef struct |
|---|
| 70 | { |
|---|
| 71 | unsigned char v[3]; //!< the vector of the vertex |
|---|
| 72 | unsigned char lightNormalIndex; //!< the index of the light normal |
|---|
| 73 | } sVertex; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | //! compressed texture offset data: coords scaled by the texture size. Only for loading |
|---|
| 77 | typedef struct |
|---|
| 78 | { |
|---|
| 79 | short s; //!< the s,t coordinates of a texture |
|---|
| 80 | short t; //!< the s,t coordinates of a texture |
|---|
| 81 | } sTexCoor; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | //! holds tha informations about a md2 frame |
|---|
| 85 | typedef struct |
|---|
| 86 | { |
|---|
| 87 | sVec3D scale; //!< scales values of the model |
|---|
| 88 | sVec3D translate; //!< translates the model |
|---|
| 89 | char name[16]; //!< frame name: something like "run32" |
|---|
| 90 | sVertex pVertices[1]; //!< first vertex of thes frame |
|---|
| 91 | } sFrame; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | //! holds the information about a triangle |
|---|
| 95 | typedef struct |
|---|
| 96 | { |
|---|
| 97 | unsigned short indexToVertices[3]; //!< index to the verteces of the triangle |
|---|
| 98 | unsigned short indexToTexCoor[3]; //!< index to the texture coordinates |
|---|
| 99 | } sTriangle; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | //! the command list of the md2 model, very md2 specific |
|---|
| 104 | typedef struct |
|---|
| 105 | { |
|---|
| 106 | float s; //!< texture coordinate 1 |
|---|
| 107 | float t; //!< texture coordinate 2 |
|---|
| 108 | int vertexIndex; //!< index of the vertex in the vertex list |
|---|
| 109 | } glCommandVertex; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | //! a md2 animation definition |
|---|
| 113 | typedef struct |
|---|
| 114 | { |
|---|
| 115 | int firstFrame; //!< first frame of the animation |
|---|
| 116 | int lastFrame; //!< last frame of the animation |
|---|
| 117 | int fps; //!< speed: number of frames per second |
|---|
| 118 | int bStoppable; //!< 1 if the animation is stoppable 0 else |
|---|
| 119 | } sAnim; |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | //! animation state definition |
|---|
| 123 | typedef struct |
|---|
| 124 | { |
|---|
| 125 | int startFrame; //!< the start frame of an animation |
|---|
| 126 | int endFrame; //!< last frame of the animation |
|---|
| 127 | int fps; //!< fps of the animaion (speed) |
|---|
| 128 | |
|---|
| 129 | float localTime; //!< the local time |
|---|
| 130 | float lastTime; //!< last time stamp |
|---|
| 131 | float interpolationState; //!< the state of the animation [0..1] |
|---|
| 132 | |
|---|
| 133 | int type; //!< animation type |
|---|
| 134 | |
|---|
| 135 | int currentFrame; //!< the current frame |
|---|
| 136 | int nextFrame; //!< the next frame in the list |
|---|
| 137 | int numPlays; //!< the number of times, this has been played in series |
|---|
| 138 | } sAnimState; |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | //! animation names enumeration |
|---|
| 142 | typedef enum animType |
|---|
| 143 | { |
|---|
| 144 | STAND, |
|---|
| 145 | RUN, |
|---|
| 146 | ATTACK, |
|---|
| 147 | PAIN_A, |
|---|
| 148 | PAIN_B, |
|---|
| 149 | PAIN_C, |
|---|
| 150 | JUMP, |
|---|
| 151 | FLIP, |
|---|
| 152 | SALUTE, |
|---|
| 153 | FALLBACK, |
|---|
| 154 | WAVE, |
|---|
| 155 | POINTT, |
|---|
| 156 | CROUCH_STAND, |
|---|
| 157 | CROUCH_WALK, |
|---|
| 158 | CROUCH_ATTACK, |
|---|
| 159 | CROUCH_PAIN, |
|---|
| 160 | CROUCH_DEATH, |
|---|
| 161 | DEATH_FALLBACK, |
|---|
| 162 | DEATH_FALLFORWARD, |
|---|
| 163 | DEATH_FALLBACKSLOW, |
|---|
| 164 | BOOM, |
|---|
| 165 | |
|---|
| 166 | MAX_ANIMATIONS |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /* forward definitions */ |
|---|
| 172 | class Material; |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | //! class to store the md2 data in |
|---|
| 177 | class MD2Data : public BaseObject |
|---|
| 178 | { |
|---|
| 179 | public: |
|---|
| 180 | MD2Data(const char* modelFileName, const char* skinFileName); |
|---|
| 181 | virtual ~MD2Data(); |
|---|
| 182 | |
|---|
| 183 | private: |
|---|
| 184 | bool loadModel(const char* fileName); |
|---|
| 185 | bool loadSkin(const char* fileName = NULL); |
|---|
| 186 | |
|---|
| 187 | public: |
|---|
| 188 | int numFrames; //!< number of frames |
|---|
| 189 | int numVertices; //!< number of vertices |
|---|
| 190 | int numTriangles; //!< number of triangles |
|---|
| 191 | int numTexCoor; //!< number of texture coordinates |
|---|
| 192 | int numGLCommands; //!< number of gl commands in the glList (tells how to draw) |
|---|
| 193 | char* fileName; //!< file name of the model File |
|---|
| 194 | char* skinFileName; //!< file name of the skin file |
|---|
| 195 | MD2Header* header; //!< the header file |
|---|
| 196 | |
|---|
| 197 | sVec3D* pVertices; //!< pointer to the vertices data block |
|---|
| 198 | sTriangle* pTriangles; //!< pointer to the triangles data |
|---|
| 199 | sTexCoor* pTexCoor; //!< pointer to the texture coordinate data |
|---|
| 200 | int* pGLCommands; //!< pointer to the gllist data block |
|---|
| 201 | int* pLightNormals; //!< pointer to the light normals |
|---|
| 202 | |
|---|
| 203 | Material* material; //!< pointer to the material |
|---|
| 204 | float scaleFactor; //!< the scale factor of the model, (global) |
|---|
| 205 | }; |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | //! This is a MD2 Model class |
|---|
| 211 | class MD2Model : public Model { |
|---|
| 212 | |
|---|
| 213 | public: |
|---|
| 214 | MD2Model(const char* modelFileName, const char* skinFileName = NULL); |
|---|
| 215 | virtual ~MD2Model(); |
|---|
| 216 | |
|---|
| 217 | virtual void draw() const; |
|---|
| 218 | void renderFrameTriangles() const; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | void setAnim(int type); |
|---|
| 222 | /** returns the current animation @returns animation type */ |
|---|
| 223 | inline int MD2Model::getAnim() { return this->animationState.type; } |
|---|
| 224 | /** scales the current model @param scaleFactor: the factor [0..1] to use for scaling */ |
|---|
| 225 | void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;} |
|---|
| 226 | |
|---|
| 227 | virtual void tick(float dtS); |
|---|
| 228 | void debug(); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | private: |
|---|
| 232 | void animate(float time); |
|---|
| 233 | void processLighting(); |
|---|
| 234 | void interpolate(/*sVec3D* verticesList*/); |
|---|
| 235 | void renderFrame() const ; |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | public: |
|---|
| 239 | /* these variables are static, because they are all the same for every model */ |
|---|
| 240 | static sVec3D anorms[NUM_VERTEX_NORMALS]; //!< the anormals |
|---|
| 241 | static float anormsDots[SHADEDOT_QUANT][256]; //!< the anormals dot products |
|---|
| 242 | static sAnim animationList[21]; //!< the anomation list |
|---|
| 243 | //! again one of these strange id software parts |
|---|
| 244 | float* shadeDots; |
|---|
| 245 | |
|---|
| 246 | MD2Data* data; //!< the md2 data pointer |
|---|
| 247 | |
|---|
| 248 | private: |
|---|
| 249 | float scaleFactor; //!< the scale factor (individual) |
|---|
| 250 | sAnimState animationState; //!< animation state of the model |
|---|
| 251 | sVec3D verticesList[MD2_MAX_VERTICES]; //!< place to temp sav the vert |
|---|
| 252 | }; |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | #endif /* _MD2MODEL_H */ |
|---|