Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/md3/md3_data.cc @ 8761

Last change on this file since 8761 was 8724, checked in by bensch, 18 years ago

merged the bsp-model-stuff back here

File size: 15.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 "md3_data.h"
18
19#include "md3_bone_frame.h"
20#include "md3_tag.h"
21#include "md3_mesh.h"
22
23#include "material.h"
24
25#include "debug.h"
26
27namespace md3
28{
29
30/********************************************************************************
31 *   MD3Data                                                                    *
32 ********************************************************************************/
33
34/**
35  \brief simple constructor
36*/
37MD3Data::MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale)
38{
39  this->filename = modelFileName;
40
41  this->parentTagIndex = -1;
42  this->parent = NULL;
43
44  this->animationState.currentFrame = 0;
45  this->animationState.nextFrame = 1;
46  this->animationState.interpolationFraction = 0.0f;
47
48  this->animation = NULL;
49  this->bInterpolate = false;
50  this->upperBound = 0;
51
52  this->loadModel(modelFileName);
53//   this->loadSkin(skinFileName);
54  this->init();
55}
56
57
58/**
59  \brief simple destructor
60
61  this will clean out all the necessary data for a specific md2model
62*/
63MD3Data::~MD3Data()
64{
65  delete this->header;
66}
67
68
69/**
70 * init data
71 */
72void MD3Data::init()
73{
74  // create the temporary data to work with (interpolation data)
75  this->tmpBoneFrame = new MD3BoneFrame();
76
77  this->tmpMesh = new sVec3D*[this->header->meshNum];
78  for( int i = 0; i < this->header->meshNum; i++)
79    this->tmpMesh[i] = new sVec3D[this->meshes[i]->header->vertexNum];
80
81  this->tmpNormal = new MD3Normal*[this->header->meshNum];
82  for( int i = 0; i < this->header->meshNum; i++)
83    this->tmpNormal[i] = new MD3Normal[this->meshes[i]->header->vertexNum];
84
85  // there are at most 4 different models and submodels
86  this->tmpMatrix = new float*[4];
87  for( int i = 0; i < 4; i++)
88    this->tmpMatrix[i] = new float[16];
89
90}
91
92
93/**
94 * link a model at the specified tag position to this model. if the position is already
95 * occupied, the old submodel will be replaced
96 *
97 *  @param tagIndex: tag to link the submodel to
98 *  @param child: the submodel that should be linked to this model
99 */
100void MD3Data::addLinkedModel(int tagIndex, MD3Data* child)
101{
102  this->sortedMap[tagIndex] = child;
103  child->parentTagIndex = tagIndex;
104}
105
106
107
108/**
109 * Return the index of the tag with the given name for this model.
110 *
111 * This will return -1 if their is no such tag.
112 */
113 int MD3Data::getTagIndexByName(std::string tagName)
114{
115   int res = -1;
116
117   if( this->header->boneFrameNum > 0) {
118     MD3Tag** tags = this->boneFrames[0]->tags;
119     for( int i = 0; i < this->header->tagNum; i++)
120       if( tags[i]->name.find(tagName) == std::string::npos)
121         return i;
122   }
123
124   return res;
125 }
126
127
128
129/**
130  \brief this will load the whole model data (vertices, opengl command list, ...)
131* @param fileName: the name of the model file
132  \return true if success
133*/
134bool MD3Data::loadModel(const std::string& fileName)
135{
136  FILE *pFile;                            //file stream
137//  char* buffer;                           //buffer for frame data
138  int fileOffset = 0;                     // file data offset
139
140
141  //! @todo this chek should include deleting a loaded model (eventually)
142  if (fileName.empty())
143    return false;
144
145  PRINTF(0)("opening file: %s\n", fileName.c_str());
146  pFile = fopen(fileName.c_str(), "rb");
147  if( unlikely(!pFile))
148    {
149      PRINTF(1)("Couldn't open the MD3 File for loading. Exiting.\n");
150      return false;
151    }
152  fileOffset += this->readHeader(pFile, fileOffset);
153  /* check for the header version: make sure its a md2 file :) */
154  if( unlikely(this->header->version != MD3_VERSION) && unlikely(this->header->ident != MD3_IDENT))
155    {
156      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str());
157      return false;
158    }
159
160
161    // check if the filesize is correct
162    if( this->header->fileSize > this->header->tagStart &&
163        this->header->fileSize >= this->header->meshStart)
164    {
165      bool bBoneFrames, bTags, bMeshes;
166      bBoneFrames = ( this->header->boneFrameNum == 0);
167      bTags = ( this->header->tagNum == 0);
168      bMeshes = ( this->header->meshNum == 0);
169
170      // read different parts of the model in correct order
171      while( !(bBoneFrames && bTags && bMeshes))
172      {
173        printf("while, fileOffset = %i\n", fileOffset);
174        if( fileOffset == this->header->boneFrameStart && !bBoneFrames)
175        {
176          fileOffset += this->readBoneFrames(pFile, fileOffset);
177          bBoneFrames = true;
178        }
179        else if( fileOffset == this->header->tagStart && !bTags)
180        {
181          fileOffset += this->readTags(pFile, fileOffset);
182          bTags = true;
183        }
184        else if( fileOffset == this->header->meshStart && !bMeshes)
185        {
186          fileOffset += this->readMeshes(pFile, fileOffset);
187          bMeshes = true;
188        }
189      }
190
191    }
192
193  fclose(pFile);
194
195  return true;
196}
197
198
199/**
200  \brief loads the skin/material stuff
201* @param fileName: name of the skin file
202  \return true if success
203*/
204bool MD3Data::loadSkin(const std::string& fileName)
205{
206//   if( fileName.empty())
207//     {
208//       this->skinFileName = "";
209//       return false;
210//     }
211//
212//   this->skinFileName = fileName;
213//
214//   this->material.setName("md2ModelMaterial");
215//   this->material.setDiffuseMap(fileName);
216//   this->material.setIllum(3);
217//   this->material.setAmbient(1.0, 1.0, 1.0);
218
219  return true;
220}
221
222
223/**
224 * read heaader
225 */
226int MD3Data::readHeader(FILE* pFile, int fileOffset)
227{
228  PRINTF(0)("Reading Header\n");
229
230  this->header = new MD3Header;
231  fread(this->header, 1, sizeof(MD3Header), pFile);
232
233    //header debug:
234  PRINTF(0)("MD3 Header debug section======================================\n");
235  printf("ident: %i\n", this->header->ident);
236  printf("version: %i\n", this->header->version);
237  printf("filename: %s\n", this->header->filename);
238  printf("boneFrameNum: %i\n", this->header->boneFrameNum);
239  printf("tag number: %i\n", this->header->tagNum);
240  printf("mesh number: %i\n", this->header->meshNum);
241  printf("max tex num: %i\n", this->header->maxTexNum);
242  printf("bone frame start: %i\n", this->header->boneFrameStart);
243  printf("tag start: %i\n", this->header->tagStart);
244  printf("mesh start: %i\n", this->header->meshStart);
245  printf("fileSize: %i\n", this->header->fileSize);
246
247  return sizeof(MD3Header);
248}
249
250
251/**
252 * read bone frames
253 */
254int MD3Data::readBoneFrames(FILE* pFile, int fileOffset)
255{
256  PRINTF(0)("Reading Bone Frames\n");
257
258  this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum];
259
260  for( int i = 0; i < this->header->boneFrameNum; i++)
261  {
262    this->boneFrames[i] = new MD3BoneFrame(i);
263
264    MD3BoneFrameData* md = new MD3BoneFrameData;
265    fread(md, 1, sizeof(MD3BoneFrameData), pFile);
266    this->boneFrames[i]->data = md;
267  }
268
269  return this->header->boneFrameNum * sizeof(MD3BoneFrameData);
270}
271
272
273/**
274 * read the tags
275 */
276int MD3Data::readTags(FILE* pFile, int fileOffset)
277{
278  PRINTF(0)("Reading Tags\n");
279
280  for( int i = 0; i < this->header->boneFrameNum; i++)
281  {
282    this->boneFrames[i]->tags = new MD3Tag*[this->header->tagNum];
283
284    for( int j = 0; j < this->header->tagNum; j++)
285    {
286      this->boneFrames[i]->tags[j] = new MD3Tag();
287      MD3TagData* md = new MD3TagData;
288      fread(md, 1, sizeof(MD3TagData), pFile);
289      this->boneFrames[i]->tags[j]->data = md;
290
291      this->boneFrames[i]->tags[j]->name = std::string(this->boneFrames[i]->tags[j]->data->name);
292      this->boneFrames[i]->tags[j]->position = Vector( this->boneFrames[i]->tags[j]->data->position[0],
293                                                       this->boneFrames[i]->tags[j]->data->position[1],
294                                                       this->boneFrames[i]->tags[j]->data->position[2]);
295      for( int k = 0; k < 3; k++)
296        for( int l = 0; l < 3; l++)
297          this->boneFrames[i]->tags[j]->matrix[k][l] = this->boneFrames[i]->tags[j]->data->matrix[k][l];
298
299      //PRINTF(0)("Tag name: %s\n", this->boneFrames[i]->tags[j]->name.c_str());
300    }
301  }
302
303  return this->header->boneFrameNum * this->header->tagNum * sizeof(MD3TagData);
304}
305
306
307/**
308 * read meshes
309 */
310int MD3Data::readMeshes(FILE* pFile, int fileOffset)
311{
312  PRINTF(0)("Reading Mesh Data\n");
313
314  fileOffset = 0;
315
316  this->meshes = new MD3Mesh*[this->header->meshNum];
317
318  for( int i = 0; i < this->header->meshNum; i++)
319  {
320    this->meshes[i] = new MD3Mesh();
321
322    int localFileOffset = 0;
323    bool   bTriangles, bTexVecs, bVertices, bTextures;            //!< the parts that have been read so far
324
325    //start reading mesh data
326    MD3MeshHeader* md = new MD3MeshHeader;
327    fread(md, 1, sizeof(MD3MeshHeader), pFile);
328    this->meshes[i]->header = md;
329    localFileOffset += sizeof(MD3MeshHeader);
330
331    PRINTF(0)("MD3 Mesh Header debug section\n");
332    printf("ident: %i\n", md->id);
333    printf("filename: %s\n", md->name);
334    printf("meshFrameNum: %i\n", md->meshFrameNum);
335    printf("textureNum: %i\n", md->textureNum);
336    printf("vertexNum: %i \n", md->vertexNum);
337    printf("triangleNum: %i\n", md->triangleNum);
338    printf("triangleStart: %i\n", md->triangleStart);
339    printf("textureStart: %i\n", md->textureStart);
340    printf("texVecStart: %i\n", md->texVecStart);
341    printf("vertexStart: %i\n", md->vertexStart);
342    printf("fileSize: %i\n", md->meshSize);
343
344    if( unlikely(this->meshes[i]->header->id != MD3_IDENT))
345    {
346      PRINTF(1)("Wrong MD3 mesh file tag, file %s could be corrupt\n", this->filename.c_str());
347      return false;
348    }
349
350    // check which parts to be loaded
351    bTriangles = ( this->meshes[i]->header->triangleNum == 0);
352    bTexVecs = ( this->meshes[i]->header->vertexNum == 0);
353    bVertices = ( this->meshes[i]->header->meshFrameNum == 0);
354    bTextures = ( this->meshes[i]->header->textureNum == 0);
355
356    // now read the data block whise
357    while( !(bTriangles && bTexVecs && bVertices && bTextures))
358    {
359      PRINTF(0)("while2: localOffset = %i\n", localFileOffset);
360      if( localFileOffset == this->meshes[i]->header->triangleStart  && !bTriangles)
361      {
362        localFileOffset += this->readMeshTriangles(pFile, localFileOffset, i);
363        bTriangles = true;
364      }
365      else if( localFileOffset == this->meshes[i]->header->textureStart && !bTextures)
366      {
367        localFileOffset += this->readMeshTextures(pFile, localFileOffset, i);
368        bTextures = true;
369      }
370      else if( localFileOffset == this->meshes[i]->header->texVecStart && !bTexVecs)
371      {
372        localFileOffset += this->readMeshTexVecs(pFile, localFileOffset, i);
373        bTexVecs = true;
374      }
375      else if( localFileOffset == this->meshes[i]->header->vertexStart && !bVertices)
376      {
377        localFileOffset += this->readMeshVertices(pFile, localFileOffset, i);
378        bVertices = true;
379      }
380    }
381    fileOffset += localFileOffset;
382    PRINTF(0)("finished reading mesh %i, got %i of %i byes\n", i, localFileOffset, md->meshSize);
383  }
384  return fileOffset;
385}
386
387
388
389/**
390 * reading in the mesh triangles
391 */
392int MD3Data::readMeshTriangles(FILE* pFile, int fileOffset, int mesh)
393{
394  PRINTF(0)("Reading Mesh Triangles\n");
395  // create the memomry to save the triangles
396  this->meshes[mesh]->triangles = new MD3Triangle[this->meshes[mesh]->header->triangleNum];
397  fread(this->meshes[mesh]->triangles, 1, sizeof(MD3Triangle) * this->meshes[mesh]->header->triangleNum, pFile);
398
399//   for( int i = 0; i < this->meshes[mesh]->header->triangleNum; i++) {
400//     PRINTF(0)("Triangle read: %i, %i, %i\n", this->meshes[mesh]->triangles[i].vertexOffset[0], this->meshes[mesh]->triangles[i].vertexOffset[1]
401//         , this->meshes[mesh]->triangles[i].vertexOffset[2]);
402//   }
403
404  return this->meshes[mesh]->header->triangleNum * sizeof(MD3Triangle);
405}
406
407
408/**
409 * reading in the mesh textures
410 */
411int MD3Data::readMeshTextures(FILE* pFile, int fileOffset, int mesh)
412{
413  PRINTF(0)("Reading Mesh Textures\n");
414
415  // create the textures
416  this->meshes[mesh]->material = new Material[this->meshes[mesh]->header->textureNum];
417
418  MD3Texture* tex = new MD3Texture[this->meshes[mesh]->header->textureNum];
419  fread(tex, 1, sizeof(MD3Texture) * this->meshes[mesh]->header->textureNum, pFile);
420
421  for( int i = 0; i < this->meshes[mesh]->header->textureNum; i++) {
422    PRINTF(0)(" texture file: %s\n", tex[i].fileName);
423#warning texture stuff hard coded. make this again
424    std::string path("/home/boenzlip/tmp/q3/Downloads/MOH/q3mdl-alien3/");
425    std::string path1(tex[i].fileName);
426    std::string fullPath( path + path1);
427    this->meshes[mesh]->material[i].setDiffuseMap(/*tex[i].fileName*/ /*fullPath.c_str()*/ "maps/creatures/gork/gorkup.tga");
428    this->meshes[mesh]->material[i].setAmbient(1, 1, 1);
429  }
430
431  return this->meshes[mesh]->header->textureNum * sizeof(MD3Texture);
432}
433
434
435/**
436 * reading in the mesh tex vecs
437 */
438int MD3Data::readMeshTexVecs(FILE* pFile, int fileOffset, int mesh)
439{
440  PRINTF(0)("Reading Mesh TexVecs\n");
441
442  this->meshes[mesh]->texVecs = new MD3TexVecs[this->meshes[mesh]->header->vertexNum];
443  fread(this->meshes[mesh]->texVecs, 1, sizeof(MD3TexVecs) * this->meshes[mesh]->header->vertexNum, pFile);
444
445//   for( int i = 0; i < this->meshes[mesh]->header->vertexNum; i++)  {
446//     PRINTF(0)("TexVec read: %f, %f\n", this->meshes[mesh]->texVecs[i].textureCoord[0], this->meshes[mesh]->texVecs[i].textureCoord[0]);
447//   }
448
449  return this->meshes[mesh]->header->vertexNum * sizeof(MD3TexVecs);
450}
451
452
453/**
454 * reading in the mesh vertices
455 */
456int MD3Data::readMeshVertices(FILE* pFile, int fileOffset, int mesh)
457{
458  PRINTF(0)("Reading Mesh Vertices\n");
459
460  // reserver memory for the vertex informations
461  this->meshes[mesh]->meshFrames = new sVec3D*[this->meshes[mesh]->header->meshFrameNum]; // * this->meshes[mesh]->header->vertexNum
462  this->meshes[mesh]->normals = new MD3Normal*[this->meshes[mesh]->header->meshFrameNum];
463
464  for( int i = 0; i < this->meshes[mesh]->header->meshFrameNum; i++)
465  {
466    this->meshes[mesh]->meshFrames[i] = new sVec3D[this->meshes[mesh]->header->vertexNum];
467    this->meshes[mesh]->normals[i] = new MD3Normal[this->meshes[mesh]->header->vertexNum];
468
469    for( int j = 0; j < this->meshes[mesh]->header->vertexNum; j++)
470    {
471      // read out the compressed data
472      MD3VertexCompressed* vc = new MD3VertexCompressed;
473      fread(vc, 1, sizeof(MD3VertexCompressed), pFile);
474
475      this->meshes[mesh]->meshFrames[i][j][0] = (float)vc->vector[0] / 64.0f;
476      this->meshes[mesh]->meshFrames[i][j][1] = (float)vc->vector[1] / 64.0f;
477      this->meshes[mesh]->meshFrames[i][j][2] = (float)vc->vector[2] / 64.0f;
478
479      this->meshes[mesh]->normals[i][j].vertexNormal[0] = (int)vc->vertexNormal[0];
480      this->meshes[mesh]->normals[i][j].vertexNormal[1] = (int)vc->vertexNormal[1];
481
482//     PRINTF(0)("nr: %i, meshframes: %f, %f, %f, normal: %f, %f\n", i, this->meshes[mesh]->meshFrames[i][0], this->meshes[mesh]->meshFrames[i][1],
483//     this->meshes[mesh]->meshFrames[i][2], this->meshes[mesh]->normals[i].vertexNormal[0], this->meshes[mesh]->normals[i].vertexNormal[1]);
484
485      delete vc;
486    }
487  }
488
489  return this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum * sizeof(MD3VertexCompressed);
490}
491
492
493
494}
495
496
497
498
499
500
501
Note: See TracBrowser for help on using the repository browser.