Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

christmas: cleaned up the model package

File size: 12.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
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
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  this->setAnim(STAND);
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 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* 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      verticesList[i][0] = currVec[i][0] + this->animationState.interpolationState * (nextVec[i][0] - currVec[i][0]);
125      verticesList[i][1] = currVec[i][1] + this->animationState.interpolationState * (nextVec[i][1] - currVec[i][1]);
126      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  this->animationState.startFrame = animationList[type].firstFrame;
143  this->animationState.endFrame = animationList[type].lastFrame;
144  this->animationState.nextFrame = animationList[type].firstFrame + 1;
145  this->animationState.fps = animationList[type].fps;
146  this->animationState.type = type;
147
148  this->animationState.interpolationState = 0.0f;
149  this->animationState.localTime = 0.0f;
150  this->animationState.lastTime = 0.0f;
151  this->animationState.currentFrame = animationList[type].firstFrame;
152}
153
154
155
156
157/**
158  \brief sets the time in seconds passed since the last tick
159* @param time: in sec
160*/
161void MD2Model::tick(float time)
162{
163  this->animationState.localTime += time;
164
165  this->animate();
166  this->processLighting();
167  this->interpolate(/*verticesList*/);
168}
169
170
171/**
172 * @brief draws the model: interface for all other classes out in the world
173 * @todo make it const and virtual
174 * FIXME
175 */
176void MD2Model::draw() const
177{
178  glPushMatrix();
179  this->renderFrame();
180  glPopMatrix();
181}
182
183
184/**
185  \brief this is an internal function to render this special frame selected by animate()
186*/
187void MD2Model::renderFrame() const
188{
189  int* pCommands = this->data->pGLCommands;
190
191  /* some face culling stuff */
192  glPushAttrib(GL_POLYGON_BIT);
193  glFrontFace(GL_CW);
194  glEnable(GL_CULL_FACE);
195  glCullFace(GL_BACK);
196
197  this->data->material->select();
198
199  /* draw the triangles */
200  while( int i = *(pCommands++)) /* strange looking while loop for maximum performance */
201    {
202      if( i < 0)
203        {
204          glBegin(GL_TRIANGLE_FAN);
205          i = -i;
206        }
207      else
208        {
209          glBegin(GL_TRIANGLE_STRIP);
210        }
211
212
213      for(; i > 0; i--, pCommands += 3) /* down counting for loop, next 3 gl commands */
214        {
215          glTexCoord2f( ((float *)pCommands)[0], ((float *)pCommands)[1] );
216          glNormal3fv(anorms[this->data->pLightNormals[pCommands[2]]]);
217          glVertex3fv(verticesList[pCommands[2]]);
218        }
219      glEnd();
220
221    }
222  glDisable(GL_CULL_FACE);
223  glPopAttrib();
224}
225
226
227/**
228  \brief animates the current model
229
230  depending on the time passed (tick function), the player will select another model
231*/
232void MD2Model::animate()
233{
234  if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps))
235    {
236      this->animationState.currentFrame = this->animationState.nextFrame;
237      this->animationState.nextFrame++;
238
239      if( this->animationState.nextFrame > this->animationState.endFrame)
240        this->animationState.nextFrame = this->animationState.startFrame;
241      this->animationState.lastTime = this->animationState.localTime;
242    }
243
244  if( this->animationState.currentFrame > (this->data->numFrames - 1) )
245    this->animationState.currentFrame = 0;
246  if( this->animationState.nextFrame > (this->data->numFrames - 1) )
247    this->animationState.nextFrame = 0;
248
249  this->animationState.interpolationState = this->animationState.fps *
250    (this->animationState.localTime - this->animationState.lastTime);
251}
252
253
254/**
255  \brief this is how id is precessing their lightning
256
257  the details of how the whole lighting process is beeing handled - i have no idea... :)
258*/
259void MD2Model::processLighting()
260{
261  shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)];
262}
263
264
265/**
266  \brief prints out debug informations
267*/
268void MD2Model::debug()
269{
270  PRINT(0)("\n==========================| MD2Model::debug() |===\n");
271  PRINT(0)("=  Model FileName:\t%s\n", this->data->fileName);
272  PRINT(0)("=  Skin FileName:\t%s\n", this->data->skinFileName);
273  PRINT(0)("=  Size in Memory:\t%i Bytes\n", this->data->header->frameSize * this->data->header->numFrames + 64); // 64bytes is the header size
274  PRINT(0)("=  Number of Vertices:\t%i\n", this->data->header->numVertices);
275  PRINT(0)("=  Number of Frames: \t%i\n", this->data->header->numFrames);
276  PRINT(0)("=  Height, Width:\t%i, %i\n", this->data->header->skinHeight, this->data->header->skinWidth);
277  PRINT(0)("=  Pointer to the data object: %p\n", this->data);
278  PRINT(0)("===================================================\n\n");
279}
280
281
282/********************************************************************************
283 *   MD2Data                                                                    *
284 ********************************************************************************/
285
286/**
287  \brief simple constructor
288*/
289MD2Data::MD2Data(const char* modelFileName, const char* skinFileName)
290{
291  this->pVertices = NULL;
292  this->pGLCommands = NULL;
293  this->pLightNormals = NULL;
294  this->pTexCoor = NULL;
295
296  this->numFrames = 0;
297  this->numVertices = 0;
298  this->numGLCommands = 0;
299  this->numTexCoor = 0;
300
301  this->scaleFactor = 0.2f;
302
303  this->fileName = NULL;
304  this->skinFileName = NULL;
305  this->loadModel(modelFileName);
306  this->loadSkin(skinFileName);
307}
308
309
310/**
311  \brief simple destructor
312
313  this will clean out all the necessary data for a specific md2model
314*/
315MD2Data::~MD2Data()
316{
317  delete [] this->fileName;
318  delete [] this->skinFileName;
319  delete this->header;
320
321  delete [] this->pVertices;
322  delete [] this->pGLCommands;
323  delete [] this->pLightNormals;
324  delete [] this->pTexCoor;
325
326  delete this->material;
327}
328
329
330
331/**
332  \brief this will load the whole model data (vertices, opengl command list, ...)
333* @param fileName: the name of the model file
334  \return true if success
335*/
336bool MD2Data::loadModel(const char* fileName)
337{
338  FILE *pFile;                            //file stream
339  char* buffer;                           //buffer for frame data
340  sFrame* frame;                          //temp frame
341  sVec3D *pVertex;
342  int* pNormals;
343
344  //! @todo this chek should include deleting a loaded model (eventually)
345  if (fileName == NULL)
346    return false;
347
348  pFile = fopen(fileName, "rb");
349  if( unlikely(!pFile))
350    {
351      PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n");
352      return false;
353    }
354  this->header = new MD2Header;
355  fread(this->header, 1, sizeof(MD2Header), pFile);
356  /* check for the header version: make sure its a md2 file :) */
357  if( unlikely(this->header->version != MD2_VERSION) && unlikely(this->header->ident != MD2_IDENT))
358    {
359      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName);
360      return false;
361    }
362
363  this->fileName = new char[strlen(fileName)+1];
364  strcpy(this->fileName, fileName);
365  /* got the data: map it to locals */
366  this->numFrames = this->header->numFrames;
367  this->numVertices = this->header->numVertices;
368  this->numTriangles = this->header->numTriangles;
369  this->numGLCommands = this->header->numGlCommands;
370  this->numTexCoor = this->header->numTexCoords;
371  /* allocate memory for the data storage */
372  this->pVertices = new sVec3D[this->numVertices * this->numFrames];
373  this->pGLCommands = new int[this->numGLCommands];
374  this->pLightNormals = new int[this->numVertices * this->numFrames];
375  this->pTriangles = new sTriangle[this->numTriangles];
376  this->pTexCoor = new sTexCoor[this->numTexCoor];
377  buffer = new char[this->numFrames * this->header->frameSize];
378
379
380  /* read frame data from the file to a temp buffer */
381  fseek(pFile, this->header->offsetFrames, SEEK_SET);
382  fread(buffer, this->header->frameSize, this->numFrames, pFile);
383  /* read opengl commands */
384  fseek(pFile, this->header->offsetGlCommands, SEEK_SET);
385  fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile);
386  /* triangle list */
387  fseek(pFile, this->header->offsetTriangles, SEEK_SET);
388  fread(this->pTriangles, sizeof(sTriangle), this->numTriangles, pFile);
389  /*  read in texture coordinates */
390  fseek(pFile, this->header->offsetTexCoords, SEEK_SET);
391  fread(this->pTexCoor, sizeof(sTexCoor), this->numTexCoor, pFile);
392
393
394  for(int i = 0; i < this->numFrames; ++i)
395    {
396      frame = (sFrame*)(buffer + this->header->frameSize * i);
397      pVertex = this->pVertices + this->numVertices  * i;
398      pNormals = this->pLightNormals + this->numVertices * i;
399
400      for(int j = 0; j < this->numVertices; ++j)
401        {
402          /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)...  */
403          pVertex[j][0] = ((frame->pVertices[j].v[0] * frame->scale[0] ) + frame->translate[0] )* this->scaleFactor;
404          pVertex[j][1] = ((frame->pVertices[j].v[2] * frame->scale[2]) + frame->translate[2]) * this->scaleFactor;
405          pVertex[j][2] = (-1.0 * (frame->pVertices[j].v[1] * frame->scale[1] + frame->translate[1])) * this->scaleFactor;
406
407          pNormals[j] = frame->pVertices[j].lightNormalIndex;
408        }
409    }
410    PRINTF(4)("Finished loading the md2 file\n");
411
412  delete [] buffer;
413  fclose(pFile);
414}
415
416
417/**
418  \brief loads the skin/material stuff
419* @param fileName: name of the skin file
420  \return true if success
421*/
422bool MD2Data::loadSkin(const char* fileName)
423{
424  if( fileName == NULL)
425    {
426      this->skinFileName = NULL;
427      return false;
428    }
429
430  this->skinFileName = new char[strlen(fileName)+1];
431  strcpy(this->skinFileName, fileName);
432
433  this->material = new Material("md2ModelTest");
434  this->material->setDiffuseMap(fileName);
435  this->material->setIllum(3);
436  this->material->setAmbient(1.0, 1.0, 1.0);
437}
Note: See TracBrowser for help on using the repository browser.