Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/md2Model.cc @ 7607

Last change on this file since 7607 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

File size: 16.4 KB
RevLine 
[4682]1/*
[4245]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13*/
14
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
16
17#include "md2Model.h"
18#include "material.h"
19
[5075]20#include "debug.h"
[7193]21#include "util/loading/resource_manager.h"
[4246]22
[4245]23
24using namespace std;
25
[4682]26//! the model anorms
[4276]27sVec3D MD2Model::anorms[NUM_VERTEX_NORMALS] = {
[4245]28 #include "anorms.h"
29};
30
[4459]31//! anormal dots, no idea of how this shall work, but it does
[4276]32float MD2Model::anormsDots[SHADEDOT_QUANT][256] = {
[4245]33  #include "anormtab.h"
34};
35
36
[4682]37//! the angle under which the model is viewd, used internaly
[4245]38float md2Angle = 0.0f;
39
40
[4459]41//! list of all different animations a std md2model supports
[4276]42sAnim MD2Model::animationList[21] =
[4245]43  {
[6222]44 // begin, end, fps, interruptable
45    {   0,  39,  9, 1 },   //!< STAND
46    {  40,  45, 10, 1 },   //!< RUN
47    {  46,  53, 10, 0 },   //!< ATTACK
48    {  54,  57,  7, 1 },   //!< PAIN_A
49    {  58,  61,  7, 1 },   //!< PAIN_B
50    {  62,  65,  7, 1 },   //!< PAIN_C
51    {  66,  71,  7, 0 },   //!< JUMP
52    {  72,  83,  7, 1 },   //!< FLIP
53    {  84,  94,  7, 1 },   //!< SALUTE
54    {  95, 111, 10, 1 },   //!< FALLBACK
55    { 112, 122,  7, 1 },   //!< WAVE
56    { 123, 134,  6, 1 },   //!< POINTT
57    { 135, 153, 10, 1 },   //!< CROUCH_STAND
58    { 154, 159,  7, 1 },   //!< CROUCH_WALK
59    { 160, 168, 10, 1 },   //!< CROUCH_ATTACK
60    { 196, 172,  7, 1 },   //!< CROUCH_PAIN
61    { 173, 177,  5, 0 },   //!< CROUCH_DEATH
[7114]62    { 178, 183,  10, 0 },   //!< DEATH_FALLBACK
63    { 184, 189,  10, 0 },   //!< DEATH_FALLFORWARD
64    { 190, 197,  10, 0 },   //!< DEATH_FALLBACKSLOW
[6222]65    { 198, 198,  5, 1 },   //!< BOOM
[4245]66  };
67
68
69
70/********************************************************************************
[4459]71 *   MD2Model                                                                   *
72 ********************************************************************************/
[4245]73
[4461]74/**
[4284]75  \brief simple constructor initializing all variables
76*/
[7221]77MD2Model::MD2Model(const std::string& modelFileName, const std::string& skinFileName, float scale)
[4245]78{
[7123]79  this->setClassID(CL_MD2_MODEL, "MD2Model");
[4280]80  /* this creates the data container via ressource manager */
[7221]81  if (!modelFileName.empty())
82    this->data = (MD2Data*)ResourceManager::getInstance()->load(modelFileName, MD2, RP_GAME, skinFileName, scale);
[4787]83  if( unlikely(this->data == NULL))
84    PRINTF(0)("The model was not found, MD2Model Loader finished abnormaly. Update the data-repos\n");
[5075]85
[7059]86  this->scaleFactor = scale;
87
[6222]88  shadeDots = MD2Model::anormsDots[0];
89  /* set the animation stat mannualy */
90  this->animationState.type = STAND;
91  this->animationState.numPlays = 1;
[5087]92  this->setAnim(STAND);
[6222]93
94  this->debug();
[7068]95
96    //write the modelinfo information
97  this->pModelInfo.numVertices = this->data->numVertices;
98  this->pModelInfo.numTriangles = this->data->numTriangles;
99  this->pModelInfo.numNormals = 0;
100  this->pModelInfo.numTexCoor = this->data->numTexCoor;
101  this->pModelInfo.pVertices = (float*)this->data->pVertices;
102  this->pModelInfo.pNormals = NULL;
103  this->pModelInfo.pTexCoor = (float*)this->data->pTexCoor;
104
105  // triangle conversion
106  this->pModelInfo.pTriangles = new sTriangleExt[this->data->numTriangles];
107  for( int i = 0; i < this->data->numTriangles; i++)
108  {
109    this->pModelInfo.pTriangles[i].indexToVertices[0] = this->data->pTriangles[i].indexToVertices[0];
110    this->pModelInfo.pTriangles[i].indexToVertices[1] = this->data->pTriangles[i].indexToVertices[1];
111    this->pModelInfo.pTriangles[i].indexToVertices[2] = this->data->pTriangles[i].indexToVertices[2];
112
113    this->pModelInfo.pTriangles[i].indexToTexCoor[0] = this->data->pTriangles[i].indexToTexCoor[0];
114    this->pModelInfo.pTriangles[i].indexToTexCoor[1] = this->data->pTriangles[i].indexToTexCoor[1];
115    this->pModelInfo.pTriangles[i].indexToTexCoor[2] = this->data->pTriangles[i].indexToTexCoor[2];
116  }
[4245]117}
118
[7068]119
[4461]120/**
[4284]121  \brief simple destructor, dereferencing all variables
[4245]122
[4284]123  this is where the ressource manager is cleaning the stuff
124*/
[4276]125MD2Model::~MD2Model()
[4245]126{
[4462]127  ResourceManager::getInstance()->unload(this->data);
[4245]128}
129
130
131/**
[4836]132 *  initializes an array of vert with the current frame scaled vertices
[6222]133 * @param this->verticesList: the list of vertices to interpolate between
[4245]134
135   we won't use the pVertices array directly, since its much easier and we need
136   saving of data anyway
137*/
[6222]138void MD2Model::interpolate(/*sVec3D* this->verticesList*/)
[4245]139{
140  sVec3D* currVec;
141  sVec3D* nextVec;
142
[4281]143  currVec = &this->data->pVertices[this->data->numVertices * this->animationState.currentFrame];
144  nextVec = &this->data->pVertices[this->data->numVertices * this->animationState.nextFrame];
[4245]145
[5086]146  for( int i = 0; i < this->data->numVertices; ++i)
[4245]147    {
[6222]148      this->verticesList[i][0] = currVec[i][0] + this->animationState.interpolationState * (nextVec[i][0] - currVec[i][0]);
149      this->verticesList[i][1] = currVec[i][1] + this->animationState.interpolationState * (nextVec[i][1] - currVec[i][1]);
150      this->verticesList[i][2] = currVec[i][2] + this->animationState.interpolationState * (nextVec[i][2] - currVec[i][2]);
[4245]151    }
152}
153
154
[4461]155/**
[4284]156  \brief sets the animation type
[4836]157* @param type: animation type
[4284]158
159  the animation types can be looked up in the animationType table
160*/
[7071]161void MD2Model::setAnim(int type, int animPlayback)
[4245]162{
163  if( (type < 0) || (type > MAX_ANIMATIONS) )
[5087]164    type = STAND;
[4245]165
[6222]166  if( MD2Model::animationList[this->animationState.type].bStoppable == 0)
167  {
168    if( this->animationState.numPlays == 0 )
169      return;
170  }
171
[4245]172  this->animationState.startFrame = animationList[type].firstFrame;
173  this->animationState.endFrame = animationList[type].lastFrame;
174  this->animationState.nextFrame = animationList[type].firstFrame + 1;
175  this->animationState.fps = animationList[type].fps;
176  this->animationState.type = type;
[6222]177  this->animationState.numPlays = 0;
[7071]178  this->animationState.animPlaybackMode = animPlayback;
[4682]179
[5087]180  this->animationState.interpolationState = 0.0f;
181  this->animationState.localTime = 0.0f;
182  this->animationState.lastTime = 0.0f;
[4245]183  this->animationState.currentFrame = animationList[type].firstFrame;
184}
185
186
[4461]187/**
[4284]188  \brief sets the time in seconds passed since the last tick
[4836]189* @param time: in sec
[4284]190*/
[4276]191void MD2Model::tick(float time)
[4245]192{
[6222]193  this->animate(time);
194  this->processLighting();
195  this->interpolate(/*this->verticesList*/);
[4245]196}
197
198
[4461]199/**
[6022]200 * @brief draws the model: interface for all other classes out in the world
201 * @todo make it const and virtual
202 * FIXME
203 */
[6222]204void MD2Model::draw() const
[4245]205{
206  glPushMatrix();
[5087]207  this->renderFrame();
[6222]208  // renderFrameTriangles();
[4245]209  glPopMatrix();
[4682]210}
[4245]211
212
[4461]213/**
[4284]214  \brief this is an internal function to render this special frame selected by animate()
215*/
[6222]216void MD2Model::renderFrame() const
[4245]217{
[4281]218  int* pCommands = this->data->pGLCommands;
[4245]219
220  /* some face culling stuff */
221  glPushAttrib(GL_POLYGON_BIT);
222  glFrontFace(GL_CW);
223  glEnable(GL_CULL_FACE);
224  glCullFace(GL_BACK);
[4682]225
[7123]226  this->data->material.select();
[4245]227
228  /* draw the triangles */
229  while( int i = *(pCommands++)) /* strange looking while loop for maximum performance */
230    {
231      if( i < 0)
[4682]232        {
233          glBegin(GL_TRIANGLE_FAN);
234          i = -i;
235        }
[4245]236      else
[4682]237        {
238          glBegin(GL_TRIANGLE_STRIP);
239        }
[4245]240
241      for(; i > 0; i--, pCommands += 3) /* down counting for loop, next 3 gl commands */
[4682]242        {
[4717]243          glTexCoord2f( ((float *)pCommands)[0], ((float *)pCommands)[1] );
[4682]244          glNormal3fv(anorms[this->data->pLightNormals[pCommands[2]]]);
[6222]245          glVertex3fv(this->verticesList[pCommands[2]]);
[4682]246        }
[4245]247      glEnd();
[4717]248
[4245]249    }
250  glDisable(GL_CULL_FACE);
251  glPopAttrib();
252}
253
254
[6222]255void MD2Model::renderFrameTriangles() const
256{
257  //static sVec3D this->verticesList[MD2_MAX_VERTICES]; /* performance: created only once in a lifetime */
258  int* pCommands = this->data->pGLCommands;
259  /* some face culling stuff */
260//   glPushAttrib(GL_POLYGON_BIT);
261//   glFrontFace(GL_CW);
262//   glEnable(GL_CULL_FACE);
263//   glCullFace(GL_BACK);
264//
265//   this->processLighting();
266//   this->interpolate(/*this->verticesList*/);
[7123]267  this->data->material.select();
[6222]268
269  /* draw the triangles */
270  glBegin(GL_TRIANGLES);
271
272  for( int i = 0, k = 0; i < this->data->numTriangles; ++i, k += 3)
273  {
274    float* v = this->data->pVertices[this->data->pTriangles[i].indexToVertices[0]];
275
276    printf("triangle: %i\n", i);
277    printf("     v0: (%f, %f, %f)\n", v[0], v[1], v[2]);
278    v = this->data->pVertices[this->data->pTriangles[i].indexToVertices[1]];
279    printf("     v1: (%f, %f, %f)\n", v[0], v[1], v[2]);
280    v = this->data->pVertices[this->data->pTriangles[i].indexToVertices[2]];
281    printf("     v2: (%f, %f, %f)\n", v[0], v[1], v[2]);
282
283
284    glNormal3f(anorms[i][0], anorms[i][1], anorms[i][2]);
285    glVertex3fv(this->data->pVertices[this->data->pTriangles[i].indexToVertices[0]]);
286
287    glNormal3f(anorms[i][0], anorms[i][1], anorms[i][2]);
288    glVertex3fv(this->data->pVertices[this->data->pTriangles[i].indexToVertices[1]]);
289
290    glNormal3f(anorms[i][0], anorms[i][1], anorms[i][2]);
291    glVertex3fv(this->data->pVertices[this->data->pTriangles[i].indexToVertices[2]]);
292  }
293
294  glEnd();
295}
296
297
[4461]298/**
[4284]299  \brief animates the current model
300
301  depending on the time passed (tick function), the player will select another model
302*/
[6222]303void MD2Model::animate(float time)
[4280]304{
[6222]305  this->animationState.localTime += time;
306
[4280]307  if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps))
308    {
309      this->animationState.currentFrame = this->animationState.nextFrame;
310      this->animationState.nextFrame++;
[4682]311
[7075]312      if( this->animationState.nextFrame > this->animationState.endFrame )
[6222]313      {
[7075]314        if( this->animationState.animPlaybackMode == MD2_ANIM_LOOP)
315        {
316          this->animationState.nextFrame = this->animationState.startFrame;
317          this->animationState.numPlays++;
318        }
319        else
320        {
321          this->animationState.nextFrame = this->animationState.endFrame;
322        }
[6222]323      }
[4280]324      this->animationState.lastTime = this->animationState.localTime;
325    }
326
[7075]327//     if( this->animationState.currentFrame > (this->data->numFrames - 1) )
328//       this->animationState.currentFrame = 0;
[4280]329
[7075]330//     if( (this->animationState.nextFrame > (this->data->numFrames - 1)) && this->animationState.animPlaybackMode == MD2_ANIM_LOOP)
331//     this->animationState.nextFrame = 0;
332
[4682]333  this->animationState.interpolationState = this->animationState.fps *
[4280]334    (this->animationState.localTime - this->animationState.lastTime);
335}
336
337
[4461]338/**
[4284]339  \brief this is how id is precessing their lightning
340
341  the details of how the whole lighting process is beeing handled - i have no idea... :)
342*/
[4280]343void MD2Model::processLighting()
344{
345  shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)];
346}
347
[4461]348
349/**
[4284]350  \brief prints out debug informations
351*/
[4276]352void MD2Model::debug()
[4245]353{
[4282]354  PRINT(0)("\n==========================| MD2Model::debug() |===\n");
[7221]355  PRINT(0)("=  Model FileName:\t%s\n", this->data->fileName.c_str());
356  PRINT(0)("=  Skin FileName:\t%s\n", this->data->skinFileName.c_str());
[4281]357  PRINT(0)("=  Size in Memory:\t%i Bytes\n", this->data->header->frameSize * this->data->header->numFrames + 64); // 64bytes is the header size
358  PRINT(0)("=  Number of Vertices:\t%i\n", this->data->header->numVertices);
359  PRINT(0)("=  Number of Frames: \t%i\n", this->data->header->numFrames);
[4282]360  PRINT(0)("=  Height, Width:\t%i, %i\n", this->data->header->skinHeight, this->data->header->skinWidth);
[4488]361  PRINT(0)("=  Pointer to the data object: %p\n", this->data);
[4245]362  PRINT(0)("===================================================\n\n");
363}
[4280]364
365
[4282]366/********************************************************************************
367 *   MD2Data                                                                    *
368 ********************************************************************************/
[4280]369
[4461]370/**
[4284]371  \brief simple constructor
372*/
[7221]373MD2Data::MD2Data(const std::string& modelFileName, const std::string& skinFileName, float scale)
[4280]374{
[7059]375  scale *= 0.1f;
376
[4280]377  this->pVertices = NULL;
378  this->pGLCommands = NULL;
[4682]379  this->pLightNormals = NULL;
[5085]380  this->pTexCoor = NULL;
[4280]381
382  this->numFrames = 0;
383  this->numVertices = 0;
384  this->numGLCommands = 0;
[5085]385  this->numTexCoor = 0;
[4459]386
[6222]387//   this->scaleFactor = 1.0f;
[7059]388  this->scaleFactor = scale;
[4682]389
[7221]390  this->fileName = "";
391  this->skinFileName = "";
[4459]392  this->loadModel(modelFileName);
393  this->loadSkin(skinFileName);
[4280]394}
395
396
[4461]397/**
[4284]398  \brief simple destructor
399
400  this will clean out all the necessary data for a specific md2model
401*/
[4280]402MD2Data::~MD2Data()
403{
[5235]404  delete this->header;
[4460]405
[4280]406  delete [] this->pVertices;
407  delete [] this->pGLCommands;
408  delete [] this->pLightNormals;
[5085]409  delete [] this->pTexCoor;
[4460]410
[4280]411}
412
413
414
[4461]415/**
[4284]416  \brief this will load the whole model data (vertices, opengl command list, ...)
[4836]417* @param fileName: the name of the model file
[4284]418  \return true if success
419*/
[7221]420bool MD2Data::loadModel(const std::string& fileName)
[4280]421{
422  FILE *pFile;                            //file stream
423  char* buffer;                           //buffer for frame data
424  sFrame* frame;                          //temp frame
425  sVec3D *pVertex;
426  int* pNormals;
427
[5284]428  //! @todo this chek should include deleting a loaded model (eventually)
[7221]429  if (fileName.empty())
[5284]430    return false;
431
[7221]432  pFile = fopen(fileName.c_str(), "rb");
[4682]433  if( unlikely(!pFile))
[4280]434    {
435      PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n");
436      return false;
437    }
438  this->header = new MD2Header;
439  fread(this->header, 1, sizeof(MD2Header), pFile);
440  /* check for the header version: make sure its a md2 file :) */
441  if( unlikely(this->header->version != MD2_VERSION) && unlikely(this->header->ident != MD2_IDENT))
442    {
[7221]443      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str());
[4280]444      return false;
445    }
446
[7221]447  this->fileName =fileName;
[4280]448  /* got the data: map it to locals */
449  this->numFrames = this->header->numFrames;
450  this->numVertices = this->header->numVertices;
451  this->numTriangles = this->header->numTriangles;
452  this->numGLCommands = this->header->numGlCommands;
[5085]453  this->numTexCoor = this->header->numTexCoords;
[4280]454  /* allocate memory for the data storage */
455  this->pVertices = new sVec3D[this->numVertices * this->numFrames];
456  this->pGLCommands = new int[this->numGLCommands];
457  this->pLightNormals = new int[this->numVertices * this->numFrames];
[4787]458  this->pTriangles = new sTriangle[this->numTriangles];
[5085]459  this->pTexCoor = new sTexCoor[this->numTexCoor];
[4280]460  buffer = new char[this->numFrames * this->header->frameSize];
461
[4787]462
[4280]463  /* read frame data from the file to a temp buffer */
464  fseek(pFile, this->header->offsetFrames, SEEK_SET);
465  fread(buffer, this->header->frameSize, this->numFrames, pFile);
466  /* read opengl commands */
467  fseek(pFile, this->header->offsetGlCommands, SEEK_SET);
468  fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile);
[4787]469  /* triangle list */
470  fseek(pFile, this->header->offsetTriangles, SEEK_SET);
471  fread(this->pTriangles, sizeof(sTriangle), this->numTriangles, pFile);
[5085]472  /*  read in texture coordinates */
473  fseek(pFile, this->header->offsetTexCoords, SEEK_SET);
474  fread(this->pTexCoor, sizeof(sTexCoor), this->numTexCoor, pFile);
[4280]475
[5087]476
[4280]477  for(int i = 0; i < this->numFrames; ++i)
478    {
[4682]479      frame = (sFrame*)(buffer + this->header->frameSize * i);
[4280]480      pVertex = this->pVertices + this->numVertices  * i;
481      pNormals = this->pLightNormals + this->numVertices * i;
482
483      for(int j = 0; j < this->numVertices; ++j)
[4682]484        {
485          /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)...  */
[6222]486           pVertex[j][0] = ((frame->pVertices[j].v[0] * frame->scale[0] ) + frame->translate[0] )* this->scaleFactor;
487           pVertex[j][1] = ((frame->pVertices[j].v[2] * frame->scale[2]) + frame->translate[2]) * this->scaleFactor;
488           pVertex[j][2] = (-1.0 * (frame->pVertices[j].v[1] * frame->scale[1] + frame->translate[1])) * this->scaleFactor;
[4682]489
[6222]490          //printf("vertex %i/%i: (%f, %f, %f)\n", j, this->numVertices, pVertex[j][0], pVertex[j][1], pVertex[j][2]);
491
[4682]492          pNormals[j] = frame->pVertices[j].lightNormalIndex;
493        }
[4280]494    }
[6222]495    PRINTF(4)("Finished loading the md2 file\n");
[4280]496
497  delete [] buffer;
498  fclose(pFile);
499}
500
501
[4461]502/**
[4284]503  \brief loads the skin/material stuff
[4836]504* @param fileName: name of the skin file
[4284]505  \return true if success
506*/
[7221]507bool MD2Data::loadSkin(const std::string& fileName)
[4280]508{
[7221]509  if( fileName.empty())
[4459]510    {
[7221]511      this->skinFileName = "";
[4459]512      return false;
513    }
514
[7221]515  this->skinFileName = fileName;
[4281]516
[7123]517  this->material.setName("md2ModelMaterial");
518  this->material.setDiffuseMap(fileName);
519  this->material.setIllum(3);
520  this->material.setAmbient(1.0, 1.0, 1.0);
[4280]521}
Note: See TracBrowser for help on using the repository browser.