1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | */ |
---|
14 | |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
16 | |
---|
17 | #include "md2Model.h" |
---|
18 | #include "material.h" |
---|
19 | |
---|
20 | #include "debug.h" |
---|
21 | #include "resource_manager.h" |
---|
22 | |
---|
23 | //#include <fstream> |
---|
24 | |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | //! the model anorms |
---|
29 | sVec3D MD2Model::anorms[NUM_VERTEX_NORMALS] = { |
---|
30 | #include "anorms.h" |
---|
31 | }; |
---|
32 | |
---|
33 | //! anormal dots, no idea of how this shall work, but it does |
---|
34 | float MD2Model::anormsDots[SHADEDOT_QUANT][256] = { |
---|
35 | #include "anormtab.h" |
---|
36 | }; |
---|
37 | |
---|
38 | //! again one of these strange id software parts |
---|
39 | static float *shadeDots = MD2Model::anormsDots[0]; |
---|
40 | |
---|
41 | //! the angle under which the model is viewd, used internaly |
---|
42 | float md2Angle = 0.0f; |
---|
43 | |
---|
44 | |
---|
45 | //! list of all different animations a std md2model supports |
---|
46 | sAnim MD2Model::animationList[21] = |
---|
47 | { |
---|
48 | // begin, end, fps |
---|
49 | { 0, 39, 9 }, //!< STAND |
---|
50 | { 40, 45, 10 }, //!< RUN |
---|
51 | { 46, 53, 10 }, //!< ATTACK |
---|
52 | { 54, 57, 7 }, //!< PAIN_A |
---|
53 | { 58, 61, 7 }, //!< PAIN_B |
---|
54 | { 62, 65, 7 }, //!< PAIN_C |
---|
55 | { 66, 71, 7 }, //!< JUMP |
---|
56 | { 72, 83, 7 }, //!< FLIP |
---|
57 | { 84, 94, 7 }, //!< SALUTE |
---|
58 | { 95, 111, 10 }, //!< FALLBACK |
---|
59 | { 112, 122, 7 }, //!< WAVE |
---|
60 | { 123, 134, 6 }, //!< POINTT |
---|
61 | { 135, 153, 10 }, //!< CROUCH_STAND |
---|
62 | { 154, 159, 7 }, //!< CROUCH_WALK |
---|
63 | { 160, 168, 10 }, //!< CROUCH_ATTACK |
---|
64 | { 196, 172, 7 }, //!< CROUCH_PAIN |
---|
65 | { 173, 177, 5 }, //!< CROUCH_DEATH |
---|
66 | { 178, 183, 7 }, //!< DEATH_FALLBACK |
---|
67 | { 184, 189, 7 }, //!< DEATH_FALLFORWARD |
---|
68 | { 190, 197, 7 }, //!< DEATH_FALLBACKSLOW |
---|
69 | { 198, 198, 5 }, //!< BOOM |
---|
70 | }; |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | /******************************************************************************** |
---|
75 | * MD2Model * |
---|
76 | ********************************************************************************/ |
---|
77 | |
---|
78 | /** |
---|
79 | \brief simple constructor initializing all variables |
---|
80 | */ |
---|
81 | MD2Model::MD2Model(const char* modelFileName, const char* skinFileName) |
---|
82 | { |
---|
83 | /* this creates the data container via ressource manager */ |
---|
84 | this->data = (MD2Data*)ResourceManager::getInstance()->load(modelFileName, MD2, RP_GAME, (void*)skinFileName); |
---|
85 | if( unlikely(this->data == NULL)) |
---|
86 | PRINTF(0)("The model was not found, MD2Model Loader finished abnormaly. Update the data-repos\n"); |
---|
87 | |
---|
88 | this->scaleFactor = this->data->scaleFactor; |
---|
89 | this->setAnim(STAND); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | \brief simple destructor, dereferencing all variables |
---|
94 | |
---|
95 | this is where the ressource manager is cleaning the stuff |
---|
96 | */ |
---|
97 | MD2Model::~MD2Model() |
---|
98 | { |
---|
99 | ResourceManager::getInstance()->unload(this->data); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * initializes an array of vert with the current frame scaled vertices |
---|
105 | * @param verticesList: the list of vertices to interpolate between |
---|
106 | |
---|
107 | we won't use the pVertices array directly, since its much easier and we need |
---|
108 | saving of data anyway |
---|
109 | */ |
---|
110 | void MD2Model::interpolate(sVec3D* verticesList) |
---|
111 | { |
---|
112 | sVec3D* currVec; |
---|
113 | sVec3D* nextVec; |
---|
114 | |
---|
115 | currVec = &this->data->pVertices[this->data->numVertices * this->animationState.currentFrame]; |
---|
116 | nextVec = &this->data->pVertices[this->data->numVertices * this->animationState.nextFrame]; |
---|
117 | |
---|
118 | for( int i = 0; i < this->data->numVertices; ++i) |
---|
119 | { |
---|
120 | verticesList[i][0] = currVec[i][0] + this->animationState.interpolationState * (nextVec[i][0] - currVec[i][0]); |
---|
121 | verticesList[i][1] = currVec[i][1] + this->animationState.interpolationState * (nextVec[i][1] - currVec[i][1]); |
---|
122 | verticesList[i][2] = currVec[i][2] + this->animationState.interpolationState * (nextVec[i][2] - currVec[i][2]); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | \brief sets the animation type |
---|
129 | * @param type: animation type |
---|
130 | |
---|
131 | the animation types can be looked up in the animationType table |
---|
132 | */ |
---|
133 | void MD2Model::setAnim(int type) |
---|
134 | { |
---|
135 | if( (type < 0) || (type > MAX_ANIMATIONS) ) |
---|
136 | type = STAND; |
---|
137 | |
---|
138 | this->animationState.startFrame = animationList[type].firstFrame; |
---|
139 | this->animationState.endFrame = animationList[type].lastFrame; |
---|
140 | this->animationState.nextFrame = animationList[type].firstFrame + 1; |
---|
141 | this->animationState.fps = animationList[type].fps; |
---|
142 | this->animationState.type = type; |
---|
143 | |
---|
144 | this->animationState.interpolationState = 0.0f; |
---|
145 | this->animationState.localTime = 0.0f; |
---|
146 | this->animationState.lastTime = 0.0f; |
---|
147 | this->animationState.currentFrame = animationList[type].firstFrame; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | static sVec3D verticesList[MD2_MAX_VERTICES]; /* performance: created only once in a lifetime */ |
---|
152 | |
---|
153 | /** |
---|
154 | \brief sets the time in seconds passed since the last tick |
---|
155 | * @param time: in sec |
---|
156 | */ |
---|
157 | void MD2Model::tick(float time) |
---|
158 | { |
---|
159 | this->animationState.localTime += time; |
---|
160 | |
---|
161 | if( likely(this->animationState.localTime > 0.0)) |
---|
162 | this->animate(); |
---|
163 | |
---|
164 | this->processLighting(); |
---|
165 | this->interpolate(verticesList); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | /** |
---|
170 | * @brief draws the model: interface for all other classes out in the world |
---|
171 | * @todo make it const and virtual |
---|
172 | * FIXME |
---|
173 | */ |
---|
174 | void MD2Model::draw() const |
---|
175 | { |
---|
176 | glPushMatrix(); |
---|
177 | this->renderFrame(); |
---|
178 | glPopMatrix(); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | /** |
---|
183 | \brief this is an internal function to render this special frame selected by animate() |
---|
184 | */ |
---|
185 | void MD2Model::renderFrame() const |
---|
186 | { |
---|
187 | int* pCommands = this->data->pGLCommands; |
---|
188 | |
---|
189 | /* some face culling stuff */ |
---|
190 | glPushAttrib(GL_POLYGON_BIT); |
---|
191 | glFrontFace(GL_CW); |
---|
192 | glEnable(GL_CULL_FACE); |
---|
193 | glCullFace(GL_BACK); |
---|
194 | |
---|
195 | |
---|
196 | this->data->material->select(); |
---|
197 | |
---|
198 | /* draw the triangles */ |
---|
199 | while( int i = *(pCommands++)) /* strange looking while loop for maximum performance */ |
---|
200 | { |
---|
201 | if( i < 0) |
---|
202 | { |
---|
203 | glBegin(GL_TRIANGLE_FAN); |
---|
204 | i = -i; |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | glBegin(GL_TRIANGLE_STRIP); |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | for(; i > 0; i--, pCommands += 3) /* down counting for loop, next 3 gl commands */ |
---|
213 | { |
---|
214 | glTexCoord2f( ((float *)pCommands)[0], ((float *)pCommands)[1] ); |
---|
215 | glNormal3fv(anorms[this->data->pLightNormals[pCommands[2]]]); |
---|
216 | glVertex3fv(verticesList[pCommands[2]]); |
---|
217 | } |
---|
218 | glEnd(); |
---|
219 | |
---|
220 | } |
---|
221 | glDisable(GL_CULL_FACE); |
---|
222 | glPopAttrib(); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | /** |
---|
227 | \brief animates the current model |
---|
228 | |
---|
229 | depending on the time passed (tick function), the player will select another model |
---|
230 | */ |
---|
231 | void MD2Model::animate() |
---|
232 | { |
---|
233 | if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps)) |
---|
234 | { |
---|
235 | this->animationState.currentFrame = this->animationState.nextFrame; |
---|
236 | this->animationState.nextFrame++; |
---|
237 | |
---|
238 | if( this->animationState.nextFrame > this->animationState.endFrame) |
---|
239 | this->animationState.nextFrame = this->animationState.startFrame; |
---|
240 | this->animationState.lastTime = this->animationState.localTime; |
---|
241 | } |
---|
242 | |
---|
243 | if( this->animationState.currentFrame > (this->data->numFrames - 1) ) |
---|
244 | this->animationState.currentFrame = 0; |
---|
245 | if( this->animationState.nextFrame > (this->data->numFrames - 1) ) |
---|
246 | this->animationState.nextFrame = 0; |
---|
247 | |
---|
248 | this->animationState.interpolationState = this->animationState.fps * |
---|
249 | (this->animationState.localTime - this->animationState.lastTime); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | /** |
---|
254 | \brief this is how id is precessing their lightning |
---|
255 | |
---|
256 | the details of how the whole lighting process is beeing handled - i have no idea... :) |
---|
257 | */ |
---|
258 | void MD2Model::processLighting() |
---|
259 | { |
---|
260 | shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)]; |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | /** |
---|
265 | \brief prints out debug informations |
---|
266 | */ |
---|
267 | void MD2Model::debug() |
---|
268 | { |
---|
269 | PRINT(0)("\n==========================| MD2Model::debug() |===\n"); |
---|
270 | PRINT(0)("= Model FileName:\t%s\n", this->data->fileName); |
---|
271 | PRINT(0)("= Skin FileName:\t%s\n", this->data->skinFileName); |
---|
272 | PRINT(0)("= Size in Memory:\t%i Bytes\n", this->data->header->frameSize * this->data->header->numFrames + 64); // 64bytes is the header size |
---|
273 | PRINT(0)("= Number of Vertices:\t%i\n", this->data->header->numVertices); |
---|
274 | PRINT(0)("= Number of Frames: \t%i\n", this->data->header->numFrames); |
---|
275 | PRINT(0)("= Height, Width:\t%i, %i\n", this->data->header->skinHeight, this->data->header->skinWidth); |
---|
276 | PRINT(0)("= Pointer to the data object: %p\n", this->data); |
---|
277 | PRINT(0)("===================================================\n\n"); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | /******************************************************************************** |
---|
282 | * MD2Data * |
---|
283 | ********************************************************************************/ |
---|
284 | |
---|
285 | /** |
---|
286 | \brief simple constructor |
---|
287 | */ |
---|
288 | MD2Data::MD2Data(const char* modelFileName, const char* skinFileName) |
---|
289 | { |
---|
290 | this->pVertices = NULL; |
---|
291 | this->pGLCommands = NULL; |
---|
292 | this->pLightNormals = NULL; |
---|
293 | this->pTexCoor = NULL; |
---|
294 | |
---|
295 | this->numFrames = 0; |
---|
296 | this->numVertices = 0; |
---|
297 | this->numGLCommands = 0; |
---|
298 | this->numTexCoor = 0; |
---|
299 | |
---|
300 | this->scaleFactor = 0.2f; |
---|
301 | |
---|
302 | this->fileName = NULL; |
---|
303 | this->skinFileName = NULL; |
---|
304 | this->loadModel(modelFileName); |
---|
305 | this->loadSkin(skinFileName); |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | /** |
---|
310 | \brief simple destructor |
---|
311 | |
---|
312 | this will clean out all the necessary data for a specific md2model |
---|
313 | */ |
---|
314 | MD2Data::~MD2Data() |
---|
315 | { |
---|
316 | delete [] this->fileName; |
---|
317 | delete [] this->skinFileName; |
---|
318 | delete this->header; |
---|
319 | |
---|
320 | delete [] this->pVertices; |
---|
321 | delete [] this->pGLCommands; |
---|
322 | delete [] this->pLightNormals; |
---|
323 | delete [] this->pTexCoor; |
---|
324 | |
---|
325 | delete this->material; |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | |
---|
330 | /** |
---|
331 | \brief this will load the whole model data (vertices, opengl command list, ...) |
---|
332 | * @param fileName: the name of the model file |
---|
333 | \return true if success |
---|
334 | */ |
---|
335 | bool MD2Data::loadModel(const char* fileName) |
---|
336 | { |
---|
337 | FILE *pFile; //file stream |
---|
338 | char* buffer; //buffer for frame data |
---|
339 | sFrame* frame; //temp frame |
---|
340 | sVec3D *pVertex; |
---|
341 | int* pNormals; |
---|
342 | |
---|
343 | //! @todo this chek should include deleting a loaded model (eventually) |
---|
344 | if (fileName == NULL) |
---|
345 | return false; |
---|
346 | |
---|
347 | pFile = fopen(fileName, "rb"); |
---|
348 | if( unlikely(!pFile)) |
---|
349 | { |
---|
350 | PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n"); |
---|
351 | return false; |
---|
352 | } |
---|
353 | this->header = new MD2Header; |
---|
354 | fread(this->header, 1, sizeof(MD2Header), pFile); |
---|
355 | /* check for the header version: make sure its a md2 file :) */ |
---|
356 | if( unlikely(this->header->version != MD2_VERSION) && unlikely(this->header->ident != MD2_IDENT)) |
---|
357 | { |
---|
358 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName); |
---|
359 | return false; |
---|
360 | } |
---|
361 | |
---|
362 | this->fileName = new char[strlen(fileName)+1]; |
---|
363 | strcpy(this->fileName, fileName); |
---|
364 | /* got the data: map it to locals */ |
---|
365 | this->numFrames = this->header->numFrames; |
---|
366 | this->numVertices = this->header->numVertices; |
---|
367 | this->numTriangles = this->header->numTriangles; |
---|
368 | this->numGLCommands = this->header->numGlCommands; |
---|
369 | this->numTexCoor = this->header->numTexCoords; |
---|
370 | /* allocate memory for the data storage */ |
---|
371 | this->pVertices = new sVec3D[this->numVertices * this->numFrames]; |
---|
372 | this->pGLCommands = new int[this->numGLCommands]; |
---|
373 | this->pLightNormals = new int[this->numVertices * this->numFrames]; |
---|
374 | this->pTriangles = new sTriangle[this->numTriangles]; |
---|
375 | this->pTexCoor = new sTexCoor[this->numTexCoor]; |
---|
376 | buffer = new char[this->numFrames * this->header->frameSize]; |
---|
377 | |
---|
378 | |
---|
379 | /* read frame data from the file to a temp buffer */ |
---|
380 | fseek(pFile, this->header->offsetFrames, SEEK_SET); |
---|
381 | fread(buffer, this->header->frameSize, this->numFrames, pFile); |
---|
382 | /* read opengl commands */ |
---|
383 | fseek(pFile, this->header->offsetGlCommands, SEEK_SET); |
---|
384 | fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile); |
---|
385 | /* triangle list */ |
---|
386 | fseek(pFile, this->header->offsetTriangles, SEEK_SET); |
---|
387 | fread(this->pTriangles, sizeof(sTriangle), this->numTriangles, pFile); |
---|
388 | /* read in texture coordinates */ |
---|
389 | fseek(pFile, this->header->offsetTexCoords, SEEK_SET); |
---|
390 | fread(this->pTexCoor, sizeof(sTexCoor), this->numTexCoor, pFile); |
---|
391 | |
---|
392 | |
---|
393 | for(int i = 0; i < this->numFrames; ++i) |
---|
394 | { |
---|
395 | frame = (sFrame*)(buffer + this->header->frameSize * i); |
---|
396 | pVertex = this->pVertices + this->numVertices * i; |
---|
397 | pNormals = this->pLightNormals + this->numVertices * i; |
---|
398 | |
---|
399 | for(int j = 0; j < this->numVertices; ++j) |
---|
400 | { |
---|
401 | /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)... */ |
---|
402 | pVertex[j][0] = ((frame->pVertices[j].v[0] * frame->scale[0] ) + frame->translate[0] )* this->scaleFactor; |
---|
403 | pVertex[j][1] = ((frame->pVertices[j].v[2] * frame->scale[2]) + frame->translate[2]) * this->scaleFactor; |
---|
404 | pVertex[j][2] = (-1.0 * (frame->pVertices[j].v[1] * frame->scale[1] + frame->translate[1])) * this->scaleFactor; |
---|
405 | |
---|
406 | pNormals[j] = frame->pVertices[j].lightNormalIndex; |
---|
407 | } |
---|
408 | } |
---|
409 | PRINTF(4)("Finished loading the md2 file\n"); |
---|
410 | |
---|
411 | delete [] buffer; |
---|
412 | fclose(pFile); |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | /** |
---|
417 | \brief loads the skin/material stuff |
---|
418 | * @param fileName: name of the skin file |
---|
419 | \return true if success |
---|
420 | */ |
---|
421 | bool MD2Data::loadSkin(const char* fileName) |
---|
422 | { |
---|
423 | if( fileName == NULL) |
---|
424 | { |
---|
425 | this->skinFileName = NULL; |
---|
426 | return false; |
---|
427 | } |
---|
428 | |
---|
429 | this->skinFileName = new char[strlen(fileName)+1]; |
---|
430 | strcpy(this->skinFileName, fileName); |
---|
431 | |
---|
432 | this->material = new Material("md2ModelTest"); |
---|
433 | this->material->setDiffuseMap(fileName); |
---|
434 | this->material->setIllum(3); |
---|
435 | this->material->setAmbient(1.0, 1.0, 1.0); |
---|
436 | } |
---|