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 | |
---|
21 | |
---|
22 | namespace md3 |
---|
23 | { |
---|
24 | |
---|
25 | /******************************************************************************** |
---|
26 | * MD3Data * |
---|
27 | ********************************************************************************/ |
---|
28 | |
---|
29 | /** |
---|
30 | \brief simple constructor |
---|
31 | */ |
---|
32 | MD3Data::MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale) |
---|
33 | { |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | this->loadModel(modelFileName); |
---|
38 | // this->loadSkin(skinFileName); |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | /** |
---|
43 | \brief simple destructor |
---|
44 | |
---|
45 | this will clean out all the necessary data for a specific md2model |
---|
46 | */ |
---|
47 | MD3Data::~MD3Data() |
---|
48 | { |
---|
49 | delete this->header; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | \brief this will load the whole model data (vertices, opengl command list, ...) |
---|
56 | * @param fileName: the name of the model file |
---|
57 | \return true if success |
---|
58 | */ |
---|
59 | bool MD3Data::loadModel(const std::string& fileName) |
---|
60 | { |
---|
61 | FILE *pFile; //file stream |
---|
62 | // char* buffer; //buffer for frame data |
---|
63 | int fileOffset = 0; // file data offset |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | //! @todo this chek should include deleting a loaded model (eventually) |
---|
68 | if (fileName.empty()) |
---|
69 | return false; |
---|
70 | |
---|
71 | PRINTF(0)("opening file: %s\n", fileName.c_str()); |
---|
72 | pFile = fopen(fileName.c_str(), "rb"); |
---|
73 | if( unlikely(!pFile)) |
---|
74 | { |
---|
75 | PRINTF(1)("Couldn't open the MD3 File for loading. Exiting.\n"); |
---|
76 | return false; |
---|
77 | } |
---|
78 | fileOffset += this->readHeader(pFile, fileOffset); |
---|
79 | /* check for the header version: make sure its a md2 file :) */ |
---|
80 | if( unlikely(this->header->version != MD3_VERSION) && unlikely(this->header->ident != MD3_IDENT)) |
---|
81 | { |
---|
82 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str()); |
---|
83 | return false; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | // check if the filesize is correct |
---|
88 | if( this->header->fileSize > this->header->tagStart && |
---|
89 | this->header->fileSize >= this->header->meshStart) |
---|
90 | { |
---|
91 | bool bBoneFrames, bTags, bMeshes; |
---|
92 | bBoneFrames = ( this->header->boneFrameNum == 0); |
---|
93 | bTags = ( this->header->tagNum == 0); |
---|
94 | bMeshes = ( this->header->meshNum == 0); |
---|
95 | |
---|
96 | // read different parts of the model in correct order |
---|
97 | while( !(bBoneFrames && bTags && bMeshes)) |
---|
98 | { |
---|
99 | printf("while\n"); |
---|
100 | if( fileOffset == this->header->boneFrameStart && !bBoneFrames) |
---|
101 | { |
---|
102 | fileOffset += this->readBoneFrames(pFile, fileOffset); |
---|
103 | bBoneFrames = true; |
---|
104 | } |
---|
105 | else if( fileOffset == this->header->tagStart && !bTags) |
---|
106 | { |
---|
107 | fileOffset += this->readTags(pFile, fileOffset); |
---|
108 | bTags = true; |
---|
109 | } |
---|
110 | else if( fileOffset == this->header->meshStart && !bMeshes) |
---|
111 | { |
---|
112 | fileOffset += this->readMeshes(pFile, fileOffset); |
---|
113 | bMeshes = true; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|
118 | |
---|
119 | #if 0 |
---|
120 | this->fileName =fileName; |
---|
121 | /* got the data: map it to locals */ |
---|
122 | this->numFrames = this->header->numFrames; |
---|
123 | this->numVertices = this->header->numVertices; |
---|
124 | this->numTriangles = this->header->numTriangles; |
---|
125 | this->numGLCommands = this->header->numGlCommands; |
---|
126 | this->numTexCoor = this->header->numTexCoords; |
---|
127 | /* allocate memory for the data storage */ |
---|
128 | this->pVertices = new sVec3D[this->numVertices * this->numFrames]; |
---|
129 | this->pGLCommands = new int[this->numGLCommands]; |
---|
130 | this->pLightNormals = new int[this->numVertices * this->numFrames]; |
---|
131 | this->pTriangles = new sTriangle[this->numTriangles]; |
---|
132 | this->pTexCoor = new sTexCoor[this->numTexCoor]; |
---|
133 | buffer = new char[this->numFrames * this->header->frameSize]; |
---|
134 | |
---|
135 | |
---|
136 | /* read frame data from the file to a temp buffer */ |
---|
137 | fseek(pFile, this->header->offsetFrames, SEEK_SET); |
---|
138 | fread(buffer, this->header->frameSize, this->numFrames, pFile); |
---|
139 | /* read opengl commands */ |
---|
140 | fseek(pFile, this->header->offsetGlCommands, SEEK_SET); |
---|
141 | fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile); |
---|
142 | /* triangle list */ |
---|
143 | fseek(pFile, this->header->offsetTriangles, SEEK_SET); |
---|
144 | fread(this->pTriangles, sizeof(sTriangle), this->numTriangles, pFile); |
---|
145 | /* read in texture coordinates */ |
---|
146 | fseek(pFile, this->header->offsetTexCoords, SEEK_SET); |
---|
147 | fread(this->pTexCoor, sizeof(sTexCoor), this->numTexCoor, pFile); |
---|
148 | |
---|
149 | |
---|
150 | for(int i = 0; i < this->numFrames; ++i) |
---|
151 | { |
---|
152 | frame = (sFrame*)(buffer + this->header->frameSize * i); |
---|
153 | pVertex = this->pVertices + this->numVertices * i; |
---|
154 | pNormals = this->pLightNormals + this->numVertices * i; |
---|
155 | |
---|
156 | for(int j = 0; j < this->numVertices; ++j) |
---|
157 | { |
---|
158 | /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)... */ |
---|
159 | pVertex[j][0] = ((frame->pVertices[j].v[0] * frame->scale[0] ) + frame->translate[0] )* this->scaleFactor; |
---|
160 | pVertex[j][1] = ((frame->pVertices[j].v[2] * frame->scale[2]) + frame->translate[2]) * this->scaleFactor; |
---|
161 | pVertex[j][2] = (-1.0 * (frame->pVertices[j].v[1] * frame->scale[1] + frame->translate[1])) * this->scaleFactor; |
---|
162 | |
---|
163 | //printf("vertex %i/%i: (%f, %f, %f)\n", j, this->numVertices, pVertex[j][0], pVertex[j][1], pVertex[j][2]); |
---|
164 | |
---|
165 | pNormals[j] = frame->pVertices[j].lightNormalIndex; |
---|
166 | } |
---|
167 | } |
---|
168 | PRINTF(4)("Finished loading the md2 file\n"); |
---|
169 | #endif |
---|
170 | |
---|
171 | //delete [] buffer; |
---|
172 | fclose(pFile); |
---|
173 | |
---|
174 | return true; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | \brief loads the skin/material stuff |
---|
180 | * @param fileName: name of the skin file |
---|
181 | \return true if success |
---|
182 | */ |
---|
183 | bool MD3Data::loadSkin(const std::string& fileName) |
---|
184 | { |
---|
185 | // if( fileName.empty()) |
---|
186 | // { |
---|
187 | // this->skinFileName = ""; |
---|
188 | // return false; |
---|
189 | // } |
---|
190 | // |
---|
191 | // this->skinFileName = fileName; |
---|
192 | // |
---|
193 | // this->material.setName("md2ModelMaterial"); |
---|
194 | // this->material.setDiffuseMap(fileName); |
---|
195 | // this->material.setIllum(3); |
---|
196 | // this->material.setAmbient(1.0, 1.0, 1.0); |
---|
197 | |
---|
198 | return true; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | /** |
---|
203 | * read heaader |
---|
204 | */ |
---|
205 | int MD3Data::readHeader(FILE* pFile, int fileOffset) |
---|
206 | { |
---|
207 | this->header = new MD3Header; |
---|
208 | fread(this->header, 1, sizeof(MD3Header), pFile); |
---|
209 | |
---|
210 | //header debug: |
---|
211 | PRINTF(0)("MD3 Header debug section======================================\n"); |
---|
212 | printf("ident: %i\n", this->header->ident); |
---|
213 | printf("version: %i\n", this->header->version); |
---|
214 | printf("filename: %s\n", this->header->filename); |
---|
215 | printf("boneFrameNum: %i\n", this->header->boneFrameNum); |
---|
216 | printf("tag number: %i\n", this->header->tagNum); |
---|
217 | printf("mesh number: %i\n", this->header->meshNum); |
---|
218 | printf("max tex num: %i\n", this->header->maxTexNum); |
---|
219 | printf("bone frame start: %i\n", this->header->boneFrameStart); |
---|
220 | printf("tag start: %i\n", this->header->tagStart); |
---|
221 | printf("mesh start: %i\n", this->header->meshStart); |
---|
222 | printf("fileSize: %i\n", this->header->fileSize); |
---|
223 | |
---|
224 | return sizeof(MD3Header); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | /** |
---|
229 | * read bone frames |
---|
230 | */ |
---|
231 | int MD3Data::readBoneFrames(FILE* pFile, int fileOffset) |
---|
232 | { |
---|
233 | this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum]; |
---|
234 | |
---|
235 | for( int i = 0; i < this->header->boneFrameNum; i++) |
---|
236 | { |
---|
237 | this->boneFrames[i] = new MD3BoneFrame(i); |
---|
238 | |
---|
239 | MD3BoneFrameData* md = new MD3BoneFrameData; |
---|
240 | fread(md, 1, sizeof(MD3BoneFrameData), pFile); |
---|
241 | } |
---|
242 | |
---|
243 | return this->header->boneFrameNum * sizeof(MD3BoneFrameData); |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | /** |
---|
248 | * read the tags |
---|
249 | */ |
---|
250 | int MD3Data::readTags(FILE* pFile, int fileOffset) |
---|
251 | { |
---|
252 | return 0; |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | /** |
---|
257 | * read meshes |
---|
258 | */ |
---|
259 | int MD3Data::readMeshes(FILE* pFile, int fileOffset) |
---|
260 | { |
---|
261 | return 0; |
---|
262 | } |
---|
263 | |
---|
264 | } |
---|
265 | |
---|