Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/md3/md3_data.cc @ 8428

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

bsp: texvecs, textures and triangles read well

File size: 11.5 KB
RevLine 
[8344]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
[8357]19#include "md3_bone_frame.h"
[8371]20#include "md3_tag.h"
[8372]21#include "md3_mesh.h"
[8344]22
[8418]23#include "material.h"
[8357]24
[8418]25
[8351]26namespace md3
27{
[8344]28
29/********************************************************************************
30 *   MD3Data                                                                    *
31 ********************************************************************************/
32
33/**
34  \brief simple constructor
35*/
36MD3Data::MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale)
37{
38
39
40
41  this->loadModel(modelFileName);
[8354]42//   this->loadSkin(skinFileName);
[8344]43}
44
45
46/**
47  \brief simple destructor
48
49  this will clean out all the necessary data for a specific md2model
50*/
51MD3Data::~MD3Data()
52{
53  delete this->header;
54}
55
56
57
58/**
59  \brief this will load the whole model data (vertices, opengl command list, ...)
60* @param fileName: the name of the model file
61  \return true if success
62*/
63bool MD3Data::loadModel(const std::string& fileName)
64{
65  FILE *pFile;                            //file stream
[8353]66//  char* buffer;                           //buffer for frame data
[8354]67  int fileOffset = 0;                     // file data offset
[8346]68
69
[8344]70
71  //! @todo this chek should include deleting a loaded model (eventually)
72  if (fileName.empty())
73    return false;
74
[8354]75  PRINTF(0)("opening file: %s\n", fileName.c_str());
[8344]76  pFile = fopen(fileName.c_str(), "rb");
77  if( unlikely(!pFile))
78    {
[8347]79      PRINTF(1)("Couldn't open the MD3 File for loading. Exiting.\n");
[8344]80      return false;
81    }
[8357]82  fileOffset += this->readHeader(pFile, fileOffset);
[8344]83  /* check for the header version: make sure its a md2 file :) */
[8354]84  if( unlikely(this->header->version != MD3_VERSION) && unlikely(this->header->ident != MD3_IDENT))
[8344]85    {
86      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str());
87      return false;
88    }
89
[8353]90
[8354]91    // check if the filesize is correct
92    if( this->header->fileSize > this->header->tagStart &&
93        this->header->fileSize >= this->header->meshStart)
94    {
95      bool bBoneFrames, bTags, bMeshes;
96      bBoneFrames = ( this->header->boneFrameNum == 0);
97      bTags = ( this->header->tagNum == 0);
98      bMeshes = ( this->header->meshNum == 0);
99
100      // read different parts of the model in correct order
101      while( !(bBoneFrames && bTags && bMeshes))
102      {
[8426]103        printf("while, fileOffset = %i\n", fileOffset);
[8354]104        if( fileOffset == this->header->boneFrameStart && !bBoneFrames)
[8355]105        {
106          fileOffset += this->readBoneFrames(pFile, fileOffset);
107          bBoneFrames = true;
108        }
[8354]109        else if( fileOffset == this->header->tagStart && !bTags)
[8355]110        {
111          fileOffset += this->readTags(pFile, fileOffset);
112          bTags = true;
113        }
[8354]114        else if( fileOffset == this->header->meshStart && !bMeshes)
[8355]115        {
116          fileOffset += this->readMeshes(pFile, fileOffset);
117          bMeshes = true;
118        }
[8354]119      }
120
121    }
122
[8344]123  fclose(pFile);
[8347]124
125  return true;
[8344]126}
127
128
129/**
130  \brief loads the skin/material stuff
131* @param fileName: name of the skin file
132  \return true if success
133*/
134bool MD3Data::loadSkin(const std::string& fileName)
135{
[8357]136//   if( fileName.empty())
137//     {
138//       this->skinFileName = "";
139//       return false;
140//     }
141//
142//   this->skinFileName = fileName;
143//
144//   this->material.setName("md2ModelMaterial");
145//   this->material.setDiffuseMap(fileName);
146//   this->material.setIllum(3);
147//   this->material.setAmbient(1.0, 1.0, 1.0);
[8344]148
[8347]149  return true;
[8344]150}
[8346]151
152
153/**
154 * read heaader
155 */
[8355]156int MD3Data::readHeader(FILE* pFile, int fileOffset)
157{
[8426]158  PRINTF(0)("Reading Header\n");
159
[8357]160  this->header = new MD3Header;
161  fread(this->header, 1, sizeof(MD3Header), pFile);
162
163    //header debug:
164  PRINTF(0)("MD3 Header debug section======================================\n");
165  printf("ident: %i\n", this->header->ident);
166  printf("version: %i\n", this->header->version);
167  printf("filename: %s\n", this->header->filename);
168  printf("boneFrameNum: %i\n", this->header->boneFrameNum);
169  printf("tag number: %i\n", this->header->tagNum);
170  printf("mesh number: %i\n", this->header->meshNum);
171  printf("max tex num: %i\n", this->header->maxTexNum);
172  printf("bone frame start: %i\n", this->header->boneFrameStart);
173  printf("tag start: %i\n", this->header->tagStart);
174  printf("mesh start: %i\n", this->header->meshStart);
175  printf("fileSize: %i\n", this->header->fileSize);
176
177  return sizeof(MD3Header);
[8355]178}
[8346]179
180
181/**
182 * read bone frames
183 */
[8355]184int MD3Data::readBoneFrames(FILE* pFile, int fileOffset)
185{
[8426]186  PRINTF(0)("Reading Bone Frames\n");
187
[8357]188  this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum];
189
190  for( int i = 0; i < this->header->boneFrameNum; i++)
[8358]191  {
[8357]192    this->boneFrames[i] = new MD3BoneFrame(i);
193
[8358]194    MD3BoneFrameData* md = new MD3BoneFrameData;
195    fread(md, 1, sizeof(MD3BoneFrameData), pFile);
[8372]196    this->boneFrames[i]->data = md;
[8358]197  }
198
199  return this->header->boneFrameNum * sizeof(MD3BoneFrameData);
[8355]200}
[8346]201
202
203/**
204 * read the tags
205 */
[8355]206int MD3Data::readTags(FILE* pFile, int fileOffset)
207{
[8426]208  PRINTF(0)("Reading Tags\n");
[8359]209//  this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum];
210
211  for( int i = 0; i < this->header->boneFrameNum; i++)
212  {
[8371]213    this->boneFrames[i]->tags = new MD3Tag*[this->header->tagNum];
214
[8359]215    for( int j = 0; j < this->header->tagNum; j++)
216    {
[8371]217      this->boneFrames[i]->tags[j] = new MD3Tag();
218      MD3TagData* md = new MD3TagData;
219      fread(md, 1, sizeof(MD3TagData), pFile);
[8372]220      this->boneFrames[i]->tags[j]->data = md;
[8359]221    }
222  }
223
[8371]224  return this->header->boneFrameNum * this->header->tagNum * sizeof(MD3TagData);
[8355]225}
[8346]226
227
228/**
229 * read meshes
230 */
[8355]231int MD3Data::readMeshes(FILE* pFile, int fileOffset)
232{
[8426]233  PRINTF(0)("Reading Mesh Data\n");
[8375]234
[8426]235  int    localFileOffset = 0;                                      //!< local file offset
236
[8372]237  this->meshes = new MD3Mesh*[this->header->meshNum];
238
239  for( int i = 0; i < this->header->meshNum; i++)
240  {
241    this->meshes[i] = new MD3Mesh();
242
[8426]243    localFileOffset = 0;
[8375]244    bool   bTriangles, bTexVecs, bVertices, bTextures;            //!< the parts that have been read so far
[8374]245
246    //start reading mesh data
[8375]247    MD3MeshHeader* md = new MD3MeshHeader;
248    fread(md, 1, sizeof(MD3MeshHeader), pFile);
249    this->meshes[i]->header = md;
250    localFileOffset += sizeof(MD3MeshHeader);
[8374]251
[8375]252    if( unlikely(this->meshes[i]->header->id != MD3_IDENT))
253    {
254      PRINTF(1)("Wrong MD3 mesh file tag, file %s could be corrupt\n", this->filename.c_str());
255      return false;
256    }
257
[8426]258    PRINTF(0)("MD3 Mesh Header debug section\n");
259    printf("ident: %i\n", md->id);
260    printf("filename: %s\n", md->name);
261    printf("meshFrameNum: %i\n", md->meshFrameNum);
262    printf("textureNum: %i\n", md->textureNum);
263    printf("vertexNum: %i \n", md->vertexNum);
264    printf("triangleNum: %i\n", md->triangleNum);
265    printf("triangleStart: %i\n", md->triangleStart);
266    printf("textureStart: %i\n", md->textureStart);
267    printf("texVecStart: %i\n", md->texVecStart);
268    printf("vertexStart: %i\n", md->vertexStart);
269    printf("fileSize: %i\n", md->meshSize);
270
[8375]271    // check which parts to be loaded
272    bTriangles = ( this->meshes[i]->header->triangleNum == 0);
273    bTexVecs = ( this->meshes[i]->header->vertexNum == 0);
274    bVertices = ( this->meshes[i]->header->meshFrameNum == 0);
275    bTextures = ( this->meshes[i]->header->textureNum == 0);
276
277    // now read the data block whise
278    while( !(bTriangles && bTexVecs && bVertices && bTextures))
279    {
[8426]280      PRINTF(0)("while2: localOffset = %i\n", localFileOffset);
[8375]281      if( localFileOffset == this->meshes[i]->header->triangleStart  && !bTriangles)
282      {
[8382]283        localFileOffset += this->readMeshTriangles(pFile, localFileOffset, i);
[8375]284        bTriangles = true;
285      }
286      else if( localFileOffset == this->meshes[i]->header->textureStart && !bTextures)
287      {
[8428]288        localFileOffset += this->readMeshTextures(pFile, localFileOffset, i);
[8375]289        bTextures = true;
290      }
291      else if( localFileOffset == this->meshes[i]->header->texVecStart && !bTexVecs)
292      {
[8382]293        localFileOffset += this->readMeshTexVecs(pFile, localFileOffset, i);
[8375]294        bTexVecs = true;
295      }
[8428]296      else if( localFileOffset == this->meshes[i]->header->vertexStart && !bVertices)
[8375]297      {
[8382]298        localFileOffset += this->readMeshVertices(pFile, localFileOffset, i);
[8375]299        bVertices = true;
300      }
301    }
[8372]302  }
[8426]303  return localFileOffset;
[8355]304}
[8351]305
[8382]306
307
[8418]308/**
309 * reading in the mesh triangles
310 */
[8382]311int MD3Data::readMeshTriangles(FILE* pFile, int fileOffset, int mesh)
312{
[8426]313  PRINTF(0)("Reading Mesh Triangles\n");
[8416]314  // create the memomry to save the triangles
[8418]315  this->meshes[mesh]->triangles = new MD3Triangle[this->meshes[mesh]->header->triangleNum];
316  fread(this->meshes[mesh]->triangles, 1, sizeof(MD3Triangle), pFile);
[8416]317
318  return this->meshes[mesh]->header->triangleNum * sizeof(MD3Triangle);
[8353]319}
320
[8418]321
322/**
323 * reading in the mesh textures
324 */
[8382]325int MD3Data::readMeshTextures(FILE* pFile, int fileOffset, int mesh)
[8416]326{
[8426]327  PRINTF(0)("Reading Mesh Textures\n");
328
[8418]329  // create the textures
330  this->meshes[mesh]->material = new Material[this->meshes[mesh]->header->textureNum];
331
332  MD3Texture* tex = new MD3Texture[this->meshes[mesh]->header->textureNum];
[8427]333  fread(tex, 1, sizeof(MD3Texture), pFile);
[8418]334
335  for( int i = 0; i < this->meshes[mesh]->header->textureNum; i++) {
336    PRINTF(0)(" texture file: %s\n", tex[i].fileName);
337    this->meshes[mesh]->material[i].setDiffuseMap(tex[i].fileName);
338    this->meshes[mesh]->material[i].setAmbient(1, 1, 1);
339  }
340
341  return this->meshes[mesh]->header->textureNum * sizeof(MD3Texture);
[8416]342}
[8382]343
[8418]344
345/**
346 * reading in the mesh tex vecs
347 */
[8382]348int MD3Data::readMeshTexVecs(FILE* pFile, int fileOffset, int mesh)
[8416]349{
[8426]350  PRINTF(0)("Reading Mesh TexVecs\n");
[8428]351
[8420]352  this->meshes[mesh]->texVecs = new MD3TexVecs[this->meshes[mesh]->header->vertexNum];
353  fread(this->meshes[mesh]->texVecs, 1, sizeof(MD3TexVecs), pFile);
354
355  return this->meshes[mesh]->header->vertexNum * sizeof(MD3TexVecs);
[8416]356}
[8382]357
[8418]358
359/**
360 * reading in the mesh vertices
361 */
[8382]362int MD3Data::readMeshVertices(FILE* pFile, int fileOffset, int mesh)
[8416]363{
[8426]364  PRINTF(0)("Reading Mesh Vertices\n");
365
[8423]366  // reserver memory for the vertex informations
367  this->meshes[mesh]->meshFrames = new sVec3D[this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum];
368  this->meshes[mesh]->normals = new MD3Normal[this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum];
369
370  // read out the compressed data
371  MD3VertexCompressed* vc = new MD3VertexCompressed[this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum];
372  fread(vc, 1, sizeof(MD3VertexCompressed), pFile);
373
374  int index;
375
376  for( int i = 0; i < this->meshes[mesh]->header->meshFrameNum; i++)
377  {
378    for( int j = 0; j < this->meshes[mesh]->header->vertexNum; j++)
379    {
380      index = i * this->meshes[mesh]->header->meshFrameNum + j;
381      this->meshes[mesh]->meshFrames[index][0] = (float)vc[index].vector[0] / 64.0f;
382      this->meshes[mesh]->meshFrames[index][1] = (float)vc[index].vector[1] / 64.0f;
383      this->meshes[mesh]->meshFrames[index][2] = (float)vc[index].vector[2] / 64.0f;
384
385      this->meshes[mesh]->normals[index].vertexNormal[0] = vc[index].vertexNormal[0];
386      this->meshes[mesh]->normals[index].vertexNormal[1] = vc[index].vertexNormal[1];
387    }
388  }
389
390  // delete the temp memory again
391  delete [] vc;
392
393  return this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum * sizeof(MD3VertexCompressed);
[8416]394}
[8382]395
396}
397
398
399
400
401
402
403
Note: See TracBrowser for help on using the repository browser.