Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8724 in orxonox.OLD for trunk/src/lib/graphics


Ignore:
Timestamp:
Jun 22, 2006, 3:14:58 PM (18 years ago)
Author:
bensch
Message:

merged the bsp-model-stuff back here

Location:
trunk/src/lib/graphics/importer
Files:
13 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/importer/Makefile.am

    r8490 r8724  
    3434                           md3/md3_model.cc \
    3535                           md3/md3_animation.cc \
     36                           md3/md3_animation_cfg.cc \
    3637                           md3/md3_bone_frame.cc \
    3738                           md3/md3_mesh.cc \
     
    7677                md3/md3_model.h \
    7778                md3/md3_animation.h \
     79                md3/md3_animation_cfg.h \
    7880                md3/md3_bone_frame.h \
    7981                md3/md3_mesh.h \
  • trunk/src/lib/graphics/importer/bsp_manager.cc

    r8490 r8724  
    5050BspManager::BspManager(WorldEntity* parent)
    5151{
    52  
     52
    5353  this->parent = parent;
    5454  /*// open a BSP file
     
    840840        this->checkBrushRayN(&curBrush);
    841841      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
    842     }
     842    }
     843   
    843844    return;
    844845  }
     
    907908
    908909}
     910
     911float BspManager::checkPatchAltitude(BspTreeNode* node)
     912{
     913  leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
     914  for(int i = 0; i < curLeaf.n_leaffaces ; i++)
     915  {
     916   
     917  }
     918  return 10.0f;
     919}
     920
    909921void BspManager::checkCollisionBox(void)
    910922{
     
    937949void BspManager::checkCollision(WorldEntity* worldEntity)
    938950{
    939  
     951
    940952  this->outputStartsOut = true;
    941953  this->outputAllSolid = false;
    942954  this->outputFraction = 1.0f;
    943  
    944   Vector position = worldEntity->getAbsCoor();
     955
     956
    945957
    946958
    947959  Vector forwardDir = worldEntity->getAbsDirX();
    948   forwardDir = forwardDir * 8.0f;
     960
    949961
    950962  Vector upDir = worldEntity->getAbsDirY();
     
    958970  dest.z  += forwardDir.z;
    959971  */
    960 
    961   dest = worldEntity->getAbsCoor() - upDir*40.0;
     972  Vector position = worldEntity->getAbsCoor() + upDir*10.0f ;
     973  dest = worldEntity->getAbsCoor() - upDir*40.0f; //
    962974  Vector out = dest;
    963975
     
    966978
    967979  bool collision = false;
    968   Vector position1 = position + Vector(0.0,0.1,0.0);
    969   Vector position2 = position + Vector(0.0,0.2,0.0);
    970   Vector dest1 = position1 + forwardDir;
     980  Vector position1 = position; // + Vector(0.0,0.6,0.0);
     981  Vector position2 = position + Vector(0.0,1.0,0.0);
     982  Vector dest1 = position + forwardDir*4.0f;
    971983  Vector dest2 = position2 + forwardDir;
    972984  dest = position - Vector(0.0, 40.0,0.0);
     
    976988
    977989  float height = 40;
    978  
     990
    979991  this->inputStart =  position;
    980992  this->inputEnd =   dest;
    981993  this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest );
    982 
    983   PRINTF(0)(" checking collision: %s, solid = %i, fraction = %f\n", worldEntity->getClassName(), this->outputAllSolid, this->outputFraction);
    984   PRINTF(0)("checking collision!! Pos.Coords: %f , %f , %f\n", position.x , position.y, position.z);
    985   PRINTF(0)("checking collision!! Dest.Coords: %f , %f , %f\n", dest.x , dest.y, dest.z); 
     994 
     995
     996
     997//   PRINTF(0)(" checking collision: %s, solid = %i, fraction = %f\n", worldEntity->getClassName(), this->outputAllSolid, this->outputFraction);
     998//   PRINTF(0)("checking collision!! Pos.Coords: %f , %f , %f\n", position.x , position.y, position.z);
     999//   PRINTF(0)("checking collision!! Dest.Coords: %f , %f , %f\n", dest.x , dest.y, dest.z);
     1000
    9861001//   position1.debug();
     1002
     1003  if(!this->outputStartsOut )
     1004  {
     1005    this->collPlane = new plane;
     1006    this->collPlane->x = 0.0f;
     1007    this->collPlane->y = 0.0f;
     1008    this->collPlane->z = 0.0f;
     1009    collision = true;
     1010  }
     1011  else
     1012  {
     1013
     1014
     1015
    9871016
    9881017  if( this->outputFraction == 1.0f)
    9891018  {
    990     if(this->outputAllSolid)
     1019    if(this->outputAllSolid )
    9911020    {
    9921021      this->collPlane = new plane;
    993       this->collPlane->x = 1.0f;
     1022      this->collPlane->x = 0.0f;
    9941023      this->collPlane->y = 0.0f;
    9951024      this->collPlane->z = 0.0f;
     
    10131042  }
    10141043
     1044
     1045  }
     1046 
     1047  plane* testPlane = this->collPlane;
     1048 
     1049  this->outputStartsOut = true;
     1050  this->outputAllSolid = false;
     1051  this->outputFraction = 1.0f;
     1052  this->inputStart =  position;
     1053  this->inputEnd =   dest1;
     1054  this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest1 );
     1055  out.x = this->outputFraction;
     1056  out.z = this->outputFraction;
     1057 
     1058  /*
     1059  out.x = position1.x + (dest.x -position1.x) * this->outputFraction;
     1060 
     1061  out.z = position1.z + (dest.z -position1.z) * this->outputFraction;
     1062  */
     1063 
     1064 
    10151065  // Return the normal here: Normal's stored in this->collPlane;
    10161066  if( collision) {
    1017     PRINTF(0)("We got a collision!! Are you sure: outputFraction = %f\n", this->outputFraction);
    1018     worldEntity->registerCollision(this->parent, worldEntity, Vector(this->collPlane->x, this->collPlane->y, this->collPlane->z), out);
    1019   }
     1067    PRINTF(5)("We got a collision!! Are you sure: outputFraction = %f\n", this->outputFraction);
     1068    worldEntity->registerCollision(this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z), out);
     1069  }
     1070  else  worldEntity->registerCollision(this->parent, worldEntity, Vector(0.0, 2.0, 0.0), dest);
    10201071
    10211072}
  • trunk/src/lib/graphics/importer/bsp_manager.h

    r8490 r8724  
    7373  void  checkCollisionRay(BspTreeNode * node,float startFraction, float endFraction, Vector* start, Vector* end);
    7474  void  checkCollisionRayN(BspTreeNode * node,float startFraction, float endFraction, Vector* start, Vector* end);
    75   void TraceBox( Vector& inputStart, Vector& inputEnd,Vector& inputMins, Vector& inputMaxs );
     75  void  TraceBox( Vector& inputStart, Vector& inputEnd,Vector& inputMins, Vector& inputMaxs );
    7676  void  checkCollisionBox(void);
    7777  void  checkBrushRay(brush* curBrush);
    7878  void  checkBrushRayN(brush* curBrush);
    79   void   checkBrushRayN(brush* curBrush, Vector& inputStart, Vector& inputEnd);
     79  void  checkBrushRayN(brush* curBrush, Vector& inputStart, Vector& inputEnd);
     80  float  checkPatchAltitude(BspTreeNode* node); //! To be implemented...
    8081
    8182  void drawDebugCube(Vector* cam);
  • trunk/src/lib/graphics/importer/interactive_model.h

    r8490 r8724  
    3737
    3838    virtual void setAnimation(int animNum, int playbackMode = 0) = 0;
     39    virtual int getAnimation() = 0;
    3940};
    4041
  • trunk/src/lib/graphics/importer/md2/md2Model.h

    r8490 r8724  
    159159  virtual void setAnimation(int type, int animPlayback = MD2_ANIM_LOOP);
    160160  /**  returns the current animation @returns animation type */
    161   inline int MD2Model::getAnim() { return this->animationState.type; }
     161  inline int MD2Model::getAnimation() { return this->animationState.type; }
    162162  /**  scales the current model @param scaleFactor: the factor [0..1] to use for scaling */
    163163  void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;}
  • trunk/src/lib/graphics/importer/md3/md3_animation.cc

    r8490 r8724  
    2222{
    2323
     24
     25
     26  MD3AnimationMap MD3Animation::animationList[25] = {
     27    { "Death 1",                 BOTH  },
     28    { "Dead 1",                  BOTH  },
     29    { "Death 2",                 BOTH  },
     30    { "Dead 2",                  BOTH  },
     31    { "Death 3",                 BOTH  },
     32    { "Dead 3",                  BOTH  },
     33    { "Gesture",                 BOTH  },
     34    { "Shoot",                   TORSO },
     35    { "Hit",                     TORSO },
     36    { "Drop Weapon",             TORSO },
     37    { "Raise Weapon",            TORSO },
     38    { "Stand With Weapon",       TORSO },
     39    { "Stand with Gauntlet",     TORSO },
     40    { "Crouched Walk",           LEGS  },
     41    { "Walk",                    LEGS  },
     42    { "Run",                     LEGS  },
     43    { "Backpedal",               LEGS  },
     44    { "Swim",                    LEGS  },
     45    { "Jump Forward",            LEGS  },
     46    { "Land Forward",            LEGS  },
     47    { "Jump Backward",           LEGS  },
     48    { "Land Backward",           LEGS  },
     49    { "Stand Idle",              LEGS  },
     50    { "Crouched Idle",           LEGS  },
     51    { "Turn In Place",           LEGS  }
     52  };
     53
     54
    2455  /**
    2556   * create an empty MD3Animation object
     
    2859  {
    2960    this->init();
    30   }
    31 
    32 
    33   /**
    34    * create a new MD3 Anumation object and initialize it with the data on the given line
    35    * @param line: the line to read from
    36    */
    37   MD3Animation::MD3Animation(std::string line)
    38   {
    39     this->init();
    40     // loadAnimation()
    4161  }
    4262
  • trunk/src/lib/graphics/importer/md3/md3_animation.h

    r8490 r8724  
    1111#include <string>
    1212
     13namespace md3
     14{
    1315
    14 typedef struct AnimationType
    15 {};
     16typedef enum MD3AnimationType {
     17  LEGS      = 0,             //!< animation only applicatble to the legs. top level model loaded from lower.md3
     18  TORSO,                     //!< animation only applicatboe to the torso. normaly loaded from upper.md3
     19  BOTH,                      //!< animation is applicable on both
     20  ALL,                       //!< animation is applicable to all submodels
     21
     22  NUM_ANIMATION_TYPE         //!< number of animations
     23};
     24
     25
     26typedef struct MD3AnimationMap {
     27  std::string        animationName;
     28  MD3AnimationType   animationType;
     29};
     30
     31
    1632
    1733
     
    2440  public:
    2541    MD3Animation();
    26     MD3Animation(std::string line);
    2742    virtual ~MD3Animation();
    2843
     
    3348
    3449  public:
    35     AnimationType    type;                        //!< specifies for what parts of a model this animation is appilcable (e.g. LEGS, BOTH)
    36     std::string      name;                        //!< name of the animation
     50    MD3AnimationType      type;                      //!< specifies for what parts of a model this animation is appilcable (e.g. LEGS)
     51    std::string           name;                      //!< name of the animation
    3752
    38     int              offset;                      //!< for LEGS animation, this is the offset value to skip TORSO frames
    39     int              first;                       //!< first frame
    40     int              numFrames;                   //!< the number of frames in the anumation. < 0 means that all available frames used
    41     int              numLoopFrames;               //!< number of looping frames
    42     int              fps;                         //!< frames per second
     53    int                   offset;                    //!< for LEGS animation, this is the offset value to skip TORSO frames
     54    int                   first;                     //!< first frame
     55    int                   numFrames;                 //!< the number of frames in the anumation. < 0 means that all available frames used
     56    int                   numLoopFrames;             //!< number of looping frames
     57    int                   fps;                       //!< frames per second
    4358
    44 
     59    static MD3AnimationMap animationList[25];         //!< the animation list
    4560};
    4661
    47 
     62}
    4863#endif /* _MD3_ANIMATION_H */
  • trunk/src/lib/graphics/importer/md3/md3_bone_frame.h

    r8490 r8724  
    66 */
    77
    8 #ifndef _MD3_ANIMATION_H
    9 #define _MD3_ANIMATION_H
     8#ifndef _MD3_BONE_FRAME_ANIMATION_H
     9#define _MD3_BONE_FRAME_ANIMATION_H
    1010
    1111#include "vector.h"
     
    5151}
    5252
    53 #endif /* _MD3_ANIMATION_H */
     53#endif /* _MD3_BONE_FRAME_ANIMATION_H */
  • trunk/src/lib/graphics/importer/md3/md3_data.cc

    r8490 r8724  
    3737MD3Data::MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale)
    3838{
    39 
    40 
     39  this->filename = modelFileName;
     40
     41  this->parentTagIndex = -1;
     42  this->parent = NULL;
     43
     44  this->animationState.currentFrame = 0;
     45  this->animationState.nextFrame = 1;
     46  this->animationState.interpolationFraction = 0.0f;
     47
     48  this->animation = NULL;
     49  this->bInterpolate = false;
     50  this->upperBound = 0;
    4151
    4252  this->loadModel(modelFileName);
    4353//   this->loadSkin(skinFileName);
     54  this->init();
    4455}
    4556
     
    5465  delete this->header;
    5566}
     67
     68
     69/**
     70 * init data
     71 */
     72void MD3Data::init()
     73{
     74  // create the temporary data to work with (interpolation data)
     75  this->tmpBoneFrame = new MD3BoneFrame();
     76
     77  this->tmpMesh = new sVec3D*[this->header->meshNum];
     78  for( int i = 0; i < this->header->meshNum; i++)
     79    this->tmpMesh[i] = new sVec3D[this->meshes[i]->header->vertexNum];
     80
     81  this->tmpNormal = new MD3Normal*[this->header->meshNum];
     82  for( int i = 0; i < this->header->meshNum; i++)
     83    this->tmpNormal[i] = new MD3Normal[this->meshes[i]->header->vertexNum];
     84
     85  // there are at most 4 different models and submodels
     86  this->tmpMatrix = new float*[4];
     87  for( int i = 0; i < 4; i++)
     88    this->tmpMatrix[i] = new float[16];
     89
     90}
     91
     92
     93/**
     94 * link a model at the specified tag position to this model. if the position is already
     95 * occupied, the old submodel will be replaced
     96 *
     97 *  @param tagIndex: tag to link the submodel to
     98 *  @param child: the submodel that should be linked to this model
     99 */
     100void MD3Data::addLinkedModel(int tagIndex, MD3Data* child)
     101{
     102  this->sortedMap[tagIndex] = child;
     103  child->parentTagIndex = tagIndex;
     104}
     105
     106
     107
     108/**
     109 * Return the index of the tag with the given name for this model.
     110 *
     111 * This will return -1 if their is no such tag.
     112 */
     113 int MD3Data::getTagIndexByName(std::string tagName)
     114{
     115   int res = -1;
     116
     117   if( this->header->boneFrameNum > 0) {
     118     MD3Tag** tags = this->boneFrames[0]->tags;
     119     for( int i = 0; i < this->header->tagNum; i++)
     120       if( tags[i]->name.find(tagName) == std::string::npos)
     121         return i;
     122   }
     123
     124   return res;
     125 }
    56126
    57127
     
    67137//  char* buffer;                           //buffer for frame data
    68138  int fileOffset = 0;                     // file data offset
    69 
    70139
    71140
     
    219288      fread(md, 1, sizeof(MD3TagData), pFile);
    220289      this->boneFrames[i]->tags[j]->data = md;
     290
     291      this->boneFrames[i]->tags[j]->name = std::string(this->boneFrames[i]->tags[j]->data->name);
     292      this->boneFrames[i]->tags[j]->position = Vector( this->boneFrames[i]->tags[j]->data->position[0],
     293                                                       this->boneFrames[i]->tags[j]->data->position[1],
     294                                                       this->boneFrames[i]->tags[j]->data->position[2]);
     295      for( int k = 0; k < 3; k++)
     296        for( int l = 0; l < 3; l++)
     297          this->boneFrames[i]->tags[j]->matrix[k][l] = this->boneFrames[i]->tags[j]->data->matrix[k][l];
     298
     299      //PRINTF(0)("Tag name: %s\n", this->boneFrames[i]->tags[j]->name.c_str());
    221300    }
    222301  }
     
    318397  fread(this->meshes[mesh]->triangles, 1, sizeof(MD3Triangle) * this->meshes[mesh]->header->triangleNum, pFile);
    319398
     399//   for( int i = 0; i < this->meshes[mesh]->header->triangleNum; i++) {
     400//     PRINTF(0)("Triangle read: %i, %i, %i\n", this->meshes[mesh]->triangles[i].vertexOffset[0], this->meshes[mesh]->triangles[i].vertexOffset[1]
     401//         , this->meshes[mesh]->triangles[i].vertexOffset[2]);
     402//   }
     403
    320404  return this->meshes[mesh]->header->triangleNum * sizeof(MD3Triangle);
    321405}
     
    337421  for( int i = 0; i < this->meshes[mesh]->header->textureNum; i++) {
    338422    PRINTF(0)(" texture file: %s\n", tex[i].fileName);
    339     this->meshes[mesh]->material[i].setDiffuseMap(tex[i].fileName);
     423#warning texture stuff hard coded. make this again
     424    std::string path("/home/boenzlip/tmp/q3/Downloads/MOH/q3mdl-alien3/");
     425    std::string path1(tex[i].fileName);
     426    std::string fullPath( path + path1);
     427    this->meshes[mesh]->material[i].setDiffuseMap(/*tex[i].fileName*/ /*fullPath.c_str()*/ "maps/creatures/gork/gorkup.tga");
    340428    this->meshes[mesh]->material[i].setAmbient(1, 1, 1);
    341429  }
     
    355443  fread(this->meshes[mesh]->texVecs, 1, sizeof(MD3TexVecs) * this->meshes[mesh]->header->vertexNum, pFile);
    356444
     445//   for( int i = 0; i < this->meshes[mesh]->header->vertexNum; i++)  {
     446//     PRINTF(0)("TexVec read: %f, %f\n", this->meshes[mesh]->texVecs[i].textureCoord[0], this->meshes[mesh]->texVecs[i].textureCoord[0]);
     447//   }
     448
    357449  return this->meshes[mesh]->header->vertexNum * sizeof(MD3TexVecs);
    358450}
     
    367459
    368460  // reserver memory for the vertex informations
    369   this->meshes[mesh]->meshFrames = new sVec3D[this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum];
    370   this->meshes[mesh]->normals = new MD3Normal[this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum];
    371 
    372   for( int i = 0; i < this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum; i++)
     461  this->meshes[mesh]->meshFrames = new sVec3D*[this->meshes[mesh]->header->meshFrameNum]; // * this->meshes[mesh]->header->vertexNum
     462  this->meshes[mesh]->normals = new MD3Normal*[this->meshes[mesh]->header->meshFrameNum];
     463
     464  for( int i = 0; i < this->meshes[mesh]->header->meshFrameNum; i++)
    373465  {
    374     // read out the compressed data
    375     MD3VertexCompressed* vc = new MD3VertexCompressed;
    376     fread(vc, 1, sizeof(MD3VertexCompressed), pFile);
    377 
    378     this->meshes[mesh]->meshFrames[i][0] = (float)vc->vector[0] / 64.0f;
    379     this->meshes[mesh]->meshFrames[i][1] = (float)vc->vector[1] / 64.0f;
    380     this->meshes[mesh]->meshFrames[i][2] = (float)vc->vector[2] / 64.0f;
    381 
    382     this->meshes[mesh]->normals[i].vertexNormal[0] = vc->vertexNormal[0];
    383     this->meshes[mesh]->normals[i].vertexNormal[1] = vc->vertexNormal[1];
    384 
    385     delete vc;
     466    this->meshes[mesh]->meshFrames[i] = new sVec3D[this->meshes[mesh]->header->vertexNum];
     467    this->meshes[mesh]->normals[i] = new MD3Normal[this->meshes[mesh]->header->vertexNum];
     468
     469    for( int j = 0; j < this->meshes[mesh]->header->vertexNum; j++)
     470    {
     471      // read out the compressed data
     472      MD3VertexCompressed* vc = new MD3VertexCompressed;
     473      fread(vc, 1, sizeof(MD3VertexCompressed), pFile);
     474
     475      this->meshes[mesh]->meshFrames[i][j][0] = (float)vc->vector[0] / 64.0f;
     476      this->meshes[mesh]->meshFrames[i][j][1] = (float)vc->vector[1] / 64.0f;
     477      this->meshes[mesh]->meshFrames[i][j][2] = (float)vc->vector[2] / 64.0f;
     478
     479      this->meshes[mesh]->normals[i][j].vertexNormal[0] = (int)vc->vertexNormal[0];
     480      this->meshes[mesh]->normals[i][j].vertexNormal[1] = (int)vc->vertexNormal[1];
     481
     482//     PRINTF(0)("nr: %i, meshframes: %f, %f, %f, normal: %f, %f\n", i, this->meshes[mesh]->meshFrames[i][0], this->meshes[mesh]->meshFrames[i][1],
     483//     this->meshes[mesh]->meshFrames[i][2], this->meshes[mesh]->normals[i].vertexNormal[0], this->meshes[mesh]->normals[i].vertexNormal[1]);
     484
     485      delete vc;
     486    }
    386487  }
    387488
    388   // delete the temp memory again
    389 //   delete vc;
    390 
    391489  return this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum * sizeof(MD3VertexCompressed);
    392490}
    393491
    394 }
    395 
    396 
    397 
    398 
    399 
    400 
    401 
     492
     493
     494}
     495
     496
     497
     498
     499
     500
     501
  • trunk/src/lib/graphics/importer/md3/md3_data.h

    r8490 r8724  
    2525#include "md_model_structure.h"
    2626
     27#include <map>
     28
    2729
    2830//! These are the needed defines for the max values when loading .MD2 files
    2931#define MD3_IDENT                       (('3'<<24) + ('P'<<16) + ('D'<<8) + 'I') //!< the md3 identifier tag in the bin file
    3032#define MD3_VERSION                     15                                       //!< the md2 version in the header
    31 #define MD2_MAX_TRIANGLES               4096                                     //!< maximal triangles count
    32 #define MD2_MAX_VERTICES                2048                                     //!< maximal vertices count
    33 #define MD2_MAX_TEXCOORDS               2048                                     //!< maximal tex coordinates
    34 #define MD2_MAX_FRAMES                  512                                      //!< maximal frames
    35 #define MD2_MAX_SKINS                   32                                       //!< maximal skins
    36 #define MD2_MAX_FRAMESIZE               (MD2_MAX_VERTICES * 4 + 128)             //!< maximal framesize
    3733
    38 #define NUM_VERTEX_NORMALS              162                                      //!< number of vertex normals
    39 #define SHADEDOT_QUANT                  16                                       //!< shade dot quantity - no idea what it is
    40 
    41 //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework
    42 #define kAnimationSpeed                 12.0f                                    //!< animation speed
    4334
    4435
     
    4839  class MD3BoneFrame;
    4940  class MD3Mesh;
     41  class MD3Normal;
     42  class MD3Animation;
    5043
    51 //! This holds the header information that is read in at the beginning of the file: id software definition
    52 struct MD3Header
    53 {
    54    int ident;                           //!< This is used to identify the file
    55    int version;                         //!< The version number of the file (Must be 8)
     44  //! This holds the header information that is read in at the beginning of the file: id software definition
     45  typedef struct MD3Header
     46  {
     47    int ident;                           //!< This is used to identify the file
     48    int version;                         //!< The version number of the file (Must be 8)
    5649
    57    char filename[68];                   //!< The filename of the model
     50    char filename[68];                   //!< The filename of the model
    5851
    59    int boneFrameNum;                    //!< number of frames
    60    int tagNum;                          //!< number of tags
    61    int meshNum;                         //!< number of mesh
    62    int maxTexNum;                       //!< number of texture
     52    int boneFrameNum;                    //!< number of frames
     53    int tagNum;                          //!< number of tags
     54    int meshNum;                         //!< number of mesh
     55    int maxTexNum;                       //!< number of texture
    6356
    64    int boneFrameStart;                  //!< start of bone frames
    65    int tagStart;                        //!< start of the tag
    66    int meshStart;                       //!< mesh start
     57    int boneFrameStart;                  //!< start of bone frames
     58    int tagStart;                        //!< start of the tag
     59    int meshStart;                       //!< mesh start
    6760
    68    int fileSize;                        //!< file size
    69 };
     61    int fileSize;                        //!< file size
     62  };
     63
     64  //!< holding the informations about the current animation state
     65  typedef struct MD3AnimationState
     66  {
     67    int     currentFrame;          //!< currently rendered animation key frame of this model. Every part has its own current frame!
     68    int     nextFrame;             //!< next animation key frame of the model.
     69    float   interpolationFraction; //!< interpolation position between currentFrame and nextFrame [0,1]
     70  };
    7071
    7172
     73  //! class to store the md2 data in
     74  class MD3Data : public BaseObject
     75  {
     76    public:
     77      MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale = 1.0f);
     78      virtual ~MD3Data();
    7279
    73 //! class to store the md2 data in
    74 class MD3Data : public BaseObject
    75 {
    76  public:
    77    MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale = 1.0f);
    78   virtual ~MD3Data();
     80      void addLinkedModel(int tagIndex, MD3Data* child);
     81      int getTagIndexByName(std::string name);
     82
     83    private:
     84      bool loadModel(const std::string& fileName);
     85      bool loadSkin(const std::string& fileName = "");
     86
     87      void init();
     88
     89      int readHeader(FILE* pFile, int fileOffset);
     90      int readBoneFrames(FILE* pFile, int fileOffset);
     91      int readTags(FILE* pFile, int fileOffset);
     92      int readMeshes(FILE* pFile, int fileOffset);
    7993
    8094
    81  private:
    82    bool loadModel(const std::string& fileName);
    83    bool loadSkin(const std::string& fileName = "");
    84 
    85    int readHeader(FILE* pFile, int fileOffset);
    86    int readBoneFrames(FILE* pFile, int fileOffset);
    87    int readTags(FILE* pFile, int fileOffset);
    88    int readMeshes(FILE* pFile, int fileOffset);
     95      int readMeshTriangles(FILE* pFile, int fileOffset, int mesh);
     96      int readMeshTextures(FILE* pFile, int fileOffset, int mesh);
     97      int readMeshTexVecs(FILE* pFile, int fileOffset, int mesh);
     98      int readMeshVertices(FILE* pFile, int fileOffset, int mesh);
    8999
    90100
    91    int readMeshTriangles(FILE* pFile, int fileOffset, int mesh);
    92    int readMeshTextures(FILE* pFile, int fileOffset, int mesh);
    93    int readMeshTexVecs(FILE* pFile, int fileOffset, int mesh);
    94    int readMeshVertices(FILE* pFile, int fileOffset, int mesh);
     101    public:
    95102
    96  public:
     103      MD3Header*                header;            //!< the header file
    97104
    98   MD3Header*          header;            //!< the header file
     105      std::map<int, MD3Data*>   sortedMap;         //!< map
     106      int                       parentTagIndex;    //!< the tag index of the parent model
     107      MD3Data*                  parent;            //!< the parent model
    99108
    100   std::string         filename;          //!< the name of the file as recorded in the .md3 file
    101   std::string         loadFilename;      //!< filename of the actual file from which data was loaded
     109      std::string               filename;          //!< the name of the file as recorded in the .md3 file
    102110
    103   int                 boneFrameNum;      //!< number of anumation key fames in the models
    104   int                 tagNum;            //!< number of tags
    105   int                 meshNum;           //!< number of meshes
     111      MD3AnimationState         animationState;    //!< the animation state of this model
    106112
    107   int                 maxTextureNum;     //!< maximum number of unique used in an md3 file
    108   int                 boneFrameStart;    //!< starting position of bone frame data structures
    109   int                 tagStart;          //!< starting position of tag-structures
     113      MD3BoneFrame**            boneFrames;        //!< array of bone frames, contains the metadata (bounding box, tags,...) for each frame
     114      MD3Mesh**                 meshes;            //!< array of meshes in the model. each containing the mesh for each of the animation
    110115
    111   int                 meshStart;         //!< starting position of mesh structures
    112 
    113   int                 fileSize;          //!< file size
     116      MD3BoneFrame*             tmpBoneFrame;      //!< a temporary bone frame
     117      sVec3D**                  tmpMesh;           //!< a temporary mesh frame
     118      MD3Normal**               tmpNormal;         //!< a temporary normals frame
     119      float**                   tmpMatrix;         //!< a temporary matrix
    114120
    115121
    116   MD3BoneFrame**      boneFrames;        //!< array of bone frames, contains the metadata (bounding box, tags,...) for each frame
    117   MD3Mesh**           meshes;            //!< array of meshes in the model. each containing the mesh for each of the animation
    118 
    119   int                 parentTagIndex;    //!< tag index
    120 
    121   //MD3Model*           parent;            //!< reference to the MD3Model
    122 
    123 
    124 };
     122     /* the animation part */
     123     MD3Animation*              animation;         //!< animation
     124     int                        op;                //!< the current operation
     125     int                        upperBound;        //!< the upper bound
     126     bool                       bInterpolate;      //!< interpolate the frames
     127  };
    125128
    126129
  • trunk/src/lib/graphics/importer/md3/md3_mesh.h

    r8490 r8724  
    7777    MD3Triangle*       triangles;               //!< indices into mesh frames and texture coord arrays
    7878    MD3TexVecs*        texVecs;                 //!< tex vecs coordinates
    79     sVec3D*            meshFrames;              //!< mesh frames
    80     MD3Normal*         normals;                 //!< 3d array of normals with spherical coordinates giving the dir of the vertex normal
     79    sVec3D**           meshFrames;              //!< mesh frames
     80    MD3Normal**        normals;                 //!< 3d array of normals with spherical coordinates giving the dir of the vertex normal
    8181
    8282};
  • trunk/src/lib/graphics/importer/md3/md3_model.cc

    r8490 r8724  
    1818
    1919#include "md3_data.h"
    20 
     20#include "md3_mesh.h"
     21#include "md3_tag.h"
     22#include "md3_bone_frame.h"
     23#include "md3_animation.h"
     24
     25#include "md3_animation_cfg.h"
     26
     27#include "material.h"
     28#include "quaternion.h"
     29
     30#include "loading/resource_manager.h"
     31
     32#include "debug.h"
    2133
    2234namespace md3
     
    2840  MD3Model::MD3Model(std::string filename, float scaling)
    2941  {
    30     this->md3Data = new MD3Data(filename, filename, scaling);
     42    this->autoAssemblePlayerModel(filename, scaling);
     43
     44    this->bDrawBones = false;
     45    this->bDrawNormals = false;
     46
     47    this->time = 0.0f;
     48
     49    // set the animation
     50    this->interpolate(this->md3Data, this->config->getAnimation("Dead 1"), MD3_ANIM_NEXT, true);
    3151  }
    3252
     
    3454
    3555  MD3Model::~MD3Model()
     56  {
     57//     delete this->tmpBoneFrame;
     58//     delete [] this->tmpMesh;
     59
     60    ///TODO deleting mesh
     61    ///TODO deleting matrices
     62  }
     63
     64
     65  /**
     66   * auto assemples a player model
     67   * @param filename is the name to the directory of the modelzzzzzz
     68   */
     69  void MD3Model::autoAssemblePlayerModel(std::string filename, float scaling)
     70  {
     71    // loading the config file
     72    std::string cfgName(filename + "/animation.cfg");
     73    this->config = (MD3AnimationCfg*)ResourceManager::getInstance()->load(cfgName, MD3_CONFIG, RP_GAME);
     74
     75    //first load the torso or the upper part
     76    std::string nameUpper(filename + "/upper.md3");
     77    if( (this->md3Data = (MD3Data*)ResourceManager::getInstance()->load(nameUpper, MD3, RP_GAME, nameUpper, scaling)) == NULL)
     78    {
     79      std::string nameTorso(filename + "/torso.md3");
     80      this->md3Data = (MD3Data*)ResourceManager::getInstance()->load(nameTorso, MD3, RP_GAME, nameTorso, scaling);
     81    }
     82
     83    if( this->md3Data == NULL)
     84    {
     85      PRINTF(1)("Problems loading the MD3Model. Abording\n");
     86      return;
     87    }
     88
     89    // load lower
     90    std::string nameLower(filename + "/lower.md3");
     91    MD3Data* lower = (MD3Data*)ResourceManager::getInstance()->load(nameLower, MD3, RP_GAME, nameLower, scaling);
     92    if( lower != NULL)
     93    {
     94      int tag = this->md3Data->getTagIndexByName("tag_lower");
     95      PRINTF(0)("Loaded the %s model on index %i\n", nameLower.c_str(), tag);
     96      if( tag >= 0)
     97       this->md3Data->addLinkedModel(tag, lower);
     98      else
     99        PRINTF(0)("Could not add %s\n", nameLower.c_str());
     100
     101    }
     102
     103
     104    // load head
     105    std::string nameHead(filename + "/head.md3");
     106    MD3Data* head = (MD3Data*)ResourceManager::getInstance()->load(nameHead, MD3, RP_GAME, nameLower, scaling);
     107    if( head != NULL)
     108    {
     109      int tag = this->md3Data->getTagIndexByName("tag_head");
     110      PRINTF(0)("Loaded the %s model on index %i\n", nameHead.c_str(), tag);
     111      if( tag >= 0)
     112        this->md3Data->addLinkedModel(tag, head);
     113      else
     114        PRINTF(0)("Could not add %s\n", nameHead.c_str());
     115    }
     116
     117  }
     118
     119
     120
     121  /**
     122   * tick float
     123   * @param time: time elapsed
     124   */
     125  void MD3Model::tick(float time)
     126  {
     127    if(this->md3Data == NULL)
     128      return;
     129
     130    this->visit(this->md3Data, time);
     131
     132    this->tick(time, this->md3Data);
     133  }
     134
     135
     136  /**
     137   * tick each data
     138   */
     139  void MD3Model::tick(float time, MD3Data* data)
     140  {
     141    // draw the bones if needed#
     142    if( this->bDrawBones)
     143    {
     144      // get bone frame, interpolate if necessary
     145      if( data->animationState.interpolationFraction != 0.0 &&
     146          data->animationState.currentFrame != data->animationState.nextFrame)
     147      {
     148        //interpolate bone frame
     149        data->tmpBoneFrame = this->interpolateBoneFrame(data, data->boneFrames[data->animationState.currentFrame],
     150                                                        data->boneFrames[data->animationState.nextFrame],
     151                                                        data->animationState.interpolationFraction);
     152      }
     153      else
     154      {
     155        data->tmpBoneFrame = data->boneFrames[data->animationState.currentFrame];
     156      }
     157    }
     158
     159
     160    //draw all meshes of current frame of this model
     161    for( int i = 0;  i < data->header->meshNum; i++)
     162    {
     163      MD3Mesh* mesh = data->meshes[i];
     164
     165      //interpolate mesh frame between the 2 current mesh frames
     166      if( data->header->boneFrameNum > 1)
     167        this->interpolateMeshFrame( data,
     168                                    mesh->meshFrames[data->animationState.currentFrame],
     169                                    mesh->meshFrames[data->animationState.nextFrame],
     170                                    data->animationState.interpolationFraction, mesh, i);
     171      else
     172        this->interpolateMeshFrame( data,
     173                                    mesh->meshFrames[data->animationState.currentFrame],
     174                                    mesh->meshFrames[data->animationState.currentFrame],
     175                                    0.0f, mesh, i);
     176
     177      // draw vertex normals if needed
     178      if( this->bDrawNormals)
     179      {
     180        // get vertex normals, interpolate if necessary
     181        if( data->animationState.interpolationFraction != 0.0 &&
     182            data->animationState.currentFrame != data->animationState.nextFrame)
     183        {
     184          //interpolate vertex normals
     185          this->interpolateVertexNormals(data,
     186                                         mesh->normals[data->animationState.currentFrame],
     187                                         mesh->normals[data->animationState.nextFrame],
     188                                         data->animationState.interpolationFraction, mesh, i);
     189        }
     190      }
     191    }
     192
     193
     194    // draw all models linked to this model
     195    std::map<int, MD3Data*>::iterator it = data->sortedMap.begin();
     196    int i = 0;
     197    while( it != data->sortedMap.end())
     198    {
     199      MD3Data* child = it->second;
     200
     201      //build transformation array m from matrix, interpolate if necessary
     202
     203      MD3Tag* currFrameTag = data->boneFrames[data->animationState.currentFrame]->tags[child->parentTagIndex];
     204
     205      if( data->animationState.interpolationFraction != 0.0 &&
     206          data->animationState.currentFrame != data->animationState.nextFrame)
     207      {
     208        //we need to interpolate
     209        MD3Tag* nextFrameTag = data->boneFrames[data->animationState.nextFrame]->tags[child->parentTagIndex];
     210        this->interpolateTransformation(child, currFrameTag, nextFrameTag, data->animationState.interpolationFraction, i);
     211      }
     212      else
     213      {
     214        //no interpolation needed, stay with last transformation
     215        //OpenGL matrix is in column-major order
     216        data->tmpMatrix[i][0] = currFrameTag->matrix[0][0];
     217        data->tmpMatrix[i][1] = currFrameTag->matrix[1][0];
     218        data->tmpMatrix[i][2] = currFrameTag->matrix[2][0];
     219        data->tmpMatrix[i][3] = 0.0f;
     220        data->tmpMatrix[i][4] = currFrameTag->matrix[0][1];
     221        data->tmpMatrix[i][5] = currFrameTag->matrix[1][1];
     222        data->tmpMatrix[i][6] = currFrameTag->matrix[2][1];
     223        data->tmpMatrix[i][7] = 0.0f;
     224        data->tmpMatrix[i][8] = currFrameTag->matrix[0][2];
     225        data->tmpMatrix[i][9] = currFrameTag->matrix[1][2];
     226        data->tmpMatrix[i][10]= currFrameTag->matrix[2][2];
     227        data->tmpMatrix[i][11]= 0.0f;
     228        data->tmpMatrix[i][12] = currFrameTag->position.x;
     229        data->tmpMatrix[i][13] = currFrameTag->position.y;
     230        data->tmpMatrix[i][14] = currFrameTag->position.z;
     231        data->tmpMatrix[i][15] = 1.0f;
     232      }
     233
     234      // switch to child coord system
     235
     236      // and tick child
     237      this->tick(time, child);
     238
     239      i++;
     240      it++;
     241    }
     242  }
     243
     244
     245  /**
     246   * this draws the md3 model
     247   */
     248  void MD3Model::draw() const
     249  {
     250    //draw current bone frame
     251    this->draw(this->md3Data);
     252  }
     253
     254
     255  /**
     256   * draw the md3model
     257   * @param data: the data to be drawn
     258   */
     259  void MD3Model::draw(MD3Data* data) const
     260  {
     261
     262    // draw the bones if needed
     263    if( this->bDrawBones)
     264    {
     265      // get bone frame, interpolate if necessary
     266      if( data->animationState.interpolationFraction != 0.0 &&
     267          data->animationState.currentFrame != data->animationState.nextFrame) {
     268        //interpolate bone frame
     269        this->drawBoneFrame(data->tmpBoneFrame);
     270      }
     271      else {
     272        //stick with current bone frame
     273        this->drawBoneFrame(data->boneFrames[data->animationState.currentFrame]);
     274      }
     275    }
     276
     277
     278    //draw all meshes of current frame of this model
     279    for( int i = 0;  i < data->header->meshNum; i++)
     280    {
     281      MD3Mesh* mesh = data->meshes[i];
     282      sVec3D* frame = data->tmpMesh[i];
     283
     284      if( mesh->header->textureNum > 0 && &mesh->material[0] != NULL)
     285        mesh->material[0].select();
     286
     287      this->drawMesh(mesh, frame);
     288
     289      // draw vertex normals if needed
     290      if( this->bDrawNormals)
     291      {
     292        // get vertex normals, interpolate if necessary
     293        if( data->animationState.interpolationFraction != 0.0 &&
     294            data->animationState.currentFrame != data->animationState.nextFrame)
     295        {
     296          //interpolate vertex normals
     297          this->drawVertexNormals(frame, data->tmpNormal[i]);
     298        }
     299        else {
     300          //stick with current vertex normals
     301          this->drawVertexNormals(frame, mesh->normals[data->animationState.currentFrame]);
     302        }
     303      }
     304    }
     305
     306
     307    // draw all models linked to this model
     308    int i = 0;
     309    std::map<int, MD3Data*>::iterator it = data->sortedMap.begin();
     310    while( it != data->sortedMap.end())
     311    {
     312      MD3Data* child = it->second;
     313
     314      //switch to child coord system
     315      glPushMatrix();
     316      glMultMatrixf(data->tmpMatrix[i]);
     317
     318      // and draw child
     319      this->draw(child);
     320
     321      glPopMatrix();
     322
     323      i++;
     324      it++;
     325    }
     326
     327  }
     328
     329
     330  /**
     331   * draws the mesh
     332   */
     333  void MD3Model::drawMesh(MD3Mesh* mesh, sVec3D* frame) const
     334  {
     335    Vector tmpVec1, tmpVec2;
     336
     337    glColor3f(1.0f, 1.0f, 1.0f);
     338    glBegin( GL_TRIANGLES);
     339
     340    // upload all triangles in the frame to OpenGL
     341    for( int t = 0; t < mesh->header->triangleNum; t++)
     342    {
     343      // calc normal vector
     344      tmpVec1.x = frame[mesh->triangles[t].vertexOffset[1]][0] - frame[mesh->triangles[t].vertexOffset[0]][0];
     345      tmpVec1.y = frame[mesh->triangles[t].vertexOffset[1]][1] - frame[mesh->triangles[t].vertexOffset[0]][1];
     346      tmpVec1.z = frame[mesh->triangles[t].vertexOffset[1]][2] - frame[mesh->triangles[t].vertexOffset[0]][2];
     347
     348      tmpVec2.x = frame[mesh->triangles[t].vertexOffset[2]][0] - frame[mesh->triangles[t].vertexOffset[0]][0];
     349      tmpVec2.y = frame[mesh->triangles[t].vertexOffset[2]][1] - frame[mesh->triangles[t].vertexOffset[0]][1];
     350      tmpVec2.z = frame[mesh->triangles[t].vertexOffset[2]][2] - frame[mesh->triangles[t].vertexOffset[0]][2];
     351
     352      Vector normal = tmpVec1.cross(tmpVec2);
     353      normal.normalize();
     354
     355//       PRINTF(0)("normal: %f, %f, %f\n", normal.x, normal.y, normal.z);
     356
     357      glNormal3f(normal.x, normal.y, normal.z);
     358      glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[0]].textureCoord);
     359      glVertex3f( frame[mesh->triangles[t].vertexOffset[0]][0],
     360                  frame[mesh->triangles[t].vertexOffset[0]][2],
     361                  frame[mesh->triangles[t].vertexOffset[0]][1]);
     362
     363      glNormal3f(normal.x, normal.y, normal.z);
     364      glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[1]].textureCoord);
     365      glVertex3f( frame[mesh->triangles[t].vertexOffset[1]][0],
     366                  frame[mesh->triangles[t].vertexOffset[1]][2],
     367                  frame[mesh->triangles[t].vertexOffset[1]][1]);
     368
     369      glNormal3f(normal.x, normal.y, normal.z);
     370      glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[2]].textureCoord);
     371      glVertex3f( frame[mesh->triangles[t].vertexOffset[2]][0],
     372                  frame[mesh->triangles[t].vertexOffset[2]][2],
     373                  frame[mesh->triangles[t].vertexOffset[2]][1]);
     374    }
     375    glEnd();
     376  }
     377
     378
     379  /**
     380   *  drawo vertex normals
     381   */
     382  void MD3Model::drawVertexNormals(sVec3D* frame, MD3Normal* normals) const
    36383  {}
    37384
    38385
     386  /**
     387   * draw bone frame
     388   */
     389  void MD3Model::drawBoneFrame(MD3BoneFrame* frame) const
     390  {
     391    float x1 = frame->mins.x;
     392    float y1 = frame->mins.y;
     393    float z1 = frame->mins.z;
     394    float x2 = frame->maxs.x;
     395    float y2 = frame->maxs.y;
     396    float z2 = frame->maxs.z;
     397
     398    glPushAttrib(GL_TEXTURE_2D);
     399    glPushAttrib(GL_LIGHTING);
     400
     401    glColor3f(1.0f,0.0f,0.0f);
     402    glPointSize(6.0f);
     403
     404    glBegin(GL_POINTS);
     405    glVertex3f(frame->position.x, frame->position.y, frame->position.z);
     406    glEnd();
     407    glPointSize(1.0f);
     408
     409    glColor3f(0.0f,1.0f,0.0f);
     410    glBegin(GL_LINE_LOOP);
     411    glVertex3f(x1,y1,z1);
     412    glVertex3f(x1,y1,z2);
     413    glVertex3f(x1,y2,z2);
     414    glVertex3f(x1,y2,z1);
     415    glEnd();
     416
     417    glBegin(GL_LINE_LOOP);
     418    glVertex3f(x2,y2,z2);
     419    glVertex3f(x2,y1,z2);
     420    glVertex3f(x2,y1,z1);
     421    glVertex3f(x2,y2,z1);
     422    glEnd();
     423
     424    glBegin(GL_LINES);
     425    glVertex3f(x1,y1,z1);
     426    glVertex3f(x2,y1,z1);
     427
     428    glVertex3f(x1,y1,z2);
     429    glVertex3f(x2,y1,z2);
     430
     431    glVertex3f(x1,y2,z2);
     432    glVertex3f(x2,y2,z2);
     433
     434    glVertex3f(x1,y2,z1);
     435    glVertex3f(x2,y2,z1);
     436    glEnd();
     437
     438     glPopAttrib();
     439     glPopAttrib();
     440  }
     441
     442
     443  /**
     444   *  interpolate bone frame
     445   * @param currBoneFrame Start bone frame.
     446   * @param nextBoneFrame End bone frame.
     447   * @param frac Interpolation fraction, in [0,1].
     448   */
     449  MD3BoneFrame* MD3Model::interpolateBoneFrame(MD3Data* data, MD3BoneFrame* currBoneFrame, MD3BoneFrame* nextBoneFrame, float frac)
     450  {
     451    data->tmpBoneFrame->mins.x      = (1.0f - frac) * currBoneFrame->mins.x       + frac * nextBoneFrame->mins.x;
     452    data->tmpBoneFrame->maxs.x      = (1.0f - frac) * currBoneFrame->maxs.x       + frac * nextBoneFrame->maxs.x;
     453    data->tmpBoneFrame->position.x  = (1.0f - frac) * currBoneFrame->position.x   + frac * nextBoneFrame->position.x;
     454    data->tmpBoneFrame->mins.y      = (1.0f - frac) * currBoneFrame->mins.y       + frac * nextBoneFrame->mins.y;
     455    data->tmpBoneFrame->maxs.y      = (1.0f - frac) * currBoneFrame->maxs.y       + frac * nextBoneFrame->maxs.y;
     456    data->tmpBoneFrame->position.y  = (1.0f - frac) * currBoneFrame->position.y   + frac * nextBoneFrame->position.y;
     457    data->tmpBoneFrame->mins.z      = (1.0f - frac) * currBoneFrame->mins.z       + frac * nextBoneFrame->mins.z;
     458    data->tmpBoneFrame->maxs.z      = (1.0f - frac) * currBoneFrame->maxs.z       + frac * nextBoneFrame->maxs.z;
     459    data->tmpBoneFrame->position.z  = (1.0f - frac) * currBoneFrame->position.z   + frac * nextBoneFrame->position.z;
     460
     461    return data->tmpBoneFrame;
     462  }
     463
     464
     465
     466  /**
     467   * interpolate mesh frame
     468   */
     469  sVec3D* MD3Model::interpolateMeshFrame(MD3Data* data, sVec3D* currMeshFrame, sVec3D* nextMeshFrame, float frac, MD3Mesh* mesh, int i)
     470  {
     471    int vertexNum = mesh->header->vertexNum;
     472
     473    if( /*frac == 0.0f*/ true)
     474    {
     475      // just copy the vertices
     476      for( int t = 0; t < vertexNum; t++)
     477      {
     478        data->tmpMesh[i][t][0]  = currMeshFrame[t][0];
     479        data->tmpMesh[i][t][1]  = currMeshFrame[t][1];
     480        data->tmpMesh[i][t][2]  = currMeshFrame[t][2];
     481      }
     482    }
     483    else
     484    {
     485      // calc interpolated vertices
     486      for( int t = 0; t < vertexNum; t++)
     487      {
     488        data->tmpMesh[i][t][0]  = (1.0f - frac)   * currMeshFrame[t][0]  + frac * nextMeshFrame[t][0];
     489        data->tmpMesh[i][t][1]  = (1.0f - frac)   * currMeshFrame[t][1]  + frac * nextMeshFrame[t][1];
     490        data->tmpMesh[i][t][2]  = (1.0f - frac)   * currMeshFrame[t][2]  + frac * nextMeshFrame[t][2];
     491      }
     492    }
     493
     494    return data->tmpMesh[i];
     495  }
     496
     497
     498  /**
     499   * interpolate vertex normal
     500   */
     501  MD3Normal* MD3Model::interpolateVertexNormals(MD3Data* data, MD3Normal* currNormals, MD3Normal* nextNormals, float frac, MD3Mesh* mesh, int i)
     502  {
     503    for( int j = 0; j < mesh->header->vertexNum; j++)
     504    {
     505      data->tmpNormal[i][j].vertexNormal[0] = (int)((1.0f - frac) * currNormals[j].vertexNormal[0] + frac * nextNormals[j].vertexNormal[0]);
     506      data->tmpNormal[i][j].vertexNormal[1] = (int)((1.0f - frac) * currNormals[j].vertexNormal[1] + frac * nextNormals[j].vertexNormal[1]);
     507    }
     508
     509    return data->tmpNormal[i];
     510  }
     511
     512
     513  /**
     514   * interpolate transformation
     515   */
     516  float* MD3Model::interpolateTransformation(MD3Data* data, MD3Tag* currFrameTag, MD3Tag* nextFrameTag, float frac, int i)
     517  {
     518    // interpolate position
     519    Vector interpolatedPosition = currFrameTag->position * (1.0f - frac) + nextFrameTag->position * frac;
     520
     521
     522    // interpolate rotation matrix
     523    float  currRot[4][4];
     524    float  nextRot[4][4];
     525    float  interpolatedMatrix[4][4];
     526
     527    Quaternion currQuat(currFrameTag->matrix); currQuat.matrix(currRot);
     528    Quaternion nextQuat(nextFrameTag->matrix); nextQuat.matrix(nextRot);
     529
     530    Quaternion interpolatedQuat = Quaternion::quatSlerp(currQuat, nextQuat, frac); interpolatedQuat.matrix(interpolatedMatrix);
     531
     532    // quaternion code is column based, so use transposed matrix when spitting out to gl
     533    data->tmpMatrix[i][0] = interpolatedMatrix[0][0];
     534    data->tmpMatrix[i][4] = interpolatedMatrix[1][0];
     535    data->tmpMatrix[i][8] = interpolatedMatrix[2][0];
     536    data->tmpMatrix[i][12] = interpolatedPosition.x;
     537    data->tmpMatrix[i][1] = interpolatedMatrix[0][1];
     538    data->tmpMatrix[i][5] = interpolatedMatrix[1][1];
     539    data->tmpMatrix[i][9] = interpolatedMatrix[2][1];
     540    data->tmpMatrix[i][13] = interpolatedPosition.y;
     541    data->tmpMatrix[i][2] = interpolatedMatrix[0][2];
     542    data->tmpMatrix[i][6] = interpolatedMatrix[1][2];
     543    data->tmpMatrix[i][10]= interpolatedMatrix[2][2];
     544    data->tmpMatrix[i][14] = interpolatedPosition.z;
     545    data->tmpMatrix[i][3] = 0.0f;
     546    data->tmpMatrix[i][7] = 0.0f;
     547    data->tmpMatrix[i][11]= 0.0f;
     548    data->tmpMatrix[i][15] = 1.0f;
     549
     550    return data->tmpMatrix[i];
     551
     552  }
     553
     554
     555
     556  /**
     557   * visit the model
     558   */
     559  void MD3Model::visit(MD3Data* data, float time)
     560  {
     561    if ( (data->filename.find("lower") == std::string::npos &&
     562          (data->animation->type == LEGS || data->animation->type == BOTH)) // this is the LEGS model and the animation is applicable
     563          ||
     564          (data->filename.find("upper") == std::string::npos &&
     565          (data->animation->type == TORSO || data->animation->type == BOTH)) // this is the TORSO model and the animation is applicable
     566          ||
     567          data->animation->type == ALL // the animation is allways applicable
     568       )
     569      this->doOp(data, time);
     570
     571    // visit children
     572//     std::map<int, MD3Data*>::iterator it = data->sortedMap.begin();
     573//     while( it != data->sortedMap.end())
     574//     {
     575//       this->visit(it->second);
     576//       it++;
     577//     }
     578  }
     579
     580
     581  /**
     582   * Create a new visitor to apply an animation operation (NEXT, REWIND, ...)
     583   * to a MD3 model. The operation is executed in the context of the specified
     584   * animation.
     585   *
     586   * @param anim The animation that provides the context for the operation.
     587   * @param op The operation to apply.
     588   * @param interpolate Should interpolation be done?
     589   */
     590  void MD3Model::interpolate(MD3Data* data, MD3Animation* anim, int op, bool bInterpolate)
     591  {
     592     data->animation = anim;
     593     if( op == MD3_ANIM_NEXT || op == MD3_ANIM_PREVIOUS || op == MD3_ANIM_REWIND)
     594      data->op = op;
     595
     596     data->bInterpolate = bInterpolate;
     597  }
     598
     599
     600  /**
     601   * calc next frame number
     602   */
     603  int MD3Model::next(MD3Data* data, int nr)
     604  {
     605    if( nr < (data->upperBound - 1))
     606      return nr + 1;
     607    else
     608    { //rewind needed
     609      if( data->animation->numFrames < 0)
     610        return data->animation->first;
     611      else {
     612        nr = (data->animation->numLoopFrames != 0)?(data->animation->numFrames - data->animation->numLoopFrames):0;
     613        return data->animation->first + nr;
     614      }
     615    }
     616  }
     617
     618
     619  /**
     620   * calc prev frame number
     621   */
     622  int MD3Model::prev(MD3Data* data, int nr)
     623  {
     624    if( nr == data->animation->first)
     625      return data->upperBound - 1;
     626    else
     627      return nr - 1;
     628  }
     629
     630
     631  /**
     632   * apply the specified operation to the animation state data members of the model
     633   * taking the specified animation into account
     634   *
     635   * @param data: the data of the model
     636   */
     637  void MD3Model::doOp(MD3Data* data, float time)
     638  {
     639    // animation to be applied could have illegal data with respect to this model,
     640    // ignore anim in this case
     641
     642    if( data->animation->first >= data->animation->numFrames || data->animation->first < 0)
     643    {
     644      PRINTF(0)("MD3: this animation type seems to be invalid, no animation calculated\n");
     645      return;
     646    }
     647
     648
     649    //calc upper bound for animation frames in this model
     650    if( data->animation->numFrames < 0)
     651      data->upperBound = data->header->boneFrameNum; //use all available frames
     652    else
     653    {
     654      if( data->header->boneFrameNum < (data->animation->first + data->animation->numFrames))
     655        data->upperBound = data->header->boneFrameNum;
     656      else
     657        data->upperBound = (data->animation->first + data->animation->numFrames);
     658    }
     659
     660
     661    switch( data->op) {
     662
     663      case MD3_ANIM_NEXT:
     664        if( data->bInterpolate)
     665        {
     666          // keyframe interpolation animation
     667          data->animationState.interpolationFraction += time * data->animation->fps;
     668
     669          if( data->animationState.interpolationFraction >= 1.0f)
     670          {
     671            data->animationState.currentFrame = data->animationState.nextFrame;
     672            data->animationState.nextFrame = next(data, data->animationState.nextFrame);
     673            data->animationState.interpolationFraction = 0.0f;
     674          }
     675        }
     676        else
     677        {
     678          // only keyframe animation
     679          this->time += time * data->animation->fps;
     680          if( this->time > 1.0f)
     681          {
     682            data->animationState.currentFrame = data->animationState.nextFrame;
     683            data->animationState.nextFrame = next(data, data->animationState.nextFrame);
     684            this->time = 0.0f;
     685          }
     686        }
     687        break;
     688
     689      case MD3_ANIM_PREVIOUS:
     690        if( data->bInterpolate)
     691        {
     692          data->animationState.interpolationFraction -= time / data->animation->fps;
     693          if( data->animationState.interpolationFraction < 0.0f)
     694          {
     695            data->animationState.nextFrame = data->animationState.currentFrame;
     696            data->animationState.currentFrame = prev(data, data->animationState.currentFrame);
     697            data->animationState.interpolationFraction = 0.8f;
     698          }
     699        }
     700        else
     701        {
     702          data->animationState.nextFrame = data->animationState.currentFrame;
     703          data->animationState.currentFrame = prev(data, data->animationState.currentFrame);
     704        }
     705        break;
     706
     707      case MD3_ANIM_REWIND:
     708        data->animationState.currentFrame = data->animation->first;
     709        data->animationState.nextFrame = next(data, data->animationState.currentFrame);
     710        data->animationState.interpolationFraction = 0.0f;
     711        break;
     712    }
     713
     714  }
     715
     716
    39717}
  • trunk/src/lib/graphics/importer/md3/md3_model.h

    r8490 r8724  
    1313
    1414
     15#define FRACTION 0.34f
     16
     17
    1518namespace md3
    1619{
     
    1821
    1922class MD3Data;
     23class MD3BoneFrame;
     24class MD3Mesh;
     25class MD3Tag;
     26class MD3AnimationCfg;
     27class MD3Animation;
     28
     29struct MD3Normal;
     30
     31
     32typedef enum MD3FrameHandling {
     33  MD3_ANIM_NEXT = 0,          //!< swith to the next frame
     34  MD3_ANIM_PREVIOUS,          //!< swith to the previous frame
     35  MD3_ANIM_REWIND             //!< rewind the animation
     36};
     37
    2038
    2139class MD3Model : public InteractiveModel
     
    2644    ~MD3Model();
    2745
    28     virtual void tick(float dt) {}
    2946    virtual void setAnimation(int animNum, int playbackMode = 0) {}
     47    virtual int getAnimation() { return 0;}
     48
     49    virtual void tick(float dt);
     50    virtual void draw() const;
     51
     52
     53  private:
     54
     55    void autoAssemblePlayerModel(std::string filename, float scaling);
     56
     57    void draw(MD3Data* data) const;
     58    void tick(float dt, MD3Data* data);
     59
     60
     61    void drawMesh(MD3Mesh* mesh, sVec3D* frame) const;
     62    void drawVertexNormals(sVec3D* frame, MD3Normal* normals) const;
     63    void drawBoneFrame(MD3BoneFrame* frame) const;
     64
     65
     66    MD3BoneFrame* interpolateBoneFrame(MD3Data* data, MD3BoneFrame* currBoneFrame, MD3BoneFrame* nextBoneFrame, float frac);
     67    sVec3D* interpolateMeshFrame(MD3Data* data, sVec3D* currMeshFrame, sVec3D* nextMeshFrame, float frac, MD3Mesh* mesh, int i);
     68    MD3Normal* interpolateVertexNormals(MD3Data* data, MD3Normal* curNormals, MD3Normal* nextNormals, float frac, MD3Mesh* mesh, int i);
     69    float* interpolateTransformation(MD3Data* data, MD3Tag* currFrameTag, MD3Tag* nextFrameTag, float frac, int i);
     70
     71
     72    void interpolate(MD3Data* data, MD3Animation* anim, int op, bool bInterpolate);
     73
     74    void visit(MD3Data* data, float time);
     75    int next(MD3Data* data, int nr);
     76    int prev(MD3Data* data, int nr);
     77    void doOp(MD3Data* data, float time);
     78
    3079
    3180  private:
    3281    MD3Data*            md3Data;           //!< reference to the md3 model data
     82
     83    bool                bDrawBones;        //!< draws the bone frames too
     84    bool                bDrawNormals;      //!< draw the normals
     85
     86    MD3AnimationCfg*    config;            //!< the config file parsed
     87
     88    float               time;
     89
    3390};
     91
    3492
    3593}
Note: See TracChangeset for help on using the changeset viewer.