Changeset 4280 in orxonox.OLD for orxonox/trunk/src/lib/graphics/importer
- Timestamp:
- May 24, 2005, 6:09:12 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/graphics/importer/md2Model.cc
r4279 r4280 21 21 22 22 #include <fstream> 23 24 23 25 24 … … 74 73 MD2Model::MD2Model() 75 74 { 75 /* 76 76 this->pVertices = NULL; 77 77 this->pGLCommands = NULL; … … 83 83 84 84 this->scaleFactor = 1.0f; 85 */ 86 87 /* this creates the data container via ressource manager */ 88 this->data = new MD2Data(); 85 89 86 90 this->setAnim(BOOM); … … 90 94 MD2Model::~MD2Model() 91 95 { 96 /* 92 97 delete [] this->pVertices; 93 98 delete [] this->pGLCommands; 94 99 delete [] this->pLightNormals; 100 */ 95 101 } 96 102 … … 99 105 bool MD2Model::loadModel(const char* fileName) 100 106 { 107 return this->data->loadModel(fileName); 108 109 101 110 FILE *pFile; //file stream 102 111 char* buffer; //buffer for frame data … … 164 173 bool MD2Model::loadSkin(const char* fileName) 165 174 { 175 return this->data->loadSkin(fileName); 176 166 177 this->skinFileName = new char[strlen(fileName)+1]; 167 178 strcpy(this->skinFileName, fileName); … … 214 225 215 226 216 void MD2Model::animate()217 {218 if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps))219 {220 this->animationState.currentFrame = this->animationState.nextFrame;221 this->animationState.nextFrame++;222 223 if( this->animationState.nextFrame > this->animationState.endFrame)224 this->animationState.nextFrame = this->animationState.startFrame;225 this->animationState.lastTime = this->animationState.localTime;226 }227 228 if( this->animationState.currentFrame > (this->numFrames - 1) )229 this->animationState.currentFrame = 0;230 if( this->animationState.nextFrame > (this->numFrames - 1) )231 this->animationState.nextFrame = 0;232 233 this->animationState.interpolationState = this->animationState.fps *234 (this->animationState.localTime - this->animationState.lastTime);235 }236 237 238 /* hhmmm... id used a very different way to do lightning... */239 void MD2Model::processLighting()240 {241 shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)];242 }243 244 245 227 void MD2Model::tick(float time) 246 228 { … … 303 285 304 286 287 void MD2Model::animate() 288 { 289 if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps)) 290 { 291 this->animationState.currentFrame = this->animationState.nextFrame; 292 this->animationState.nextFrame++; 293 294 if( this->animationState.nextFrame > this->animationState.endFrame) 295 this->animationState.nextFrame = this->animationState.startFrame; 296 this->animationState.lastTime = this->animationState.localTime; 297 } 298 299 if( this->animationState.currentFrame > (this->numFrames - 1) ) 300 this->animationState.currentFrame = 0; 301 if( this->animationState.nextFrame > (this->numFrames - 1) ) 302 this->animationState.nextFrame = 0; 303 304 this->animationState.interpolationState = this->animationState.fps * 305 (this->animationState.localTime - this->animationState.lastTime); 306 } 307 308 309 /* hhmmm... id used a very different way to do lightning... */ 310 void MD2Model::processLighting() 311 { 312 shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)]; 313 } 314 315 305 316 void MD2Model::debug() 306 317 { … … 314 325 PRINT(0)("===================================================\n\n"); 315 326 } 327 328 329 330 331 332 MD2Data::MD2Data() 333 { 334 this->pVertices = NULL; 335 this->pGLCommands = NULL; 336 this->pLightNormals = NULL; 337 338 this->numFrames = 0; 339 this->numVertices = 0; 340 this->numGLCommands = 0; 341 342 this->scaleFactor = 1.0f; 343 } 344 345 346 MD2Data::~MD2Data() 347 { 348 delete [] this->pVertices; 349 delete [] this->pGLCommands; 350 delete [] this->pLightNormals; 351 } 352 353 354 355 bool MD2Data::loadModel(const char* fileName) 356 { 357 FILE *pFile; //file stream 358 char* buffer; //buffer for frame data 359 sFrame* frame; //temp frame 360 sVec3D *pVertex; 361 int* pNormals; 362 363 pFile = fopen(fileName, "rb"); 364 if( unlikely(!pFile)) 365 { 366 PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n"); 367 return false; 368 } 369 this->header = new MD2Header; 370 fread(this->header, 1, sizeof(MD2Header), pFile); 371 /* check for the header version: make sure its a md2 file :) */ 372 if( unlikely(this->header->version != MD2_VERSION) && unlikely(this->header->ident != MD2_IDENT)) 373 { 374 PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName); 375 return false; 376 } 377 378 this->fileName = new char[strlen(fileName)+1]; 379 strcpy(this->fileName, fileName); 380 /* got the data: map it to locals */ 381 this->numFrames = this->header->numFrames; 382 this->numVertices = this->header->numVertices; 383 this->numTriangles = this->header->numTriangles; 384 this->numGLCommands = this->header->numGlCommands; 385 /* allocate memory for the data storage */ 386 this->pVertices = new sVec3D[this->numVertices * this->numFrames]; 387 this->pGLCommands = new int[this->numGLCommands]; 388 this->pLightNormals = new int[this->numVertices * this->numFrames]; 389 buffer = new char[this->numFrames * this->header->frameSize]; 390 391 /* read frame data from the file to a temp buffer */ 392 fseek(pFile, this->header->offsetFrames, SEEK_SET); 393 fread(buffer, this->header->frameSize, this->numFrames, pFile); 394 /* read opengl commands */ 395 fseek(pFile, this->header->offsetGlCommands, SEEK_SET); 396 fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile); 397 398 for(int i = 0; i < this->numFrames; ++i) 399 { 400 frame = (sFrame*)(buffer + this->header->frameSize * i); 401 pVertex = this->pVertices + this->numVertices * i; 402 pNormals = this->pLightNormals + this->numVertices * i; 403 404 for(int j = 0; j < this->numVertices; ++j) 405 { 406 /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)... */ 407 pVertex[j][0] = (frame->pVertices[j].v[0] * frame->scale[0]) + frame->translate[0]; 408 pVertex[j][1] = (frame->pVertices[j].v[2] * frame->scale[2]) + frame->translate[2]; 409 pVertex[j][2] = -1.0 * (frame->pVertices[j].v[1] * frame->scale[1] + frame->translate[1]); 410 411 pNormals[j] = frame->pVertices[j].lightNormalIndex; 412 } 413 } 414 415 delete [] buffer; 416 fclose(pFile); 417 } 418 419 420 bool MD2Data::loadSkin(const char* fileName) 421 { 422 this->skinFileName = new char[strlen(fileName)+1]; 423 strcpy(this->skinFileName, fileName); 424 this->material = new Material("md2ModelTest"); 425 this->material->setDiffuseMap(fileName); 426 this->material->setIllum(3); 427 this->material->setAmbient(1.0, 1.0, 1.0); 428 }
Note: See TracChangeset
for help on using the changeset viewer.