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