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 | co-programmer: Benjamin Grauer |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE |
---|
17 | |
---|
18 | #include "p_node.h" |
---|
19 | #include "null_parent.h" |
---|
20 | |
---|
21 | #include "load_param.h" |
---|
22 | #include "class_list.h" |
---|
23 | |
---|
24 | #include "stdincl.h" |
---|
25 | #include "compiler.h" |
---|
26 | #include "error.h" |
---|
27 | #include "debug.h" |
---|
28 | #include "list.h" |
---|
29 | #include "vector.h" |
---|
30 | |
---|
31 | #include "color.h" |
---|
32 | |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * standard constructor |
---|
38 | */ |
---|
39 | PNode::PNode () |
---|
40 | { |
---|
41 | init(NULL); |
---|
42 | |
---|
43 | NullParent::getInstance()->addChild(this); |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * @param root the load-Element for the PNode |
---|
48 | */ |
---|
49 | PNode::PNode(const TiXmlElement* root) |
---|
50 | { |
---|
51 | this->init(NULL); |
---|
52 | this->loadParams(root); |
---|
53 | |
---|
54 | if (this->parent == NULL) |
---|
55 | NullParent::getInstance()->addChild(this); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * constructor with coodinates |
---|
60 | * @param absCoordinate the Absolute coordinate of the Object |
---|
61 | * @param parent The parent-node of this node. |
---|
62 | */ |
---|
63 | PNode::PNode (const Vector& absCoor, PNode* parent ) |
---|
64 | { |
---|
65 | this->init(parent); |
---|
66 | |
---|
67 | if (likely(parent != NULL)) |
---|
68 | parent->addChild (this); |
---|
69 | |
---|
70 | this->setAbsCoor(absCoor); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * standard deconstructor |
---|
75 | * |
---|
76 | * There are two general ways to delete a PNode |
---|
77 | * 1. delete instance; |
---|
78 | * -> result |
---|
79 | * delete this Node and all its children and children's children... |
---|
80 | * (danger if you still need the children's instance somewhere else!!) |
---|
81 | * |
---|
82 | * 2. instance->remove2D(); delete instance; |
---|
83 | * -> result: |
---|
84 | * moves its children to the NullParent |
---|
85 | * then deletes the Element. |
---|
86 | */ |
---|
87 | PNode::~PNode () |
---|
88 | { |
---|
89 | // remove the Node, delete it's children. |
---|
90 | PNode* tmp; |
---|
91 | while (this->children.size() > 0) |
---|
92 | { |
---|
93 | tmp = this->children.front(); |
---|
94 | this->children.pop_front(); |
---|
95 | delete tmp; |
---|
96 | } |
---|
97 | if (this->parent != NULL) |
---|
98 | { |
---|
99 | this->parent->children.remove(this); |
---|
100 | this->parent = NULL; |
---|
101 | } |
---|
102 | |
---|
103 | // remove all other allocated memory. |
---|
104 | if (this->toCoordinate != NULL) |
---|
105 | delete this->toCoordinate; |
---|
106 | if (this->toDirection != NULL) |
---|
107 | delete this->toDirection; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | /** |
---|
112 | * initializes a PNode |
---|
113 | * @param parent the parent for this PNode |
---|
114 | */ |
---|
115 | void PNode::init(PNode* parent) |
---|
116 | { |
---|
117 | this->setClassID(CL_PARENT_NODE, "PNode"); |
---|
118 | |
---|
119 | this->bRelCoorChanged = true; |
---|
120 | this->bRelDirChanged = true; |
---|
121 | this->parent = parent; |
---|
122 | this->parentMode = PNODE_PARENT_MODE_DEFAULT; |
---|
123 | |
---|
124 | // iterators |
---|
125 | this->toCoordinate = NULL; |
---|
126 | this->toDirection = NULL; |
---|
127 | this->bias = 1.0; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * loads parameters of the PNode |
---|
132 | * @param root the XML-element to load the properties of |
---|
133 | */ |
---|
134 | void PNode::loadParams(const TiXmlElement* root) |
---|
135 | { |
---|
136 | static_cast<BaseObject*>(this)->loadParams(root); |
---|
137 | |
---|
138 | LoadParam(root, "rel-coor", this, PNode, setRelCoor) |
---|
139 | .describe("Sets The relative position of the Node to its parent."); |
---|
140 | |
---|
141 | LoadParam(root, "abs-coor", this, PNode, setAbsCoor) |
---|
142 | .describe("Sets The absolute Position of the Node."); |
---|
143 | |
---|
144 | LoadParam(root, "rel-dir", this, PNode, setRelDir) |
---|
145 | .describe("Sets The relative rotation of the Node to its parent."); |
---|
146 | |
---|
147 | LoadParam(root, "abs-dir", this, PNode, setAbsDir) |
---|
148 | .describe("Sets The absolute rotation of the Node."); |
---|
149 | |
---|
150 | LoadParam(root, "parent", this, PNode, setParent) |
---|
151 | .describe("the Name of the Parent of this PNode"); |
---|
152 | |
---|
153 | LoadParam(root, "parent-mode", this, PNode, setParentMode) |
---|
154 | .describe("the mode to connect this node to its parent ()"); |
---|
155 | |
---|
156 | // cycling properties |
---|
157 | if (root != NULL) |
---|
158 | { |
---|
159 | LOAD_PARAM_START_CYCLE(root, element); |
---|
160 | { |
---|
161 | LoadParam_CYCLE(element, "parent", this, PNode, addChild) |
---|
162 | .describe("adds a new Child to the current Node."); |
---|
163 | |
---|
164 | } |
---|
165 | LOAD_PARAM_END_CYCLE(element); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * set relative coordinates |
---|
171 | * @param relCoord relative coordinates to its parent |
---|
172 | * |
---|
173 | * |
---|
174 | * it is very importand, that you use this function, if you want to update the |
---|
175 | * relCoordinates. If you don't use this, the PNode won't recognize, that something |
---|
176 | * has changed and won't update the children Nodes. |
---|
177 | */ |
---|
178 | void PNode::setRelCoor (const Vector& relCoord) |
---|
179 | { |
---|
180 | if (this->toCoordinate!= NULL) |
---|
181 | { |
---|
182 | delete this->toCoordinate; |
---|
183 | this->toCoordinate = NULL; |
---|
184 | } |
---|
185 | |
---|
186 | this->relCoordinate = relCoord; |
---|
187 | this->bRelCoorChanged = true; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * set relative coordinates |
---|
192 | * @param x x-relative coordinates to its parent |
---|
193 | * @param y y-relative coordinates to its parent |
---|
194 | * @param z z-relative coordinates to its parent |
---|
195 | * @see void PNode::setRelCoor (const Vector& relCoord) |
---|
196 | */ |
---|
197 | void PNode::setRelCoor (float x, float y, float z) |
---|
198 | { |
---|
199 | this->setRelCoor(Vector(x, y, z)); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * sets a new relative position smoothely |
---|
204 | * @param relCoordSoft the new Position to iterate to |
---|
205 | * @param bias how fast to iterate to this position |
---|
206 | */ |
---|
207 | void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias) |
---|
208 | { |
---|
209 | if (likely(this->toCoordinate == NULL)) |
---|
210 | this->toCoordinate = new Vector(); |
---|
211 | |
---|
212 | *this->toCoordinate = relCoordSoft; |
---|
213 | this->bias = bias; |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | /** |
---|
218 | * set relative coordinates smoothely |
---|
219 | * @param x x-relative coordinates to its parent |
---|
220 | * @param y y-relative coordinates to its parent |
---|
221 | * @param z z-relative coordinates to its parent |
---|
222 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
---|
223 | */ |
---|
224 | void PNode::setRelCoorSoft (float x, float y, float z, float bias) |
---|
225 | { |
---|
226 | this->setRelCoorSoft(Vector(x, y, z), bias); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /** |
---|
231 | * @param absCoord set absolute coordinate |
---|
232 | */ |
---|
233 | void PNode::setAbsCoor (const Vector& absCoord) |
---|
234 | { |
---|
235 | if (this->toCoordinate!= NULL) |
---|
236 | { |
---|
237 | delete this->toCoordinate; |
---|
238 | this->toCoordinate = NULL; |
---|
239 | } |
---|
240 | |
---|
241 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
242 | { |
---|
243 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
244 | if (likely(this->parent != NULL)) |
---|
245 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
246 | else |
---|
247 | this->relCoordinate = absCoord; |
---|
248 | } |
---|
249 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
250 | { |
---|
251 | if (likely(this->parent != NULL)) |
---|
252 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
253 | else |
---|
254 | this->relCoordinate = absCoord; |
---|
255 | } |
---|
256 | |
---|
257 | this->bRelCoorChanged = true; |
---|
258 | // this->absCoordinate = absCoord; |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | /** |
---|
263 | * @param x x-coordinate. |
---|
264 | * @param y y-coordinate. |
---|
265 | * @param z z-coordinate. |
---|
266 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
267 | */ |
---|
268 | void PNode::setAbsCoor(float x, float y, float z) |
---|
269 | { |
---|
270 | this->setAbsCoor(Vector(x, y, z)); |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * @param absCoord set absolute coordinate |
---|
275 | * @todo check off |
---|
276 | */ |
---|
277 | void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias) |
---|
278 | { |
---|
279 | if (this->toCoordinate == NULL) |
---|
280 | this->toCoordinate = new Vector; |
---|
281 | |
---|
282 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
283 | { |
---|
284 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
285 | if (likely(this->parent != NULL)) |
---|
286 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
287 | else |
---|
288 | *this->toCoordinate = absCoordSoft; |
---|
289 | } |
---|
290 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
291 | { |
---|
292 | if (likely(this->parent != NULL)) |
---|
293 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
294 | else |
---|
295 | *this->toCoordinate = absCoordSoft; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | /** |
---|
301 | * shift coordinate relative |
---|
302 | * @param shift shift vector |
---|
303 | * |
---|
304 | * this function shifts the current coordinates about the vector shift. this is |
---|
305 | * usefull because from some place else you can: |
---|
306 | * PNode* someNode = ...; |
---|
307 | * Vector objectMovement = calculateShift(); |
---|
308 | * someNode->shiftCoor(objectMovement); |
---|
309 | * |
---|
310 | * this is the internal method of: |
---|
311 | * PNode* someNode = ...; |
---|
312 | * Vector objectMovement = calculateShift(); |
---|
313 | * Vector currentCoor = someNode->getRelCoor(); |
---|
314 | * Vector newCoor = currentCoor + objectMovement; |
---|
315 | * someNode->setRelCoor(newCoor); |
---|
316 | * |
---|
317 | */ |
---|
318 | void PNode::shiftCoor (const Vector& shift) |
---|
319 | { |
---|
320 | this->relCoordinate += shift; |
---|
321 | this->bRelCoorChanged = true; |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * set relative direction |
---|
326 | * @param relDir to its parent |
---|
327 | */ |
---|
328 | void PNode::setRelDir (const Quaternion& relDir) |
---|
329 | { |
---|
330 | if (this->toDirection!= NULL) |
---|
331 | { |
---|
332 | delete this->toDirection; |
---|
333 | this->toDirection = NULL; |
---|
334 | } |
---|
335 | this->relDirection = relDir; |
---|
336 | this->bRelCoorChanged = true; |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | * @see void PNode::setRelDir (const Quaternion& relDir) |
---|
341 | * @param x the x direction |
---|
342 | * @param y the y direction |
---|
343 | * @param z the z direction |
---|
344 | * |
---|
345 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
346 | */ |
---|
347 | void PNode::setRelDir (float x, float y, float z) |
---|
348 | { |
---|
349 | this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | /** |
---|
354 | * sets the Relative Direction of this node to its parent in a Smoothed way |
---|
355 | * @param relDirSoft the direction to iterate to smoothely. |
---|
356 | * @param bias how fast to iterate to the new Direction |
---|
357 | */ |
---|
358 | void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias) |
---|
359 | { |
---|
360 | if (likely(this->toDirection == NULL)) |
---|
361 | this->toDirection = new Quaternion(); |
---|
362 | |
---|
363 | *this->toDirection = relDirSoft; |
---|
364 | this->bias = bias; |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * @see void PNode::setRelDirSoft (const Quaternion& relDir) |
---|
369 | * @param x the x direction |
---|
370 | * @param y the y direction |
---|
371 | * @param z the z direction |
---|
372 | * |
---|
373 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
374 | */ |
---|
375 | void PNode::setRelDirSoft(float x, float y, float z, float bias) |
---|
376 | { |
---|
377 | this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * sets the absolute direction |
---|
382 | * @param absDir absolute coordinates |
---|
383 | */ |
---|
384 | void PNode::setAbsDir (const Quaternion& absDir) |
---|
385 | { |
---|
386 | if (this->toDirection!= NULL) |
---|
387 | { |
---|
388 | delete this->toDirection; |
---|
389 | this->toDirection = NULL; |
---|
390 | } |
---|
391 | |
---|
392 | if (likely(this->parent != NULL)) |
---|
393 | this->relDirection = absDir / this->parent->getAbsDir(); |
---|
394 | else |
---|
395 | this->relDirection = absDir; |
---|
396 | |
---|
397 | this->bRelDirChanged = true; |
---|
398 | } |
---|
399 | |
---|
400 | /** |
---|
401 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
402 | * @param x the x direction |
---|
403 | * @param y the y direction |
---|
404 | * @param z the z direction |
---|
405 | * |
---|
406 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
407 | */ |
---|
408 | void PNode::setAbsDir (float x, float y, float z) |
---|
409 | { |
---|
410 | this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * sets the absolute direction |
---|
415 | * @param absDir absolute coordinates |
---|
416 | */ |
---|
417 | void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias) |
---|
418 | { |
---|
419 | if (this->toDirection == NULL) |
---|
420 | this->toDirection = new Quaternion(); |
---|
421 | |
---|
422 | if (likely(this->parent != NULL)) |
---|
423 | *this->toDirection = absDirSoft / this->parent->getAbsDir(); |
---|
424 | else |
---|
425 | *this->toDirection = absDirSoft; |
---|
426 | |
---|
427 | this->bias = bias; |
---|
428 | } |
---|
429 | |
---|
430 | /** |
---|
431 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
432 | * @param x the x direction |
---|
433 | * @param y the y direction |
---|
434 | * @param z the z direction |
---|
435 | * |
---|
436 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
437 | */ |
---|
438 | void PNode::setAbsDirSoft (float x, float y, float z, float bias) |
---|
439 | { |
---|
440 | this->setAbsDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | /** |
---|
445 | * shift Direction |
---|
446 | * @param shift the direction around which to shift. |
---|
447 | */ |
---|
448 | void PNode::shiftDir (const Quaternion& shift) |
---|
449 | { |
---|
450 | this->relDirection = this->relDirection * shift; |
---|
451 | this->bRelDirChanged = true; |
---|
452 | } |
---|
453 | |
---|
454 | /** |
---|
455 | * adds a child and makes this node to a parent |
---|
456 | * @param child child reference |
---|
457 | * use this to add a child to this node. |
---|
458 | */ |
---|
459 | void PNode::addChild (PNode* child) |
---|
460 | { |
---|
461 | if( likely(child->parent != NULL)) |
---|
462 | { |
---|
463 | PRINTF(5)("PNode::addChild() - reparenting node: removing it and adding it again\n"); |
---|
464 | child->parent->children.remove(child); |
---|
465 | } |
---|
466 | child->parent = this; |
---|
467 | if (unlikely(this != NULL)) |
---|
468 | this->children.push_back(child); |
---|
469 | child->parentCoorChanged(); |
---|
470 | } |
---|
471 | |
---|
472 | /** |
---|
473 | * @see PNode::addChild(PNode* child); |
---|
474 | * @param childName the name of the child to add to this PNode |
---|
475 | */ |
---|
476 | void PNode::addChild (const char* childName) |
---|
477 | { |
---|
478 | PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE)); |
---|
479 | if (childNode != NULL) |
---|
480 | this->addChild(childNode); |
---|
481 | } |
---|
482 | |
---|
483 | /** |
---|
484 | * removes a child from the node |
---|
485 | * @param child the child to remove from this pNode. |
---|
486 | * |
---|
487 | * Children from pNode will not be lost, they are referenced to NullPointer |
---|
488 | */ |
---|
489 | void PNode::removeChild (PNode* child) |
---|
490 | { |
---|
491 | if (child != NULL) |
---|
492 | { |
---|
493 | child->removeNode(); |
---|
494 | // this->children->remove(child); |
---|
495 | // child->parent = NULL; |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | /** |
---|
500 | * remove this pnode from the tree and adds all following to NullParent |
---|
501 | * |
---|
502 | * this can be the case, if an entity in the world is being destroyed. |
---|
503 | */ |
---|
504 | void PNode::removeNode() |
---|
505 | { |
---|
506 | list<PNode*>::iterator child; |
---|
507 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
508 | NullParent::getInstance()->addChild(*child); |
---|
509 | |
---|
510 | if (this->parent != NULL) |
---|
511 | this->parent->children.remove(this); |
---|
512 | } |
---|
513 | |
---|
514 | /** |
---|
515 | * sets the parent of this PNode |
---|
516 | * @param parent the Parent to set |
---|
517 | */ |
---|
518 | void PNode::setParent (PNode* parent) |
---|
519 | { |
---|
520 | parent->addChild(this); |
---|
521 | } |
---|
522 | |
---|
523 | /** |
---|
524 | * @see PNode::setParent(PNode* parent); |
---|
525 | * @param parentName the name of the Parent to set to this PNode |
---|
526 | */ |
---|
527 | void PNode::setParent (const char* parentName) |
---|
528 | { |
---|
529 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
530 | if (parentNode != NULL) |
---|
531 | parentNode->addChild(this); |
---|
532 | } |
---|
533 | |
---|
534 | /** |
---|
535 | * does the reparenting in a very smooth way |
---|
536 | * @param parentNode the new Node to connect this node to. |
---|
537 | * @param bias the speed to iterate to this new Positions |
---|
538 | */ |
---|
539 | void PNode::setParentSoft(PNode* parentNode, float bias) |
---|
540 | { |
---|
541 | // return if the new parent and the old one match |
---|
542 | if (this->parent == parentNode) |
---|
543 | return; |
---|
544 | |
---|
545 | // store the Valures to iterate to. |
---|
546 | if (likely(this->toCoordinate == NULL)) |
---|
547 | { |
---|
548 | this->toCoordinate = new Vector(); |
---|
549 | *this->toCoordinate = this->getRelCoor(); |
---|
550 | } |
---|
551 | if (likely(this->toDirection == NULL)) |
---|
552 | { |
---|
553 | this->toDirection = new Quaternion(); |
---|
554 | *this->toDirection = this->getRelDir(); |
---|
555 | } |
---|
556 | this->bias = bias; |
---|
557 | |
---|
558 | |
---|
559 | Vector tmpV = this->getAbsCoor(); |
---|
560 | Quaternion tmpQ = this->getAbsDir(); |
---|
561 | |
---|
562 | parentNode->addChild(this); |
---|
563 | |
---|
564 | if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL) |
---|
565 | this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()); |
---|
566 | else |
---|
567 | this->relCoordinate = tmpV - parentNode->getAbsCoor(); |
---|
568 | |
---|
569 | this->relDirection = tmpQ / parentNode->getAbsDir(); |
---|
570 | } |
---|
571 | |
---|
572 | /** |
---|
573 | * does the reparenting in a very smooth way |
---|
574 | * @param parentName the name of the Parent to reconnect to |
---|
575 | * @param bias the speed to iterate to this new Positions |
---|
576 | */ |
---|
577 | void PNode::setParentSoft(const char* parentName, float bias) |
---|
578 | { |
---|
579 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
580 | if (parentNode != NULL) |
---|
581 | this->setParentSoft(parentNode, bias); |
---|
582 | } |
---|
583 | |
---|
584 | /** |
---|
585 | * sets the mode of this parent manually |
---|
586 | * @param parentMode a String representing this parentingMode |
---|
587 | */ |
---|
588 | void PNode::setParentMode (const char* parentingMode) |
---|
589 | { |
---|
590 | this->setParentMode(PNode::charToParentingMode(parentingMode)); |
---|
591 | } |
---|
592 | |
---|
593 | /** |
---|
594 | * updates the absCoordinate/absDirection |
---|
595 | * @param dt The time passed since the last update |
---|
596 | * |
---|
597 | * this is used to go through the parent-tree to update all the absolute coordinates |
---|
598 | * and directions. this update should be done by the engine, so you don't have to |
---|
599 | * worry, normaly... |
---|
600 | */ |
---|
601 | void PNode::updateNode (float dt) |
---|
602 | { |
---|
603 | if( likely(this->parent != NULL)) |
---|
604 | { |
---|
605 | // movement for nodes with smoothMove enabled |
---|
606 | if (unlikely(this->toCoordinate != NULL)) |
---|
607 | { |
---|
608 | Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias; |
---|
609 | if (likely(moveVect.len() >= PNODE_ITERATION_DELTA)) |
---|
610 | { |
---|
611 | this->shiftCoor(moveVect); |
---|
612 | } |
---|
613 | else |
---|
614 | { |
---|
615 | delete this->toCoordinate; |
---|
616 | this->toCoordinate = NULL; |
---|
617 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
618 | } |
---|
619 | } |
---|
620 | if (unlikely(this->toDirection != NULL)) |
---|
621 | { |
---|
622 | Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, fabsf(dt)*this->bias); |
---|
623 | if (this->relDirection.distance(rotQuat) >PNODE_ITERATION_DELTA) |
---|
624 | { |
---|
625 | this->relDirection = rotQuat; |
---|
626 | this->bRelDirChanged; |
---|
627 | } |
---|
628 | else |
---|
629 | { |
---|
630 | delete this->toDirection; |
---|
631 | this->toDirection = NULL; |
---|
632 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
633 | } |
---|
634 | } |
---|
635 | |
---|
636 | // MAIN UPDATE ///////////////////////////////////// |
---|
637 | this->lastAbsCoordinate = this->absCoordinate; |
---|
638 | |
---|
639 | PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
640 | |
---|
641 | |
---|
642 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
643 | { |
---|
644 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
645 | this->prevRelCoordinate = this->relCoordinate; |
---|
646 | this->absDirection = this->relDirection * parent->getAbsDir();; |
---|
647 | } |
---|
648 | |
---|
649 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
---|
650 | { |
---|
651 | /* update the current absCoordinate */ |
---|
652 | this->prevRelCoordinate = this->relCoordinate; |
---|
653 | this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate; |
---|
654 | } |
---|
655 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
---|
656 | { |
---|
657 | /* update the current absCoordinate */ |
---|
658 | this->prevRelCoordinate = this->relCoordinate; |
---|
659 | this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate); |
---|
660 | } |
---|
661 | ///////////////////////////////////////////////// |
---|
662 | } |
---|
663 | else |
---|
664 | { |
---|
665 | PRINTF(4)("NullParent::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
666 | if (this->bRelCoorChanged) |
---|
667 | { |
---|
668 | this->prevRelCoordinate = this->relCoordinate; |
---|
669 | this->absCoordinate = this->relCoordinate; |
---|
670 | } |
---|
671 | if (this->bRelDirChanged) |
---|
672 | { |
---|
673 | this->prevRelDirection = this->relDirection; |
---|
674 | this->absDirection = this->getAbsDir () * this->relDirection; |
---|
675 | } |
---|
676 | } |
---|
677 | |
---|
678 | if(this->children.size() > 0) |
---|
679 | { |
---|
680 | list<PNode*>::iterator child; |
---|
681 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
682 | { |
---|
683 | /* if this node has changed, make sure, that all children are updated also */ |
---|
684 | if( likely(this->bRelCoorChanged)) |
---|
685 | (*child)->parentCoorChanged (); |
---|
686 | if( likely(this->bRelDirChanged)) |
---|
687 | (*child)->parentDirChanged (); |
---|
688 | |
---|
689 | (*child)->updateNode(dt); |
---|
690 | } |
---|
691 | } |
---|
692 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
693 | this->bRelCoorChanged = false; |
---|
694 | this->bRelDirChanged = false; |
---|
695 | } |
---|
696 | |
---|
697 | |
---|
698 | void PNode::countChildNodes(int& nodes) const |
---|
699 | { |
---|
700 | nodes++; |
---|
701 | list<PNode*>::const_iterator child; |
---|
702 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
703 | (*child)->countChildNodes(nodes); |
---|
704 | } |
---|
705 | |
---|
706 | |
---|
707 | /** |
---|
708 | * displays some information about this pNode |
---|
709 | * @param depth The deph into which to debug the children of this PNode to. |
---|
710 | * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...) |
---|
711 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
712 | */ |
---|
713 | void PNode::debugNode(unsigned int depth, unsigned int level) const |
---|
714 | { |
---|
715 | for (unsigned int i = 0; i < level; i++) |
---|
716 | PRINT(0)(" |"); |
---|
717 | if (this->children.size() > 0) |
---|
718 | PRINT(0)(" +"); |
---|
719 | else |
---|
720 | PRINT(0)(" -"); |
---|
721 | |
---|
722 | int childNodeCount = 0; |
---|
723 | this->countChildNodes(childNodeCount); |
---|
724 | |
---|
725 | PRINT(0)("PNode(%s::%s) - absCoord: (%0.2f, %0.2f, %0.2f), relCoord(%0.2f, %0.2f, %0.2f), direction(%0.2f, %0.2f, %0.2f) - %s - %d childs\n", |
---|
726 | this->getClassName(), |
---|
727 | this->getName(), |
---|
728 | this->absCoordinate.x, |
---|
729 | this->absCoordinate.y, |
---|
730 | this->absCoordinate.z, |
---|
731 | this->relCoordinate.x, |
---|
732 | this->relCoordinate.y, |
---|
733 | this->relCoordinate.z, |
---|
734 | this->getAbsDirV().x, |
---|
735 | this->getAbsDirV().y, |
---|
736 | this->getAbsDirV().z, |
---|
737 | this->parentingModeToChar(parentMode), |
---|
738 | childNodeCount); |
---|
739 | if (depth >= 2 || depth == 0) |
---|
740 | { |
---|
741 | list<PNode*>::const_iterator child; |
---|
742 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
743 | { |
---|
744 | if (depth == 0) |
---|
745 | (*child)->debugNode(0, level + 1); |
---|
746 | else |
---|
747 | (*child)->debugNode(depth - 1, level +1); |
---|
748 | } |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | /** |
---|
753 | * displays the PNode at its position with its rotation as a cube. |
---|
754 | * @param depth The deph into which to debug the children of this PNode to. |
---|
755 | * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...) |
---|
756 | * @param size the Size of the Box to draw. |
---|
757 | * @param color the color of the Box to display. |
---|
758 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
759 | */ |
---|
760 | void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const |
---|
761 | { |
---|
762 | // if this is the first Element we draw |
---|
763 | if (level == 0) |
---|
764 | { |
---|
765 | glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes |
---|
766 | glMatrixMode(GL_MODELVIEW); // goto the ModelView Matrix |
---|
767 | |
---|
768 | glDisable(GL_LIGHTING); // disable lighting (we do not need them for just lighting) |
---|
769 | glDisable(GL_BLEND); // '' |
---|
770 | glDisable(GL_TEXTURE_2D); // '' |
---|
771 | glDisable(GL_DEPTH_TEST); // '' |
---|
772 | } |
---|
773 | |
---|
774 | glPushMatrix(); // repush the Matrix-stack |
---|
775 | /* translate */ |
---|
776 | glTranslatef (this->getAbsCoor ().x, |
---|
777 | this->getAbsCoor ().y, |
---|
778 | this->getAbsCoor ().z); |
---|
779 | // this->getAbsDir ().matrix (matrix); |
---|
780 | |
---|
781 | /* rotate */ |
---|
782 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
783 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
784 | /* set the new Color */ |
---|
785 | glColor3f(color.x, color.y, color.z); |
---|
786 | { /* draw a cube of size size */ |
---|
787 | glBegin(GL_LINE_STRIP); |
---|
788 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
789 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
790 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
791 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
792 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
793 | glEnd(); |
---|
794 | glBegin(GL_LINE_STRIP); |
---|
795 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
796 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
797 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
798 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
799 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
800 | glEnd(); |
---|
801 | |
---|
802 | glBegin(GL_LINES); |
---|
803 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
804 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
805 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
806 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
807 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
808 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
809 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
810 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
811 | glEnd(); |
---|
812 | } |
---|
813 | |
---|
814 | glPopMatrix(); |
---|
815 | if (depth >= 2 || depth == 0) |
---|
816 | { |
---|
817 | /* rotate the current color in HSV space around 20 degree */ |
---|
818 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
---|
819 | list<PNode*>::const_iterator child; |
---|
820 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
821 | { |
---|
822 | // drawing the Dependency graph |
---|
823 | if (this != NullParent::getInstance()) |
---|
824 | { |
---|
825 | glBegin(GL_LINES); |
---|
826 | glColor3f(color.x, color.y, color.z); |
---|
827 | glVertex3f(this->getAbsCoor ().x, |
---|
828 | this->getAbsCoor ().y, |
---|
829 | this->getAbsCoor ().z); |
---|
830 | glColor3f(childColor.x, childColor.y, childColor.z); |
---|
831 | glVertex3f((*child)->getAbsCoor ().x, |
---|
832 | (*child)->getAbsCoor ().y, |
---|
833 | (*child)->getAbsCoor ().z); |
---|
834 | glEnd(); |
---|
835 | } |
---|
836 | /* if we want to draw the children too */ |
---|
837 | if (depth == 0) /* -> all of them */ |
---|
838 | (*child)->debugDraw(0, size, childColor, level+1); |
---|
839 | else /* -> only the Next one */ |
---|
840 | (*child)->debugDraw(depth - 1, size, childColor, level +1); |
---|
841 | } |
---|
842 | } |
---|
843 | if (level == 0) |
---|
844 | glPopAttrib(); /* pop the saved attributes back out */ |
---|
845 | } |
---|
846 | |
---|
847 | |
---|
848 | |
---|
849 | ///////////////////// |
---|
850 | // HELPER_FUCTIONS // |
---|
851 | ///////////////////// |
---|
852 | |
---|
853 | /** |
---|
854 | * converts a parentingMode into a string that is the name of it |
---|
855 | * @param parentingMode the ParentingMode to convert |
---|
856 | * @return the converted string |
---|
857 | */ |
---|
858 | const char* PNode::parentingModeToChar(int parentingMode) |
---|
859 | { |
---|
860 | if (parentingMode == PNODE_LOCAL_ROTATE) |
---|
861 | return "local-rotate"; |
---|
862 | else if (parentingMode == PNODE_ROTATE_MOVEMENT) |
---|
863 | return "rotate-movement"; |
---|
864 | else if (parentingMode == PNODE_MOVEMENT) |
---|
865 | return "movement"; |
---|
866 | else if (parentingMode == PNODE_ALL) |
---|
867 | return "all"; |
---|
868 | else if (parentingMode == PNODE_ROTATE_AND_MOVE) |
---|
869 | return "rotate-and-move"; |
---|
870 | } |
---|
871 | |
---|
872 | /** |
---|
873 | * converts a parenting-mode-string into a int |
---|
874 | * @param parentingMode the string naming the parentingMode |
---|
875 | * @return the int corresponding to the named parentingMode |
---|
876 | */ |
---|
877 | PARENT_MODE PNode::charToParentingMode(const char* parentingMode) |
---|
878 | { |
---|
879 | if (!strcmp(parentingMode, "local-rotate")) |
---|
880 | return (PNODE_LOCAL_ROTATE); |
---|
881 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
882 | return (PNODE_ROTATE_MOVEMENT); |
---|
883 | else if (!strcmp(parentingMode, "movement")) |
---|
884 | return (PNODE_MOVEMENT); |
---|
885 | else if (!strcmp(parentingMode, "all")) |
---|
886 | return (PNODE_ALL); |
---|
887 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
888 | return (PNODE_ROTATE_AND_MOVE); |
---|
889 | } |
---|