Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4172 in orxonox.OLD for orxonox/branches/md2_loader


Ignore:
Timestamp:
May 13, 2005, 3:40:23 PM (20 years ago)
Author:
patrick
Message:

orxonox/branches/md2_loader: reimplemente the whole md2_loader to make it more performant

Location:
orxonox/branches/md2_loader
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/md2_loader/ChangeLog

    r3995 r4172  
     12005-05-10      Benjamin Grauer <bensch@orxonox.ethz.ch>
     2        Merged the gui into the executable
     3        Windows compiles again.
     4
    152005-04-27      orxonox
    26        Tagged Version 0.2.3-pre-alpha
  • orxonox/branches/md2_loader/src/defs/debug.h

    r4139 r4172  
    5959#define DEBUG_MODULE_PNODE              0
    6060#define DEBUG_MODULE_WORLD_ENTITY       0
    61 #define DEBUG_MODULE_COMMAND_NODE       4
     61#define DEBUG_MODULE_COMMAND_NODE       0
    6262#define DEBUG_MODULE_GRAPHICS           0
    63 #define DEBUG_MODULE_LOAD               2
     63#define DEBUG_MODULE_LOAD               0
    6464
    6565#define DEBUG_MODULE_IMPORTER           2
  • orxonox/branches/md2_loader/src/lib/graphics/importer/abstract_model.h

    r4159 r4172  
    3030using namespace std;
    3131
    32 //template<class T> class tList;
    33 ;
     32
     33
     34//! this is a small and performant 3D vector
     35typedef float sVec3D[3];
     36
     37//! small and performant 2D vector
     38typedef float sVec2D[2];
     39
     40//! compressed vertex data: char insetead of float, the value will be expanded by the scale value. only for loading
     41typedef struct
     42{
     43  byte v[3];
     44  unsigned char lightNormalIndex;
     45} sVertex;
     46
     47//! compressed texture offset data: coords scaled by the texture size. Only for loading
     48typedef struct
     49{
     50  short s,t;
     51} sTexCoor;
     52
     53
     54//! holds tha informations about a md2 frame
     55typedef struct
     56{
     57  sVec3D scale;                        //!< scales values of the model
     58  sVec3D translate;                    //!< translates the model
     59  char name[16];                       //!< frame name: something like "run32"
     60  sVertex pVertices[1];                //!< first vertex of thes frame
     61} sFrame;
     62
     63
     64//! holds the information about a triangle
     65typedef struct
     66{
     67  short indexToVertices[3];             //!< index to the verteces of the triangle
     68  short indexToTexCoor[3];              //!< index to the texture coordinates
     69} sTriangle;
     70
     71
     72//! a md2 animation definition
     73typedef struct
     74{
     75  int firstFrame;                       //!< first frame of the animation
     76  int lastFrame;                        //!< last frame of the animation
     77  int fps;                              //!< speed: number of frames per second
     78} sAnim;
     79
     80//! animation state definition
     81typedef struct
     82{
     83  int startFrame;
     84  int endFrame;
     85  int fps;
     86
     87  float localTime;
     88  float lastTime;
     89  float interpolationState;            //!< the state of the animation [0..1]
     90 
     91  int type;                            //!< animation type
     92
     93  int currentFrame;
     94  int nextFrame;
     95} sAnimState;
     96
    3497
    3598//! This is our 3D point class.  CONFLICTING with Vector.cc
    3699class CVector3
    37100{
    38 public:
    39         float x, y, z;
     101 public:
     102  float x, y, z;
    40103};
    41104
     
    43106class CVector2
    44107{
    45 public:
    46         float x, y;
     108 public:
     109  float x, y;
    47110};
    48111
     
    50113struct tFace
    51114{
    52         int vertIndex[3];                       // indicies for the verts that make up this triangle
    53         int coordIndex[3];                      // indicies for the tex coords to texture this face
     115  int vertIndex[3];                     // indicies for the verts that make up this triangle
     116  int coordIndex[3];                    // indicies for the tex coords to texture this face
    54117};
    55118
     
    57120struct tMaterialInfo
    58121{
    59         char  strName[255];                     // The texture name
    60         char  strFile[255];                     // The texture file name (If this is set it's a texture map)
    61         byte  color[3]; // The color of the object (R, G, B)
    62         int   texureId;                         // the texture ID
    63         float uTile;                            // u tiling of texture 
    64         float vTile;                            // v tiling of texture 
    65         float uOffset;                      // u offset of texture
    66         float vOffset;                          // v offset of texture
     122  char  strName[255];                   // The texture name
     123  char  strFile[255];                   // The texture file name (If this is set it's a texture map)
     124  byte  color[3];       // The color of the object (R, G, B)
     125  int   texureId;                               // the texture ID
     126  float uTile;                          // u tiling of texture 
     127  float vTile;                          // v tiling of texture 
     128  float uOffset;                                // u offset of texture
     129  float vOffset;                                // v offset of texture
    67130} ;
    68131
     
    70133struct t3DObject
    71134{
    72         int  numOfVerts;                        // The number of verts in the model
    73         int  numOfFaces;                        // The number of faces in the model
    74         int  numTexVertex;                      // The number of texture coordinates
    75         int  materialID;                        // The texture ID to use, which is the index into our texture array
    76         bool bHasTexture;                       // This is TRUE if there is a texture map for this object
    77         char strName[255];                      // The name of the object
    78         CVector3  *pVerts;                      // The object's vertices
    79         CVector3  *pNormals;                    // The object's normals
    80         CVector2  *pTexVerts;                   // The texture's UV coordinates
    81         tFace *pFaces;                          // The faces information of the object
     135  int  numOfVerts;                      // The number of verts in the model
     136  int  numOfFaces;                      // The number of faces in the model
     137  int  numTexVertex;                    // The number of texture coordinates
     138  int  materialID;                      // The texture ID to use, which is the index into our texture array
     139  bool bHasTexture;                     // This is TRUE if there is a texture map for this object
     140  char strName[255];                    // The name of the object
     141  CVector3  *pVerts;                    // The object's vertices
     142  CVector3  *pNormals;                  // The object's normals
     143  CVector2  *pTexVerts;                 // The texture's UV coordinates
     144  tFace *pFaces;                                // The faces information of the object
    82145};
    83146
     
    85148struct tAnimationInfo
    86149{
    87         char strName[255];                      // This stores the name of the animation (Jump, Pain, etc..)
    88         int startFrame;                         // This stores the first frame number for this animation
    89         int endFrame;                           // This stores the last frame number for this animation
     150  char strName[255];                    // This stores the name of the animation (Jump, Pain, etc..)
     151  int startFrame;                               // This stores the first frame number for this animation
     152  int endFrame;                         // This stores the last frame number for this animation
    90153};
    91154
     
    93156struct t3DModel
    94157{
    95         int numOfObjects;                                       // The number of objects in the model
    96         int numOfMaterials;                                     // The number of materials for the model
    97         int numOfAnimations;                            // The number of animations in this model (NEW)
    98         int currentAnim;                                        // The current index into pAnimations list (NEW)
    99         int currentFrame;                                       // The current frame of the current animation (NEW)
    100         vector<tAnimationInfo> animationList; // The list of animations (NEW)
    101         vector<tMaterialInfo> materialList;     // The list of material information (Textures and colors)
    102         vector<t3DObject> objectList;                   // The object list for our model
     158  int numOfObjects;                                     // The number of objects in the model
     159  int numOfMaterials;                                   // The number of materials for the model
     160  int numOfAnimations;                          // The number of animations in this model (NEW)
     161  int currentAnim;                                      // The current index into pAnimations list (NEW)
     162  int currentFrame;                                     // The current frame of the current animation (NEW)
     163  vector<tAnimationInfo> animationList; // The list of animations (NEW)
     164  vector<tMaterialInfo> materialList;   // The list of material information (Textures and colors)
     165  vector<t3DObject> objectList;                 // The object list for our model
    103166};
    104167
  • orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc

    r4169 r4172  
    1616
    1717#include "md2Model.h"
     18#include "material.h"
    1819
    1920#include <fstream>
     
    107108    {
    108109      pModel->currentFrame = nextFrame;
    109       printf("changing frame\n");
    110110      this->localTime = 0.0f;
    111111    }
    112   printf("t = %f\n", this->localTime);
    113112  return (this->localTime / kAnimationSpeed);
    114113}
     
    455454  if( this->pFrames) delete this->pFrames;
    456455}
     456
     457
     458/********************************************************************************
     459 *   MD2LOADER2                                                                 *
     460 ********************************************************************************/
     461
     462MD2Model2::MD2Model2()
     463{
     464  this->pVertices = NULL;
     465  this->pGLCommands = NULL;
     466  this->pLightNormals = NULL;
     467
     468  this->numFrames = 0;
     469  this->numVertices = 0;
     470  this->numGLCommands = 0;
     471 
     472  this->textureID = 0;
     473  this->scaleFactor = 1.0f;
     474
     475  //this->setAnim(0);
     476}
     477
     478
     479MD2Model2::~MD2Model2()
     480{
     481  delete [] this->pVertices;
     482  delete [] this->pGLCommands;
     483  delete [] this->pLightNormals;
     484}
     485
     486
     487
     488bool MD2Model2::loadModel(const char *fileName)
     489{
     490  FILE *pFile;                            //file stream
     491  tMd2Header header;
     492  char* buffer;                           //buffer for frame data
     493  sFrame* frame;                          //temp frame
     494  sVec3D *pVertex;
     495  int* pNormals;
     496
     497  pFile = fopen(fileName, "rb");
     498  if( unlikely(!pFile))
     499    {
     500      PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n");
     501      return false;
     502    }
     503  fread(&header, 1, sizeof(tMd2Header), pFile);
     504  /* check for the header version: make sure its a md2 file :) */
     505  if( unlikely(header.version != MD2_VERSION) && unlikely(header.ident != MD2_IDENT))
     506    {
     507      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName);
     508      return false;
     509    }
     510
     511  /* got the data: map it to locals */
     512  this->numFrames = header.numFrames;
     513  this->numVertices = header.numVertices;
     514  this->numGLCommands = header.numGlCommands;
     515  /* allocate memory for the data storage */
     516  this->pVertices = new sVec3D[this->numVertices * this->numFrames];
     517  this->pGLCommands = new int[this->numGLCommands];
     518  this->pLightNormals = new int[this->numVertices * this->numFrames];
     519  buffer = new char[this->numFrames * header.frameSize];
     520
     521  /* read frame data from the file to a temp buffer */
     522  fseek(pFile, header.offsetFrames, SEEK_SET);
     523  fread(buffer, header.frameSize, this->numFrames, pFile);
     524  /* read opengl commands */
     525  fseek(pFile, header.offsetGlCommands, SEEK_SET);
     526  fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile);
     527
     528  for(int i = 0; i < this->numFrames; ++i)
     529    {
     530      frame = (sFrame*)(buffer + header.frameSize * i);
     531      pVertex = this->pVertices + this->numVertices  * i;
     532      pNormals = this->pLightNormals + this->numVertices * i;
     533
     534      for(int j = 0; j < this->numVertices; ++j)
     535        {
     536          /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)...  */
     537          pVertex[i][0] = (frame->pVertices[i].v[0] * frame->scale[0]) + frame->translate[0];
     538          pVertex[i][1] = (frame->pVertices[i].v[1] * frame->scale[1]) + frame->translate[1];
     539          pVertex[i][2] = (frame->pVertices[i].v[2] * frame->scale[2]) + frame->translate[2];     
     540
     541          pNormals[i] = frame->pVertices[i].lightNormalIndex;
     542        }
     543    }
     544
     545  delete [] buffer;
     546  fclose(pFile);
     547}
     548
     549
     550bool MD2Model2::loadSkin(const char* fileName)
     551{
     552  this->material = new Material("md2ModelTest");
     553  this->material->setDiffuseMap(fileName);
     554  this->material->setIllum(3);
     555  this->material->setAmbient(1.0, 1.0, 1.0);
     556}
     557
     558
     559void MD2Model2::draw()
     560{
     561  glPushMatrix();
     562  /* rotate because id software uses another orientation then the openGL default */
     563  /* \todo: rotate the axis already when loading... easy :) */
     564  glRotatef(-90.0, 1.0, 0.0, 0.0);
     565  glRotatef(-90.0, 0.0, 0.0, 1.0);
     566
     567  this->renderFrame();
     568
     569  glPopMatrix();
     570}
     571
     572
     573/**
     574   \brief initializes an array of vert with the current frame scaled vertices
     575
     576   we won't use the pVertices array directly, since its much easier and we need
     577   saving of data anyway
     578*/
     579void MD2Model2::interpolate(sVec3D* verticesList)
     580{
     581  for(int i = 0; i < this->numFrames; ++i)
     582    {
     583      verticesList[i][0] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][0] * this->scaleFactor;
     584      verticesList[i][1] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][1] * this->scaleFactor;
     585      verticesList[i][2] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][2] * this->scaleFactor;
     586    }
     587}
     588
     589
     590/* hhmmm... id used a very different way to do lightning...*/
     591void MD2Model2::processLighting()
     592{
     593}
     594
     595
     596void MD2Model2::renderFrame()
     597{
     598  static sVec3D verticesList[MD2_MAX_VERTICES]; /* performance: created only once in a lifetime */
     599  int* pCommands = this->pGLCommands;
     600
     601  /* some face culling stuff */
     602  glPushAttrib(GL_POLYGON_BIT);
     603  glFrontFace(GL_CW);
     604  glEnable(GL_CULL_FACE);
     605  glCullFace(GL_BACK);
     606     
     607  this->interpolate(verticesList);
     608  this->material->select();
     609
     610  /* draw the triangles */
     611  /* \todo: take int i out of while loop */
     612  while( int i = *(pCommands++)) /* strange looking while loop for maximum performance */
     613    {
     614      if( i < 0)
     615        {
     616          glBegin(GL_TRIANGLE_FAN);
     617          i = -i;
     618        }
     619      else
     620        {
     621          glBegin(GL_TRIANGLE_STRIP);
     622        }
     623
     624      for(; i > 0; --i, pCommands += 3) /* down counting for loop, next 3 gl commands */
     625        {
     626          /* for quake2 lighting */
     627          //float l = shadedots[this->pLightNormals[pCommands[2]]];
     628          //glColor3f(l * lcolor[0], l * lcolor[1], l * lcolor[2]);
     629
     630          glTexCoord2f( ((float *)pCommands)[0], ((float *)pCommands)[1] );
     631          //glNormal3fv(anorms[this->pLightNormals[2]]);
     632          glVertex3fv(verticesList[pCommands[2]]);
     633        }
     634      glEnd();
     635    }
     636  glDisable(GL_CULL_FACE);
     637  glPopAttrib();
     638}
     639
  • orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.h

    r4168 r4172  
    2525
    2626//! These are the needed defines for the max values when loading .MD2 files
     27#define MD2_IDENT                       (('2'<<24) + ('P'<<16) + ('D'<<8) + 'I')
     28#define MD2_VERSION                     8
    2729#define MD2_MAX_TRIANGLES               4096
    2830#define MD2_MAX_VERTICES                2048
     
    3133#define MD2_MAX_SKINS                   32
    3234#define MD2_MAX_FRAMESIZE               (MD2_MAX_VERTICES * 4 + 128)
     35
     36#define NUM_VERTEX_NORMALS              162
     37#define SHADEDOT_QUANT                  16
    3338
    3439//! This stores the speed of the animation between each key frame - currently conflicting with the animation framework
     
    107112typedef char tMd2Skin[64];
    108113
     114
    109115//! This is a MD2 Model class
    110116class MD2Model : public AbstractModel {
     
    137143};
    138144
     145/* forward definitions */
     146class Material;
     147
     148//! This is a MD2 Model class
     149class MD2Model2 : public AbstractModel {
     150
     151public:
     152  MD2Model2();
     153  virtual ~MD2Model2();
     154 
     155  bool loadModel(const char* filename);
     156  bool loadSkin(const char* filename);
     157 
     158  void drawFrame(int frame);
     159  void draw();
     160 
     161  void setAnim(int type);
     162  void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;}
     163
     164  void tick(float dtS);
     165
     166private:
     167  void animate(float time); 
     168  void processLighting();
     169  void interpolate(sVec3D* verticesList);
     170  void renderFrame();
     171
     172 public:
     173  /* these variables are static, because they are all the same for every model */
     174  static sVec3D anorms[NUM_VERTEX_NORMALS];
     175  static float anormsDots[SHADEDOT_QUANT][256];
     176  static sAnim animationList[21];
     177
     178 private:
     179  int numFrames;
     180  int numVertices;
     181  int numGLCommands;
     182
     183  sVec3D* pVertices;
     184  int* pGLCommands;
     185  int* pLightNormals;
     186
     187  unsigned int textureID;
     188  Material* material;
     189  sAnimState animationState;
     190  float scaleFactor;
     191};
     192
     193
    139194//! A class that handles all of the loading code
    140195class MD2Loader : public BaseObject {
  • orxonox/branches/md2_loader/src/world_entities/test_entity.cc

    r4168 r4172  
    2828TestEntity::TestEntity () : WorldEntity()
    2929
    30   this->md2Model = new MD2Model();
     30  //this->md2Model = new MD2Model();
     31
     32  MD2Model2* model2 = new MD2Model2();
     33  model2->loadModel("../data/models/tris.md2");
    3134
    3235  this->material = new Material("Clown");
     
    4346void TestEntity::tick (float time)
    4447{
    45   this->md2Model->tick(time);
     48  //this->md2Model->tick(time);
    4649}
    4750
     
    6871
    6972  /* TESTGING TESTING TESTING */
    70   this->material->select();
     73  //this->material->select();
    7174  //this->md2Model->draw(this->model);
    72   this->md2Model->animate();
     75  //this->md2Model->animate();
    7376
    7477  glPopMatrix();
Note: See TracChangeset for help on using the changeset viewer.