Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/md3/md3_model.cc @ 8637

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

bsp: md3 animation work

File size: 19.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 "md3_model.h"
18
19#include "md3_data.h"
20#include "md3_mesh.h"
21#include "md3_tag.h"
22#include "md3_bone_frame.h"
23
24#include "md3_animation_cfg.h"
25
26#include "material.h"
27#include "quaternion.h"
28
29#include "loading/resource_manager.h"
30
31#include "debug.h"
32
33namespace md3
34{
35
36  /**
37   * md3 model
38   */
39  MD3Model::MD3Model(std::string filename, float scaling)
40  {
41    this->autoAssemblePlayerModel(filename, scaling);
42
43    this->bDrawBones = false;
44    this->bDrawNormals = false;
45  }
46
47
48
49  MD3Model::~MD3Model()
50  {
51//     delete this->tmpBoneFrame;
52//     delete [] this->tmpMesh;
53
54    ///TODO deleting mesh
55    ///TODO deleting matrices
56  }
57
58
59  /**
60   * auto assemples a player model
61   * @param filename is the name to the directory of the modelzzzzzz
62   */
63  void MD3Model::autoAssemblePlayerModel(std::string filename, float scaling)
64  {
65    // loading the config file
66    std::string cfgName(filename + "/animation.cfg");
67    this->config = (MD3AnimationCfg*)ResourceManager::getInstance()->load(cfgName, MD3_CONFIG, RP_GAME);
68
69    //first load the torso or the upper part
70    std::string nameUpper(filename + "/upper.md3");
71    if( (this->md3Data = (MD3Data*)ResourceManager::getInstance()->load(nameUpper, MD3, RP_GAME, nameUpper, scaling)) == NULL)
72    {
73      std::string nameTorso(filename + "/torso.md3");
74      this->md3Data = (MD3Data*)ResourceManager::getInstance()->load(nameTorso, MD3, RP_GAME, nameTorso, scaling);
75    }
76
77    if( this->md3Data == NULL)
78    {
79      PRINTF(1)("Problems loading the MD3Model. Abording\n");
80      return;
81    }
82
83    // load lower
84    std::string nameLower(filename + "/lower.md3");
85    MD3Data* lower = (MD3Data*)ResourceManager::getInstance()->load(nameLower, MD3, RP_GAME, nameLower, scaling);
86    if( lower != NULL)
87    {
88      int tag = this->md3Data->getTagIndexByName("tag_lower");
89      PRINTF(0)("Loaded the %s model on index %i\n", nameLower.c_str(), tag);
90      if( tag >= 0)
91       this->md3Data->addLinkedModel(tag, lower);
92      else
93        PRINTF(0)("Could not add %s\n", nameLower.c_str());
94    }
95
96
97    // load head
98    std::string nameHead(filename + "/head.md3");
99    MD3Data* head = (MD3Data*)ResourceManager::getInstance()->load(nameHead, MD3, RP_GAME, nameLower, scaling);
100    if( head != NULL)
101    {
102      int tag = this->md3Data->getTagIndexByName("tag_head");
103      PRINTF(0)("Loaded the %s model on index %i\n", nameHead.c_str(), tag);
104      if( tag >= 0)
105        this->md3Data->addLinkedModel(tag, head);
106      else
107        PRINTF(0)("Could not add %s\n", nameHead.c_str());
108    }
109
110  }
111
112
113
114  /**
115   * tick float
116   * @param time: time elapsed
117   */
118  void MD3Model::tick(float time)
119  {
120
121    this->tick(time, this->md3Data);
122  }
123
124
125  /**
126   * tick each data
127   */
128  void MD3Model::tick(float time, MD3Data* data)
129  {
130    // draw the bones if needed
131    if( this->bDrawBones)
132    {
133      // get bone frame, interpolate if necessary
134      if( data->animationState.interpolationFraction != 0.0 &&
135          data->animationState.currentFrame != data->animationState.nextFrame)
136      {
137        //interpolate bone frame
138        data->tmpBoneFrame = this->interpolateBoneFrame(data, data->boneFrames[data->animationState.currentFrame],
139                                                        data->boneFrames[data->animationState.nextFrame],
140                                                        data->animationState.interpolationFraction);
141      }
142      else
143      {
144        data->tmpBoneFrame = data->boneFrames[data->animationState.currentFrame];
145      }
146    }
147
148    //draw all meshes of current frame of this model
149    for( int i = 0;  i < data->header->meshNum; i++)
150    {
151      MD3Mesh* mesh = data->meshes[i];
152
153      // get mesh frame, do interpolation if necessary
154      sVec3D* frame;
155      if( data->animationState.interpolationFraction != 0.0 &&
156          data->animationState.currentFrame != data->animationState.nextFrame)
157      {
158        //interpolate mesh frame between the 2 current mesh frames
159        frame = this->interpolateMeshFrame( data, data->meshes[data->animationState.currentFrame]->meshFrames,
160                                            data->meshes[data->animationState.nextFrame]->meshFrames,
161                                            data->animationState.interpolationFraction, mesh, i);
162      }
163      else
164      {
165        //no interpolation needed, just draw current frame
166        frame = &mesh->meshFrames[data->animationState.currentFrame];
167      }
168      data->tmpMesh[i] = frame;
169
170      // draw vertex normals if needed
171      if( this->bDrawNormals)
172      {
173        // get vertex normals, interpolate if necessary
174        if( data->animationState.interpolationFraction != 0.0 &&
175            data->animationState.currentFrame != data->animationState.nextFrame)
176        {
177          //interpolate vertex normals
178          this->interpolateVertexNormals(data, &mesh->normals[data->animationState.currentFrame],
179                                         &mesh->normals[data->animationState.nextFrame],
180                                         data->animationState.interpolationFraction, mesh, i);
181        }
182      }
183    }
184
185
186    // draw all models linked to this model
187    std::map<int, MD3Data*>::iterator it = data->sortedMap.begin();
188    int i = 0;
189    while( it != data->sortedMap.end())
190    {
191      MD3Data* child = it->second;
192
193      //build transformation array m from matrix, interpolate if necessary
194
195      MD3Tag* currFrameTag = data->boneFrames[data->animationState.currentFrame]->tags[child->parentTagIndex];
196
197      if( data->animationState.interpolationFraction != 0.0 &&
198          data->animationState.currentFrame != data->animationState.nextFrame)
199      {
200        //we need to interpolate
201        MD3Tag* nextFrameTag = data->boneFrames[data->animationState.nextFrame]->tags[child->parentTagIndex];
202        this->interpolateTransformation(child, currFrameTag, nextFrameTag, data->animationState.interpolationFraction, i);
203      }
204      else
205      {
206        //no interpolation needed, stay with last transformation
207        //OpenGL matrix is in column-major order
208        data->tmpMatrix[i][0] = currFrameTag->matrix[0][0];
209        data->tmpMatrix[i][1] = currFrameTag->matrix[1][0];
210        data->tmpMatrix[i][2] = currFrameTag->matrix[2][0];
211        data->tmpMatrix[i][3] = 0.0f;
212        data->tmpMatrix[i][4] = currFrameTag->matrix[0][1];
213        data->tmpMatrix[i][5] = currFrameTag->matrix[1][1];
214        data->tmpMatrix[i][6] = currFrameTag->matrix[2][1];
215        data->tmpMatrix[i][7] = 0.0f;
216        data->tmpMatrix[i][8] = currFrameTag->matrix[0][2];
217        data->tmpMatrix[i][9] = currFrameTag->matrix[1][2];
218        data->tmpMatrix[i][10]= currFrameTag->matrix[2][2];
219        data->tmpMatrix[i][11]= 0.0f;
220        data->tmpMatrix[i][12] = currFrameTag->position.x;
221        data->tmpMatrix[i][13] = currFrameTag->position.y;
222        data->tmpMatrix[i][14] = currFrameTag->position.z;
223        data->tmpMatrix[i][15] = 1.0f;
224      }
225
226      // switch to child coord system
227
228      // and tick child
229      this->tick(time, child);
230
231      i++;
232      it++;
233    }
234  }
235
236
237  /**
238   * this draws the md3 model
239   */
240  void MD3Model::draw() const
241  {
242    PRINTF(0)("\ndraw========================\n");
243    //draw current bone frame
244    this->draw(this->md3Data);
245  }
246
247
248  /**
249   * draw the md3model
250   * @param data: the data to be drawn
251   */
252  void MD3Model::draw(MD3Data* data) const
253  {
254
255    // draw the bones if needed
256    if( this->bDrawBones)
257    {
258      // get bone frame, interpolate if necessary
259      if( data->animationState.interpolationFraction != 0.0 &&
260          data->animationState.currentFrame != data->animationState.nextFrame) {
261        //interpolate bone frame
262        this->drawBoneFrame(data->tmpBoneFrame);
263      }
264      else {
265        //stick with current bone frame
266        this->drawBoneFrame(data->boneFrames[data->animationState.currentFrame]);
267      }
268    }
269
270
271    //draw all meshes of current frame of this model
272    for( int i = 0;  i < data->header->meshNum; i++)
273    {
274      MD3Mesh* mesh = data->meshes[i];
275
276      if( mesh->header->textureNum > 0 && &mesh->material[0] != NULL)
277        mesh->material[0].select();
278
279      // get mesh frame, do interpolation if necessary
280      sVec3D* frame = data->tmpMesh[i];
281
282      this->drawMesh(mesh, frame);
283
284      // draw vertex normals if needed
285      if( this->bDrawNormals)
286      {
287        // get vertex normals, interpolate if necessary
288        if( data->animationState.interpolationFraction != 0.0 &&
289            data->animationState.currentFrame != data->animationState.nextFrame)
290        {
291          //interpolate vertex normals
292          this->drawVertexNormals(frame, data->tmpNormal[i]);
293        }
294        else {
295          //stick with current vertex normals
296          this->drawVertexNormals(frame, &mesh->normals[data->animationState.currentFrame]);
297        }
298      }
299    }
300
301
302    // draw all models linked to this model
303    int i = 0;
304    std::map<int, MD3Data*>::iterator it = data->sortedMap.begin();
305    while( it != data->sortedMap.end())
306    {
307      MD3Data* child = it->second;
308
309      //switch to child coord system
310      glPushMatrix();
311      glMultMatrixf(data->tmpMatrix[i]);
312
313      // and draw child
314      this->draw(child);
315
316      glPopMatrix();
317
318      i++;
319      it++;
320    }
321
322  }
323
324
325  /**
326   * draws the mesh
327   */
328  void MD3Model::drawMesh(MD3Mesh* mesh, sVec3D* frame) const
329  {
330    PRINTF(0)("drawMesh: %s\n", mesh->header->name);
331    Vector tmpVec1, tmpVec2;
332
333    glColor3f(1.0f, 1.0f, 1.0f);
334    glBegin( GL_TRIANGLES);
335
336    // upload all triangles in the frame to OpenGL
337    for( int t = 0; t < mesh->header->triangleNum; t++)
338    {
339      // calc normal vector
340      tmpVec1.x = frame[mesh->triangles[t].vertexOffset[1]][0] - frame[mesh->triangles[t].vertexOffset[0]][0];
341      tmpVec1.y = frame[mesh->triangles[t].vertexOffset[1]][1] - frame[mesh->triangles[t].vertexOffset[0]][1];
342      tmpVec1.z = frame[mesh->triangles[t].vertexOffset[1]][2] - frame[mesh->triangles[t].vertexOffset[0]][2];
343
344      tmpVec2.x = frame[mesh->triangles[t].vertexOffset[2]][0] - frame[mesh->triangles[t].vertexOffset[0]][0];
345      tmpVec2.y = frame[mesh->triangles[t].vertexOffset[2]][1] - frame[mesh->triangles[t].vertexOffset[0]][1];
346      tmpVec2.z = frame[mesh->triangles[t].vertexOffset[2]][2] - frame[mesh->triangles[t].vertexOffset[0]][2];
347
348      Vector normal = tmpVec1.cross(tmpVec2);
349      normal.normalize();
350
351//       PRINTF(0)("normal: %f, %f, %f\n", normal.x, normal.y, normal.z);
352
353      glNormal3f(normal.x, normal.y, normal.z);
354      glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[0]].textureCoord);
355      glVertex3f( frame[mesh->triangles[t].vertexOffset[0]][0],
356                  frame[mesh->triangles[t].vertexOffset[0]][2],
357                  frame[mesh->triangles[t].vertexOffset[0]][1]);
358
359      glNormal3f(normal.x, normal.y, normal.z);
360      glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[1]].textureCoord);
361      glVertex3f( frame[mesh->triangles[t].vertexOffset[1]][0],
362                  frame[mesh->triangles[t].vertexOffset[1]][2],
363                  frame[mesh->triangles[t].vertexOffset[1]][1]);
364
365      glNormal3f(normal.x, normal.y, normal.z);
366      glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[2]].textureCoord);
367      glVertex3f( frame[mesh->triangles[t].vertexOffset[2]][0],
368                  frame[mesh->triangles[t].vertexOffset[2]][2],
369                  frame[mesh->triangles[t].vertexOffset[2]][1]);
370    }
371    glEnd();
372  }
373
374
375  /**
376   *  drawo vertex normals
377   */
378  void MD3Model::drawVertexNormals(sVec3D* frame, MD3Normal* normals) const
379  {}
380
381
382  /**
383   * draw bone frame
384   */
385  void MD3Model::drawBoneFrame(MD3BoneFrame* frame) const
386  {
387    float x1 = frame->mins.x;
388    float y1 = frame->mins.y;
389    float z1 = frame->mins.z;
390    float x2 = frame->maxs.x;
391    float y2 = frame->maxs.y;
392    float z2 = frame->maxs.z;
393
394    glPushAttrib(GL_TEXTURE_2D);
395    glPushAttrib(GL_LIGHTING);
396
397    glColor3f(1.0f,0.0f,0.0f);
398    glPointSize(6.0f);
399
400    glBegin(GL_POINTS);
401    glVertex3f(frame->position.x, frame->position.y, frame->position.z);
402    glEnd();
403    glPointSize(1.0f);
404
405    glColor3f(0.0f,1.0f,0.0f);
406    glBegin(GL_LINE_LOOP);
407    glVertex3f(x1,y1,z1);
408    glVertex3f(x1,y1,z2);
409    glVertex3f(x1,y2,z2);
410    glVertex3f(x1,y2,z1);
411    glEnd();
412
413    glBegin(GL_LINE_LOOP);
414    glVertex3f(x2,y2,z2);
415    glVertex3f(x2,y1,z2);
416    glVertex3f(x2,y1,z1);
417    glVertex3f(x2,y2,z1);
418    glEnd();
419
420    glBegin(GL_LINES);
421    glVertex3f(x1,y1,z1);
422    glVertex3f(x2,y1,z1);
423
424    glVertex3f(x1,y1,z2);
425    glVertex3f(x2,y1,z2);
426
427    glVertex3f(x1,y2,z2);
428    glVertex3f(x2,y2,z2);
429
430    glVertex3f(x1,y2,z1);
431    glVertex3f(x2,y2,z1);
432    glEnd();
433
434     glPopAttrib();
435     glPopAttrib();
436  }
437
438
439  /**
440   *  interpolate bone frame
441   * @param currBoneFrame Start bone frame.
442   * @param nextBoneFrame End bone frame.
443   * @param frac Interpolation fraction, in [0,1].
444   */
445  MD3BoneFrame* MD3Model::interpolateBoneFrame(MD3Data* data, MD3BoneFrame* currBoneFrame, MD3BoneFrame* nextBoneFrame, float frac)
446  {
447    data->tmpBoneFrame->mins.x      = (1.0f - frac) * currBoneFrame->mins.x       + frac * nextBoneFrame->mins.x;
448    data->tmpBoneFrame->maxs.x      = (1.0f - frac) * currBoneFrame->maxs.x       + frac * nextBoneFrame->maxs.x;
449    data->tmpBoneFrame->position.= (1.0f - frac) * currBoneFrame->position.x   + frac * nextBoneFrame->position.x;
450    data->tmpBoneFrame->mins.y      = (1.0f - frac) * currBoneFrame->mins.y       + frac * nextBoneFrame->mins.y;
451    data->tmpBoneFrame->maxs.y      = (1.0f - frac) * currBoneFrame->maxs.y       + frac * nextBoneFrame->maxs.y;
452    data->tmpBoneFrame->position.= (1.0f - frac) * currBoneFrame->position.y   + frac * nextBoneFrame->position.y;
453    data->tmpBoneFrame->mins.z      = (1.0f - frac) * currBoneFrame->mins.z       + frac * nextBoneFrame->mins.z;
454    data->tmpBoneFrame->maxs.z      = (1.0f - frac) * currBoneFrame->maxs.z       + frac * nextBoneFrame->maxs.z;
455    data->tmpBoneFrame->position.= (1.0f - frac) * currBoneFrame->position.z   + frac * nextBoneFrame->position.z;
456
457    return data->tmpBoneFrame;
458  }
459
460
461
462  /**
463   * interpolate mesh frame
464   */
465  sVec3D* MD3Model::interpolateMeshFrame(MD3Data* data, sVec3D* currMeshFrame, sVec3D* nextMeshFrame, float frac, MD3Mesh* mesh, int i)
466  {
467    int vertexNum = mesh->header->vertexNum;
468
469    // calc interpolated vertices
470    for( int t = 0; t < vertexNum * 3.0f; t++)
471    {
472      data->tmpMesh[i][t][0]  = (1.0f - frac)   * currMeshFrame[t][0]  + frac * nextMeshFrame[t][0];
473      data->tmpMesh[i][t][1]  = (1.0f - frac)   * currMeshFrame[t][1]  + frac * nextMeshFrame[t][1];
474      data->tmpMesh[i][t][2]  = (1.0f - frac)   * currMeshFrame[t][2]  + frac * nextMeshFrame[t][2];
475    }
476
477    return data->tmpMesh[i];
478  }
479
480
481  /**
482   * interpolate vertex normal
483   */
484  MD3Normal* MD3Model::interpolateVertexNormals(MD3Data* data, MD3Normal* currNormals, MD3Normal* nextNormals, float frac, MD3Mesh* mesh, int i)
485  {
486    for( int j = 0; j < mesh->header->vertexNum; j++)
487    {
488      data->tmpNormal[i][j].vertexNormal[0] = (int)((1.0f - frac) * currNormals[j].vertexNormal[0] + frac * nextNormals[j].vertexNormal[0]);
489      data->tmpNormal[i][j].vertexNormal[1] = (int)((1.0f - frac) * currNormals[j].vertexNormal[1] + frac * nextNormals[j].vertexNormal[1]);
490    }
491
492    return data->tmpNormal[i];
493  }
494
495
496  /**
497   * interpolate transformation
498   */
499  float* MD3Model::interpolateTransformation(MD3Data* data, MD3Tag* currFrameTag, MD3Tag* nextFrameTag, float frac, int i)
500  {
501    // interpolate position
502    Vector interpolatedPosition = currFrameTag->position * (1.0f - frac) + nextFrameTag->position * frac;
503
504
505    // interpolate rotation matrix
506    float  currRot[4][4];
507    float  nextRot[4][4];
508    float  interpolatedMatrix[4][4];
509
510    Quaternion currQuat(currFrameTag->matrix); currQuat.matrix(currRot);
511    Quaternion nextQuat(nextFrameTag->matrix); nextQuat.matrix(nextRot);
512
513    Quaternion interpolatedQuat = Quaternion::quatSlerp(currQuat, nextQuat, frac); interpolatedQuat.matrix(interpolatedMatrix);
514
515    // quaternion code is column based, so use transposed matrix when spitting out to gl
516    data->tmpMatrix[i][0] = interpolatedMatrix[0][0];
517    data->tmpMatrix[i][4] = interpolatedMatrix[1][0];
518    data->tmpMatrix[i][8] = interpolatedMatrix[2][0];
519    data->tmpMatrix[i][12] = interpolatedPosition.x;
520    data->tmpMatrix[i][1] = interpolatedMatrix[0][1];
521    data->tmpMatrix[i][5] = interpolatedMatrix[1][1];
522    data->tmpMatrix[i][9] = interpolatedMatrix[2][1];
523    data->tmpMatrix[i][13] = interpolatedPosition.y;
524    data->tmpMatrix[i][2] = interpolatedMatrix[0][2];
525    data->tmpMatrix[i][6] = interpolatedMatrix[1][2];
526    data->tmpMatrix[i][10]= interpolatedMatrix[2][2];
527    data->tmpMatrix[i][14] = interpolatedPosition.z;
528    data->tmpMatrix[i][3] = 0.0f;
529    data->tmpMatrix[i][7] = 0.0f;
530    data->tmpMatrix[i][11]= 0.0f;
531    data->tmpMatrix[i][15] = 1.0f;
532
533    return data->tmpMatrix[i];
534
535  }
536
537
538
539
540  /**
541   * Create a new visitor to apply an animation operation (NEXT, REWIND, ...)
542   * to a MD3 model. The operation is executed in the context of the specified
543   * animation.
544   *
545   * @param anim The animation that provides the context for the operation.
546   * @param op The operation to apply.
547   * @param interpolate Should interpolation be done?
548   */
549  void MD3Model::interpolate(MD3Data* data, MD3Animation* anim, int op, bool interpolate)
550  {
551
552// //     data->
553//     this.anim=anim;
554//     if( op == NEXT || op == PREVIOUS || op == REWIND)
555//       this.op=op;
556//
557//     this.interpolate=interpolate;
558  }
559
560
561  /**
562   * calc next frame number
563   */
564  int MD3Model::next(int nr)
565  {
566#if 0
567    if( nr < (upperBound-1))
568      return nr+1;
569    else
570    { //rewind needed
571      if( anim.num < 0)
572        return anim.first;
573      else {
574        nr = (anim.looping != 0)?(anim.num - anim.looping):0;
575        return anim.first + nr;
576      }
577    }
578#endif
579  }
580
581
582  /**
583   * calc prev frame number
584   */
585  int MD3Model::prev(int nr)
586  {
587#if 0
588    if( nr == anim.first)
589      return upperBound-1;
590    else
591      return nr-1;
592#endif
593  }
594
595
596  /**
597   * apply the specified operation to the animation state data members of the model
598   * taking the specified animation into account
599   */
600  void MD3Model::doOp(MD3Data* data)
601  {
602#if 0
603    //anim to be applied could have illegal data with respect to this model,
604    //ignore anim in this case
605    if( anim.first >= model.boneFrameNum || anim.first < 0)
606      return;
607
608                //calc upper bound for animation frames in this model
609    if( anim.num < 0)
610      upperBound=model.boneFrameNum; //use all available frames
611    else
612      upperBound=model.boneFrameNum<(anim.first+anim.num)?model.boneFrameNum:(anim.first+anim.num);
613
614    switch (op) {
615      case NEXT:
616        if (interpolate) {
617          model.interpolationFraction+=FRACTION;
618          if (model.interpolationFraction>=1.0f) {
619            model.currentFrame=model.nextFrame;
620            model.nextFrame=next(model.nextFrame);
621            model.interpolationFraction=0.0f;
622          }
623        }
624        else {
625          model.currentFrame=model.nextFrame;
626          model.nextFrame=next(model.nextFrame);
627        }
628        break;
629      case PREVIOUS:
630        if (interpolate) {
631          model.interpolationFraction-=FRACTION;
632          if (model.interpolationFraction<0.0f) {
633            model.nextFrame=model.currentFrame;
634            model.currentFrame=prev(model.currentFrame);
635            model.interpolationFraction=0.8f;
636          }
637        }
638        else {
639          model.nextFrame=model.currentFrame;
640          model.currentFrame=prev(model.currentFrame);
641        }
642        break;
643      case REWIND:
644        model.currentFrame=anim.first;
645        model.nextFrame=next(model.currentFrame);
646        model.interpolationFraction=0.0f;
647        break;
648    }
649#endif
650  }
651
652
653
654}
Note: See TracBrowser for help on using the repository browser.