Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bsp: reading the material/texture informations

File size: 9.0 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\n");
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  this->header = new MD3Header;
159  fread(this->header, 1, sizeof(MD3Header), pFile);
160
161    //header debug:
162  PRINTF(0)("MD3 Header debug section======================================\n");
163  printf("ident: %i\n", this->header->ident);
164  printf("version: %i\n", this->header->version);
165  printf("filename: %s\n", this->header->filename);
166  printf("boneFrameNum: %i\n", this->header->boneFrameNum);
167  printf("tag number: %i\n", this->header->tagNum);
168  printf("mesh number: %i\n", this->header->meshNum);
169  printf("max tex num: %i\n", this->header->maxTexNum);
170  printf("bone frame start: %i\n", this->header->boneFrameStart);
171  printf("tag start: %i\n", this->header->tagStart);
172  printf("mesh start: %i\n", this->header->meshStart);
173  printf("fileSize: %i\n", this->header->fileSize);
174
175  return sizeof(MD3Header);
176}
177
178
179/**
180 * read bone frames
181 */
182int MD3Data::readBoneFrames(FILE* pFile, int fileOffset)
183{
184  this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum];
185
186  for( int i = 0; i < this->header->boneFrameNum; i++)
187  {
188    this->boneFrames[i] = new MD3BoneFrame(i);
189
190    MD3BoneFrameData* md = new MD3BoneFrameData;
191    fread(md, 1, sizeof(MD3BoneFrameData), pFile);
192    this->boneFrames[i]->data = md;
193  }
194
195  return this->header->boneFrameNum * sizeof(MD3BoneFrameData);
196}
197
198
199/**
200 * read the tags
201 */
202int MD3Data::readTags(FILE* pFile, int fileOffset)
203{
204//  this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum];
205
206  for( int i = 0; i < this->header->boneFrameNum; i++)
207  {
208    this->boneFrames[i]->tags = new MD3Tag*[this->header->tagNum];
209
210    for( int j = 0; j < this->header->tagNum; j++)
211    {
212      this->boneFrames[i]->tags[j] = new MD3Tag();
213      MD3TagData* md = new MD3TagData;
214      fread(md, 1, sizeof(MD3TagData), pFile);
215      this->boneFrames[i]->tags[j]->data = md;
216    }
217  }
218
219  return this->header->boneFrameNum * this->header->tagNum * sizeof(MD3TagData);
220}
221
222
223/**
224 * read meshes
225 */
226int MD3Data::readMeshes(FILE* pFile, int fileOffset)
227{
228  int    localFileOffset = fileOffset;                          //!< local file offset
229
230  this->meshes = new MD3Mesh*[this->header->meshNum];
231
232  for( int i = 0; i < this->header->meshNum; i++)
233  {
234    this->meshes[i] = new MD3Mesh();
235
236    bool   bTriangles, bTexVecs, bVertices, bTextures;            //!< the parts that have been read so far
237
238    //start reading mesh data
239    MD3MeshHeader* md = new MD3MeshHeader;
240    fread(md, 1, sizeof(MD3MeshHeader), pFile);
241    this->meshes[i]->header = md;
242    localFileOffset += sizeof(MD3MeshHeader);
243
244    if( unlikely(this->meshes[i]->header->id != MD3_IDENT))
245    {
246      PRINTF(1)("Wrong MD3 mesh file tag, file %s could be corrupt\n", this->filename.c_str());
247      return false;
248    }
249
250    // check which parts to be loaded
251    bTriangles = ( this->meshes[i]->header->triangleNum == 0);
252    bTexVecs = ( this->meshes[i]->header->vertexNum == 0);
253    bVertices = ( this->meshes[i]->header->meshFrameNum == 0);
254    bTextures = ( this->meshes[i]->header->textureNum == 0);
255
256    // now read the data block whise
257    while( !(bTriangles && bTexVecs && bVertices && bTextures))
258    {
259      if( localFileOffset == this->meshes[i]->header->triangleStart  && !bTriangles)
260      {
261        localFileOffset += this->readMeshTriangles(pFile, localFileOffset, i);
262        bTriangles = true;
263      }
264      else if( localFileOffset == this->meshes[i]->header->textureStart && !bTextures)
265      {
266        fileOffset += this->readMeshTextures(pFile, localFileOffset, i);
267        bTextures = true;
268      }
269      else if( localFileOffset == this->meshes[i]->header->texVecStart && !bTexVecs)
270      {
271        localFileOffset += this->readMeshTexVecs(pFile, localFileOffset, i);
272        bTexVecs = true;
273      }
274      else if( fileOffset == this->meshes[i]->header->vertexStart && !bVertices)
275      {
276        localFileOffset += this->readMeshVertices(pFile, localFileOffset, i);
277        bVertices = true;
278      }
279    }
280  }
281  return localFileOffset - fileOffset;
282}
283
284
285
286/**
287 * reading in the mesh triangles
288 */
289int MD3Data::readMeshTriangles(FILE* pFile, int fileOffset, int mesh)
290{
291  // create the memomry to save the triangles
292  this->meshes[mesh]->triangles = new MD3Triangle[this->meshes[mesh]->header->triangleNum];
293  fread(this->meshes[mesh]->triangles, 1, sizeof(MD3Triangle), pFile);
294
295  return this->meshes[mesh]->header->triangleNum * sizeof(MD3Triangle);
296}
297
298
299/**
300 * reading in the mesh textures
301 */
302int MD3Data::readMeshTextures(FILE* pFile, int fileOffset, int mesh)
303{
304  // create the textures
305  this->meshes[mesh]->material = new Material[this->meshes[mesh]->header->textureNum];
306
307  MD3Texture* tex = new MD3Texture[this->meshes[mesh]->header->textureNum];
308  fread(this->meshes[mesh]->material, 1, sizeof(MD3Texture), pFile);
309
310  for( int i = 0; i < this->meshes[mesh]->header->textureNum; i++) {
311    PRINTF(0)(" texture file: %s\n", tex[i].fileName);
312    this->meshes[mesh]->material[i].setDiffuseMap(tex[i].fileName);
313    this->meshes[mesh]->material[i].setAmbient(1, 1, 1);
314  }
315
316  return this->meshes[mesh]->header->textureNum * sizeof(MD3Texture);
317}
318
319
320/**
321 * reading in the mesh tex vecs
322 */
323int MD3Data::readMeshTexVecs(FILE* pFile, int fileOffset, int mesh)
324{
325  return 0;
326}
327
328
329/**
330 * reading in the mesh vertices
331 */
332int MD3Data::readMeshVertices(FILE* pFile, int fileOffset, int mesh)
333{
334  return 0;
335}
336
337}
338
339
340
341
342
343
344
Note: See TracBrowser for help on using the repository browser.