1 | //========================================
|
---|
2 | // Class abstracting the geometric mesh.
|
---|
3 | // Nicolau Sunyer & Sergi Funtané (10/13/2005).
|
---|
4 | // Universitat de Girona.
|
---|
5 | //========================================
|
---|
6 |
|
---|
7 | #include <stdlib.h>
|
---|
8 | #include <string.h>
|
---|
9 | #include "CMesh.h"
|
---|
10 | #include "Main.h"
|
---|
11 | #include "MyFrame.h"
|
---|
12 |
|
---|
13 | //Constructor.
|
---|
14 | CMesh :: CMesh()
|
---|
15 | {
|
---|
16 |
|
---|
17 | }
|
---|
18 |
|
---|
19 | //Destructor.
|
---|
20 | CMesh::~CMesh()
|
---|
21 | {
|
---|
22 |
|
---|
23 | }
|
---|
24 |
|
---|
25 | void CMesh::ShowMesh(char *filename)
|
---|
26 | {
|
---|
27 | FILE *file = NULL;
|
---|
28 |
|
---|
29 | file = fopen(filename, "w");
|
---|
30 |
|
---|
31 | for (int i = 0; i < m_pObject._geo[0]._numTriSets; i++)
|
---|
32 | for (int j = 0; j < m_pObject._geo[0]._triSet[i]._numTris; j++)
|
---|
33 | {
|
---|
34 | fprintf(file, "\nx = %d, y = %d, z = %d", m_pObject._geo[0]._triSet[i]._index[3 * j], m_pObject._geo[0]._triSet[i]._index[3 * j + 1], m_pObject._geo[0]._triSet[i]._index[3 * j + 2]);
|
---|
35 | }
|
---|
36 |
|
---|
37 | fclose(file);
|
---|
38 |
|
---|
39 | }
|
---|
40 |
|
---|
41 | void CMesh::NormalizeIt(wxProgressDialog &dialog)
|
---|
42 | {
|
---|
43 | float WBMax[3], WBMin[3];
|
---|
44 | int i, j;
|
---|
45 | bool cont = true;
|
---|
46 |
|
---|
47 | WBMax[0] = m_pObject._geo[0]._triSet[0]._ver[0];
|
---|
48 | WBMax[1] = m_pObject._geo[0]._triSet[0]._ver[1];
|
---|
49 | WBMax[2] = m_pObject._geo[0]._triSet[0]._ver[2];
|
---|
50 | WBMin[0] = m_pObject._geo[0]._triSet[0]._ver[0];
|
---|
51 | WBMin[1] = m_pObject._geo[0]._triSet[0]._ver[1];
|
---|
52 | WBMin[2] = m_pObject._geo[0]._triSet[0]._ver[2];
|
---|
53 |
|
---|
54 | for (i = 0; i < m_pObject._geo[0]._numTriSets && cont; i++)
|
---|
55 | for (j = 0; j < m_pObject._geo[0]._triSet[i]._numVer && cont; j++)
|
---|
56 | {
|
---|
57 | cont = dialog.Update((m_pObject._geo[0]._numTriSets / 100) * m_pObject._geo[0]._numTriSets);
|
---|
58 |
|
---|
59 | if (WBMax[0] < m_pObject._geo[0]._triSet[i]._ver[3 * j]) WBMax[0] = m_pObject._geo[0]._triSet[i]._ver[3 * j];
|
---|
60 | if (WBMax[1] < m_pObject._geo[0]._triSet[i]._ver[3 * j + 1]) WBMax[1] = m_pObject._geo[0]._triSet[i]._ver[3 * j + 1];
|
---|
61 | if (WBMax[2] < m_pObject._geo[0]._triSet[i]._ver[3 * j + 2]) WBMax[2] = m_pObject._geo[0]._triSet[i]._ver[3 * j + 2];
|
---|
62 | if (WBMin[0] > m_pObject._geo[0]._triSet[i]._ver[3 * j]) WBMin[0] = m_pObject._geo[0]._triSet[i]._ver[3 * j];
|
---|
63 | if (WBMin[1] > m_pObject._geo[0]._triSet[i]._ver[3 * j + 1]) WBMin[1] = m_pObject._geo[0]._triSet[i]._ver[3 * j + 1];
|
---|
64 | if (WBMin[2] > m_pObject._geo[0]._triSet[i]._ver[3 * j + 2]) WBMin[2] = m_pObject._geo[0]._triSet[i]._ver[3 * j + 2];
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | float sizeX = WBMax[0] - WBMin[0];
|
---|
69 | float sizeY = WBMax[1] - WBMin[1];
|
---|
70 | float sizeZ = WBMax[2] - WBMin[2];
|
---|
71 | float dispX = 0.5*(WBMax[0] + WBMin[0]);
|
---|
72 | float dispY = 0.5*(WBMax[1] + WBMin[1]);
|
---|
73 | float dispZ = 0.5*(WBMax[2] + WBMin[2]);
|
---|
74 | float norm;
|
---|
75 |
|
---|
76 | if (sizeX>=sizeY)
|
---|
77 | {
|
---|
78 | if (sizeX>=sizeZ) norm = 0.5*sizeX;
|
---|
79 | else norm = 0.5*sizeZ;
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | if (sizeY>=sizeZ) norm = 0.5*sizeY;
|
---|
84 | else norm = 0.5*sizeZ;
|
---|
85 | }
|
---|
86 |
|
---|
87 | for (i = 0; i < m_pObject._geo[0]._numTriSets; i++)
|
---|
88 | for (j = 0; j < m_pObject._geo[0]._triSet[i]._numVer; j++)
|
---|
89 | {
|
---|
90 | m_pObject._geo[0]._triSet[i]._ver[3 * j] = (m_pObject._geo[0]._triSet[i]._ver[3 * j] - dispX) / norm;
|
---|
91 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 1] = (m_pObject._geo[0]._triSet[i]._ver[3 * j + 1] - dispY) / norm;
|
---|
92 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 2] = (m_pObject._geo[0]._triSet[i]._ver[3 * j + 2] - dispZ) / norm;
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 | float CMesh::getValue(const char *string, int &i)
|
---|
98 | {
|
---|
99 | int j = 0;
|
---|
100 | float value;
|
---|
101 | char substring[100];
|
---|
102 |
|
---|
103 | while ((string[i] != ' ') && (string[i] != '\0'))
|
---|
104 | {
|
---|
105 | substring[j] = string[i];
|
---|
106 | i++;
|
---|
107 | j++;
|
---|
108 | }
|
---|
109 |
|
---|
110 | substring[j] = '\0';
|
---|
111 |
|
---|
112 | value = atof(substring);
|
---|
113 |
|
---|
114 | return (value);
|
---|
115 |
|
---|
116 | }
|
---|
117 |
|
---|
118 | void CMesh::XMLToSNStr(char *name)
|
---|
119 | {
|
---|
120 | int i = 0, j = 0, k;
|
---|
121 | const char *string;
|
---|
122 | bool cont = true, color = false;
|
---|
123 |
|
---|
124 | // Read root element and decide from there what type
|
---|
125 | String response;
|
---|
126 | TiXmlDocument* doc = new TiXmlDocument(name);
|
---|
127 | // Some double-parsing here but never mind
|
---|
128 | if (!doc->LoadFile())
|
---|
129 | {
|
---|
130 | exit (1);
|
---|
131 | }
|
---|
132 |
|
---|
133 | TiXmlElement *root = 0;
|
---|
134 | root = doc->RootElement();
|
---|
135 |
|
---|
136 |
|
---|
137 | if (!root)
|
---|
138 | {
|
---|
139 | doc->Clear();
|
---|
140 | delete doc;
|
---|
141 | exit(-1);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | TiXmlElement *element = root->FirstChildElement("submeshes");
|
---|
146 | if (!element)
|
---|
147 | {
|
---|
148 | doc->Clear();
|
---|
149 | delete doc;
|
---|
150 | exit(-1);
|
---|
151 | }
|
---|
152 |
|
---|
153 | element = element->FirstChildElement("submesh");
|
---|
154 | if (!element)
|
---|
155 | {
|
---|
156 | doc->Clear();
|
---|
157 | delete doc;
|
---|
158 | exit(-1);
|
---|
159 | }
|
---|
160 |
|
---|
161 | m_pObject._numGeos = 1;
|
---|
162 | m_pObject._geo = new snGeo[1];
|
---|
163 |
|
---|
164 | m_pObject._geo[0]._numTriSets = 0;
|
---|
165 |
|
---|
166 | TiXmlElement *cnt;
|
---|
167 |
|
---|
168 | for (cnt = element; cnt; cnt = cnt->NextSiblingElement()) m_pObject._geo[0]._numTriSets += 1;
|
---|
169 |
|
---|
170 | //progress bar
|
---|
171 | wxProgressDialog dialog(wxT("Parsing XML file"),
|
---|
172 | wxT("Progress..."),
|
---|
173 | m_pObject._geo[0]._numTriSets, //range
|
---|
174 | 0, //parent
|
---|
175 | wxPD_APP_MODAL|wxPD_AUTO_HIDE|
|
---|
176 | wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME|wxPD_CAN_ABORT);
|
---|
177 |
|
---|
178 | m_pObject._geo[0]._triSet = new snTriSet[m_pObject._geo[0]._numTriSets];
|
---|
179 |
|
---|
180 | for (element = element; element && cont; element = element->NextSiblingElement())
|
---|
181 | {
|
---|
182 | cont = dialog.Update((m_pObject._geo[0]._numTriSets / 100) * i);
|
---|
183 |
|
---|
184 | cnt = element->FirstChildElement("faces");
|
---|
185 | if (!cnt)
|
---|
186 | {
|
---|
187 | doc->Clear();
|
---|
188 | delete doc;
|
---|
189 | exit(-1);
|
---|
190 | }
|
---|
191 |
|
---|
192 | const char *auxString = cnt->Attribute("count");
|
---|
193 |
|
---|
194 | m_pObject._geo[0]._triSet[i]._numTris = atoi(auxString);
|
---|
195 | m_pObject._geo[0]._triSet[i]._index = new int[m_pObject._geo[0]._triSet[i]._numTris * 3];
|
---|
196 |
|
---|
197 |
|
---|
198 | TiXmlElement *tri, *aux;
|
---|
199 |
|
---|
200 | j = 0;
|
---|
201 | for(tri = cnt; tri; tri = cnt->NextSiblingElement("faces"))
|
---|
202 | {
|
---|
203 | for (aux = tri->FirstChildElement(); aux; aux = aux->NextSiblingElement())
|
---|
204 | {
|
---|
205 | m_pObject._geo[0]._triSet[i]._index[3 * j] = atoi(aux->Attribute("v1"));
|
---|
206 | m_pObject._geo[0]._triSet[i]._index[3 * j + 1] = atoi(aux->Attribute("v2"));
|
---|
207 | m_pObject._geo[0]._triSet[i]._index[3 * j + 2] = atoi(aux->Attribute("v3"));
|
---|
208 | j++;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | tri = cnt->NextSiblingElement("geometry");
|
---|
213 |
|
---|
214 | int NVer = atoi(tri->Attribute("vertexcount"));
|
---|
215 |
|
---|
216 | tri = tri->FirstChildElement("vertexbuffer");
|
---|
217 |
|
---|
218 | const char *cad = tri->Attribute("colours_diffuse");
|
---|
219 |
|
---|
220 | if (cad != NULL)
|
---|
221 | color = true;
|
---|
222 | //->if (!strcmp(cad, "true")) color = true;
|
---|
223 | else
|
---|
224 | color = false;
|
---|
225 |
|
---|
226 | m_pObject._geo[0]._triSet[i]._numVer = NVer;
|
---|
227 |
|
---|
228 |
|
---|
229 | m_pObject._geo[0]._triSet[i]._ver = new float[NVer * 3];
|
---|
230 | m_pObject._geo[0]._triSet[i]._nor = new float[NVer * 3];
|
---|
231 | m_pObject._geo[0]._triSet[i]._tex = new float[NVer * 2];
|
---|
232 | m_pObject._geo[0]._triSet[i]._refl = new float[NVer * 3];
|
---|
233 |
|
---|
234 |
|
---|
235 | TiXmlElement *aux1;
|
---|
236 |
|
---|
237 |
|
---|
238 | aux = tri->FirstChildElement();
|
---|
239 |
|
---|
240 |
|
---|
241 | for (j = 0; j < NVer; j++)
|
---|
242 | {
|
---|
243 | aux1 = aux->FirstChildElement("position");
|
---|
244 | m_pObject._geo[0]._triSet[i]._ver[3 * j] = atof(aux1->Attribute("x"));
|
---|
245 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 1] = atof(aux1->Attribute("y"));
|
---|
246 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 2] = atof(aux1->Attribute("z"));
|
---|
247 |
|
---|
248 | aux1 = aux1->NextSiblingElement("normal");
|
---|
249 | m_pObject._geo[0]._triSet[i]._nor[3 * j] = atof(aux1->Attribute("x"));
|
---|
250 | m_pObject._geo[0]._triSet[i]._nor[3 * j + 1] = atof(aux1->Attribute("y"));
|
---|
251 | m_pObject._geo[0]._triSet[i]._nor[3 * j + 2] = atof(aux1->Attribute("z"));
|
---|
252 |
|
---|
253 | aux1 = aux1->NextSiblingElement("texcoord");
|
---|
254 | m_pObject._geo[0]._triSet[i]._tex[2 * j] = atof(aux1->Attribute("u"));
|
---|
255 | m_pObject._geo[0]._triSet[i]._tex[2 * j + 1] = atof(aux1->Attribute("v"));
|
---|
256 |
|
---|
257 | /*->aux1 = aux->FirstChildElement("colour_diffuse");
|
---|
258 | response = aux1->Attribute("value");
|
---|
259 | /*->m_pObject._geo[0]._triSet[i]._ver[3 * j] = atof(aux1->Attribute("x"));
|
---|
260 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 1] = atof(aux1->Attribute("y"));
|
---|
261 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 2] = atof(aux1->Attribute("z"));*/
|
---|
262 |
|
---|
263 | if (color)
|
---|
264 | {
|
---|
265 | aux1 = aux->FirstChildElement("colour_diffuse");
|
---|
266 | response = aux1->Attribute("value");
|
---|
267 |
|
---|
268 | string = response.c_str();
|
---|
269 | k = 0;
|
---|
270 | m_pObject._geo[0]._triSet[i]._refl[3 * j] = getValue(string, k);
|
---|
271 | k++;
|
---|
272 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 1] = getValue(string, k);
|
---|
273 | k++;
|
---|
274 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 2] = getValue(string, k);
|
---|
275 | k++;
|
---|
276 | getValue(string, k);
|
---|
277 | }
|
---|
278 | else
|
---|
279 | {
|
---|
280 | m_pObject._geo[0]._triSet[i]._refl[3 * j] = 0.5;
|
---|
281 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 1] = 0.5;
|
---|
282 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 2] = 0.5;
|
---|
283 | }
|
---|
284 |
|
---|
285 | aux = aux->NextSiblingElement();
|
---|
286 | }
|
---|
287 |
|
---|
288 | i++;
|
---|
289 | }
|
---|
290 |
|
---|
291 | NormalizeIt(dialog);
|
---|
292 |
|
---|
293 | doc->Clear();
|
---|
294 | delete doc;
|
---|
295 |
|
---|
296 | }
|
---|
297 |
|
---|
298 | void CMesh::LoadMesh(char *argv)
|
---|
299 | {
|
---|
300 | /*->logMgr = new LogManager();
|
---|
301 | logMgr->createLog(opts.logFile, false, !opts.quietMode);
|
---|
302 | rgm = new ResourceGroupManager();
|
---|
303 | mth = new Math();
|
---|
304 | meshMgr = new MeshManager();
|
---|
305 | matMgr = new MaterialManager();
|
---|
306 | matMgr->initialise();
|
---|
307 | skelMgr = new SkeletonManager();
|
---|
308 | meshSerializer = new MeshSerializer();
|
---|
309 | xmlMeshSerializer = new XMLMeshSerializer();
|
---|
310 | skeletonSerializer = new SkeletonSerializer();
|
---|
311 | xmlSkeletonSerializer = new XMLSkeletonSerializer();
|
---|
312 | bufferManager = new DefaultHardwareBufferManager(); // needed because we don't have a rendersystem
|
---|
313 |
|
---|
314 | /*->meshToXML(opts);
|
---|
315 | opts.source = opts.dest;
|
---|
316 | opts.sourceExt = opts.destExt;*/
|
---|
317 | XMLToSNStr(argv);
|
---|
318 |
|
---|
319 |
|
---|
320 | /*->delete xmlSkeletonSerializer;
|
---|
321 | delete skeletonSerializer;
|
---|
322 | delete xmlMeshSerializer;
|
---|
323 | delete meshSerializer;
|
---|
324 | delete skelMgr;
|
---|
325 | delete matMgr;
|
---|
326 | delete meshMgr;
|
---|
327 | delete bufferManager;
|
---|
328 | delete mth;
|
---|
329 | delete rgm;
|
---|
330 | delete logMgr;*/
|
---|
331 |
|
---|
332 | }
|
---|
333 |
|
---|
334 | void CMesh::FreeMem()
|
---|
335 | {
|
---|
336 |
|
---|
337 | for(int i = 0; i<m_pObject._numGeos;i++)
|
---|
338 | {
|
---|
339 |
|
---|
340 | for(int j = 0; j<m_pObject._geo[i]._numTriSets;j++)
|
---|
341 | {
|
---|
342 | delete m_pObject._geo[i]._triSet[j]._index;
|
---|
343 | delete m_pObject._geo[i]._triSet[j]._ver;
|
---|
344 | delete m_pObject._geo[i]._triSet[j]._nor;
|
---|
345 | delete m_pObject._geo[i]._triSet[j]._tex;
|
---|
346 | delete m_pObject._geo[i]._triSet[j]._refl;
|
---|
347 | }
|
---|
348 | delete m_pObject._geo[i]._triSet;
|
---|
349 | }
|
---|
350 |
|
---|
351 | delete m_pObject._geo;
|
---|
352 |
|
---|
353 | }
|
---|
354 |
|
---|