Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/coord/p_node.cc @ 6074

Last change on this file since 6074 was 6074, checked in by bensch, 18 years ago

orxonox/trunk: taken out NullParent.
THE TRUNK IS NOT RUNNING FOR THE TIME BEING. DO NOT BE ALARMED, THIS IS TEMPORARY

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