Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: md2 scaling patch

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