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