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 | |
---|
24 | namespace md3 |
---|
25 | { |
---|
26 | |
---|
27 | /******************************************************************************** |
---|
28 | * MD3Data * |
---|
29 | ********************************************************************************/ |
---|
30 | |
---|
31 | /** |
---|
32 | \brief simple constructor |
---|
33 | */ |
---|
34 | MD3Data::MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale) |
---|
35 | { |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | this->loadModel(modelFileName); |
---|
40 | // this->loadSkin(skinFileName); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | \brief simple destructor |
---|
46 | |
---|
47 | this will clean out all the necessary data for a specific md2model |
---|
48 | */ |
---|
49 | MD3Data::~MD3Data() |
---|
50 | { |
---|
51 | delete this->header; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | \brief this will load the whole model data (vertices, opengl command list, ...) |
---|
58 | * @param fileName: the name of the model file |
---|
59 | \return true if success |
---|
60 | */ |
---|
61 | bool MD3Data::loadModel(const std::string& fileName) |
---|
62 | { |
---|
63 | FILE *pFile; //file stream |
---|
64 | // char* buffer; //buffer for frame data |
---|
65 | int fileOffset = 0; // file data offset |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | //! @todo this chek should include deleting a loaded model (eventually) |
---|
70 | if (fileName.empty()) |
---|
71 | return false; |
---|
72 | |
---|
73 | PRINTF(0)("opening file: %s\n", fileName.c_str()); |
---|
74 | pFile = fopen(fileName.c_str(), "rb"); |
---|
75 | if( unlikely(!pFile)) |
---|
76 | { |
---|
77 | PRINTF(1)("Couldn't open the MD3 File for loading. Exiting.\n"); |
---|
78 | return false; |
---|
79 | } |
---|
80 | fileOffset += this->readHeader(pFile, fileOffset); |
---|
81 | /* check for the header version: make sure its a md2 file :) */ |
---|
82 | if( unlikely(this->header->version != MD3_VERSION) && unlikely(this->header->ident != MD3_IDENT)) |
---|
83 | { |
---|
84 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str()); |
---|
85 | return false; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | // check if the filesize is correct |
---|
90 | if( this->header->fileSize > this->header->tagStart && |
---|
91 | this->header->fileSize >= this->header->meshStart) |
---|
92 | { |
---|
93 | bool bBoneFrames, bTags, bMeshes; |
---|
94 | bBoneFrames = ( this->header->boneFrameNum == 0); |
---|
95 | bTags = ( this->header->tagNum == 0); |
---|
96 | bMeshes = ( this->header->meshNum == 0); |
---|
97 | |
---|
98 | // read different parts of the model in correct order |
---|
99 | while( !(bBoneFrames && bTags && bMeshes)) |
---|
100 | { |
---|
101 | printf("while\n"); |
---|
102 | if( fileOffset == this->header->boneFrameStart && !bBoneFrames) |
---|
103 | { |
---|
104 | fileOffset += this->readBoneFrames(pFile, fileOffset); |
---|
105 | bBoneFrames = true; |
---|
106 | } |
---|
107 | else if( fileOffset == this->header->tagStart && !bTags) |
---|
108 | { |
---|
109 | fileOffset += this->readTags(pFile, fileOffset); |
---|
110 | bTags = true; |
---|
111 | } |
---|
112 | else if( fileOffset == this->header->meshStart && !bMeshes) |
---|
113 | { |
---|
114 | fileOffset += this->readMeshes(pFile, fileOffset); |
---|
115 | bMeshes = true; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | fclose(pFile); |
---|
122 | |
---|
123 | return true; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | \brief loads the skin/material stuff |
---|
129 | * @param fileName: name of the skin file |
---|
130 | \return true if success |
---|
131 | */ |
---|
132 | bool MD3Data::loadSkin(const std::string& fileName) |
---|
133 | { |
---|
134 | // if( fileName.empty()) |
---|
135 | // { |
---|
136 | // this->skinFileName = ""; |
---|
137 | // return false; |
---|
138 | // } |
---|
139 | // |
---|
140 | // this->skinFileName = fileName; |
---|
141 | // |
---|
142 | // this->material.setName("md2ModelMaterial"); |
---|
143 | // this->material.setDiffuseMap(fileName); |
---|
144 | // this->material.setIllum(3); |
---|
145 | // this->material.setAmbient(1.0, 1.0, 1.0); |
---|
146 | |
---|
147 | return true; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | /** |
---|
152 | * read heaader |
---|
153 | */ |
---|
154 | int MD3Data::readHeader(FILE* pFile, int fileOffset) |
---|
155 | { |
---|
156 | this->header = new MD3Header; |
---|
157 | fread(this->header, 1, sizeof(MD3Header), pFile); |
---|
158 | |
---|
159 | //header debug: |
---|
160 | PRINTF(0)("MD3 Header debug section======================================\n"); |
---|
161 | printf("ident: %i\n", this->header->ident); |
---|
162 | printf("version: %i\n", this->header->version); |
---|
163 | printf("filename: %s\n", this->header->filename); |
---|
164 | printf("boneFrameNum: %i\n", this->header->boneFrameNum); |
---|
165 | printf("tag number: %i\n", this->header->tagNum); |
---|
166 | printf("mesh number: %i\n", this->header->meshNum); |
---|
167 | printf("max tex num: %i\n", this->header->maxTexNum); |
---|
168 | printf("bone frame start: %i\n", this->header->boneFrameStart); |
---|
169 | printf("tag start: %i\n", this->header->tagStart); |
---|
170 | printf("mesh start: %i\n", this->header->meshStart); |
---|
171 | printf("fileSize: %i\n", this->header->fileSize); |
---|
172 | |
---|
173 | return sizeof(MD3Header); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | /** |
---|
178 | * read bone frames |
---|
179 | */ |
---|
180 | int MD3Data::readBoneFrames(FILE* pFile, int fileOffset) |
---|
181 | { |
---|
182 | this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum]; |
---|
183 | |
---|
184 | for( int i = 0; i < this->header->boneFrameNum; i++) |
---|
185 | { |
---|
186 | this->boneFrames[i] = new MD3BoneFrame(i); |
---|
187 | |
---|
188 | MD3BoneFrameData* md = new MD3BoneFrameData; |
---|
189 | fread(md, 1, sizeof(MD3BoneFrameData), pFile); |
---|
190 | this->boneFrames[i]->data = md; |
---|
191 | } |
---|
192 | |
---|
193 | return this->header->boneFrameNum * sizeof(MD3BoneFrameData); |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | /** |
---|
198 | * read the tags |
---|
199 | */ |
---|
200 | int MD3Data::readTags(FILE* pFile, int fileOffset) |
---|
201 | { |
---|
202 | // this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum]; |
---|
203 | |
---|
204 | for( int i = 0; i < this->header->boneFrameNum; i++) |
---|
205 | { |
---|
206 | this->boneFrames[i]->tags = new MD3Tag*[this->header->tagNum]; |
---|
207 | |
---|
208 | for( int j = 0; j < this->header->tagNum; j++) |
---|
209 | { |
---|
210 | this->boneFrames[i]->tags[j] = new MD3Tag(); |
---|
211 | MD3TagData* md = new MD3TagData; |
---|
212 | fread(md, 1, sizeof(MD3TagData), pFile); |
---|
213 | this->boneFrames[i]->tags[j]->data = md; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | return this->header->boneFrameNum * this->header->tagNum * sizeof(MD3TagData); |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * read meshes |
---|
223 | */ |
---|
224 | int MD3Data::readMeshes(FILE* pFile, int fileOffset) |
---|
225 | { |
---|
226 | int localFileOffset = fileOffset; //!< local file offset |
---|
227 | |
---|
228 | this->meshes = new MD3Mesh*[this->header->meshNum]; |
---|
229 | |
---|
230 | for( int i = 0; i < this->header->meshNum; i++) |
---|
231 | { |
---|
232 | this->meshes[i] = new MD3Mesh(); |
---|
233 | |
---|
234 | bool bTriangles, bTexVecs, bVertices, bTextures; //!< the parts that have been read so far |
---|
235 | |
---|
236 | //start reading mesh data |
---|
237 | MD3MeshHeader* md = new MD3MeshHeader; |
---|
238 | fread(md, 1, sizeof(MD3MeshHeader), pFile); |
---|
239 | this->meshes[i]->header = md; |
---|
240 | localFileOffset += sizeof(MD3MeshHeader); |
---|
241 | |
---|
242 | if( unlikely(this->meshes[i]->header->id != MD3_IDENT)) |
---|
243 | { |
---|
244 | PRINTF(1)("Wrong MD3 mesh file tag, file %s could be corrupt\n", this->filename.c_str()); |
---|
245 | return false; |
---|
246 | } |
---|
247 | |
---|
248 | // check which parts to be loaded |
---|
249 | bTriangles = ( this->meshes[i]->header->triangleNum == 0); |
---|
250 | bTexVecs = ( this->meshes[i]->header->vertexNum == 0); |
---|
251 | bVertices = ( this->meshes[i]->header->meshFrameNum == 0); |
---|
252 | bTextures = ( this->meshes[i]->header->textureNum == 0); |
---|
253 | |
---|
254 | // now read the data block whise |
---|
255 | while( !(bTriangles && bTexVecs && bVertices && bTextures)) |
---|
256 | { |
---|
257 | if( localFileOffset == this->meshes[i]->header->triangleStart && !bTriangles) |
---|
258 | { |
---|
259 | localFileOffset += this->readMeshTriangles(pFile, localFileOffset, i); |
---|
260 | bTriangles = true; |
---|
261 | } |
---|
262 | else if( localFileOffset == this->meshes[i]->header->textureStart && !bTextures) |
---|
263 | { |
---|
264 | fileOffset += this->readMeshTextures(pFile, localFileOffset, i); |
---|
265 | bTextures = true; |
---|
266 | } |
---|
267 | else if( localFileOffset == this->meshes[i]->header->texVecStart && !bTexVecs) |
---|
268 | { |
---|
269 | localFileOffset += this->readMeshTexVecs(pFile, localFileOffset, i); |
---|
270 | bTexVecs = true; |
---|
271 | } |
---|
272 | else if( fileOffset == this->meshes[i]->header->vertexStart && !bVertices) |
---|
273 | { |
---|
274 | localFileOffset += this->readMeshVertices(pFile, localFileOffset, i); |
---|
275 | bVertices = true; |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | return localFileOffset - fileOffset; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | |
---|
284 | int MD3Data::readMeshTriangles(FILE* pFile, int fileOffset, int mesh) |
---|
285 | { |
---|
286 | this->meshes[mesh]->triangles = new int[this->meshes[mesh]->header->triangleNum][3]; |
---|
287 | } |
---|
288 | |
---|
289 | int MD3Data::readMeshTextures(FILE* pFile, int fileOffset, int mesh) |
---|
290 | {} |
---|
291 | |
---|
292 | int MD3Data::readMeshTexVecs(FILE* pFile, int fileOffset, int mesh) |
---|
293 | {} |
---|
294 | |
---|
295 | int MD3Data::readMeshVertices(FILE* pFile, int fileOffset, int mesh) |
---|
296 | {} |
---|
297 | |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | |
---|
305 | |
---|