Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6172 was 6172, checked in by patrick, 19 years ago

christmas: framework update

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