Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/christmas_branche/src/lib/graphics/importer/md2Model.cc @ 6188

Last change on this file since 6188 was 6188, checked in by patrick, 18 years ago

christmas: got the md2 bug. the devil is in the detail

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