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 | String::iterator iter;
|
---|
127 |
|
---|
128 | TiXmlDocument* doc = new TiXmlDocument(name);
|
---|
129 | // Some double-parsing here but never mind
|
---|
130 | if (!doc->LoadFile())
|
---|
131 | {
|
---|
132 | exit (1);
|
---|
133 | }
|
---|
134 |
|
---|
135 | TiXmlElement *root = 0;
|
---|
136 | root = doc->RootElement();
|
---|
137 |
|
---|
138 |
|
---|
139 | if (!root)
|
---|
140 | {
|
---|
141 | doc->Clear();
|
---|
142 | delete doc;
|
---|
143 | exit(-1);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | TiXmlElement *element = root->FirstChildElement("submeshes");
|
---|
148 | if (!element)
|
---|
149 | {
|
---|
150 | doc->Clear();
|
---|
151 | delete doc;
|
---|
152 | exit(-1);
|
---|
153 | }
|
---|
154 |
|
---|
155 | element = element->FirstChildElement("submesh");
|
---|
156 | if (!element)
|
---|
157 | {
|
---|
158 | doc->Clear();
|
---|
159 | delete doc;
|
---|
160 | exit(-1);
|
---|
161 | }
|
---|
162 |
|
---|
163 | m_pObject._numGeos = 1;
|
---|
164 | m_pObject._geo = new snGeo[1];
|
---|
165 |
|
---|
166 | m_pObject._geo[0]._numTriSets = 0;
|
---|
167 |
|
---|
168 | TiXmlElement *cnt;
|
---|
169 |
|
---|
170 | for (cnt = element; cnt; cnt = cnt->NextSiblingElement()) m_pObject._geo[0]._numTriSets += 1;
|
---|
171 |
|
---|
172 | //progress bar
|
---|
173 | wxProgressDialog dialog(wxT("Parsing XML file"),
|
---|
174 | wxT("Progress..."),
|
---|
175 | m_pObject._geo[0]._numTriSets, //range
|
---|
176 | 0, //parent
|
---|
177 | wxPD_APP_MODAL|wxPD_AUTO_HIDE|
|
---|
178 | wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME|wxPD_CAN_ABORT);
|
---|
179 |
|
---|
180 | m_pObject._geo[0]._triSet = new snTriSet[m_pObject._geo[0]._numTriSets];
|
---|
181 |
|
---|
182 | for (element = element; element && cont; element = element->NextSiblingElement())
|
---|
183 | {
|
---|
184 | cont = dialog.Update((m_pObject._geo[0]._numTriSets / 100) * i);
|
---|
185 |
|
---|
186 | cnt = element->FirstChildElement("faces");
|
---|
187 | if (!cnt)
|
---|
188 | {
|
---|
189 | doc->Clear();
|
---|
190 | delete doc;
|
---|
191 | exit(-1);
|
---|
192 | }
|
---|
193 |
|
---|
194 | const char *auxString = cnt->Attribute("count");
|
---|
195 |
|
---|
196 | m_pObject._geo[0]._triSet[i]._numTris = atoi(auxString);
|
---|
197 | m_pObject._geo[0]._triSet[i]._index = new int[m_pObject._geo[0]._triSet[i]._numTris * 3];
|
---|
198 |
|
---|
199 |
|
---|
200 | TiXmlElement *tri, *aux;
|
---|
201 |
|
---|
202 | j = 0;
|
---|
203 | for(tri = cnt; tri; tri = cnt->NextSiblingElement("faces"))
|
---|
204 | {
|
---|
205 | for (aux = tri->FirstChildElement(); aux; aux = aux->NextSiblingElement())
|
---|
206 | {
|
---|
207 | m_pObject._geo[0]._triSet[i]._index[3 * j] = atoi(aux->Attribute("v1"));
|
---|
208 | m_pObject._geo[0]._triSet[i]._index[3 * j + 1] = atoi(aux->Attribute("v2"));
|
---|
209 | m_pObject._geo[0]._triSet[i]._index[3 * j + 2] = atoi(aux->Attribute("v3"));
|
---|
210 | j++;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | tri = cnt->NextSiblingElement("geometry");
|
---|
215 |
|
---|
216 | int NVer = atoi(tri->Attribute("vertexcount"));
|
---|
217 |
|
---|
218 | tri = tri->FirstChildElement("vertexbuffer");
|
---|
219 |
|
---|
220 | const char *cad = tri->Attribute("colours_diffuse");
|
---|
221 |
|
---|
222 | if (!strcmp(cad, "true")) color = true;
|
---|
223 |
|
---|
224 |
|
---|
225 | m_pObject._geo[0]._triSet[i]._numVer = NVer;
|
---|
226 |
|
---|
227 |
|
---|
228 | m_pObject._geo[0]._triSet[i]._ver = new float[NVer * 3];
|
---|
229 | m_pObject._geo[0]._triSet[i]._nor = new float[NVer * 3];
|
---|
230 | m_pObject._geo[0]._triSet[i]._tex = new float[NVer * 2];
|
---|
231 | m_pObject._geo[0]._triSet[i]._refl = new float[NVer * 3];
|
---|
232 |
|
---|
233 |
|
---|
234 | TiXmlElement *aux1;
|
---|
235 |
|
---|
236 |
|
---|
237 | aux = tri->FirstChildElement();
|
---|
238 |
|
---|
239 |
|
---|
240 | for (j = 0; j < NVer; j++)
|
---|
241 | {
|
---|
242 | aux1 = aux->FirstChildElement("position");
|
---|
243 | m_pObject._geo[0]._triSet[i]._ver[3 * j] = atof(aux1->Attribute("x"));
|
---|
244 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 1] = atof(aux1->Attribute("y"));
|
---|
245 | m_pObject._geo[0]._triSet[i]._ver[3 * j + 2] = atof(aux1->Attribute("z"));
|
---|
246 |
|
---|
247 | aux1 = aux1->NextSiblingElement("normal");
|
---|
248 | m_pObject._geo[0]._triSet[i]._nor[3 * j] = atof(aux1->Attribute("x"));
|
---|
249 | m_pObject._geo[0]._triSet[i]._nor[3 * j + 1] = atof(aux1->Attribute("y"));
|
---|
250 | m_pObject._geo[0]._triSet[i]._nor[3 * j + 2] = atof(aux1->Attribute("z"));
|
---|
251 |
|
---|
252 | aux1 = aux1->NextSiblingElement("texcoord");
|
---|
253 | m_pObject._geo[0]._triSet[i]._tex[2 * j] = atof(aux1->Attribute("u"));
|
---|
254 | m_pObject._geo[0]._triSet[i]._tex[2 * j + 1] = atof(aux1->Attribute("v"));
|
---|
255 |
|
---|
256 | aux1 = aux->FirstChildElement("colour_diffuse");
|
---|
257 | response = aux1->Attribute("value");
|
---|
258 |
|
---|
259 | if (color)
|
---|
260 | {
|
---|
261 | string = response.c_str();
|
---|
262 | k = 0;
|
---|
263 | m_pObject._geo[0]._triSet[i]._refl[3 * j] = getValue(string, k);
|
---|
264 | k++;
|
---|
265 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 1] = getValue(string, k);
|
---|
266 | k++;
|
---|
267 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 2] = getValue(string, k);
|
---|
268 | k++;
|
---|
269 | getValue(string, k);
|
---|
270 | }
|
---|
271 | else
|
---|
272 | {
|
---|
273 | m_pObject._geo[0]._triSet[i]._refl[3 * j] = 0.5;
|
---|
274 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 1] = 0.5;
|
---|
275 | m_pObject._geo[0]._triSet[i]._refl[3 * j + 2] = 0.5;
|
---|
276 | }
|
---|
277 |
|
---|
278 | aux = aux->NextSiblingElement();
|
---|
279 | }
|
---|
280 |
|
---|
281 | i++;
|
---|
282 | }
|
---|
283 |
|
---|
284 | NormalizeIt(dialog);
|
---|
285 |
|
---|
286 | doc->Clear();
|
---|
287 | delete doc;
|
---|
288 |
|
---|
289 | }
|
---|
290 |
|
---|
291 | void CMesh::LoadMesh(char *argv)
|
---|
292 | {
|
---|
293 | /*->logMgr = new LogManager();
|
---|
294 | logMgr->createLog(opts.logFile, false, !opts.quietMode);
|
---|
295 | rgm = new ResourceGroupManager();
|
---|
296 | mth = new Math();
|
---|
297 | meshMgr = new MeshManager();
|
---|
298 | matMgr = new MaterialManager();
|
---|
299 | matMgr->initialise();
|
---|
300 | skelMgr = new SkeletonManager();
|
---|
301 | meshSerializer = new MeshSerializer();
|
---|
302 | xmlMeshSerializer = new XMLMeshSerializer();
|
---|
303 | skeletonSerializer = new SkeletonSerializer();
|
---|
304 | xmlSkeletonSerializer = new XMLSkeletonSerializer();
|
---|
305 | bufferManager = new DefaultHardwareBufferManager(); // needed because we don't have a rendersystem
|
---|
306 |
|
---|
307 | /*->meshToXML(opts);
|
---|
308 | opts.source = opts.dest;
|
---|
309 | opts.sourceExt = opts.destExt;*/
|
---|
310 | XMLToSNStr(argv);
|
---|
311 |
|
---|
312 |
|
---|
313 | /*->delete xmlSkeletonSerializer;
|
---|
314 | delete skeletonSerializer;
|
---|
315 | delete xmlMeshSerializer;
|
---|
316 | delete meshSerializer;
|
---|
317 | delete skelMgr;
|
---|
318 | delete matMgr;
|
---|
319 | delete meshMgr;
|
---|
320 | delete bufferManager;
|
---|
321 | delete mth;
|
---|
322 | delete rgm;
|
---|
323 | delete logMgr;*/
|
---|
324 |
|
---|
325 | }
|
---|
326 |
|
---|
327 | void CMesh::FreeMem()
|
---|
328 | {
|
---|
329 |
|
---|
330 | for(int i = 0; i<m_pObject._numGeos;i++)
|
---|
331 | {
|
---|
332 |
|
---|
333 | for(int j = 0; j<m_pObject._geo[i]._numTriSets;j++)
|
---|
334 | {
|
---|
335 | delete m_pObject._geo[i]._triSet[j]._index;
|
---|
336 | delete m_pObject._geo[i]._triSet[j]._ver;
|
---|
337 | delete m_pObject._geo[i]._triSet[j]._nor;
|
---|
338 | delete m_pObject._geo[i]._triSet[j]._tex;
|
---|
339 | delete m_pObject._geo[i]._triSet[j]._refl;
|
---|
340 | }
|
---|
341 | delete m_pObject._geo[i]._triSet;
|
---|
342 | }
|
---|
343 |
|
---|
344 | delete m_pObject._geo;
|
---|
345 |
|
---|
346 | }
|
---|
347 |
|
---|