1 | /*! |
---|
2 | \file md2Model.h |
---|
3 | \brief 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 "abstract_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') |
---|
28 | #define MD2_VERSION 8 |
---|
29 | #define MD2_MAX_TRIANGLES 4096 |
---|
30 | #define MD2_MAX_VERTICES 2048 |
---|
31 | #define MD2_MAX_TEXCOORDS 2048 |
---|
32 | #define MD2_MAX_FRAMES 512 |
---|
33 | #define MD2_MAX_SKINS 32 |
---|
34 | #define MD2_MAX_FRAMESIZE (MD2_MAX_VERTICES * 4 + 128) |
---|
35 | |
---|
36 | #define NUM_VERTEX_NORMALS 162 |
---|
37 | #define SHADEDOT_QUANT 16 |
---|
38 | |
---|
39 | //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework |
---|
40 | #define kAnimationSpeed 12.0f |
---|
41 | |
---|
42 | //! This holds the header information that is read in at the beginning of the file: id software definition |
---|
43 | struct tMd2Header |
---|
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 | //! This is used to store the vertices that are read in for the current frame directly from the file (compressed) |
---|
69 | struct tMd2AliasTriangle |
---|
70 | { |
---|
71 | byte vertex[3]; |
---|
72 | byte lightNormalIndex; |
---|
73 | }; |
---|
74 | |
---|
75 | //! This stores the normals and vertices for the frames (uncompressed) |
---|
76 | struct tMd2Triangle |
---|
77 | { |
---|
78 | float vertex[3]; |
---|
79 | float normal[3]; |
---|
80 | }; |
---|
81 | |
---|
82 | //! This stores the indices into the vertex and texture coordinate arrays |
---|
83 | struct tMd2Face |
---|
84 | { |
---|
85 | short vertexIndices[3]; |
---|
86 | short textureIndices[3]; |
---|
87 | }; |
---|
88 | |
---|
89 | //! This stores UV coordinates |
---|
90 | struct tMd2TexCoord |
---|
91 | { |
---|
92 | short u, v; |
---|
93 | }; |
---|
94 | |
---|
95 | //! This stores the animation scale, translation and name information for a frame, plus verts |
---|
96 | struct tMd2AliasFrame |
---|
97 | { |
---|
98 | float scale[3]; |
---|
99 | float translate[3]; |
---|
100 | char name[16]; |
---|
101 | tMd2AliasTriangle aliasVertices[1]; |
---|
102 | }; |
---|
103 | |
---|
104 | //! This stores the frames vertices after they have been transformed |
---|
105 | struct tMd2Frame |
---|
106 | { |
---|
107 | char strName[16]; |
---|
108 | tMd2Triangle *pVertices; |
---|
109 | }; |
---|
110 | |
---|
111 | //! This stores a skin name |
---|
112 | typedef char tMd2Skin[64]; |
---|
113 | |
---|
114 | typedef enum |
---|
115 | { |
---|
116 | STAND, |
---|
117 | RUN, |
---|
118 | ATTACK, |
---|
119 | PAIN_A, |
---|
120 | PAIN_B, |
---|
121 | PAIN_C, |
---|
122 | JUMP, |
---|
123 | FLIP, |
---|
124 | SALUTE, |
---|
125 | FALLBACK, |
---|
126 | WAVE, |
---|
127 | POINT, |
---|
128 | CROUCH_STAND, |
---|
129 | CROUCH_WALK, |
---|
130 | CROUCH_ATTACK, |
---|
131 | CROUCH_PAIN, |
---|
132 | CROUCH_DEATH, |
---|
133 | DEATH_FALLBACK, |
---|
134 | DEATH_FALLFORWARD, |
---|
135 | DEATH_FALLBACKSLOW, |
---|
136 | BOOM, |
---|
137 | |
---|
138 | MAX_ANIMATIONS |
---|
139 | } animType; |
---|
140 | |
---|
141 | |
---|
142 | //! This is a MD2 Model class |
---|
143 | class MD2Model : public AbstractModel { |
---|
144 | |
---|
145 | public: |
---|
146 | MD2Model(); |
---|
147 | virtual ~MD2Model(); |
---|
148 | |
---|
149 | bool loadModel(const char* filename); |
---|
150 | bool loadSkin(const char* filename); |
---|
151 | |
---|
152 | void drawModel(float time); |
---|
153 | void drawFrame(int frame); |
---|
154 | void draw(); |
---|
155 | |
---|
156 | void setAnim(int type); |
---|
157 | void scaleModel(float s); |
---|
158 | |
---|
159 | void tick(float dtS); |
---|
160 | |
---|
161 | void animate(/*float time*/); |
---|
162 | private: |
---|
163 | void processLightning(); |
---|
164 | void interpolate(CVector3* vertlist); |
---|
165 | void renderFrame(); |
---|
166 | float getCurrentTime(t3DModel *pModel, int nextFrame); |
---|
167 | |
---|
168 | t3DModel* model; |
---|
169 | float localTime; |
---|
170 | }; |
---|
171 | |
---|
172 | /* forward definitions */ |
---|
173 | class Material; |
---|
174 | |
---|
175 | |
---|
176 | //! A class that handles all of the loading code |
---|
177 | class MD2Loader : public BaseObject { |
---|
178 | |
---|
179 | public: |
---|
180 | MD2Loader(); |
---|
181 | virtual ~MD2Loader(); |
---|
182 | |
---|
183 | bool importMD2(t3DModel *pModel, char *fileName, char *texture = NULL); |
---|
184 | |
---|
185 | private: |
---|
186 | void readMD2Data(); |
---|
187 | void parseAnimations(t3DModel *pModel); |
---|
188 | void convertDataStructures(t3DModel *pModel); |
---|
189 | void computeNormals(t3DModel *pModel); |
---|
190 | void cleanUp(); |
---|
191 | |
---|
192 | FILE *pFile; |
---|
193 | tMd2Header header; //!< The header data |
---|
194 | tMd2Skin *pSkins; //!< The skin data |
---|
195 | tMd2TexCoord *pTexCoords; //!< The texture coordinates |
---|
196 | tMd2Face *pTriangles; //!< Face index information |
---|
197 | tMd2Frame *pFrames; //!< The frames of animation (vertices) |
---|
198 | }; |
---|
199 | |
---|
200 | |
---|
201 | //! This is a MD2 Model class |
---|
202 | class MD2Model2 : public AbstractModel { |
---|
203 | |
---|
204 | public: |
---|
205 | MD2Model2(); |
---|
206 | virtual ~MD2Model2(); |
---|
207 | |
---|
208 | bool loadModel(const char* filename); |
---|
209 | bool loadSkin(const char* filename); |
---|
210 | |
---|
211 | void drawFrame(int frame); |
---|
212 | void draw(); |
---|
213 | void draw2(); |
---|
214 | |
---|
215 | void setAnim(int type); |
---|
216 | void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;} |
---|
217 | |
---|
218 | void tick(float dtS); |
---|
219 | void debug(); |
---|
220 | |
---|
221 | |
---|
222 | private: |
---|
223 | void animate(); |
---|
224 | void processLighting(); |
---|
225 | void interpolate(sVec3D* verticesList); |
---|
226 | void renderFrame(); |
---|
227 | |
---|
228 | public: |
---|
229 | /* these variables are static, because they are all the same for every model */ |
---|
230 | static sVec3D anorms[NUM_VERTEX_NORMALS]; |
---|
231 | static float anormsDots[SHADEDOT_QUANT][256]; |
---|
232 | static sAnim animationList[21]; |
---|
233 | |
---|
234 | private: |
---|
235 | int numFrames; |
---|
236 | int numVertices; |
---|
237 | int numTriangles; |
---|
238 | int numGLCommands; |
---|
239 | char* fileName; |
---|
240 | tMd2Header* header; |
---|
241 | |
---|
242 | sVec3D* pVertices; |
---|
243 | int* pGLCommands; |
---|
244 | int* pLightNormals; |
---|
245 | glCommandVertex* pGLCommands2; |
---|
246 | |
---|
247 | unsigned int textureID; |
---|
248 | Material* material; |
---|
249 | sAnimState animationState; |
---|
250 | float scaleFactor; |
---|
251 | }; |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | #endif /* _MD2MODEL_H */ |
---|