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 | #include "config.h" |
---|
20 | #ifdef HAVE_SDL_SDL_H |
---|
21 | #include <SDL/SDL.h> |
---|
22 | #include <SDL/SDL_endian.h> |
---|
23 | #else |
---|
24 | #include <SDL.h> |
---|
25 | #include <SDL_endian.h> |
---|
26 | #endif |
---|
27 | #include "debug.h" |
---|
28 | #include "util/loading/resource_manager.h" |
---|
29 | |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | //! the model anorms |
---|
34 | sVec3D MD2Model::anorms[NUM_VERTEX_NORMALS] = { |
---|
35 | #include "anorms.h" |
---|
36 | }; |
---|
37 | |
---|
38 | //! anormal dots, no idea of how this shall work, but it does |
---|
39 | float MD2Model::anormsDots[SHADEDOT_QUANT][256] = { |
---|
40 | #include "anormtab.h" |
---|
41 | }; |
---|
42 | |
---|
43 | |
---|
44 | //! the angle under which the model is viewd, used internaly |
---|
45 | float md2Angle = 0.0f; |
---|
46 | |
---|
47 | |
---|
48 | //! list of all different animations a std md2model supports |
---|
49 | sAnim MD2Model::animationList[21] = |
---|
50 | { |
---|
51 | // begin, end, fps, interruptable |
---|
52 | { 0, 39, 9, 1 }, //!< STAND |
---|
53 | { 40, 45, 10, 1 }, //!< RUN |
---|
54 | { 46, 53, 10, 0 }, //!< ATTACK |
---|
55 | { 54, 57, 7, 1 }, //!< PAIN_A |
---|
56 | { 58, 61, 7, 1 }, //!< PAIN_B |
---|
57 | { 62, 65, 7, 1 }, //!< PAIN_C |
---|
58 | { 66, 71, 7, 0 }, //!< JUMP |
---|
59 | { 72, 83, 7, 1 }, //!< FLIP |
---|
60 | { 84, 94, 7, 1 }, //!< SALUTE |
---|
61 | { 95, 111, 10, 1 }, //!< FALLBACK |
---|
62 | { 112, 122, 7, 1 }, //!< WAVE |
---|
63 | { 123, 134, 6, 1 }, //!< POINTT |
---|
64 | { 135, 153, 10, 1 }, //!< CROUCH_STAND |
---|
65 | { 154, 159, 7, 1 }, //!< CROUCH_WALK |
---|
66 | { 160, 168, 10, 1 }, //!< CROUCH_ATTACK |
---|
67 | { 196, 172, 7, 1 }, //!< CROUCH_PAIN |
---|
68 | { 173, 177, 5, 0 }, //!< CROUCH_DEATH |
---|
69 | { 178, 183, 10, 0 }, //!< DEATH_FALLBACK |
---|
70 | { 184, 189, 10, 0 }, //!< DEATH_FALLFORWARD |
---|
71 | { 190, 197, 10, 0 }, //!< DEATH_FALLBACKSLOW |
---|
72 | { 198, 198, 5, 1 }, //!< BOOM |
---|
73 | }; |
---|
74 | |
---|
75 | #ifdef SDL_LIL_ENDIAN |
---|
76 | #define BULK_CONV( _ptr, _num ) do { \ |
---|
77 | int _cnt = _num;\ |
---|
78 | int* _iptr = (int*)_ptr;\ |
---|
79 | for( int _l = 0; _l<_cnt; ++_l )\ |
---|
80 | _iptr[_l] = SDL_SwapLE32( _iptr[_l] );\ |
---|
81 | } while( 0 ) |
---|
82 | #define BULK_CONV16( _ptr, _num ) do { \ |
---|
83 | short _cnt = _num;\ |
---|
84 | short* _iptr = (short*)_ptr;\ |
---|
85 | for( int _l = 0; _l<_cnt; ++_l )\ |
---|
86 | _iptr[_l] = SDL_SwapLE16( _iptr[_l] );\ |
---|
87 | } while( 0 ) |
---|
88 | #else |
---|
89 | #define BULK_CONV( _ptr, _num ) |
---|
90 | #define BULK_CONV16( _ptr, _num ) |
---|
91 | #endif |
---|
92 | |
---|
93 | /******************************************************************************** |
---|
94 | * MD2Model * |
---|
95 | ********************************************************************************/ |
---|
96 | |
---|
97 | /** |
---|
98 | \brief simple constructor initializing all variables |
---|
99 | */ |
---|
100 | MD2Model::MD2Model(const std::string& modelFileName, const std::string& skinFileName, float scale) |
---|
101 | { |
---|
102 | this->setClassID(CL_MD2_MODEL, "MD2Model"); |
---|
103 | /* this creates the data container via ressource manager */ |
---|
104 | if (!modelFileName.empty()) |
---|
105 | this->data = (MD2Data*)ResourceManager::getInstance()->load(modelFileName, MD2, RP_GAME, skinFileName, scale); |
---|
106 | if( unlikely(this->data == NULL)) |
---|
107 | PRINTF(0)("The model was not found, MD2Model Loader finished abnormaly. Update the data-repos\n"); |
---|
108 | |
---|
109 | this->scaleFactor = scale; |
---|
110 | |
---|
111 | shadeDots = MD2Model::anormsDots[0]; |
---|
112 | /* set the animation stat mannualy */ |
---|
113 | this->animationState.type = STAND; |
---|
114 | this->animationState.numPlays = 1; |
---|
115 | this->setAnimation(STAND); |
---|
116 | |
---|
117 | this->debug(); |
---|
118 | |
---|
119 | //write the modelinfo information |
---|
120 | this->pModelInfo.numVertices = this->data->numVertices; |
---|
121 | this->pModelInfo.numTriangles = this->data->numTriangles; |
---|
122 | this->pModelInfo.numNormals = 0; |
---|
123 | this->pModelInfo.numTexCoor = this->data->numTexCoor; |
---|
124 | this->pModelInfo.pVertices = (float*)this->data->pVertices; |
---|
125 | this->pModelInfo.pNormals = NULL; |
---|
126 | this->pModelInfo.pTexCoor = (float*)this->data->pTexCoor; |
---|
127 | |
---|
128 | // triangle conversion |
---|
129 | this->pModelInfo.pTriangles = new sTriangleExt[this->data->numTriangles]; |
---|
130 | for( int i = 0; i < this->data->numTriangles; i++) |
---|
131 | { |
---|
132 | this->pModelInfo.pTriangles[i].indexToVertices[0] = this->data->pTriangles[i].indexToVertices[0]; |
---|
133 | this->pModelInfo.pTriangles[i].indexToVertices[1] = this->data->pTriangles[i].indexToVertices[1]; |
---|
134 | this->pModelInfo.pTriangles[i].indexToVertices[2] = this->data->pTriangles[i].indexToVertices[2]; |
---|
135 | |
---|
136 | this->pModelInfo.pTriangles[i].indexToTexCoor[0] = this->data->pTriangles[i].indexToTexCoor[0]; |
---|
137 | this->pModelInfo.pTriangles[i].indexToTexCoor[1] = this->data->pTriangles[i].indexToTexCoor[1]; |
---|
138 | this->pModelInfo.pTriangles[i].indexToTexCoor[2] = this->data->pTriangles[i].indexToTexCoor[2]; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | \brief simple destructor, dereferencing all variables |
---|
145 | |
---|
146 | this is where the ressource manager is cleaning the stuff |
---|
147 | */ |
---|
148 | MD2Model::~MD2Model() |
---|
149 | { |
---|
150 | this->pModelInfo.pVertices = NULL; |
---|
151 | this->pModelInfo.pNormals = NULL; |
---|
152 | this->pModelInfo.pTexCoor = NULL; |
---|
153 | this->pModelInfo.pTriangles = NULL; |
---|
154 | |
---|
155 | ResourceManager::getInstance()->unload(this->data); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | * initializes an array of vert with the current frame scaled vertices |
---|
161 | * @param this->verticesList: the list of vertices to interpolate between |
---|
162 | |
---|
163 | we won't use the pVertices array directly, since its much easier and we need |
---|
164 | saving of data anyway |
---|
165 | */ |
---|
166 | void MD2Model::interpolate(/*sVec3D* this->verticesList*/) |
---|
167 | { |
---|
168 | sVec3D* currVec; |
---|
169 | sVec3D* nextVec; |
---|
170 | |
---|
171 | currVec = &this->data->pVertices[this->data->numVertices * this->animationState.currentFrame]; |
---|
172 | nextVec = &this->data->pVertices[this->data->numVertices * this->animationState.nextFrame]; |
---|
173 | |
---|
174 | for( int i = 0; i < this->data->numVertices; ++i) |
---|
175 | { |
---|
176 | this->verticesList[i][0] = currVec[i][0] + this->animationState.interpolationState * (nextVec[i][0] - currVec[i][0]); |
---|
177 | this->verticesList[i][1] = currVec[i][1] + this->animationState.interpolationState * (nextVec[i][1] - currVec[i][1]); |
---|
178 | this->verticesList[i][2] = currVec[i][2] + this->animationState.interpolationState * (nextVec[i][2] - currVec[i][2]); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | \brief sets the animation type |
---|
185 | * @param type: animation type |
---|
186 | |
---|
187 | the animation types can be looked up in the animationType table |
---|
188 | */ |
---|
189 | void MD2Model::setAnimation(int type, int animPlayback) |
---|
190 | { |
---|
191 | if( (type < 0) || (type > MAX_ANIMATIONS) ) |
---|
192 | type = STAND; |
---|
193 | |
---|
194 | if( MD2Model::animationList[this->animationState.type].bStoppable == 0) |
---|
195 | { |
---|
196 | if( this->animationState.numPlays == 0 ) |
---|
197 | return; |
---|
198 | } |
---|
199 | |
---|
200 | this->animationState.startFrame = animationList[type].firstFrame; |
---|
201 | this->animationState.endFrame = animationList[type].lastFrame; |
---|
202 | this->animationState.nextFrame = animationList[type].firstFrame + 1; |
---|
203 | this->animationState.fps = animationList[type].fps; |
---|
204 | this->animationState.type = type; |
---|
205 | this->animationState.numPlays = 0; |
---|
206 | this->animationState.animPlaybackMode = animPlayback; |
---|
207 | |
---|
208 | this->animationState.interpolationState = 0.0f; |
---|
209 | this->animationState.localTime = 0.0f; |
---|
210 | this->animationState.lastTime = 0.0f; |
---|
211 | this->animationState.currentFrame = animationList[type].firstFrame; |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /** |
---|
216 | \brief sets the time in seconds passed since the last tick |
---|
217 | * @param time: in sec |
---|
218 | */ |
---|
219 | void MD2Model::tick(float time) |
---|
220 | { |
---|
221 | this->animate(time); |
---|
222 | this->processLighting(); |
---|
223 | this->interpolate(/*this->verticesList*/); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | * @brief draws the model: interface for all other classes out in the world |
---|
229 | * @todo make it const and virtual |
---|
230 | * FIXME |
---|
231 | */ |
---|
232 | void MD2Model::draw() const |
---|
233 | { |
---|
234 | glPushMatrix(); |
---|
235 | this->renderFrame(); |
---|
236 | // renderFrameTriangles(); |
---|
237 | glPopMatrix(); |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | /** |
---|
242 | \brief this is an internal function to render this special frame selected by animate() |
---|
243 | */ |
---|
244 | void MD2Model::renderFrame() const |
---|
245 | { |
---|
246 | int* pCommands = this->data->pGLCommands; |
---|
247 | |
---|
248 | /* some face culling stuff */ |
---|
249 | glPushAttrib(GL_POLYGON_BIT); |
---|
250 | glFrontFace(GL_CW); |
---|
251 | glEnable(GL_CULL_FACE); |
---|
252 | glCullFace(GL_BACK); |
---|
253 | |
---|
254 | this->data->material.select(); |
---|
255 | |
---|
256 | /* draw the triangles */ |
---|
257 | while( int i = *(pCommands++)) /* strange looking while loop for maximum performance */ |
---|
258 | { |
---|
259 | if( i < 0) |
---|
260 | { |
---|
261 | glBegin(GL_TRIANGLE_FAN); |
---|
262 | i = -i; |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | glBegin(GL_TRIANGLE_STRIP); |
---|
267 | } |
---|
268 | |
---|
269 | for(; i > 0; i--, pCommands += 3) /* down counting for loop, next 3 gl commands */ |
---|
270 | { |
---|
271 | glTexCoord2f( ((float *)pCommands)[0], ((float *)pCommands)[1] ); |
---|
272 | glNormal3fv(anorms[this->data->pLightNormals[pCommands[2]]]); |
---|
273 | glVertex3fv(this->verticesList[pCommands[2]]); |
---|
274 | } |
---|
275 | glEnd(); |
---|
276 | |
---|
277 | } |
---|
278 | glDisable(GL_CULL_FACE); |
---|
279 | glPopAttrib(); |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | void MD2Model::renderFrameTriangles() const |
---|
284 | { |
---|
285 | //static sVec3D this->verticesList[MD2_MAX_VERTICES]; /* performance: created only once in a lifetime */ |
---|
286 | int* pCommands = this->data->pGLCommands; |
---|
287 | /* some face culling stuff */ |
---|
288 | // glPushAttrib(GL_POLYGON_BIT); |
---|
289 | // glFrontFace(GL_CW); |
---|
290 | // glEnable(GL_CULL_FACE); |
---|
291 | // glCullFace(GL_BACK); |
---|
292 | // |
---|
293 | // this->processLighting(); |
---|
294 | // this->interpolate(/*this->verticesList*/); |
---|
295 | this->data->material.select(); |
---|
296 | |
---|
297 | /* draw the triangles */ |
---|
298 | glBegin(GL_TRIANGLES); |
---|
299 | |
---|
300 | for( int i = 0, k = 0; i < this->data->numTriangles; ++i, k += 3) |
---|
301 | { |
---|
302 | float* v = this->data->pVertices[this->data->pTriangles[i].indexToVertices[0]]; |
---|
303 | |
---|
304 | printf("triangle: %i\n", i); |
---|
305 | printf(" v0: (%f, %f, %f)\n", v[0], v[1], v[2]); |
---|
306 | v = this->data->pVertices[this->data->pTriangles[i].indexToVertices[1]]; |
---|
307 | printf(" v1: (%f, %f, %f)\n", v[0], v[1], v[2]); |
---|
308 | v = this->data->pVertices[this->data->pTriangles[i].indexToVertices[2]]; |
---|
309 | printf(" v2: (%f, %f, %f)\n", v[0], v[1], v[2]); |
---|
310 | |
---|
311 | |
---|
312 | glNormal3f(anorms[i][0], anorms[i][1], anorms[i][2]); |
---|
313 | glVertex3fv(this->data->pVertices[this->data->pTriangles[i].indexToVertices[0]]); |
---|
314 | |
---|
315 | glNormal3f(anorms[i][0], anorms[i][1], anorms[i][2]); |
---|
316 | glVertex3fv(this->data->pVertices[this->data->pTriangles[i].indexToVertices[1]]); |
---|
317 | |
---|
318 | glNormal3f(anorms[i][0], anorms[i][1], anorms[i][2]); |
---|
319 | glVertex3fv(this->data->pVertices[this->data->pTriangles[i].indexToVertices[2]]); |
---|
320 | } |
---|
321 | |
---|
322 | glEnd(); |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | /** |
---|
327 | \brief animates the current model |
---|
328 | |
---|
329 | depending on the time passed (tick function), the player will select another model |
---|
330 | */ |
---|
331 | void MD2Model::animate(float time) |
---|
332 | { |
---|
333 | this->animationState.localTime += time; |
---|
334 | |
---|
335 | if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps)) |
---|
336 | { |
---|
337 | this->animationState.currentFrame = this->animationState.nextFrame; |
---|
338 | this->animationState.nextFrame++; |
---|
339 | |
---|
340 | if( this->animationState.nextFrame > this->animationState.endFrame ) |
---|
341 | { |
---|
342 | if( this->animationState.animPlaybackMode == MD2_ANIM_LOOP) |
---|
343 | { |
---|
344 | this->animationState.nextFrame = this->animationState.startFrame; |
---|
345 | this->animationState.numPlays++; |
---|
346 | } |
---|
347 | else |
---|
348 | { |
---|
349 | this->animationState.nextFrame = this->animationState.endFrame; |
---|
350 | } |
---|
351 | } |
---|
352 | this->animationState.lastTime = this->animationState.localTime; |
---|
353 | } |
---|
354 | |
---|
355 | // if( this->animationState.currentFrame > (this->data->numFrames - 1) ) |
---|
356 | // this->animationState.currentFrame = 0; |
---|
357 | |
---|
358 | // if( (this->animationState.nextFrame > (this->data->numFrames - 1)) && this->animationState.animPlaybackMode == MD2_ANIM_LOOP) |
---|
359 | // this->animationState.nextFrame = 0; |
---|
360 | |
---|
361 | this->animationState.interpolationState = this->animationState.fps * |
---|
362 | (this->animationState.localTime - this->animationState.lastTime); |
---|
363 | } |
---|
364 | |
---|
365 | |
---|
366 | /** |
---|
367 | \brief this is how id is precessing their lightning |
---|
368 | |
---|
369 | the details of how the whole lighting process is beeing handled - i have no idea... :) |
---|
370 | */ |
---|
371 | void MD2Model::processLighting() |
---|
372 | { |
---|
373 | shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)]; |
---|
374 | } |
---|
375 | |
---|
376 | |
---|
377 | /** |
---|
378 | \brief prints out debug informations |
---|
379 | */ |
---|
380 | void MD2Model::debug() |
---|
381 | { |
---|
382 | PRINT(0)("\n==========================| MD2Model::debug() |===\n"); |
---|
383 | PRINT(0)("= Model FileName:\t%s\n", this->data->fileName.c_str()); |
---|
384 | PRINT(0)("= Skin FileName:\t%s\n", this->data->skinFileName.c_str()); |
---|
385 | PRINT(0)("= Size in Memory:\t%i Bytes\n", this->data->header->frameSize * this->data->header->numFrames + 64); // 64bytes is the header size |
---|
386 | PRINT(0)("= Number of Vertices:\t%i\n", this->data->header->numVertices); |
---|
387 | PRINT(0)("= Number of Frames: \t%i\n", this->data->header->numFrames); |
---|
388 | PRINT(0)("= Height, Width:\t%i, %i\n", this->data->header->skinHeight, this->data->header->skinWidth); |
---|
389 | PRINT(0)("= Pointer to the data object: %p\n", this->data); |
---|
390 | PRINT(0)("===================================================\n\n"); |
---|
391 | } |
---|
392 | |
---|
393 | |
---|
394 | /******************************************************************************** |
---|
395 | * MD2Data * |
---|
396 | ********************************************************************************/ |
---|
397 | |
---|
398 | /** |
---|
399 | \brief simple constructor |
---|
400 | */ |
---|
401 | MD2Data::MD2Data(const std::string& modelFileName, const std::string& skinFileName, float scale) |
---|
402 | { |
---|
403 | scale *= 0.1f; |
---|
404 | |
---|
405 | this->pVertices = NULL; |
---|
406 | this->pGLCommands = NULL; |
---|
407 | this->pLightNormals = NULL; |
---|
408 | this->pTexCoor = NULL; |
---|
409 | |
---|
410 | this->numFrames = 0; |
---|
411 | this->numVertices = 0; |
---|
412 | this->numGLCommands = 0; |
---|
413 | this->numTexCoor = 0; |
---|
414 | |
---|
415 | // this->scaleFactor = 1.0f; |
---|
416 | this->scaleFactor = scale; |
---|
417 | |
---|
418 | this->fileName = ""; |
---|
419 | this->skinFileName = ""; |
---|
420 | this->loadModel(modelFileName); |
---|
421 | this->loadSkin(skinFileName); |
---|
422 | } |
---|
423 | |
---|
424 | |
---|
425 | /** |
---|
426 | \brief simple destructor |
---|
427 | |
---|
428 | this will clean out all the necessary data for a specific md2model |
---|
429 | */ |
---|
430 | MD2Data::~MD2Data() |
---|
431 | { |
---|
432 | delete this->header; |
---|
433 | |
---|
434 | delete [] this->pVertices; |
---|
435 | delete [] this->pGLCommands; |
---|
436 | delete [] this->pLightNormals; |
---|
437 | delete [] this->pTexCoor; |
---|
438 | } |
---|
439 | |
---|
440 | |
---|
441 | |
---|
442 | /** |
---|
443 | \brief this will load the whole model data (vertices, opengl command list, ...) |
---|
444 | * @param fileName: the name of the model file |
---|
445 | \return true if success |
---|
446 | */ |
---|
447 | bool MD2Data::loadModel(const std::string& fileName) |
---|
448 | { |
---|
449 | FILE *pFile; //file stream |
---|
450 | char* buffer; //buffer for frame data |
---|
451 | sFrame* frame; //temp frame |
---|
452 | sVec3D *pVertex; |
---|
453 | int* pNormals; |
---|
454 | |
---|
455 | //! @todo this chek should include deleting a loaded model (eventually) |
---|
456 | if (fileName.empty()) |
---|
457 | return false; |
---|
458 | |
---|
459 | pFile = fopen(fileName.c_str(), "rb"); |
---|
460 | if( unlikely(!pFile)) |
---|
461 | { |
---|
462 | PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n"); |
---|
463 | return false; |
---|
464 | } |
---|
465 | this->header = new MD2Header; |
---|
466 | fread(this->header, 1, sizeof(MD2Header), pFile); |
---|
467 | BULK_CONV( this->header, sizeof(MD2Header)/4 ); |
---|
468 | /* check for the header version: make sure its a md2 file :) */ |
---|
469 | if( unlikely( this->header->version != MD2_VERSION) && unlikely( this->header->ident != MD2_IDENT)) |
---|
470 | { |
---|
471 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str()); |
---|
472 | return false; |
---|
473 | } |
---|
474 | |
---|
475 | this->fileName = fileName; |
---|
476 | /* got the data: map it to locals */ |
---|
477 | this->numFrames = this->header->numFrames; |
---|
478 | this->numVertices = this->header->numVertices; |
---|
479 | this->numTriangles = this->header->numTriangles; |
---|
480 | this->numGLCommands = this->header->numGlCommands; |
---|
481 | this->numTexCoor = this->header->numTexCoords; |
---|
482 | /* allocate memory for the data storage */ |
---|
483 | this->pVertices = new sVec3D[this->numVertices * this->numFrames]; |
---|
484 | this->pGLCommands = new int[this->numGLCommands]; |
---|
485 | this->pLightNormals = new int[this->numVertices * this->numFrames]; |
---|
486 | this->pTriangles = new sTriangle[this->numTriangles]; |
---|
487 | this->pTexCoor = new sTexCoor[this->numTexCoor]; |
---|
488 | buffer = new char[this->numFrames * this->header->frameSize]; |
---|
489 | |
---|
490 | |
---|
491 | /* read frame data from the file to a temp buffer */ |
---|
492 | fseek(pFile, this->header->offsetFrames, SEEK_SET); |
---|
493 | fread(buffer, this->header->frameSize, this->numFrames, pFile); |
---|
494 | //BULK_CONV( buffer, this->header->frameSize*this->numFrames*sizeof(char)/4 ); |
---|
495 | /* read opengl commands */ |
---|
496 | fseek(pFile, this->header->offsetGlCommands, SEEK_SET); |
---|
497 | |
---|
498 | fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile); |
---|
499 | BULK_CONV( this->pGLCommands, this->numGLCommands ); |
---|
500 | /* triangle list */ |
---|
501 | fseek(pFile, this->header->offsetTriangles, SEEK_SET); |
---|
502 | fread(this->pTriangles, sizeof(sTriangle), this->numTriangles, pFile); |
---|
503 | BULK_CONV16( this->pTriangles, this->numTriangles*sizeof(sTriangle)/2 ); |
---|
504 | |
---|
505 | /* read in texture coordinates */ |
---|
506 | fseek(pFile, this->header->offsetTexCoords, SEEK_SET); |
---|
507 | fread(this->pTexCoor, sizeof(sTexCoor), this->numTexCoor, pFile); |
---|
508 | BULK_CONV16( this->pTexCoor, this->numTexCoor*sizeof(sTexCoor)/2 ); |
---|
509 | |
---|
510 | for(int i = 0; i < this->numFrames; ++i) |
---|
511 | { |
---|
512 | frame = (sFrame*)(buffer + this->header->frameSize * i); |
---|
513 | //Convert the translate and scale Vec3D if needed. |
---|
514 | BULK_CONV( frame, 6 ); |
---|
515 | BULK_CONV( frame->pVertices, 3 ); |
---|
516 | pVertex = this->pVertices + this->numVertices * i; |
---|
517 | pNormals = this->pLightNormals + this->numVertices * i; |
---|
518 | |
---|
519 | for(int j = 0; j < this->numVertices; ++j) |
---|
520 | { |
---|
521 | /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)... */ |
---|
522 | pVertex[j][0] = ((frame->pVertices[j].v[0] * frame->scale[0] ) + frame->translate[0] )* this->scaleFactor; |
---|
523 | pVertex[j][1] = ((frame->pVertices[j].v[2] * frame->scale[2]) + frame->translate[2]) * this->scaleFactor; |
---|
524 | pVertex[j][2] = (-1.0 * (frame->pVertices[j].v[1] * frame->scale[1] + frame->translate[1])) * this->scaleFactor; |
---|
525 | |
---|
526 | //printf("vertex %i/%i: (%f, %f, %f)\n", j, this->numVertices, pVertex[j][0], pVertex[j][1], pVertex[j][2]); |
---|
527 | |
---|
528 | pNormals[j] = frame->pVertices[j].lightNormalIndex; |
---|
529 | } |
---|
530 | } |
---|
531 | PRINTF(4)("Finished loading the md2 file\n"); |
---|
532 | |
---|
533 | delete [] buffer; |
---|
534 | fclose(pFile); |
---|
535 | } |
---|
536 | |
---|
537 | |
---|
538 | /** |
---|
539 | \brief loads the skin/material stuff |
---|
540 | * @param fileName: name of the skin file |
---|
541 | \return true if success |
---|
542 | */ |
---|
543 | bool MD2Data::loadSkin(const std::string& fileName) |
---|
544 | { |
---|
545 | if( fileName.empty()) |
---|
546 | { |
---|
547 | this->skinFileName = ""; |
---|
548 | return false; |
---|
549 | } |
---|
550 | |
---|
551 | this->skinFileName = fileName; |
---|
552 | |
---|
553 | this->material.setName("md2ModelMaterial"); |
---|
554 | this->material.setDiffuseMap(fileName); |
---|
555 | this->material.setIllum(3); |
---|
556 | this->material.setAmbient(1.0, 1.0, 1.0); |
---|
557 | } |
---|