Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4869 was 4836, checked in by bensch, 19 years ago

orxonox/trunk: renamed all the \param → @param and so on in Doxygen tags.
Thanks a lot to the kDevelop team. this took since the last commit :)

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