Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bsp: md3 model data read in as it seams correctly. no drawing yet! interactive model introduced

File size: 11.4 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
210  for( int i = 0; i < this->header->boneFrameNum; i++)
211  {
212    this->boneFrames[i]->tags = new MD3Tag*[this->header->tagNum];
213
214    for( int j = 0; j < this->header->tagNum; j++)
215    {
216      this->boneFrames[i]->tags[j] = new MD3Tag();
217      MD3TagData* md = new MD3TagData;
218      fread(md, 1, sizeof(MD3TagData), pFile);
219      this->boneFrames[i]->tags[j]->data = md;
220    }
221  }
222
223  return this->header->boneFrameNum * this->header->tagNum * sizeof(MD3TagData);
224}
225
226
227/**
228 * read meshes
229 */
230int MD3Data::readMeshes(FILE* pFile, int fileOffset)
231{
232  PRINTF(0)("Reading Mesh Data\n");
233
234  fileOffset = 0;
235
236  this->meshes = new MD3Mesh*[this->header->meshNum];
237
238  for( int i = 0; i < this->header->meshNum; i++)
239  {
240    this->meshes[i] = new MD3Mesh();
241
242    int localFileOffset = 0;
243    bool   bTriangles, bTexVecs, bVertices, bTextures;            //!< the parts that have been read so far
244
245    //start reading mesh data
246    MD3MeshHeader* md = new MD3MeshHeader;
247    fread(md, 1, sizeof(MD3MeshHeader), pFile);
248    this->meshes[i]->header = md;
249    localFileOffset += sizeof(MD3MeshHeader);
250
251    PRINTF(0)("MD3 Mesh Header debug section\n");
252    printf("ident: %i\n", md->id);
253    printf("filename: %s\n", md->name);
254    printf("meshFrameNum: %i\n", md->meshFrameNum);
255    printf("textureNum: %i\n", md->textureNum);
256    printf("vertexNum: %i \n", md->vertexNum);
257    printf("triangleNum: %i\n", md->triangleNum);
258    printf("triangleStart: %i\n", md->triangleStart);
259    printf("textureStart: %i\n", md->textureStart);
260    printf("texVecStart: %i\n", md->texVecStart);
261    printf("vertexStart: %i\n", md->vertexStart);
262    printf("fileSize: %i\n", md->meshSize);
263
264    if( unlikely(this->meshes[i]->header->id != MD3_IDENT))
265    {
266      PRINTF(1)("Wrong MD3 mesh file tag, file %s could be corrupt\n", this->filename.c_str());
267      return false;
268    }
269
270    // check which parts to be loaded
271    bTriangles = ( this->meshes[i]->header->triangleNum == 0);
272    bTexVecs = ( this->meshes[i]->header->vertexNum == 0);
273    bVertices = ( this->meshes[i]->header->meshFrameNum == 0);
274    bTextures = ( this->meshes[i]->header->textureNum == 0);
275
276    // now read the data block whise
277    while( !(bTriangles && bTexVecs && bVertices && bTextures))
278    {
279      PRINTF(0)("while2: localOffset = %i\n", localFileOffset);
280      if( localFileOffset == this->meshes[i]->header->triangleStart  && !bTriangles)
281      {
282        localFileOffset += this->readMeshTriangles(pFile, localFileOffset, i);
283        bTriangles = true;
284      }
285      else if( localFileOffset == this->meshes[i]->header->textureStart && !bTextures)
286      {
287        localFileOffset += this->readMeshTextures(pFile, localFileOffset, i);
288        bTextures = true;
289      }
290      else if( localFileOffset == this->meshes[i]->header->texVecStart && !bTexVecs)
291      {
292        localFileOffset += this->readMeshTexVecs(pFile, localFileOffset, i);
293        bTexVecs = true;
294      }
295      else if( localFileOffset == this->meshes[i]->header->vertexStart && !bVertices)
296      {
297        localFileOffset += this->readMeshVertices(pFile, localFileOffset, i);
298        bVertices = true;
299      }
300    }
301    fileOffset += localFileOffset;
302    PRINTF(0)("finished reading mesh %i, got %i of %i byes\n", i, localFileOffset, md->meshSize);
303  }
304  return fileOffset;
305}
306
307
308
309/**
310 * reading in the mesh triangles
311 */
312int MD3Data::readMeshTriangles(FILE* pFile, int fileOffset, int mesh)
313{
314  PRINTF(0)("Reading Mesh Triangles\n");
315  // create the memomry to save the triangles
316  this->meshes[mesh]->triangles = new MD3Triangle[this->meshes[mesh]->header->triangleNum];
317  fread(this->meshes[mesh]->triangles, 1, sizeof(MD3Triangle) * this->meshes[mesh]->header->triangleNum, pFile);
318
319  return this->meshes[mesh]->header->triangleNum * sizeof(MD3Triangle);
320}
321
322
323/**
324 * reading in the mesh textures
325 */
326int MD3Data::readMeshTextures(FILE* pFile, int fileOffset, int mesh)
327{
328  PRINTF(0)("Reading Mesh Textures\n");
329
330  // create the textures
331  this->meshes[mesh]->material = new Material[this->meshes[mesh]->header->textureNum];
332
333  MD3Texture* tex = new MD3Texture[this->meshes[mesh]->header->textureNum];
334  fread(tex, 1, sizeof(MD3Texture) * this->meshes[mesh]->header->textureNum, pFile);
335
336  for( int i = 0; i < this->meshes[mesh]->header->textureNum; i++) {
337    PRINTF(0)(" texture file: %s\n", tex[i].fileName);
338    this->meshes[mesh]->material[i].setDiffuseMap(tex[i].fileName);
339    this->meshes[mesh]->material[i].setAmbient(1, 1, 1);
340  }
341
342  return this->meshes[mesh]->header->textureNum * sizeof(MD3Texture);
343}
344
345
346/**
347 * reading in the mesh tex vecs
348 */
349int MD3Data::readMeshTexVecs(FILE* pFile, int fileOffset, int mesh)
350{
351  PRINTF(0)("Reading Mesh TexVecs\n");
352
353  this->meshes[mesh]->texVecs = new MD3TexVecs[this->meshes[mesh]->header->vertexNum];
354  fread(this->meshes[mesh]->texVecs, 1, sizeof(MD3TexVecs) * this->meshes[mesh]->header->vertexNum, pFile);
355
356  return this->meshes[mesh]->header->vertexNum * sizeof(MD3TexVecs);
357}
358
359
360/**
361 * reading in the mesh vertices
362 */
363int MD3Data::readMeshVertices(FILE* pFile, int fileOffset, int mesh)
364{
365  PRINTF(0)("Reading Mesh Vertices\n");
366
367  // reserver memory for the vertex informations
368  this->meshes[mesh]->meshFrames = new sVec3D[this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum];
369  this->meshes[mesh]->normals = new MD3Normal[this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum];
370
371  for( int i = 0; i < this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum; i++)
372  {
373    // read out the compressed data
374    MD3VertexCompressed* vc = new MD3VertexCompressed;
375    fread(vc, 1, sizeof(MD3VertexCompressed), pFile);
376
377    this->meshes[mesh]->meshFrames[i][0] = (float)vc->vector[0] / 64.0f;
378    this->meshes[mesh]->meshFrames[i][1] = (float)vc->vector[1] / 64.0f;
379    this->meshes[mesh]->meshFrames[i][2] = (float)vc->vector[2] / 64.0f;
380
381    this->meshes[mesh]->normals[i].vertexNormal[0] = vc->vertexNormal[0];
382    this->meshes[mesh]->normals[i].vertexNormal[1] = vc->vertexNormal[1];
383
384    delete vc;
385  }
386
387  // delete the temp memory again
388//   delete vc;
389
390  return this->meshes[mesh]->header->meshFrameNum * this->meshes[mesh]->header->vertexNum * sizeof(MD3VertexCompressed);
391}
392
393}
394
395
396
397
398
399
400
Note: See TracBrowser for help on using the repository browser.