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