Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: md2model data/model seperation

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