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