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