Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

christmas: jumping now also works

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