Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4280 in orxonox.OLD


Ignore:
Timestamp:
May 24, 2005, 6:09:12 PM (19 years ago)
Author:
patrick
Message:

orxonox/trunk: started separating data from md2 model: iimplementing MD2Data class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/graphics/importer/md2Model.cc

    r4279 r4280  
    2121
    2222#include <fstream>
    23 
    2423
    2524
     
    7473MD2Model::MD2Model()
    7574{
     75  /*
    7676  this->pVertices = NULL;
    7777  this->pGLCommands = NULL;
     
    8383 
    8484  this->scaleFactor = 1.0f;
     85  */
     86
     87  /* this creates the data container via ressource manager */
     88  this->data = new MD2Data();
    8589
    8690  this->setAnim(BOOM);
     
    9094MD2Model::~MD2Model()
    9195{
     96  /*
    9297  delete [] this->pVertices;
    9398  delete [] this->pGLCommands;
    9499  delete [] this->pLightNormals;
     100  */
    95101}
    96102
     
    99105bool MD2Model::loadModel(const char* fileName)
    100106{
     107  return this->data->loadModel(fileName);
     108 
     109
    101110  FILE *pFile;                            //file stream
    102111  char* buffer;                           //buffer for frame data
     
    164173bool MD2Model::loadSkin(const char* fileName)
    165174{
     175  return this->data->loadSkin(fileName);
     176
    166177  this->skinFileName = new char[strlen(fileName)+1];
    167178  strcpy(this->skinFileName, fileName);
     
    214225
    215226
    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 
    245227void MD2Model::tick(float time)
    246228{
     
    303285
    304286
     287void 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... */
     310void MD2Model::processLighting()
     311{
     312  shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)];
     313}
     314
     315
    305316void MD2Model::debug()
    306317{
     
    314325  PRINT(0)("===================================================\n\n");
    315326}
     327
     328
     329
     330
     331
     332MD2Data::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
     346MD2Data::~MD2Data()
     347{
     348  delete [] this->pVertices;
     349  delete [] this->pGLCommands;
     350  delete [] this->pLightNormals;
     351}
     352
     353
     354
     355bool 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
     420bool 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.