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
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
26namespace md3
27{
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);
42//   this->loadSkin(skinFileName);
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
66//  char* buffer;                           //buffer for frame data
67  int fileOffset = 0;                     // file data offset
68
69
70
71  //! @todo this chek should include deleting a loaded model (eventually)
72  if (fileName.empty())
73    return false;
74
75  PRINTF(0)("opening file: %s\n", fileName.c_str());
76  pFile = fopen(fileName.c_str(), "rb");
77  if( unlikely(!pFile))
78    {
79      PRINTF(1)("Couldn't open the MD3 File for loading. Exiting.\n");
80      return false;
81    }
82  fileOffset += this->readHeader(pFile, fileOffset);
83  /* check for the header version: make sure its a md2 file :) */
84  if( unlikely(this->header->version != MD3_VERSION) && unlikely(this->header->ident != MD3_IDENT))
85    {
86      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str());
87      return false;
88    }
89
90
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      {
103        printf("while, fileOffset = %i\n", fileOffset);
104        if( fileOffset == this->header->boneFrameStart && !bBoneFrames)
105        {
106          fileOffset += this->readBoneFrames(pFile, fileOffset);
107          bBoneFrames = true;
108        }
109        else if( fileOffset == this->header->tagStart && !bTags)
110        {
111          fileOffset += this->readTags(pFile, fileOffset);
112          bTags = true;
113        }
114        else if( fileOffset == this->header->meshStart && !bMeshes)
115        {
116          fileOffset += this->readMeshes(pFile, fileOffset);
117          bMeshes = true;
118        }
119      }
120
121    }
122
123  fclose(pFile);
124
125  return true;
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{
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);
148
149  return true;
150}
151
152
153/**
154 * read heaader
155 */
156int MD3Data::readHeader(FILE* pFile, int fileOffset)
157{
158  PRINTF(0)("Reading Header\n");
159
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);
178}
179
180
181/**
182 * read bone frames
183 */
184int MD3Data::readBoneFrames(FILE* pFile, int fileOffset)
185{
186  PRINTF(0)("Reading Bone Frames\n");
187
188  this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum];
189
190  for( int i = 0; i < this->header->boneFrameNum; i++)
191  {
192    this->boneFrames[i] = new MD3BoneFrame(i);
193
194    MD3BoneFrameData* md = new MD3BoneFrameData;
195    fread(md, 1, sizeof(MD3BoneFrameData), pFile);
196    this->boneFrames[i]->data = md;
197  }
198
199  return this->header->boneFrameNum * sizeof(MD3BoneFrameData);
200}
201
202
203/**
204 * read the tags
205 */
206int MD3Data::readTags(FILE* pFile, int fileOffset)
207{
208  PRINTF(0)("Reading Tags\n");
209//  this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum];
210
211  for( int i = 0; i < this->header->boneFrameNum; i++)
212  {
213    this->boneFrames[i]->tags = new MD3Tag*[this->header->tagNum];
214
215    for( int j = 0; j < this->header->tagNum; j++)
216    {
217      this->boneFrames[i]->tags[j] = new MD3Tag();
218      MD3TagData* md = new MD3TagData;
219      fread(md, 1, sizeof(MD3TagData), pFile);
220      this->boneFrames[i]->tags[j]->data = md;
221    }
222  }
223
224  return this->header->boneFrameNum * this->header->tagNum * sizeof(MD3TagData);
225}
226
227
228/**
229 * read meshes
230 */
231int MD3Data::readMeshes(FILE* pFile, int fileOffset)
232{
233  PRINTF(0)("Reading Mesh Data\n");
234
235  int    localFileOffset = 0;                                      //!< local file offset
236
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
243    localFileOffset = 0;
244    bool   bTriangles, bTexVecs, bVertices, bTextures;            //!< the parts that have been read so far
245
246    //start reading mesh data
247    MD3MeshHeader* md = new MD3MeshHeader;
248    fread(md, 1, sizeof(MD3MeshHeader), pFile);
249    this->meshes[i]->header = md;
250    localFileOffset += sizeof(MD3MeshHeader);
251
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
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
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    {
280      PRINTF(0)("while2: localOffset = %i\n", localFileOffset);
281      if( localFileOffset == this->meshes[i]->header->triangleStart  && !bTriangles)
282      {
283        localFileOffset += this->readMeshTriangles(pFile, localFileOffset, i);
284        bTriangles = true;
285      }
286      else if( localFileOffset == this->meshes[i]->header->textureStart && !bTextures)
287      {
288        localFileOffset += this->readMeshTextures(pFile, localFileOffset, i);
289        bTextures = true;
290      }
291      else if( localFileOffset == this->meshes[i]->header->texVecStart && !bTexVecs)
292      {
293        localFileOffset += this->readMeshTexVecs(pFile, localFileOffset, i);
294        bTexVecs = true;
295      }
296      else if( localFileOffset == this->meshes[i]->header->vertexStart && !bVertices)
297      {
298        localFileOffset += this->readMeshVertices(pFile, localFileOffset, i);
299        bVertices = true;
300      }
301    }
302  }
303  return localFileOffset;
304}
305
306
307
308/**
309 * reading in the mesh triangles
310 */
311int MD3Data::readMeshTriangles(FILE* pFile, int fileOffset, int mesh)
312{
313  PRINTF(0)("Reading Mesh Triangles\n");
314  // create the memomry to save the triangles
315  this->meshes[mesh]->triangles = new MD3Triangle[this->meshes[mesh]->header->triangleNum];
316  fread(this->meshes[mesh]->triangles, 1, sizeof(MD3Triangle), pFile);
317
318  return this->meshes[mesh]->header->triangleNum * sizeof(MD3Triangle);
319}
320
321
322/**
323 * reading in the mesh textures
324 */
325int MD3Data::readMeshTextures(FILE* pFile, int fileOffset, int mesh)
326{
327  PRINTF(0)("Reading Mesh Textures\n");
328
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];
333  fread(tex, 1, sizeof(MD3Texture), pFile);
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);
342}
343
344
345/**
346 * reading in the mesh tex vecs
347 */
348int MD3Data::readMeshTexVecs(FILE* pFile, int fileOffset, int mesh)
349{
350  PRINTF(0)("Reading Mesh TexVecs\n");
351
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);
356}
357
358
359/**
360 * reading in the mesh vertices
361 */
362int MD3Data::readMeshVertices(FILE* pFile, int fileOffset, int mesh)
363{
364  PRINTF(0)("Reading Mesh Vertices\n");
365
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);
394}
395
396}
397
398
399
400
401
402
403
Note: See TracBrowser for help on using the repository browser.