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