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 | |
---|
23 | #include "interactive_model.h" |
---|
24 | #include "material.h" |
---|
25 | |
---|
26 | #include "md_model_structure.h" |
---|
27 | |
---|
28 | //! These are the needed defines for the max values when loading .MD2 files |
---|
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 |
---|
31 | #define MD2_MAX_TRIANGLES 4096 //!< maximal triangles count |
---|
32 | #define MD2_MAX_VERTICES 3048 //!< maximal vertices count |
---|
33 | #define MD2_MAX_TEXCOORDS 3048 //!< 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 |
---|
37 | |
---|
38 | #define NUM_VERTEX_NORMALS 162 //!< number of vertex normals |
---|
39 | #define SHADEDOT_QUANT 16 //!< shade dot quantity - no idea what it is |
---|
40 | |
---|
41 | //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework |
---|
42 | #define kAnimationSpeed 12.0f //!< animation speed |
---|
43 | |
---|
44 | //! This holds the header information that is read in at the beginning of the file: id software definition |
---|
45 | struct MD2Header |
---|
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 |
---|
67 | }; |
---|
68 | |
---|
69 | |
---|
70 | //! animation names enumeration |
---|
71 | typedef enum MD2animType |
---|
72 | { |
---|
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 |
---|
85 | CROUCH_STAND, |
---|
86 | CROUCH_WALK, |
---|
87 | CROUCH_ATTACK, |
---|
88 | CROUCH_PAIN, |
---|
89 | CROUCH_DEATH, |
---|
90 | DEATH_FALLBACK, |
---|
91 | DEATH_FALLFORWARD, |
---|
92 | DEATH_FALLBACKSLOW, |
---|
93 | BOOM, |
---|
94 | WALK, |
---|
95 | |
---|
96 | MAX_ANIMATIONS |
---|
97 | }; |
---|
98 | |
---|
99 | |
---|
100 | typedef enum animPlayback |
---|
101 | { |
---|
102 | MD2_ANIM_LOOP = 0, |
---|
103 | MD2_ANIM_ONCE, |
---|
104 | |
---|
105 | MD2_ANIM_NUM |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | /* forward definitions */ |
---|
111 | class Material; |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | //! class to store the md2 data in |
---|
116 | class MD2Data : public BaseObject |
---|
117 | { |
---|
118 | public: |
---|
119 | MD2Data(const std::string& modelFileName, const std::string& skinFileName, float scale = 1.0f); |
---|
120 | virtual ~MD2Data(); |
---|
121 | |
---|
122 | private: |
---|
123 | bool loadModel(const std::string& fileName); |
---|
124 | bool loadSkin(const std::string& fileName = ""); |
---|
125 | |
---|
126 | public: |
---|
127 | int numFrames; //!< number of frames |
---|
128 | int numVertices; //!< number of vertices |
---|
129 | int numTriangles; //!< number of triangles |
---|
130 | int numTexCoor; //!< number of texture coordinates |
---|
131 | int numGLCommands; //!< number of gl commands in the glList (tells how to draw) |
---|
132 | std::string fileName; //!< file name of the model File |
---|
133 | std::string skinFileName; //!< file name of the skin file |
---|
134 | MD2Header* header; //!< the header file |
---|
135 | |
---|
136 | sVec3D* pVertices; //!< pointer to the vertices data block |
---|
137 | sTriangle* pTriangles; //!< pointer to the triangles data |
---|
138 | sTexCoor* pTexCoor; //!< pointer to the texture coordinate data |
---|
139 | int* pGLCommands; //!< pointer to the gllist data block |
---|
140 | int* pLightNormals; //!< pointer to the light normals |
---|
141 | |
---|
142 | Material material; //!< pointer to the material |
---|
143 | float scaleFactor; //!< the scale factor of the model, (global) |
---|
144 | }; |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | //! This is a MD2 Model class |
---|
150 | class MD2Model : public InteractiveModel { |
---|
151 | |
---|
152 | public: |
---|
153 | MD2Model(const std::string& modelFileName, const std::string& skinFileName = "", float scale = 1.0f); |
---|
154 | virtual ~MD2Model(); |
---|
155 | |
---|
156 | virtual void draw() const; |
---|
157 | void renderFrameTriangles() const; |
---|
158 | |
---|
159 | virtual void setAnimation(int type, int animPlayback = MD2_ANIM_LOOP); |
---|
160 | virtual void setAnimation(int firstFrame, int lastFrame, int fps, int bStoppable, int animPlayback); |
---|
161 | /** returns the current animation @returns animation type */ |
---|
162 | inline int MD2Model::getAnimation() { return this->animationState.type; } |
---|
163 | virtual void setAnimationSpeed(float speed) { this->animationSpeed = speed; } |
---|
164 | virtual bool isAnimationFinished() { return (this->animationState.currentFrame == this->animationState.endFrame )?true:false; } |
---|
165 | /** scales the current model @param scaleFactor: the factor [0..1] to use for scaling */ |
---|
166 | void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;} |
---|
167 | |
---|
168 | virtual void tick(float dtS); |
---|
169 | void debug(); |
---|
170 | |
---|
171 | |
---|
172 | private: |
---|
173 | void animate(float time); |
---|
174 | void processLighting(); |
---|
175 | void interpolate(/*sVec3D* verticesList*/); |
---|
176 | void renderFrame() const ; |
---|
177 | |
---|
178 | |
---|
179 | public: |
---|
180 | /* these variables are static, because they are all the same for every model */ |
---|
181 | static sVec3D anorms[NUM_VERTEX_NORMALS]; //!< the anormals |
---|
182 | static float anormsDots[SHADEDOT_QUANT][256]; //!< the anormals dot products |
---|
183 | static sAnim animationList[22]; //!< the anomation list |
---|
184 | //! again one of these strange id software parts |
---|
185 | float* shadeDots; |
---|
186 | |
---|
187 | MD2Data* data; //!< the md2 data pointer |
---|
188 | |
---|
189 | private: |
---|
190 | float scaleFactor; //!< the scale factor (individual) |
---|
191 | float animationSpeed; //!< the speed of the animation (factor for the time) |
---|
192 | sAnimState animationState; //!< animation state of the model |
---|
193 | sVec3D verticesList[MD2_MAX_VERTICES]; //!< place to temp sav the vert |
---|
194 | }; |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | #endif /* _MD2MODEL_H */ |
---|