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