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
RevLine 
[4570]1/*
[3246]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
[5419]13   co-programmer: Benjamin Grauer
[3246]14*/
15
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE
[3246]17
18#include "p_node.h"
[4761]19
20#include "load_param.h"
21#include "class_list.h"
22
[3860]23#include "compiler.h"
[3608]24#include "debug.h"
25
[6054]26#include "glincl.h"
[5406]27#include "color.h"
[3246]28
29using namespace std;
30
31
32/**
[6048]33 * @brief standard constructor
[5420]34 */
[6074]35PNode::PNode (PNode* parent)
[3365]36{
[6073]37  init();
[6074]38  if (parent != NULL)
39    parent->addChild(this);
[3365]40}
[3246]41
42/**
[6074]43 * @brief initializes a PNode
44 * @param parent the parent for this PNode
[5420]45 */
[6074]46void PNode::init()
[3365]47{
[6074]48  this->setClassID(CL_PARENT_NODE, "PNode");
[3365]49
[6074]50  this->bRelCoorChanged = true;
51  this->bRelDirChanged = true;
52  this->parent = NULL;
53  this->parentMode = PNODE_PARENT_MODE_DEFAULT;
54  this->bActive = true;
[4993]55
[6074]56  // smooth-movers
57  this->toCoordinate = NULL;
58  this->toDirection = NULL;
59  this->bias = 1.0;
[3365]60}
61
[6074]62// NullParent Reference
63PNode* PNode::nullParent = NULL;
[6073]64
[3365]65/**
[6048]66 * @brief standard deconstructor
[5296]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 *
[6073]74 * 2. instance->removeNode(); delete instance;
[5296]75 *   -> result:
76 *    moves its children to the NullParent
77 *    then deletes the Element.
78 */
[4570]79PNode::~PNode ()
[3365]80{
[6073]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      {
[6074]93        if (this == PNode::getNullParent() && (*deleteNode)->parentMode & PNODE_REPARENT_TO_NULLPARENT)
[6073]94          delete (*deleteNode);
95        else
96          (*deleteNode)->reparent();
97      }
98      else
99        delete (*deleteNode);
100    }
101
[6071]102  if (this->parent != NULL)
103   {
104     this->parent->children.remove(this);
105     this->parent = NULL;
106   }
107
[5296]108  // remove all other allocated memory.
[5088]109  if (this->toCoordinate != NULL)
110    delete this->toCoordinate;
111  if (this->toDirection != NULL)
112    delete this->toDirection;
[3365]113}
[3246]114
[5296]115
[4448]116/**
[6048]117 * @brief loads parameters of the PNode
[4836]118 * @param root the XML-element to load the properties of
[5420]119 */
[4436]120void PNode::loadParams(const TiXmlElement* root)
121{
122  static_cast<BaseObject*>(this)->loadParams(root);
[4610]123
[5671]124  LoadParam(root, "rel-coor", this, PNode, setRelCoor)
[4771]125      .describe("Sets The relative position of the Node to its parent.");
126
[5671]127  LoadParam(root, "abs-coor", this, PNode, setAbsCoor)
[4610]128      .describe("Sets The absolute Position of the Node.");
129
[5671]130  LoadParam(root, "rel-dir", this, PNode, setRelDir)
[4771]131      .describe("Sets The relative rotation of the Node to its parent.");
[4761]132
[5671]133  LoadParam(root, "abs-dir", this, PNode, setAbsDir)
[4771]134      .describe("Sets The absolute rotation of the Node.");
135
[5671]136  LoadParam(root, "parent", this, PNode, setParent)
[4761]137      .describe("the Name of the Parent of this PNode");
[4765]138
[5671]139  LoadParam(root, "parent-mode", this, PNode, setParentMode)
[4765]140      .describe("the mode to connect this node to its parent ()");
141
142  // cycling properties
[4785]143  if (root != NULL)
[4765]144  {
[5654]145    LOAD_PARAM_START_CYCLE(root, element);
[4785]146    {
[6074]147      LoadParam_CYCLE(element, "child", this, PNode, addChild)
[4785]148          .describe("adds a new Child to the current Node.");
[4765]149
[4785]150    }
[5654]151    LOAD_PARAM_END_CYCLE(element);
[4765]152  }
[4436]153}
[3365]154
155/**
[6048]156 * @brief set relative coordinates
[4836]157 * @param relCoord relative coordinates to its parent
[5420]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 */
[3810]164void PNode::setRelCoor (const Vector& relCoord)
[3675]165{
[5113]166  if (this->toCoordinate!= NULL)
167  {
168    delete this->toCoordinate;
169    this->toCoordinate = NULL;
170  }
171
[4993]172  this->relCoordinate = relCoord;
[3675]173  this->bRelCoorChanged = true;
174}
175
176/**
[6048]177 * @brief set relative coordinates
[4836]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
[4993]181 * @see  void PNode::setRelCoor (const Vector& relCoord)
[5420]182 */
[4610]183void PNode::setRelCoor (float x, float y, float z)
184{
185  this->setRelCoor(Vector(x, y, z));
186}
187
[4992]188/**
[6048]189 * @brief sets a new relative position smoothely
[4992]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)
[4987]194{
[4993]195  if (likely(this->toCoordinate == NULL))
196    this->toCoordinate = new Vector();
[4987]197
[4993]198  *this->toCoordinate = relCoordSoft;
[4992]199  this->bias = bias;
[4987]200}
201
[4990]202
[4610]203/**
[6048]204 * @brief set relative coordinates smoothely
[4990]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
[4993]208 * @see  void PNode::setRelCoorSoft (const Vector&, float)
[4990]209 */
[4992]210void PNode::setRelCoorSoft (float x, float y, float z, float bias)
[4990]211{
[4992]212  this->setRelCoorSoft(Vector(x, y, z), bias);
[4990]213}
214
[5382]215
[4990]216/**
[4836]217 * @param absCoord set absolute coordinate
[5091]218 */
[3809]219void PNode::setAbsCoor (const Vector& absCoord)
[3675]220{
[5113]221  if (this->toCoordinate!= NULL)
222  {
223    delete this->toCoordinate;
224    this->toCoordinate = NULL;
225  }
226
[4993]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;
[3675]245}
246
[5382]247
[3675]248/**
[4836]249 * @param x x-coordinate.
250 * @param y y-coordinate.
251 * @param z z-coordinate.
[4987]252 * @see void PNode::setAbsCoor (const Vector& absCoord)
[4610]253 */
254void PNode::setAbsCoor(float x, float y, float z)
255{
256  this->setAbsCoor(Vector(x, y, z));
257}
258
[6054]259
[4610]260/**
[5406]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/**
[6048]288 * @brief shift coordinate relative
[4836]289 * @param shift shift vector
[4987]290 *
[5420]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 *
[5382]304 */
[3809]305void PNode::shiftCoor (const Vector& shift)
[3683]306{
[4993]307  this->relCoordinate += shift;
308  this->bRelCoorChanged = true;
[3683]309}
310
[6054]311
[3683]312/**
[6048]313 * @brief set relative direction
[4836]314 * @param relDir to its parent
[5091]315 */
[3810]316void PNode::setRelDir (const Quaternion& relDir)
[3675]317{
[5113]318  if (this->toDirection!= NULL)
319  {
320    delete this->toDirection;
321    this->toDirection = NULL;
322  }
[4993]323  this->relDirection = relDir;
[5819]324
[3675]325  this->bRelCoorChanged = true;
326}
327
[6054]328
[3365]329/**
[4771]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
[4990]342
[4771]343/**
[6048]344 * @brief sets the Relative Direction of this node to its parent in a Smoothed way
[4990]345 * @param relDirSoft the direction to iterate to smoothely.
[4992]346 * @param bias how fast to iterate to the new Direction
[4990]347 */
[4992]348void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias)
[4990]349{
350  if (likely(this->toDirection == NULL))
351    this->toDirection = new Quaternion();
352
353  *this->toDirection = relDirSoft;
[4992]354  this->bias = bias;
[5819]355  this->bRelDirChanged = true;
[4990]356}
357
[6054]358
[4990]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 */
[4992]367void PNode::setRelDirSoft(float x, float y, float z, float bias)
[4990]368{
[4992]369  this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
[4990]370}
371
[6054]372
[4990]373/**
[6048]374 * @brief sets the absolute direction
[4836]375 * @param absDir absolute coordinates
[5091]376 */
[3810]377void PNode::setAbsDir (const Quaternion& absDir)
[3675]378{
[5113]379  if (this->toDirection!= NULL)
380  {
381    delete this->toDirection;
382    this->toDirection = NULL;
383  }
384
[5001]385  if (likely(this->parent != NULL))
386    this->relDirection = absDir / this->parent->getAbsDir();
[4996]387  else
[5001]388   this->relDirection = absDir;
[4993]389
390  this->bRelDirChanged = true;
[3675]391}
392
[6054]393
[3675]394/**
[4771]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
[6054]407
[4771]408/**
[6048]409 * @brief sets the absolute direction
[5414]410 * @param absDir absolute coordinates
[6048]411 * @param bias how fast to iterator to the new Position
[5414]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;
[5915]424  this->bRelDirChanged = true;
[5414]425}
426
[6054]427
[5414]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/**
[6048]443 * @brief shift Direction
[5091]444 * @param shift the direction around which to shift.
445 */
[3802]446void PNode::shiftDir (const Quaternion& shift)
447{
[4993]448  this->relDirection = this->relDirection * shift;
[3802]449  this->bRelDirChanged = true;
450}
[3365]451
[6048]452
[3683]453/**
[6048]454 * @brief adds a child and makes this node to a parent
[5091]455 * @param child child reference
[4993]456 * use this to add a child to this node.
[5420]457 */
[5382]458void PNode::addChild (PNode* child)
[3365]459{
[4993]460  if( likely(child->parent != NULL))
[3521]461    {
[5255]462      PRINTF(5)("PNode::addChild() - reparenting node: removing it and adding it again\n");
[5770]463      child->parent->children.remove(child);
[3521]464    }
[4993]465  child->parent = this;
[5397]466  if (unlikely(this != NULL))
[5770]467    this->children.push_back(child);
[4993]468  child->parentCoorChanged();
[3365]469}
470
[6048]471
[3365]472/**
[5091]473 * @see PNode::addChild(PNode* child);
[4765]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
[6048]483
[4765]484/**
[6048]485 * @brief removes a child from the node
[5091]486 * @param child the child to remove from this pNode.
[4993]487 *
[6054]488 * Children from pNode will not be lost, they are Reparented by the rules of the ParentMode
[5420]489 */
[4993]490void PNode::removeChild (PNode* child)
[3365]491{
[5214]492  if (child != NULL)
[5769]493   child->removeNode();
[3365]494}
495
[6054]496
[3365]497/**
[6048]498 * @brief remove this pnode from the tree and adds all following to NullParent
[5420]499 *
500 * this can be the case, if an entity in the world is being destroyed.
501 */
[5769]502void PNode::removeNode()
[3537]503{
[6054]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  }
[5214]514  if (this->parent != NULL)
[5770]515    this->parent->children.remove(this);
[3537]516}
517
[3365]518
[4761]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
[6054]530
[4990]531/**
[6048]532 * @brief does the reparenting in a very smooth way
[4990]533 * @param parentNode the new Node to connect this node to.
[4993]534 * @param bias the speed to iterate to this new Positions
[4990]535 */
[5382]536void PNode::setParentSoft(PNode* parentNode, float bias)
[4987]537{
[5382]538  // return if the new parent and the old one match
[4992]539  if (this->parent == parentNode)
540    return;
541
[5382]542  // store the Valures to iterate to.
[4993]543  if (likely(this->toCoordinate == NULL))
[4989]544  {
[4993]545    this->toCoordinate = new Vector();
546    *this->toCoordinate = this->getRelCoor();
[4989]547  }
[4990]548  if (likely(this->toDirection == NULL))
549  {
550    this->toDirection = new Quaternion();
551    *this->toDirection = this->getRelDir();
552  }
[4992]553  this->bias = bias;
[4987]554
555
[4990]556  Vector tmpV = this->getAbsCoor();
557  Quaternion tmpQ = this->getAbsDir();
[4987]558
559  parentNode->addChild(this);
560
[5382]561 if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL)
562   this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor());
[5000]563 else
[5382]564   this->relCoordinate = tmpV - parentNode->getAbsCoor();
[4991]565
[5382]566 this->relDirection = tmpQ / parentNode->getAbsDir();
[4987]567}
568
[6054]569
[4993]570/**
[6048]571 * @brief does the reparenting in a very smooth way
[4993]572 * @param parentName the name of the Parent to reconnect to
573 * @param bias the speed to iterate to this new Positions
574 */
[5382]575void PNode::setParentSoft(const char* parentName, float bias)
[4987]576{
577  PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE));
578  if (parentNode != NULL)
[5382]579    this->setParentSoft(parentNode, bias);
[4987]580}
581
[6054]582/**
583 * @param parentMode sets the parentingMode of this Node
584 */
585void PNode::setParentMode(PARENT_MODE parentMode)
586{
587  this->parentMode &= (0xfff0 | parentMode);
588}
[6048]589
[3365]590/**
[6048]591 * @brief sets the mode of this parent manually
[4765]592 * @param parentMode a String representing this parentingMode
593 */
594void PNode::setParentMode (const char* parentingMode)
595{
[4993]596  this->setParentMode(PNode::charToParentingMode(parentingMode));
[4765]597}
[3537]598
[6054]599
[3365]600/**
[6054]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)
[6074]607    this->setParent(PNode::getNullParent());
[6054]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/**
[6048]625 * @brief updates the absCoordinate/absDirection
[4836]626 * @param dt The time passed since the last update
[5420]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 */
[5769]632void PNode::updateNode (float dt)
[3365]633{
[4440]634  if( likely(this->parent != NULL))
635    {
[6054]636      if (!(this->parentMode & PNODE_STATIC_NODE))
[4987]637      {
[6054]638        // movement for nodes with smoothMove enabled
639        if (unlikely(this->toCoordinate != NULL))
[4987]640        {
[6054]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          }
[4987]652        }
[6054]653        if (unlikely(this->toDirection != NULL))
[4987]654        {
[6054]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          }
[4987]667        }
[4990]668
[6054]669        // MAIN UPDATE /////////////////////////////////////
670        this->lastAbsCoordinate = this->absCoordinate;
[4145]671
[6054]672        PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
[3800]673
[4570]674
[6054]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        }
[4570]681
[6054]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        /////////////////////////////////////////////////
[5007]695      }
[6054]696  }
697   else // Nodes without a Parent are handled faster :: MOST LIKELY THE NULLPARENT
[4440]698    {
[6054]699      if (!(this->parentMode & PNODE_STATIC_NODE))
[5118]700      {
[6054]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        }
[5118]712      }
[4993]713    }
[3365]714
[6054]715    if(!this->children.empty() && (this->bActive || this->parentMode & PNODE_UPDATE_CHILDREN_IF_INACTIVE ))
[4993]716    {
[5770]717      list<PNode*>::iterator child;
718      for (child = this->children.begin(); child != this->children.end(); child ++)
[4574]719      {
720        /* if this node has changed, make sure, that all children are updated also */
[4993]721        if( likely(this->bRelCoorChanged))
[5770]722          (*child)->parentCoorChanged ();
[4993]723        if( likely(this->bRelDirChanged))
[5770]724          (*child)->parentDirChanged ();
[4993]725
[5770]726        (*child)->updateNode(dt);
[4993]727      }
[4440]728    }
[4993]729    this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
730    this->bRelCoorChanged = false;
731    this->bRelDirChanged = false;
[3365]732}
733
[5769]734
[6048]735/**
736 * @brief counts total amount the children walking through the entire tree.
737 * @param nodes the counter
738 */
[5769]739void PNode::countChildNodes(int& nodes) const
740{
741  nodes++;
[5770]742  list<PNode*>::const_iterator child;
743  for (child = this->children.begin(); child != this->children.end(); child ++)
744    (*child)->countChildNodes(nodes);
[5769]745}
746
747
[3450]748/**
[6048]749 * @brief displays some information about this pNode
[4836]750 * @param depth The deph into which to debug the children of this PNode to.
[5383]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).
[5420]753 */
[5769]754void PNode::debugNode(unsigned int depth, unsigned int level) const
[3365]755{
[4574]756  for (unsigned int i = 0; i < level; i++)
[4575]757    PRINT(0)(" |");
[5770]758  if (this->children.size() > 0)
[4575]759    PRINT(0)(" +");
760  else
761    PRINT(0)(" -");
[5769]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",
[4574]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,
[4993]774           this->relCoordinate.z,
[4996]775           this->getAbsDirV().x,
776           this->getAbsDirV().y,
777           this->getAbsDirV().z,
[5769]778           this->parentingModeToChar(parentMode),
779           childNodeCount);
[4574]780  if (depth >= 2 || depth == 0)
781  {
[5770]782    list<PNode*>::const_iterator child;
783    for (child = this->children.begin(); child != this->children.end(); child ++)
[4574]784    {
785      if (depth == 0)
[5770]786        (*child)->debugNode(0, level + 1);
[4574]787      else
[5770]788        (*child)->debugNode(depth - 1, level +1);
[4574]789    }
790  }
[3365]791}
792
[4570]793/**
[6048]794 * @brief displays the PNode at its position with its rotation as a cube.
[5383]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
[4570]802{
[5432]803  // if this is the first Element we draw
[5383]804  if (level == 0)
805  {
[5432]806    glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes
807    glMatrixMode(GL_MODELVIEW);  // goto the ModelView Matrix
[5383]808
[5432]809    glDisable(GL_LIGHTING);      // disable lighting (we do not need them for just lighting)
810    glDisable(GL_BLEND);         // ''
811    glDisable(GL_TEXTURE_2D);    // ''
[5438]812    glDisable(GL_DEPTH_TEST);    // ''
[5383]813  }
814
[5432]815  glPushMatrix();                // repush the Matrix-stack
[4570]816  /* translate */
817  glTranslatef (this->getAbsCoor ().x,
818                this->getAbsCoor ().y,
819                this->getAbsCoor ().z);
[4998]820//  this->getAbsDir ().matrix (matrix);
821
[5432]822  /* rotate */
[4998]823  Vector tmpRot = this->getAbsDir().getSpacialAxis();
[5432]824  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
825  /* set the new Color */
[5008]826  glColor3f(color.x, color.y, color.z);
[5432]827  { /* draw a cube of size size */
[4570]828    glBegin(GL_LINE_STRIP);
[4995]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);
[4570]834    glEnd();
835    glBegin(GL_LINE_STRIP);
[4995]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);
[4570]841    glEnd();
[4995]842
[4570]843    glBegin(GL_LINES);
[4995]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);
[4570]852    glEnd();
853  }
854
855  glPopMatrix();
[5007]856  if (depth >= 2 || depth == 0)
857  {
[5432]858    /* rotate the current color in HSV space around 20 degree */
[5115]859    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
[5770]860    list<PNode*>::const_iterator child;
861    for (child = this->children.begin(); child != this->children.end(); child ++)
[5007]862    {
[5394]863      // drawing the Dependency graph
[6074]864     if (this != PNode::getNullParent())
[5394]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);
[5770]872        glVertex3f((*child)->getAbsCoor ().x,
873                   (*child)->getAbsCoor ().y,
874                   (*child)->getAbsCoor ().z);
[5394]875        glEnd();
876      }
[5915]877
[5432]878      /* if we want to draw the children too */
879      if (depth == 0) /* -> all of them */
[5770]880        (*child)->debugDraw(0, size, childColor, level+1);
[5432]881      else            /* -> only the Next one */
[5770]882        (*child)->debugDraw(depth - 1, size, childColor, level +1);
[5007]883    }
884  }
[5383]885  if (level == 0)
[5432]886    glPopAttrib(); /* pop the saved attributes back out */
[4570]887}
[4993]888
889
890
891/////////////////////
892// HELPER_FUCTIONS //
893/////////////////////
894
895/**
[6048]896 * @brief converts a parentingMode into a string that is the name of it
[4993]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/**
[6048]915 * @brief converts a parenting-mode-string into a int
[4993]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.