1 | #include "IVReader.h"
|
---|
2 | #include <string>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <sstream>
|
---|
5 |
|
---|
6 | IVMeshListNode *IVReader::meshList = NULL;
|
---|
7 | int IVReader::instanceCnt = 0;
|
---|
8 | Ogre::Log *IVReader::IVLog = NULL;
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 | IVMeshListNode::IVMeshListNode(std::string name, IVManualMeshLoader *loader)
|
---|
13 | {
|
---|
14 | this->name = name;
|
---|
15 | this->loader = loader;
|
---|
16 | next = NULL;
|
---|
17 | }
|
---|
18 |
|
---|
19 | IVMeshListNode::~IVMeshListNode()
|
---|
20 | {
|
---|
21 | if (loader != NULL) delete loader;
|
---|
22 | }
|
---|
23 |
|
---|
24 | void IVMeshListNode::attachNewNode(std::string name, IVManualMeshLoader *loader)
|
---|
25 | {
|
---|
26 | IVMeshListNode *help = this;
|
---|
27 |
|
---|
28 | while (help->next != NULL) help = help->next;
|
---|
29 | help->next = new IVMeshListNode(name, loader);
|
---|
30 | }
|
---|
31 |
|
---|
32 | IVManualMeshLoader *IVMeshListNode::getManualMeshLoader(std::string name)
|
---|
33 | {
|
---|
34 | IVMeshListNode *help = this;
|
---|
35 | if (help->name == name) return help->loader;
|
---|
36 | while (help->next != NULL)
|
---|
37 | {
|
---|
38 | help = help->next;
|
---|
39 | if (help->name == name) return help->loader;
|
---|
40 | }
|
---|
41 | return NULL;
|
---|
42 | }
|
---|
43 |
|
---|
44 | IVDefListNode::IVDefListNode(std::string name, IVNode *node)
|
---|
45 | {
|
---|
46 | this->name = name;
|
---|
47 | this->node = node;
|
---|
48 | next = NULL;
|
---|
49 | }
|
---|
50 |
|
---|
51 | IVDefListNode::~IVDefListNode()
|
---|
52 | {
|
---|
53 | }
|
---|
54 |
|
---|
55 | void IVDefListNode::attachNewNode(std::string name, IVNode *node)
|
---|
56 | {
|
---|
57 | IVDefListNode *help = this;
|
---|
58 |
|
---|
59 | while (help->next != NULL) help = help->next;
|
---|
60 | help->next = new IVDefListNode(name, node);
|
---|
61 | }
|
---|
62 |
|
---|
63 | IVNode *IVDefListNode::getDef(std::string name)
|
---|
64 | {
|
---|
65 | IVDefListNode *help = this;
|
---|
66 | if (help->name == name) return help->node;
|
---|
67 | while (help->next != NULL)
|
---|
68 | {
|
---|
69 | help = help->next;
|
---|
70 | if (help->name == name) return help->node;
|
---|
71 | }
|
---|
72 | return NULL;
|
---|
73 | }
|
---|
74 |
|
---|
75 | IVReader::IVReader()
|
---|
76 | {
|
---|
77 | root = NULL; meshList = NULL; defList = NULL; fileName = NULL;
|
---|
78 | instanceCnt++;
|
---|
79 | IVLog = NULL;
|
---|
80 | }
|
---|
81 |
|
---|
82 | IVReader::~IVReader()
|
---|
83 | {
|
---|
84 | collapse();
|
---|
85 |
|
---|
86 | instanceCnt--;
|
---|
87 | if (instanceCnt == 0) if (meshList != NULL)
|
---|
88 | {
|
---|
89 | IVMeshListNode *help = meshList, *delme = NULL;
|
---|
90 | while (help->next != NULL)
|
---|
91 | {
|
---|
92 | delme = help; help = help->next; delete delme;
|
---|
93 | }
|
---|
94 | delete help;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | void IVReader::collapse()
|
---|
99 | {
|
---|
100 | if (root != NULL) { delete root; root = NULL; }
|
---|
101 | if (defList != NULL)
|
---|
102 | {
|
---|
103 | IVDefListNode *help = defList, *delme = NULL;
|
---|
104 | while (help->next != NULL)
|
---|
105 | {
|
---|
106 | delme = help; help = help->next; delete delme;
|
---|
107 | }
|
---|
108 | delete help;
|
---|
109 | defList = NULL;
|
---|
110 | }
|
---|
111 | if (this->fileName != NULL) { delete [] this->fileName; this->fileName = NULL; }
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | void IVReader::setLog(Ogre::Log *IVLog)
|
---|
116 | {
|
---|
117 | this->IVLog = IVLog;
|
---|
118 | }
|
---|
119 |
|
---|
120 | std::string IVReader::intToStr(int num)
|
---|
121 | {
|
---|
122 | std::ostringstream myStream;
|
---|
123 | myStream << num;
|
---|
124 | myStream.flush();
|
---|
125 |
|
---|
126 | return(myStream.str());
|
---|
127 | }
|
---|
128 |
|
---|
129 | std::string IVReader::realToStr(Ogre::Real num)
|
---|
130 | {
|
---|
131 | std::ostringstream myStream;
|
---|
132 | myStream << num;
|
---|
133 | myStream.flush();
|
---|
134 |
|
---|
135 | return(myStream.str());
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool IVReader::loadFile(const char* fileName)
|
---|
139 | {
|
---|
140 | std::string message = "trying to load "; message = message + fileName + "...";
|
---|
141 |
|
---|
142 | if (IVLog != NULL)
|
---|
143 | IVLog->logMessage(message);
|
---|
144 |
|
---|
145 | collapse();
|
---|
146 | this->fileName = new char[strlen(fileName)+1]; strcpy(this->fileName, fileName);
|
---|
147 | treeCnt = 0;
|
---|
148 |
|
---|
149 | infile = fopen(fileName, "r"); root = NULL;
|
---|
150 |
|
---|
151 | if (infile == NULL)
|
---|
152 | {
|
---|
153 | message = "! Error loading File "; message = message + fileName + " !";
|
---|
154 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 |
|
---|
158 | if ((fgets(buf, BUFFERSIZE, infile) == NULL) && !feof(infile))
|
---|
159 | {
|
---|
160 | message = "! Error loading File "; message = message + fileName + " !";
|
---|
161 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
162 | fclose(infile);
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (!checkHeader(buf))
|
---|
167 | {
|
---|
168 | message = "! Invalid Header !";
|
---|
169 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
170 | fclose(infile);
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 |
|
---|
174 | IVTokenType tmptyp = IV_TOKEN_INVALID;
|
---|
175 | eof = false;
|
---|
176 |
|
---|
177 | bufOffset = 0;
|
---|
178 | char token[BUFFERSIZE] = "\0", oldtoken[BUFFERSIZE] = "\0", fieldtoken[BUFFERSIZE] = "\0";
|
---|
179 | int level = 0;
|
---|
180 |
|
---|
181 | while (getNextElement(token, &tmptyp))
|
---|
182 | {
|
---|
183 | if (tmptyp == IV_TOKEN_BRACEOPEN)
|
---|
184 | {
|
---|
185 | root = new IVNode(oldtoken);
|
---|
186 | level++;
|
---|
187 | break;
|
---|
188 | }
|
---|
189 |
|
---|
190 | strcpy(oldtoken, token);
|
---|
191 | }
|
---|
192 |
|
---|
193 | IVNode *actualNode = root, *parent = root, *fieldNode = root, *newNode = NULL;
|
---|
194 | IVTokenType oldtmptyp = tmptyp;
|
---|
195 | bool textfield = false, realfield = false; IVType fieldtyp = IV_INVALID;
|
---|
196 | bool def = false; std::string defLabel;
|
---|
197 | Ogre::Real rblock[BLOCKSIZE];
|
---|
198 | float *rvalues;
|
---|
199 | int cnt = 0; int blockcnt = 0;
|
---|
200 | while (getNextElement(token, &tmptyp))
|
---|
201 | {
|
---|
202 | if ((tmptyp == IV_TOKEN_BRACKETOPEN))
|
---|
203 | if (!getNextElement(token, &tmptyp)) break;
|
---|
204 | if ((tmptyp == IV_TOKEN_INVALID))
|
---|
205 | if (!getNextElement(token, &tmptyp)) break;
|
---|
206 | if (tmptyp == IV_TOKEN_BRACEOPEN)
|
---|
207 | {
|
---|
208 | actualNode = new IVNode(oldtoken);
|
---|
209 | if (parent == NULL)
|
---|
210 | {
|
---|
211 | message = "Error loading file "; message = message + fileName + "! Invalid tree structure.";
|
---|
212 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
213 | fclose(infile);
|
---|
214 | return false;
|
---|
215 | }
|
---|
216 | parent->attachNode(actualNode);
|
---|
217 | parent = actualNode;
|
---|
218 | level++;
|
---|
219 |
|
---|
220 | if (def)
|
---|
221 | {
|
---|
222 | addDefToList(defLabel, actualNode);
|
---|
223 | def = false;
|
---|
224 | }
|
---|
225 |
|
---|
226 | } else
|
---|
227 | if (tmptyp == IV_TOKEN_BRACECLOSE)
|
---|
228 | {
|
---|
229 | if (realfield)
|
---|
230 | {
|
---|
231 | if (blockcnt == 0) rvalues = (Ogre::Real *)malloc((cnt)*sizeof(Ogre::Real));
|
---|
232 | else rvalues = (Ogre::Real *)realloc(rvalues, (BLOCKSIZE*(blockcnt) + cnt)*sizeof(Ogre::Real));
|
---|
233 | memcpy((void *)&rvalues[BLOCKSIZE*blockcnt], rblock, cnt*sizeof(Ogre::Real));
|
---|
234 | fieldNode->addField(fieldtoken, rvalues, BLOCKSIZE*blockcnt+cnt, fieldtyp);
|
---|
235 | realfield = false;
|
---|
236 | // free(rvalues);
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (level > 0)
|
---|
240 | {
|
---|
241 | actualNode = parent = actualNode->getParent();
|
---|
242 | level--;
|
---|
243 | }
|
---|
244 | } else
|
---|
245 | {
|
---|
246 | if (oldtmptyp == IV_TOKEN_DEF)
|
---|
247 | {
|
---|
248 | def = true; defLabel = ""; defLabel = defLabel + token;
|
---|
249 | } else
|
---|
250 | if (oldtmptyp == IV_TOKEN_USE)
|
---|
251 | {
|
---|
252 | newNode = new IVNode("USE");
|
---|
253 | if (parent == NULL)
|
---|
254 | {
|
---|
255 | message = "Error loading file "; message = message + fileName + "! Invalid tree structure.";
|
---|
256 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
257 | fclose(infile);
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 | char *s = (char *)malloc(sizeof(char)*(strlen(token)+1));
|
---|
261 | memcpy(s, token, strlen(token));
|
---|
262 | s[strlen(token)] = '\0';
|
---|
263 | newNode->addField("label", s);
|
---|
264 | actualNode->attachNode(newNode);
|
---|
265 | } else
|
---|
266 | if (!def)
|
---|
267 | if (oldtmptyp == IV_TOKEN_STRING)
|
---|
268 | {
|
---|
269 | if (!textfield)
|
---|
270 | {
|
---|
271 | if (tmptyp == IV_TOKEN_STRING)
|
---|
272 | {
|
---|
273 | /* char *s = new char[strlen(token)+1];
|
---|
274 | strcpy(s, token);
|
---|
275 | */
|
---|
276 | char *s = (char *)malloc(sizeof(char)*(strlen(token)+1));
|
---|
277 | memcpy(s, token, strlen(token));
|
---|
278 | s[strlen(token)] = '\0';
|
---|
279 | actualNode->addField(oldtoken, s);
|
---|
280 | // delete [] s;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | if (!realfield)
|
---|
284 | {
|
---|
285 | if ((tmptyp == IV_TOKEN_REAL) || (tmptyp == IV_TOKEN_INT))
|
---|
286 | {
|
---|
287 | cnt = 0; blockcnt = 0;
|
---|
288 | char *stopstring;
|
---|
289 | rblock[0] = strtod(token, &stopstring);
|
---|
290 | cnt++;
|
---|
291 | strcpy(fieldtoken, oldtoken);
|
---|
292 | fieldNode = actualNode;
|
---|
293 | if (tmptyp == IV_TOKEN_REAL) fieldtyp = IV_REAL;
|
---|
294 | if (tmptyp == IV_TOKEN_INT) fieldtyp = IV_INT;
|
---|
295 | realfield = true;
|
---|
296 | }
|
---|
297 | }
|
---|
298 | textfield = !textfield;
|
---|
299 | }
|
---|
300 | else
|
---|
301 | {
|
---|
302 | if (realfield)
|
---|
303 | {
|
---|
304 | if ((tmptyp == IV_TOKEN_REAL) || (tmptyp == IV_TOKEN_INT))
|
---|
305 | {
|
---|
306 | char *stopstring;
|
---|
307 | if (cnt >= BLOCKSIZE)
|
---|
308 | {
|
---|
309 | if (blockcnt == 0) rvalues = (Ogre::Real *)malloc((BLOCKSIZE)*sizeof(Ogre::Real));
|
---|
310 | else rvalues = (Ogre::Real *)realloc(rvalues, (BLOCKSIZE*(blockcnt+1))*sizeof(Ogre::Real));
|
---|
311 | memcpy((void *)&rvalues[BLOCKSIZE*blockcnt], rblock, BLOCKSIZE*sizeof(Ogre::Real));
|
---|
312 | cnt = 0;
|
---|
313 | blockcnt++;
|
---|
314 |
|
---|
315 | }
|
---|
316 | rblock[cnt] = strtod(token, &stopstring);
|
---|
317 | cnt++;
|
---|
318 | if (tmptyp == IV_TOKEN_REAL) fieldtyp = IV_REAL;
|
---|
319 | }
|
---|
320 | else
|
---|
321 | {
|
---|
322 | if (blockcnt == 0) rvalues = (Ogre::Real *)malloc((cnt)*sizeof(Ogre::Real));
|
---|
323 | else rvalues = (Ogre::Real *)realloc(rvalues, (BLOCKSIZE*(blockcnt) + cnt)*sizeof(Ogre::Real));
|
---|
324 | memcpy((void *)&rvalues[BLOCKSIZE*blockcnt], rblock, cnt*sizeof(Ogre::Real));
|
---|
325 | fieldNode->addField(fieldtoken, rvalues, BLOCKSIZE*blockcnt+cnt, fieldtyp);
|
---|
326 | realfield = false;
|
---|
327 | // free(rvalues);
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | textfield = false;
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | strcpy(oldtoken, token);
|
---|
337 | oldtmptyp = tmptyp;
|
---|
338 | }
|
---|
339 | message = ""; ; message = message + fileName + " loaded.";
|
---|
340 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
341 | fclose(infile);
|
---|
342 |
|
---|
343 | return true;
|
---|
344 | }
|
---|
345 |
|
---|
346 | void IVReader::print()
|
---|
347 | {
|
---|
348 | if (root != NULL) root->print();
|
---|
349 | }
|
---|
350 |
|
---|
351 | bool IVReader::checkHeader(const char *string)
|
---|
352 | {
|
---|
353 | for (int i=0; i < validIVHeaderCnt; i++)
|
---|
354 | if (strcmp(validIVHeader[i], string) == 0) return true;
|
---|
355 | return false;
|
---|
356 | }
|
---|
357 | bool IVReader::isSpace(char c)
|
---|
358 | {
|
---|
359 | if ((c==' ') || (c=='\t')) return true;
|
---|
360 | return false;
|
---|
361 | }
|
---|
362 | bool IVReader::isNewline(char c)
|
---|
363 | {
|
---|
364 | if (c=='\n') return true;
|
---|
365 | return false;
|
---|
366 | }
|
---|
367 | bool IVReader::isDigit(char c)
|
---|
368 | {
|
---|
369 | if ((c>=48) && (c<=57)) return true;
|
---|
370 | return false;
|
---|
371 | }
|
---|
372 | bool IVReader::isHexDigit(char c)
|
---|
373 | {
|
---|
374 | if ( ((c>=48) && (c<=57)) || ((c>=65) && (c<=70)) || ((c>=97) && (c<=102)) ) return true;
|
---|
375 | return false;
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | bool IVReader::getNextElement(char *token, IVTokenType *type)
|
---|
380 | {
|
---|
381 | if (eof) return false;
|
---|
382 | bool quoted = false;
|
---|
383 | while (isSpace(buf[bufOffset])) bufOffset++;
|
---|
384 | int i=0;
|
---|
385 | while ((!isSpace(buf[bufOffset])) || (quoted))
|
---|
386 | {
|
---|
387 | if (!quoted)
|
---|
388 | {
|
---|
389 | if (isNewline(buf[bufOffset]) || (buf[bufOffset] == COMMENTCHAR))
|
---|
390 | {
|
---|
391 | token[i] = '\0';
|
---|
392 | classify(token, type);
|
---|
393 | bufOffset = 0;
|
---|
394 | if (fgets(buf, BUFFERSIZE, infile) == NULL)
|
---|
395 | {
|
---|
396 | eof = true;
|
---|
397 | }
|
---|
398 | if (i==0) return getNextElement(token, type);
|
---|
399 | return true;
|
---|
400 | }
|
---|
401 | if ((buf[bufOffset] == ',') || (buf[bufOffset] == ']'))
|
---|
402 | {
|
---|
403 | token[i] = '\0';
|
---|
404 | bufOffset++;
|
---|
405 | classify(token, type);
|
---|
406 | return true;
|
---|
407 | }
|
---|
408 |
|
---|
409 |
|
---|
410 |
|
---|
411 | } else if (isNewline(buf[bufOffset])) { eof = true; }
|
---|
412 |
|
---|
413 |
|
---|
414 | token[i] = buf[bufOffset];
|
---|
415 | i++;
|
---|
416 | if (buf[bufOffset] == '"')
|
---|
417 | {
|
---|
418 | quoted = !quoted; i--;
|
---|
419 | }
|
---|
420 | bufOffset++;
|
---|
421 |
|
---|
422 |
|
---|
423 | }
|
---|
424 | token[i] = '\0';
|
---|
425 | classify(token, type);
|
---|
426 | return true;
|
---|
427 | }
|
---|
428 |
|
---|
429 | void IVReader::classify(char *token, IVTokenType *type)
|
---|
430 | {
|
---|
431 | bool isInt = true, isReal = true;
|
---|
432 | if (!isDigit(token[0]) && (token[0] != '-') && (token[0] != '+'))
|
---|
433 | {
|
---|
434 | isInt = false;
|
---|
435 | if (token[0] != '.') isReal = false;
|
---|
436 | }
|
---|
437 | int i = 1;
|
---|
438 | while ((token[i] != '\0') && isReal)
|
---|
439 | {
|
---|
440 | if (isInt && (i!=2) && !isHexDigit(token[i]))
|
---|
441 | {
|
---|
442 | isInt = false;
|
---|
443 | }
|
---|
444 | if (isInt && (i==2) && !isHexDigit(token[i]) && (token[i] != 'x') && (token[i] != 'X')) isInt = false;
|
---|
445 | if (isReal && (!isDigit(token[i])) && (token[i] != '.') && (token[i] != 'e') && (token[i] != 'E') && (token[i] != 'd') && (token[i] != 'D') && (token[i] != '-') && (token[i] != '+')) isReal = false;
|
---|
446 | i++;
|
---|
447 | }
|
---|
448 | if (isInt) *type = IV_TOKEN_INT;
|
---|
449 | else if (isReal) *type = IV_TOKEN_REAL;
|
---|
450 | else *type = IV_TOKEN_STRING;
|
---|
451 |
|
---|
452 | if (strcmp(token, "DEF") == 0) *type = IV_TOKEN_DEF;
|
---|
453 | if (strcmp(token, "USE") == 0) *type = IV_TOKEN_USE;
|
---|
454 |
|
---|
455 | if (token[1] == '\0')
|
---|
456 | {
|
---|
457 | if (token[0] == '{') *type = IV_TOKEN_BRACEOPEN;
|
---|
458 | if (token[0] == '}') *type = IV_TOKEN_BRACECLOSE;
|
---|
459 | if (token[0] == '[') *type = IV_TOKEN_BRACKETOPEN;
|
---|
460 | if (token[0] == ']') *type = IV_TOKEN_BRACKETCLOSE;
|
---|
461 | if (token[0] == '(') *type = IV_TOKEN_PARENTHESISOPEN;
|
---|
462 | if (token[0] == ')') *type = IV_TOKEN_PARENTHESISCLOSE;
|
---|
463 | }
|
---|
464 | if (token[0] == '\0')
|
---|
465 | {
|
---|
466 | *type = IV_TOKEN_INVALID;
|
---|
467 | }
|
---|
468 |
|
---|
469 | }
|
---|
470 |
|
---|
471 | /*
|
---|
472 | IVNode *IVReader::getNode(const char* name)
|
---|
473 | {
|
---|
474 | return root->getNodeRecursive(name);
|
---|
475 | }
|
---|
476 | */
|
---|
477 | void IVReader::addMeshToList(std::string name, IVManualMeshLoader *loader)
|
---|
478 | {
|
---|
479 | if (meshList == NULL)
|
---|
480 | {
|
---|
481 | meshList = new IVMeshListNode(name, loader);
|
---|
482 | }
|
---|
483 | else
|
---|
484 | {
|
---|
485 | meshList->attachNewNode(name, loader);
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | void IVReader::addDefToList(std::string name, IVNode *node)
|
---|
490 | {
|
---|
491 | if (defList == NULL)
|
---|
492 | {
|
---|
493 | defList = new IVDefListNode(name, node);
|
---|
494 | }
|
---|
495 | else
|
---|
496 | {
|
---|
497 | defList->attachNewNode(name, node);
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | Ogre::Entity *IVReader::createEntity(Ogre::SceneManager* sceneMgr, std::string name, IVMeshData *mData, Ogre::Vector3 *translation)
|
---|
502 | {
|
---|
503 | using namespace Ogre;
|
---|
504 |
|
---|
505 | std::string meshName = name.substr(name.find('/',0), name.length()) + "/Mesh";
|
---|
506 | std::string entityName = name + "/Entity";
|
---|
507 |
|
---|
508 | Mesh* pMesh = NULL;
|
---|
509 | if ((pMesh = (Mesh *) MeshManager::getSingleton().getByName(meshName).getPointer()) == NULL)
|
---|
510 | {
|
---|
511 |
|
---|
512 | IVMeshData *data = mData->expand();
|
---|
513 |
|
---|
514 | IVManualMeshLoader *loader = new IVManualMeshLoader(data);
|
---|
515 |
|
---|
516 | if (translation != NULL) *translation = data->boundingBox->getCenter();
|
---|
517 |
|
---|
518 | // data->collapse();
|
---|
519 | // delete data;
|
---|
520 |
|
---|
521 |
|
---|
522 | addMeshToList(meshName, loader);
|
---|
523 |
|
---|
524 | Mesh* pMesh = MeshManager::getSingleton().createManual(meshName, "IVGroup", loader).getPointer();
|
---|
525 |
|
---|
526 | pMesh->load();
|
---|
527 | pMesh->touch();
|
---|
528 | }
|
---|
529 | else
|
---|
530 | {
|
---|
531 | if (translation != NULL)
|
---|
532 | {
|
---|
533 | *translation = meshList->getManualMeshLoader(meshName)->getBoundingBox()->getCenter();
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | Entity *pEntity = sceneMgr->createEntity(entityName, meshName);
|
---|
538 |
|
---|
539 |
|
---|
540 | if (0) pEntity->setRenderQueueGroup(Ogre::RENDER_QUEUE_MAIN);
|
---|
541 |
|
---|
542 | return pEntity;
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 |
|
---|
547 | void IVReader::buildTree(Ogre::SceneManager* sceneMgr, Ogre::SceneNode *sceneNodeRoot)
|
---|
548 | {
|
---|
549 | using namespace Ogre;
|
---|
550 | if (root == NULL) return;
|
---|
551 |
|
---|
552 | std::string s = intToStr(treeCnt) + "/" + fileName + "/Root";
|
---|
553 | SceneNode *actualSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
554 | s = "/"; s = s + fileName + "/RootMaterial";
|
---|
555 |
|
---|
556 | Material* pMaterial = NULL;
|
---|
557 | if ((pMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
|
---|
558 | {
|
---|
559 | pMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
|
---|
560 | }
|
---|
561 | pMaterial->load();
|
---|
562 |
|
---|
563 | nodeCnt = 1; matCnt = 1;
|
---|
564 |
|
---|
565 |
|
---|
566 | buildTree(sceneMgr, actualSceneNode, root, NULL, pMaterial);
|
---|
567 | treeCnt++;
|
---|
568 |
|
---|
569 |
|
---|
570 | std::string message = "Tree of "; message = message + fileName + " built (" + intToStr(nodeCnt) + " Nodes)";
|
---|
571 | if (IVLog != NULL) IVLog->logMessage(message);
|
---|
572 |
|
---|
573 | }
|
---|
574 |
|
---|
575 | void IVReader::buildTree(Ogre::SceneManager* sceneMgr,
|
---|
576 | Ogre::SceneNode *sceneNodeRoot,
|
---|
577 | IVNode *IVReaderoot, IVMeshData *mData,
|
---|
578 | Ogre::Material *material)
|
---|
579 | {
|
---|
580 | using namespace Ogre;
|
---|
581 |
|
---|
582 | Material *newMaterial = NULL;
|
---|
583 |
|
---|
584 | bool meshDataCreated = false;
|
---|
585 | if (mData == NULL)
|
---|
586 | {
|
---|
587 | mData = new IVMeshData();
|
---|
588 | meshDataCreated = true;
|
---|
589 | }
|
---|
590 |
|
---|
591 | IVType t; int i;
|
---|
592 |
|
---|
593 | IVNode *help = IVReaderoot->getNextChildNode(true);
|
---|
594 | while (help != NULL)
|
---|
595 | {
|
---|
596 | if (strcmp(help->getName(), "USE") == 0)
|
---|
597 | {
|
---|
598 | char *defLabel = (char *) help->getField("label", &t, &i);
|
---|
599 | if (t == IV_STRING)
|
---|
600 | {
|
---|
601 | help = defList->getDef(defLabel);
|
---|
602 | }
|
---|
603 | }
|
---|
604 | if ((strcmp(help->getName(), "Separator") == 0) || (strcmp(help->getName(), "TransformSeparator") == 0))
|
---|
605 | {
|
---|
606 | /* std::string s = sceneNodeRoot->getName() + "/Sep" + intToStr(nodeCnt);
|
---|
607 | nodeCnt++;
|
---|
608 | SceneNode *actualSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
609 | buildTree(sceneMgr, actualSceneNode, help, mData, actualMaterial);
|
---|
610 | */
|
---|
611 | if (newMaterial == NULL) newMaterial = material;
|
---|
612 | buildTree(sceneMgr, sceneNodeRoot, help, mData, newMaterial);
|
---|
613 | }
|
---|
614 |
|
---|
615 | if (strcmp(help->getName(), "Texture2") == 0)
|
---|
616 | {
|
---|
617 | char *textureFileName = (char *) help->getField("filename", &t, &i);
|
---|
618 | if (t == IV_STRING)
|
---|
619 | {
|
---|
620 | std::string s = sceneNodeRoot->getName().substr(sceneNodeRoot->getName().find('/',0), sceneNodeRoot->getName().length()) + "/Material" + intToStr(matCnt);
|
---|
621 | matCnt++;
|
---|
622 | if ((newMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
|
---|
623 | {
|
---|
624 | newMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
|
---|
625 | newMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(textureFileName);
|
---|
626 | }
|
---|
627 | newMaterial->load();
|
---|
628 | newMaterial->reload();
|
---|
629 | }
|
---|
630 | }
|
---|
631 | // can be included
|
---|
632 | if (strcmp(help->getName(), "Material") == 0)
|
---|
633 | {
|
---|
634 | Real *ambientColor = (Real *) help->getField("ambientColor", &t, &i);
|
---|
635 | if ((i != 3) || (t != IV_REAL)) ambientColor = NULL;
|
---|
636 | Real *diffuseColor = (Real *) help->getField("diffuseColor", &t, &i);
|
---|
637 | if ((i != 3) || (t != IV_REAL)) diffuseColor = NULL;
|
---|
638 | Real *specularColor = (Real *) help->getField("specularColor", &t, &i);
|
---|
639 | if ((i != 3) || (t != IV_REAL)) specularColor = NULL;
|
---|
640 | Real *emissiveColor = (Real *) help->getField("emissiveColor", &t, &i);
|
---|
641 | if ((i != 3) || (t != IV_REAL)) emissiveColor = NULL;
|
---|
642 | Real *shininess = (Real *) help->getField("shininess", &t, &i);
|
---|
643 | if ((i != 1) || (t != IV_REAL)) shininess = NULL;
|
---|
644 | Real *transparency = (Real *) help->getField("transparency", &t, &i);
|
---|
645 | if ((i != 1) || (t != IV_REAL)) transparency = NULL;
|
---|
646 |
|
---|
647 | if (newMaterial == NULL)
|
---|
648 | {
|
---|
649 | std::string s = sceneNodeRoot->getName().substr(sceneNodeRoot->getName().find('/',0), sceneNodeRoot->getName().length()) + "/Material" + intToStr(matCnt);
|
---|
650 | matCnt++;
|
---|
651 | if ((newMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
|
---|
652 | {
|
---|
653 | if (material == NULL) newMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
|
---|
654 | else newMaterial = material->clone(s).getPointer();
|
---|
655 |
|
---|
656 | Real alpha = 1.f;
|
---|
657 | if (transparency != NULL) alpha = 1.f - transparency[0];
|
---|
658 | if (ambientColor != NULL) newMaterial->setAmbient(ambientColor[0], ambientColor[1], ambientColor[2]);
|
---|
659 | if (diffuseColor != NULL) newMaterial->setDiffuse(diffuseColor[0], diffuseColor[1], diffuseColor[2], alpha);
|
---|
660 | if (specularColor != NULL) newMaterial->setSpecular(specularColor[0], specularColor[1], specularColor[2], alpha);
|
---|
661 | if (emissiveColor != NULL) newMaterial->setSelfIllumination(emissiveColor[0], emissiveColor[1], emissiveColor[2]);
|
---|
662 | if (shininess != NULL) newMaterial->setShininess(shininess[0]);
|
---|
663 |
|
---|
664 | // if (material->getTechnique(0)->getPass(0)->getNumTextureUnitStates() > 0)
|
---|
665 | // {
|
---|
666 | // newMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName());
|
---|
667 | // }
|
---|
668 | }
|
---|
669 | newMaterial->load();
|
---|
670 | newMaterial->reload();
|
---|
671 | }
|
---|
672 | else
|
---|
673 | {
|
---|
674 | Real alpha = 1.f;
|
---|
675 | if (transparency != NULL) alpha = 1.f - transparency[0];
|
---|
676 | if (ambientColor != NULL) newMaterial->setAmbient(ambientColor[0], ambientColor[1], ambientColor[2]);
|
---|
677 | if (diffuseColor != NULL) newMaterial->setDiffuse(diffuseColor[0], diffuseColor[1], diffuseColor[2], alpha);
|
---|
678 | if (specularColor != NULL) newMaterial->setSpecular(specularColor[0], specularColor[1], specularColor[2], alpha);
|
---|
679 | if (emissiveColor != NULL) newMaterial->setSelfIllumination(emissiveColor[0], emissiveColor[1], emissiveColor[2]);
|
---|
680 | if (shininess != NULL) newMaterial->setShininess(shininess[0]);
|
---|
681 | newMaterial->load();
|
---|
682 | }
|
---|
683 | }
|
---|
684 |
|
---|
685 | if (strcmp(help->getName(), "Coordinate3") == 0)
|
---|
686 | {
|
---|
687 | mData->vertices = (Real *) help->getField("point", &t, &mData->vcnt);
|
---|
688 | if ((t != IV_REAL) && (t != IV_INT)) mData->vertices = NULL;
|
---|
689 | }
|
---|
690 | if (strcmp(help->getName(), "Normal") == 0)
|
---|
691 | {
|
---|
692 | mData->normals = (Real *) help->getField("vector", &t, &mData->ncnt);
|
---|
693 | if ((t != IV_REAL) && (t != IV_INT)) mData->normals = NULL;
|
---|
694 | }
|
---|
695 | if (strcmp(help->getName(), "TextureCoordinate2") == 0)
|
---|
696 | {
|
---|
697 | mData->texCoords = (Real *) help->getField("point", &t, &mData->tcnt);
|
---|
698 | if ((t != IV_REAL) && (t != IV_INT)) mData->texCoords = NULL;
|
---|
699 | }
|
---|
700 |
|
---|
701 | if (strcmp(help->getName(), "IndexedFaceSet") == 0)
|
---|
702 | {
|
---|
703 | std::string s = sceneNodeRoot->getName() + "/IFS" + intToStr(nodeCnt);
|
---|
704 | nodeCnt++;
|
---|
705 | SceneNode *triangleStripSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
706 |
|
---|
707 | mData->indices = (Real *) help->getField("coordIndex", &t, &mData->icnt);
|
---|
708 | if (t != IV_INT) mData->indices = NULL;
|
---|
709 |
|
---|
710 | mData->normalIndices = (Real *) help->getField("normalIndex", &t, &mData->nicnt);
|
---|
711 | if (t != IV_INT) mData->normalIndices = NULL;
|
---|
712 |
|
---|
713 | mData->texCoordIndices = (Real *) help->getField("textureCoordIndex", &t, &mData->ticnt);
|
---|
714 | if (t != IV_INT) mData->texCoordIndices = NULL;
|
---|
715 |
|
---|
716 |
|
---|
717 | mData->roType = IV_ROT_FACE_SET;
|
---|
718 |
|
---|
719 | Vector3 translation = Vector3(0,0,0);
|
---|
720 | Entity *pEntity = createEntity(sceneMgr, s, mData, &translation);
|
---|
721 | if (newMaterial == NULL) newMaterial = material;
|
---|
722 | if (newMaterial != NULL) pEntity->setMaterialName(newMaterial->getName());
|
---|
723 |
|
---|
724 | triangleStripSceneNode->attachObject(pEntity);
|
---|
725 | triangleStripSceneNode->translate(translation);
|
---|
726 |
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | if (strcmp(help->getName(), "IndexedTriangleStripSet") == 0)
|
---|
731 | {
|
---|
732 | std::string s = sceneNodeRoot->getName() + "/ITS" + intToStr(nodeCnt);
|
---|
733 | nodeCnt++;
|
---|
734 | SceneNode *triangleStripSceneNode = sceneNodeRoot->createChildSceneNode(s);
|
---|
735 |
|
---|
736 | mData->indices = (Real *) help->getField("coordIndex", &t, &mData->icnt);
|
---|
737 | if (t != IV_INT) mData->indices = NULL;
|
---|
738 |
|
---|
739 | mData->normalIndices = (Real *) help->getField("normalIndex", &t, &mData->nicnt);
|
---|
740 | if (t != IV_INT) mData->normalIndices = NULL;
|
---|
741 |
|
---|
742 | mData->texCoordIndices = (Real *) help->getField("textureCoordIndex", &t, &mData->ticnt);
|
---|
743 | if (t != IV_INT) mData->texCoordIndices = NULL;
|
---|
744 |
|
---|
745 | IVNode *n = help->getNextChildNode(true);
|
---|
746 | while ((n != NULL) && (strcmp(help->getName(), "VertexProperty") == 0))
|
---|
747 | {
|
---|
748 | n = help->getNextChildNode();
|
---|
749 | }
|
---|
750 | if (n != NULL)
|
---|
751 | {
|
---|
752 | mData->vertices = (Real *) n->getField("vertex", &t, &mData->vcnt);
|
---|
753 | if ((t != IV_REAL) && (t != IV_INT)) mData->vertices = NULL;
|
---|
754 |
|
---|
755 | mData->normals = (Real *) n->getField("normal", &t, &mData->ncnt);
|
---|
756 | if ((t != IV_REAL) && (t != IV_INT)) mData->normals = NULL;
|
---|
757 | mData->texCoords = (Real *) n->getField("texCoord", &t, &mData->tcnt);
|
---|
758 | if ((t != IV_REAL) && (t != IV_INT)) mData->texCoords = NULL;
|
---|
759 | }
|
---|
760 |
|
---|
761 | mData->roType = IV_ROT_TRIANGLE_STRIP;
|
---|
762 |
|
---|
763 | Vector3 translation = Vector3(0,0,0);
|
---|
764 | Entity *pEntity = createEntity(sceneMgr, s, mData, &translation);
|
---|
765 | if (newMaterial == NULL) newMaterial = material;
|
---|
766 | if (newMaterial != NULL) pEntity->setMaterialName(newMaterial->getName());
|
---|
767 |
|
---|
768 | triangleStripSceneNode->attachObject(pEntity);
|
---|
769 | triangleStripSceneNode->translate(translation);
|
---|
770 |
|
---|
771 | }
|
---|
772 |
|
---|
773 | help = IVReaderoot->getNextChildNode();
|
---|
774 | }
|
---|
775 | if (meshDataCreated) if (mData != NULL) delete mData;
|
---|
776 | }
|
---|
777 |
|
---|