Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

christmas: free movement, but not yet good

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