Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/lib/coord/p_node.cc @ 6685

Last change on this file since 6685 was 6634, checked in by bensch, 19 years ago

orxonox/trunk: merged the network-branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6500:HEAD
minor conflicts in texture and one Makefile resolved to the trunk

also made a small patch to texture, so it Modulates with GL_REPEAT

File size: 35.1 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 <algorithm>
24#include "compiler.h"
25#include "debug.h"
26
27#include "glincl.h"
28#include "color.h"
29
30#include "synchronizeable.h"
31
32#include "shell_command.h"
33SHELL_COMMAND(debugNode, PNode, debugNodeSC);
34
35using namespace std;
36
37
38/**
39 * @brief standard constructor
40 * @param parent the Parent of this Node. __NULL__ if __No Parent__ requested, PNode::getNullParent(), if connected to NullParent directly (default)
41 * @param nodeFlags all flags to set. THIS_WILL_OVERWRITE Default_Values.
42 */
43PNode::PNode (PNode* parent, long nodeFlags)
44{
45  this->setClassID(CL_PARENT_NODE, "PNode");
46
47  this->bRelCoorChanged = true;
48  this->bRelDirChanged = true;
49  this->parent = NULL;
50  this->parentMode = nodeFlags;
51  this->bActive = true;
52
53  // smooth-movers
54  this->toCoordinate = NULL;
55  this->toDirection = NULL;
56  this->bias = 1.0;
57
58  if (parent != NULL)
59    parent->addChild(this);
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::nullParent && (*deleteNode)->parentMode & PNODE_REPARENT_TO_NULL)
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->eraseChild(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  if (this == PNode::nullParent)
115    PNode::nullParent = NULL;
116}
117
118
119/**
120 * @brief loads parameters of the PNode
121 * @param root the XML-element to load the properties of
122 */
123void PNode::loadParams(const TiXmlElement* root)
124{
125  BaseObject::loadParams(root);
126
127  LoadParam(root, "rel-coor", this, PNode, setRelCoor)
128      .describe("Sets The relative position of the Node to its parent.");
129
130  LoadParam(root, "abs-coor", this, PNode, setAbsCoor)
131      .describe("Sets The absolute Position of the Node.");
132
133  LoadParam(root, "rel-dir", this, PNode, setRelDir)
134      .describe("Sets The relative rotation of the Node to its parent.");
135
136  LoadParam(root, "abs-dir", this, PNode, setAbsDir)
137      .describe("Sets The absolute rotation of the Node.");
138
139  LoadParam(root, "parent", this, PNode, setParent)
140      .describe("the Name of the Parent of this PNode");
141
142  LoadParam(root, "parent-mode", this, PNode, setParentMode)
143      .describe("the mode to connect this node to its parent ()");
144
145  // cycling properties
146  if (root != NULL)
147  {
148    LOAD_PARAM_START_CYCLE(root, element);
149    {
150      LoadParam_CYCLE(element, "child", this, PNode, addChild)
151          .describe("adds a new Child to the current Node.");
152
153    }
154    LOAD_PARAM_END_CYCLE(element);
155  }
156}
157
158
159/**
160 *  init the pnode to a well definied state
161 *
162 * this function actualy only updates the PNode tree
163 */
164void PNode::init()
165{
166  /* just update all aboslute positions via timestep 0.001ms */
167  this->updateNode(0.001f);
168  this->updateNode(0.001f);
169}
170
171
172/**
173 * @brief set relative coordinates
174 * @param relCoord relative coordinates to its parent
175 *
176 *
177 * it is very importand, that you use this function, if you want to update the
178 * relCoordinates. If you don't use this, the PNode won't recognize, that something
179 * has changed and won't update the children Nodes.
180 */
181void PNode::setRelCoor (const Vector& relCoord)
182{
183  if (this->toCoordinate!= NULL)
184  {
185    delete this->toCoordinate;
186    this->toCoordinate = NULL;
187  }
188
189  this->relCoordinate = relCoord;
190  this->bRelCoorChanged = true;
191}
192
193/**
194 * @brief set relative coordinates
195 * @param x x-relative coordinates to its parent
196 * @param y y-relative coordinates to its parent
197 * @param z z-relative coordinates to its parent
198 * @see  void PNode::setRelCoor (const Vector& relCoord)
199 */
200void PNode::setRelCoor (float x, float y, float z)
201{
202  this->setRelCoor(Vector(x, y, z));
203}
204
205/**
206 * @brief sets a new relative position smoothely
207 * @param relCoordSoft the new Position to iterate to
208 * @param bias how fast to iterate to this position
209 */
210void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias)
211{
212  if (likely(this->toCoordinate == NULL))
213    this->toCoordinate = new Vector();
214
215  *this->toCoordinate = relCoordSoft;
216  this->toStep = 0.0f;
217  this->bias = bias;
218}
219
220
221/**
222 * @brief set relative coordinates smoothely
223 * @param x x-relative coordinates to its parent
224 * @param y y-relative coordinates to its parent
225 * @param z z-relative coordinates to its parent
226 * @see  void PNode::setRelCoorSoft (const Vector&, float)
227 */
228void PNode::setRelCoorSoft (float x, float y, float z, float bias)
229{
230  this->setRelCoorSoft(Vector(x, y, z), bias);
231}
232
233
234/**
235 * @param absCoord set absolute coordinate
236 */
237void PNode::setAbsCoor (const Vector& absCoord)
238{
239  if (this->toCoordinate!= NULL)
240  {
241    delete this->toCoordinate;
242    this->toCoordinate = NULL;
243  }
244
245  if( likely(this->parentMode & PNODE_MOVEMENT))
246  {
247      /* if you have set the absolute coordinates this overrides all other changes */
248    if (likely(this->parent != NULL))
249      this->relCoordinate = absCoord - parent->getAbsCoor ();
250    else
251      this->relCoordinate = absCoord;
252  }
253  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
254  {
255    if (likely(this->parent != NULL))
256      this->relCoordinate = absCoord - parent->getAbsCoor ();
257    else
258      this->relCoordinate = absCoord;
259  }
260
261  this->bRelCoorChanged = true;
262//  this->absCoordinate = absCoord;
263}
264
265
266/**
267 * @param x x-coordinate.
268 * @param y y-coordinate.
269 * @param z z-coordinate.
270 * @see void PNode::setAbsCoor (const Vector& absCoord)
271 */
272void PNode::setAbsCoor(float x, float y, float z)
273{
274  this->setAbsCoor(Vector(x, y, z));
275}
276
277
278/**
279 * @param absCoord set absolute coordinate
280 * @todo check off
281 */
282void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias)
283{
284  if (this->toCoordinate == NULL)
285    this->toCoordinate = new Vector;
286
287  if( likely(this->parentMode & PNODE_MOVEMENT))
288  {
289      /* if you have set the absolute coordinates this overrides all other changes */
290    if (likely(this->parent != NULL))
291      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
292    else
293      *this->toCoordinate = absCoordSoft;
294  }
295  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
296  {
297    if (likely(this->parent != NULL))
298      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
299    else
300      *this->toCoordinate = absCoordSoft;
301  }
302  this->toStep = 0.0f;
303}
304
305
306/**
307 * @brief shift coordinate relative
308 * @param shift shift vector
309 *
310 * this function shifts the current coordinates about the vector shift. this is
311 * usefull because from some place else you can:
312 * PNode* someNode = ...;
313 * Vector objectMovement = calculateShift();
314 * someNode->shiftCoor(objectMovement);
315 *
316 * this is the internal method of:
317 * PNode* someNode = ...;
318 * Vector objectMovement = calculateShift();
319 * Vector currentCoor = someNode->getRelCoor();
320 * Vector newCoor = currentCoor + objectMovement;
321 * someNode->setRelCoor(newCoor);
322 *
323 */
324void PNode::shiftCoor (const Vector& shift)
325{
326  this->relCoordinate += shift;
327  this->bRelCoorChanged = true;
328}
329
330
331/**
332 * @brief set relative direction
333 * @param relDir to its parent
334 */
335void PNode::setRelDir (const Quaternion& relDir)
336{
337  if (this->toDirection!= NULL)
338  {
339    delete this->toDirection;
340    this->toDirection = NULL;
341  }
342  this->relDirection = relDir;
343
344  this->bRelCoorChanged = true;
345}
346
347
348/**
349 * @see void PNode::setRelDir (const Quaternion& relDir)
350 * @param x the x direction
351 * @param y the y direction
352 * @param z the z direction
353 *
354 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
355 */
356void PNode::setRelDir (float x, float y, float z)
357{
358  this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
359}
360
361
362/**
363 * @brief sets the Relative Direction of this node to its parent in a Smoothed way
364 * @param relDirSoft the direction to iterate to smoothely.
365 * @param bias how fast to iterate to the new Direction
366 */
367void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias)
368{
369  if (likely(this->toDirection == NULL))
370    this->toDirection = new Quaternion();
371
372  *this->toDirection = relDirSoft;
373  this->bias = bias;
374  this->toStep = 0.0f;
375  this->bRelDirChanged = true;
376}
377
378
379/**
380 * @see void PNode::setRelDirSoft (const Quaternion& relDir)
381 * @param x the x direction
382 * @param y the y direction
383 * @param z the z direction
384 *
385 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
386 */
387void PNode::setRelDirSoft(float x, float y, float z, float bias)
388{
389  this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
390}
391
392
393/**
394 * @brief sets the absolute direction
395 * @param absDir absolute coordinates
396 */
397void PNode::setAbsDir (const Quaternion& absDir)
398{
399  if (this->toDirection!= NULL)
400  {
401    delete this->toDirection;
402    this->toDirection = NULL;
403  }
404
405  if (likely(this->parent != NULL))
406    this->relDirection = absDir / this->parent->getAbsDir();
407  else
408   this->relDirection = absDir;
409
410  this->bRelDirChanged = true;
411}
412
413
414/**
415 * @see void PNode::setAbsDir (const Quaternion& relDir)
416 * @param x the x direction
417 * @param y the y direction
418 * @param z the z direction
419 *
420 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
421 */
422void PNode::setAbsDir (float x, float y, float z)
423{
424  this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
425}
426
427
428/**
429 * @brief sets the absolute direction
430 * @param absDir absolute coordinates
431 * @param bias how fast to iterator to the new Position
432 */
433void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias)
434{
435  if (this->toDirection == NULL)
436    this->toDirection = new Quaternion();
437
438  if (likely(this->parent != NULL))
439    *this->toDirection = absDirSoft / this->parent->getAbsDir();
440  else
441   *this->toDirection = absDirSoft;
442
443  this->bias = bias;
444  this->toStep = 0.0f;
445  this->bRelDirChanged = true;
446}
447
448
449/**
450 * @see void PNode::setAbsDir (const Quaternion& relDir)
451 * @param x the x direction
452 * @param y the y direction
453 * @param z the z direction
454 *
455 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
456 */
457void PNode::setAbsDirSoft (float x, float y, float z, float bias)
458{
459  this->setAbsDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
460}
461
462
463/**
464 * @brief shift Direction
465 * @param shift the direction around which to shift.
466 */
467void PNode::shiftDir (const Quaternion& shift)
468{
469  this->relDirection = this->relDirection * shift;
470  this->bRelDirChanged = true;
471}
472
473
474/**
475 * @brief adds a child and makes this node to a parent
476 * @param child child reference
477 * use this to add a child to this node.
478 */
479void PNode::addChild (PNode* child)
480{
481  if( likely(child->parent != NULL))
482      child->parent->eraseChild(child);
483  if (this->checkIntegrity(child))
484  {
485    child->parent = this;
486    if (unlikely(this != NULL))
487      this->children.push_back(child);
488    child->parentCoorChanged();
489  }
490  else
491  {
492    PRINTF(1)("Tried to reparent to own child '%s::%s' to '%s::%s'.\n",
493              this->getClassName(), this->getName(), child->getClassName(), child->getName());
494    child->parent = NULL;
495    child->parentCoorChanged();
496  }
497}
498
499
500/**
501 * @see PNode::addChild(PNode* child);
502 * @param childName the name of the child to add to this PNode
503 */
504void PNode::addChild (const char* childName)
505{
506  PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE));
507//  PRINTF(0)("Adding the Child: %s to: %s\n", childName, this->getName());
508//  assert( childNode != NULL );
509  if (childNode != NULL)
510  {
511    this->addChild(childNode);
512  }
513}
514
515
516/**
517 * @brief removes a child from the node
518 * @param child the child to remove from this pNode.
519 *
520 * Children from pNode will not be lost, they are Reparented by the rules of the ParentMode
521 */
522void PNode::removeChild (PNode* child)
523{
524  if (child != NULL)
525   child->removeNode();
526}
527
528
529/**
530 * !! PRIVATE FUNCTION
531 * @brief reparents a node (happens on Parents Node delete or remove and Flags are set.)
532 */
533void PNode::reparent()
534{
535  if (this->parentMode & PNODE_REPARENT_TO_NULL)
536    this->setParent((PNode*)NULL);
537  else if (this->parentMode & PNODE_REPARENT_TO_PARENTS_PARENT && this->parent != NULL)
538    this->setParent(this->parent->getParent());
539  else
540    this->setParent(PNode::getNullParent());
541}
542
543/**
544 * ereases child from the nodes children
545 * @param chuld the child to remove
546 */
547void PNode::eraseChild(PNode* child)
548{
549  std::list<PNode*>::iterator childRemover = std::find(this->children.begin(), this->children.end(), child);
550  if(childRemover != this->children.end())
551    this->children.erase(childRemover);
552}
553
554
555/**
556 * @brief remove this pnode from the tree and adds all following to NullParent
557 *
558 * this can be the case, if an entity in the world is being destroyed.
559 */
560void PNode::removeNode()
561{
562  list<PNode*>::iterator child = this->children.begin();
563  list<PNode*>::iterator reparenter;
564  while (child != this->children.end())
565  {
566    reparenter = child;
567    child++;
568    if (this->parentMode & PNODE_REPARENT_CHILDREN_ON_REMOVE ||
569        (*reparenter)->parentMode & PNODE_REPARENT_ON_PARENTS_REMOVE)
570    {
571      printf("TEST----------------%s ---- %s\n", this->getClassName(), (*reparenter)->getClassName());
572      (*reparenter)->reparent();
573      printf("REPARENTED TO: %s::%s\n",(*reparenter)->getParent()->getClassName(),(*reparenter)->getParent()->getName());
574    }
575  }
576  if (this->parent != NULL)
577  {
578    this->parent->eraseChild(this);
579    this->parent = NULL;
580  }
581}
582
583
584/**
585 * @see PNode::setParent(PNode* parent);
586 * @param parentName the name of the Parent to set to this PNode
587 */
588void PNode::setParent (const char* parentName)
589{
590  PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE));
591  if (parentNode != NULL)
592    parentNode->addChild(this);
593  else
594    PRINTF(2)("Not Found PNode's (%s::%s) new Parent by Name: %s\n",
595              this->getClassName(), this->getName(), parentName);
596}
597
598
599/**
600 * @brief does the reparenting in a very smooth way
601 * @param parentNode the new Node to connect this node to.
602 * @param bias the speed to iterate to this new Positions
603 */
604void PNode::setParentSoft(PNode* parentNode, float bias)
605{
606  // return if the new parent and the old one match
607  if (this->parent == parentNode )
608    return;
609  if (parentNode == NULL)
610    parentNode = PNode::getNullParent();
611
612  // store the Valures to iterate to.
613  if (likely(this->toCoordinate == NULL))
614  {
615    this->toCoordinate = new Vector();
616    *this->toCoordinate = this->getRelCoor();
617  }
618  if (likely(this->toDirection == NULL))
619  {
620    this->toDirection = new Quaternion();
621    *this->toDirection = this->getRelDir();
622  }
623  this->bias = bias;
624  this->toStep = 0.0f;
625
626  Vector tmpV = this->getAbsCoor();
627  Quaternion tmpQ = this->getAbsDir();
628
629  parentNode->addChild(this);
630
631 if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL)
632   this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor());
633 else
634   this->relCoordinate = tmpV - parentNode->getAbsCoor();
635
636 this->relDirection = tmpQ / parentNode->getAbsDir();
637}
638
639
640/**
641 * @brief does the reparenting in a very smooth way
642 * @param parentName the name of the Parent to reconnect to
643 * @param bias the speed to iterate to this new Positions
644 */
645void PNode::setParentSoft(const char* parentName, float bias)
646{
647  PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE));
648  if (parentNode != NULL)
649    this->setParentSoft(parentNode, bias);
650}
651
652/**
653 * @param parentMode sets the parentingMode of this Node
654 */
655void PNode::setParentMode(PARENT_MODE parentMode)
656{
657  this->parentMode = ((this->parentMode & 0xfff0) | parentMode);
658}
659
660/**
661 * @brief sets the mode of this parent manually
662 * @param parentMode a String representing this parentingMode
663 */
664void PNode::setParentMode (const char* parentingMode)
665{
666  this->setParentMode(PNode::charToParentingMode(parentingMode));
667}
668
669/**
670 * @brief adds special mode Flags to this PNode
671 * @see PARENT_MODE
672 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
673 */
674void PNode::addNodeFlags(unsigned short nodeFlags)
675{
676  this->parentMode |= nodeFlags;
677}
678
679/**
680 * @brief removes special mode Flags to this PNode
681 * @see PARENT_MODE
682 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
683 */
684void PNode::removeNodeFlags(unsigned short nodeFlags)
685{
686  this->parentMode &= !nodeFlags;
687}
688
689/**
690 * @returns the NullParent (and if needed creates it)
691 */
692PNode* PNode::createNullParent()
693{
694  if (likely(PNode::nullParent == NULL))
695  {
696    PNode::nullParent = new PNode(NULL, PNODE_PARENT_MODE_DEFAULT | PNODE_REPARENT_TO_NULL);
697    PNode::nullParent->setName("NullParent");
698  }
699  return PNode::nullParent;
700}
701
702
703/**
704 * !! PRIVATE FUNCTION
705 * @brief checks the upward integrity (e.g if PNode is somewhere up the Node tree.)
706 * @param checkParent the Parent to check.
707 * @returns true if the integrity-check succeeds, false otherwise.
708 *
709 * If there is a second occurence of checkParent before NULL, then a loop could get
710 * into the Tree, and we do not want this.
711 */
712bool PNode::checkIntegrity(const PNode* checkParent) const
713{
714  const PNode* parent = this;
715  while ( (parent = parent->getParent()) != NULL)
716    if (unlikely(parent == checkParent))
717      return false;
718  return true;
719}
720
721
722/**
723 * @brief updates the absCoordinate/absDirection
724 * @param dt The time passed since the last update
725 *
726 * this is used to go through the parent-tree to update all the absolute coordinates
727 * and directions. this update should be done by the engine, so you don't have to
728 * worry, normaly...
729 */
730void PNode::updateNode (float dt)
731{
732  if (!(this->parentMode & PNODE_STATIC_NODE))
733  {
734    if( likely(this->parent != NULL))
735    {
736        // movement for nodes with smoothMove enabled
737        if (unlikely(this->toCoordinate != NULL))
738        {
739          Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
740          if (likely(moveVect.len() >= PNODE_ITERATION_DELTA))
741          {
742            this->shiftCoor(moveVect);
743          }
744          else
745          {
746            delete this->toCoordinate;
747            this->toCoordinate = NULL;
748            PRINTF(5)("SmoothMove of %s finished\n", this->getName());
749          }
750        }
751        if (unlikely(this->toDirection != NULL))
752        {
753          this->toStep += fabs(dt) * bias;
754          //printf("%s::%s %f\n", this->getClassName(), this->getName(), this->toStep );
755          Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, toStep);
756          if (this->relDirection.distance(rotQuat) >PNODE_ITERATION_DELTA)
757          {
758            this->relDirection = rotQuat;
759            this->bRelDirChanged;
760          }
761          else
762          {
763            delete this->toDirection;
764            this->toDirection = NULL;
765            PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
766          }
767        }
768
769        // MAIN UPDATE /////////////////////////////////////
770        this->lastAbsCoordinate = this->absCoordinate;
771
772        PRINTF(5)("PNode::update - '%s::%s' - (%f, %f, %f)\n", this->getClassName(), this->getName(),
773                      this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
774
775
776        if(this->bRelDirChanged && this->parentMode & PNODE_LOCAL_ROTATE )
777        {
778          /* update the current absDirection - remember * means rotation around sth.*/
779          this->prevRelCoordinate = this->relCoordinate;
780          this->absDirection = parent->getAbsDir() * this->relDirection;
781        }
782
783        if(likely(this->bRelCoorChanged && this->parentMode & PNODE_MOVEMENT))
784        {
785        /* update the current absCoordinate */
786          this->prevRelCoordinate = this->relCoordinate;
787          this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate;
788        }
789        else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
790        {
791          /* update the current absCoordinate */
792          this->prevRelCoordinate = this->relCoordinate;
793          this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate);
794        }
795        /////////////////////////////////////////////////
796      }
797
798  else // Nodes without a Parent are handled faster :: MOST LIKELY THE NULLPARENT
799    {
800      PRINTF(4)("update ParentLess Node (%s::%s) - (%f, %f, %f)\n", this->getClassName(), this->getName(),
801                this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
802        if (this->bRelCoorChanged)
803        {
804          this->prevRelCoordinate = this->relCoordinate;
805          this->absCoordinate = this->relCoordinate;
806        }
807        if (this->bRelDirChanged)
808        {
809          this->prevRelDirection = this->relDirection;
810          this->absDirection = this->getAbsDir () * this->relDirection;
811        }
812      }
813    }
814
815    if(!this->children.empty() && (this->bActive || this->parentMode & PNODE_UPDATE_CHILDREN_IF_INACTIVE ))
816    {
817      list<PNode*>::iterator child;
818      for (child = this->children.begin(); child != this->children.end(); child ++)
819      {
820        /* if this node has changed, make sure, that all children are updated also */
821        if( likely(this->bRelCoorChanged))
822          (*child)->parentCoorChanged ();
823        if( likely(this->bRelDirChanged))
824          (*child)->parentDirChanged ();
825
826        (*child)->updateNode(dt);
827      }
828    }
829    this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
830    this->bRelCoorChanged = false;
831    this->bRelDirChanged = false;
832}
833
834
835
836
837
838/*************
839 * DEBUGGING *
840 *************/
841/**
842 * @brief counts total amount the children walking through the entire tree.
843 * @param nodes the counter
844 */
845void PNode::countChildNodes(int& nodes) const
846{
847  nodes++;
848  list<PNode*>::const_iterator child;
849  for (child = this->children.begin(); child != this->children.end(); child ++)
850    (*child)->countChildNodes(nodes);
851}
852
853
854/**
855 * @brief displays some information about this pNode
856 * @param depth The deph into which to debug the children of this PNode to.
857 * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...)
858 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
859 */
860void PNode::debugNode(unsigned int depth, unsigned int level) const
861{
862  for (unsigned int i = 0; i < level; i++)
863    PRINT(0)(" |");
864  if (this->children.size() > 0)
865    PRINT(0)(" +");
866  else
867    PRINT(0)(" -");
868
869  int childNodeCount = 0;
870  this->countChildNodes(childNodeCount);
871
872  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",
873           this->getClassName(),
874           this->getName(),
875           this->absCoordinate.x,
876           this->absCoordinate.y,
877           this->absCoordinate.z,
878           this->relCoordinate.x,
879           this->relCoordinate.y,
880           this->relCoordinate.z,
881           this->getAbsDirV().x,
882           this->getAbsDirV().y,
883           this->getAbsDirV().z,
884           this->parentingModeToChar(parentMode),
885           childNodeCount);
886  if (depth >= 2 || depth == 0)
887  {
888    list<PNode*>::const_iterator child;
889    for (child = this->children.begin(); child != this->children.end(); child ++)
890    {
891      if (depth == 0)
892        (*child)->debugNode(0, level + 1);
893      else
894        (*child)->debugNode(depth - 1, level +1);
895    }
896  }
897}
898
899/**
900 * @brief displays the PNode at its position with its rotation as a cube.
901 * @param  depth The deph into which to debug the children of this PNode to.
902 * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...)
903 * @param size the Size of the Box to draw.
904 * @param color the color of the Box to display.
905 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
906 */
907void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const
908{
909  // if this is the first Element we draw
910  if (level == 0)
911  {
912    glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes
913    glMatrixMode(GL_MODELVIEW);  // goto the ModelView Matrix
914
915    glDisable(GL_LIGHTING);      // disable lighting (we do not need them for just lighting)
916    glDisable(GL_BLEND);         // ''
917    glDisable(GL_TEXTURE_2D);    // ''
918    glDisable(GL_DEPTH_TEST);    // ''
919  }
920
921  glPushMatrix();                // repush the Matrix-stack
922  /* translate */
923  glTranslatef (this->getAbsCoor ().x,
924                this->getAbsCoor ().y,
925                this->getAbsCoor ().z);
926//  this->getAbsDir ().matrix (matrix);
927
928  /* rotate */
929  Vector tmpRot = this->getAbsDir().getSpacialAxis();
930  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
931  /* set the new Color */
932  glColor3f(color.x, color.y, color.z);
933  { /* draw a cube of size size */
934    glBegin(GL_LINE_STRIP);
935    glVertex3f(-.5*size, -.5*size,  -.5*size);
936    glVertex3f(+.5*size, -.5*size,  -.5*size);
937    glVertex3f(+.5*size, -.5*size,  +.5*size);
938    glVertex3f(-.5*size, -.5*size,  +.5*size);
939    glVertex3f(-.5*size, -.5*size,  -.5*size);
940    glEnd();
941    glBegin(GL_LINE_STRIP);
942    glVertex3f(-.5*size, +.5*size,  -.5*size);
943    glVertex3f(+.5*size, +.5*size,  -.5*size);
944    glVertex3f(+.5*size, +.5*size,  +.5*size);
945    glVertex3f(-.5*size, +.5*size,  +.5*size);
946    glVertex3f(-.5*size, +.5*size,  -.5*size);
947    glEnd();
948
949    glBegin(GL_LINES);
950    glVertex3f(-.5*size, -.5*size,  -.5*size);
951    glVertex3f(-.5*size, +.5*size,  -.5*size);
952    glVertex3f(+.5*size, -.5*size,  -.5*size);
953    glVertex3f(+.5*size, +.5*size,  -.5*size);
954    glVertex3f(+.5*size, -.5*size,  +.5*size);
955    glVertex3f(+.5*size, +.5*size,  +.5*size);
956    glVertex3f(-.5*size, -.5*size,  +.5*size);
957    glVertex3f(-.5*size, +.5*size,  +.5*size);
958    glEnd();
959  }
960
961  glPopMatrix();
962  if (depth >= 2 || depth == 0)
963  {
964    /* rotate the current color in HSV space around 20 degree */
965    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
966    list<PNode*>::const_iterator child;
967    for (child = this->children.begin(); child != this->children.end(); child ++)
968    {
969      // drawing the Dependency graph
970     if (this != PNode::getNullParent())
971      {
972       glBegin(GL_LINES);
973       glColor3f(color.x, color.y, color.z);
974       glVertex3f(this->getAbsCoor ().x,
975                  this->getAbsCoor ().y,
976                  this->getAbsCoor ().z);
977        glColor3f(childColor.x, childColor.y, childColor.z);
978        glVertex3f((*child)->getAbsCoor ().x,
979                   (*child)->getAbsCoor ().y,
980                   (*child)->getAbsCoor ().z);
981        glEnd();
982      }
983
984      /* if we want to draw the children too */
985      if (depth == 0) /* -> all of them */
986        (*child)->debugDraw(0, size, childColor, level+1);
987      else            /* -> only the Next one */
988        (*child)->debugDraw(depth - 1, size, childColor, level +1);
989    }
990  }
991  if (level == 0)
992    glPopAttrib(); /* pop the saved attributes back out */
993}
994
995
996
997/////////////////////
998// HELPER_FUCTIONS //
999/////////////////////
1000
1001/**
1002 * @brief converts a parentingMode into a string that is the name of it
1003 * @param parentingMode the ParentingMode to convert
1004 * @return the converted string
1005 */
1006const char* PNode::parentingModeToChar(int parentingMode)
1007{
1008  if (parentingMode == PNODE_LOCAL_ROTATE)
1009    return "local-rotate";
1010  else if (parentingMode == PNODE_ROTATE_MOVEMENT)
1011    return "rotate-movement";
1012  else if (parentingMode == PNODE_MOVEMENT)
1013    return "movement";
1014  else if (parentingMode == PNODE_ALL)
1015    return "all";
1016  else if (parentingMode == PNODE_ROTATE_AND_MOVE)
1017    return "rotate-and-move";
1018}
1019
1020/**
1021 * @brief converts a parenting-mode-string into a int
1022 * @param parentingMode the string naming the parentingMode
1023 * @return the int corresponding to the named parentingMode
1024 */
1025PARENT_MODE PNode::charToParentingMode(const char* parentingMode)
1026{
1027  if (!strcmp(parentingMode, "local-rotate"))
1028    return (PNODE_LOCAL_ROTATE);
1029  else  if (!strcmp(parentingMode, "rotate-movement"))
1030    return (PNODE_ROTATE_MOVEMENT);
1031  else  if (!strcmp(parentingMode, "movement"))
1032    return (PNODE_MOVEMENT);
1033  else  if (!strcmp(parentingMode, "all"))
1034    return (PNODE_ALL);
1035  else  if (!strcmp(parentingMode, "rotate-and-move"))
1036    return (PNODE_ROTATE_AND_MOVE);
1037}
1038
1039/**
1040 * Writes data from network containing information about the state
1041 * @param data pointer to data
1042 * @param length length of data
1043 * @param sender hostID of sender
1044 */
1045int PNode::writeState( const byte * data, int length, int sender )
1046{
1047  SYNCHELP_READ_BEGIN();
1048
1049  SYNCHELP_READ_FKT( BaseObject::writeState );
1050
1051  char * parentName = NULL;
1052  SYNCHELP_READ_STRINGM( parentName );
1053
1054  if ( strcmp(parentName, "")==0 )
1055  {
1056    setParent( (char*)NULL );
1057  }
1058  else
1059  {
1060    setParent( parentName );
1061  }
1062
1063  delete[] parentName;
1064
1065  int parentMode;
1066  SYNCHELP_READ_INT( parentMode );
1067  this->setParentMode((PARENT_MODE)parentMode);
1068
1069  float f1, f2, f3, f4;
1070
1071  SYNCHELP_READ_FLOAT( f1 );
1072  SYNCHELP_READ_FLOAT( f2 );
1073  SYNCHELP_READ_FLOAT( f3 );
1074  this->setRelCoor( f1, f2, f3 );
1075
1076
1077  SYNCHELP_READ_FLOAT( f1 );
1078  SYNCHELP_READ_FLOAT( f2 );
1079  SYNCHELP_READ_FLOAT( f3 );
1080  SYNCHELP_READ_FLOAT( f4 );
1081  this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1082
1083  int n;
1084  char * childName;
1085
1086  PRINTF(0)("JKLO %d %d %d %d\n", data[__synchelp_read_i], data[__synchelp_read_i+1], data[__synchelp_read_i+2], data[__synchelp_read_i+3]);
1087  SYNCHELP_READ_INT( n );
1088  PRINTF(0)("read %s:n=%d\n", this->getName(), n);
1089
1090  for (int i = 0; i<n; i++)
1091  {
1092    SYNCHELP_READ_STRINGM( childName );
1093    PRINTF(0)("RCVD CHILD = %s\n", childName);
1094    addChild( childName );
1095    delete childName;
1096    childName = NULL;
1097  }
1098
1099  return SYNCHELP_READ_N;
1100}
1101
1102/**
1103 * data copied in data will bee sent to another host
1104 * @param data pointer to data
1105 * @param maxLength max length of data
1106 * @return the number of bytes writen
1107 */
1108int PNode::readState( byte * data, int maxLength )
1109{
1110  SYNCHELP_WRITE_BEGIN();
1111
1112  SYNCHELP_WRITE_FKT( BaseObject::readState );
1113
1114  if ( this->parent )
1115  {
1116    SYNCHELP_WRITE_STRING( parent->getName() );
1117  }
1118  else
1119  {
1120    SYNCHELP_WRITE_STRING( "" );
1121  }
1122
1123  SYNCHELP_WRITE_INT( this->parentMode );
1124
1125  SYNCHELP_WRITE_FLOAT( this->relCoordinate.x );
1126  SYNCHELP_WRITE_FLOAT( this->relCoordinate.y );
1127  SYNCHELP_WRITE_FLOAT( this->relCoordinate.z );
1128
1129  SYNCHELP_WRITE_FLOAT( this->relDirection.w );
1130  SYNCHELP_WRITE_FLOAT( this->relDirection.v.x );
1131  SYNCHELP_WRITE_FLOAT( this->relDirection.v.y );
1132  SYNCHELP_WRITE_FLOAT( this->relDirection.v.z );
1133
1134  int n = children.size();
1135
1136  //check if camera is in children
1137  for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1138  {
1139    if ( (*it)->isA(CL_CAMERA) )
1140      n--;
1141  }
1142  PRINTF(0)("write %s:n=%d\n", this->getName(), n);
1143  SYNCHELP_WRITE_INT( n );
1144  PRINTF(0)("ASDF %d %d %d %d\n", data[__synchelp_write_i-4], data[__synchelp_write_i-3], data[__synchelp_write_i-2], data[__synchelp_write_i-1]);
1145
1146  for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1147  {
1148    //dont add camera because there is only one camera attached to local player
1149    if ( !(*it)->isA(CL_CAMERA) )
1150    {
1151      PRINTF(0)("SENDING CHILD: %s\n", (*it)->getName());
1152      SYNCHELP_WRITE_STRING( (*it)->getName() );
1153    }
1154  }
1155
1156  return SYNCHELP_WRITE_N;
1157}
1158
1159#define __FLAG_COOR 1
1160#define __FLAG_ROT  2
1161
1162#define __OFFSET_POS 1
1163#define __OFFSET_ROT 0.05
1164
1165/**
1166 * Writes data from network containing information about the state which has changed
1167 * @param data pointer to data
1168 * @param length length of data
1169 * @param sender hostID of sender
1170 */
1171int PNode::writeSync( const byte * data, int length, int sender )
1172{
1173  SYNCHELP_READ_BEGIN();
1174
1175  if ( this->getHostID()==this->getOwner() )
1176  {
1177    return SYNCHELP_READ_N;
1178  }
1179
1180  byte flags = 0;
1181  SYNCHELP_READ_BYTE( flags );
1182  //PRINTF(0)("%s::FLAGS = %d\n", this->getName(), flags);
1183
1184  float f1, f2, f3, f4;
1185
1186  if ( flags & __FLAG_COOR )
1187  {
1188    SYNCHELP_READ_FLOAT( f1 );
1189    SYNCHELP_READ_FLOAT( f2 );
1190    SYNCHELP_READ_FLOAT( f3 );
1191    PRINTF(0)("RCVD COOR: %f %f %f\n", f1, f2, f3);
1192    this->setRelCoor( f1, f2, f3 );
1193  }
1194
1195  if ( flags & __FLAG_ROT )
1196  {
1197    SYNCHELP_READ_FLOAT( f1 );
1198    SYNCHELP_READ_FLOAT( f2 );
1199    SYNCHELP_READ_FLOAT( f3 );
1200    SYNCHELP_READ_FLOAT( f4 );
1201    PRINTF(0)("RCVD QUAT: %f %f %f %f\n", f1, f2, f3, f4);
1202    //this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1203    Quaternion q;
1204    q.w = f1;
1205    q.v.x = f2;
1206    q.v.y = f3;
1207    q.v.z = f4;
1208    this->setAbsDir( q );
1209  }
1210
1211  return SYNCHELP_READ_N;
1212}
1213
1214/**
1215 * data copied in data will bee sent to another host
1216 * @param data pointer to data
1217 * @param maxLength max length of data
1218 * @return the number of bytes writen
1219 */
1220int PNode::readSync( byte * data, int maxLength )
1221{
1222  SYNCHELP_WRITE_BEGIN();
1223
1224  if ( this->getHostID()!=this->getOwner() )
1225  {
1226    return SYNCHELP_WRITE_N;
1227  }
1228
1229  byte flags = 0;
1230  if ( fabs( coorx - relCoordinate.x ) > __OFFSET_POS ||
1231       fabs( coory - relCoordinate.y ) > __OFFSET_POS ||
1232       fabs( coorz - relCoordinate.z ) > __OFFSET_POS )
1233    flags |= __FLAG_COOR;
1234
1235  if ( fabs( rotw - absDirection.w ) > __OFFSET_ROT ||
1236       fabs( rotx - absDirection.v.x ) > __OFFSET_ROT ||
1237       fabs( roty - absDirection.v.y ) > __OFFSET_ROT ||
1238       fabs( rotz - absDirection.v.z ) > __OFFSET_ROT )
1239    flags |= __FLAG_ROT;
1240
1241
1242  SYNCHELP_WRITE_BYTE( flags );
1243  //PRINTF(0)("FLAGS = %d\n", flags);
1244
1245  if ( flags & __FLAG_COOR )
1246  {
1247
1248    PRINTF(0)("SEND COOR: %f %f %f\n", this->relCoordinate.x, this->relCoordinate.y, this->relCoordinate.z);
1249
1250    SYNCHELP_WRITE_FLOAT( this->relCoordinate.x );
1251    SYNCHELP_WRITE_FLOAT( this->relCoordinate.y );
1252    SYNCHELP_WRITE_FLOAT( this->relCoordinate.z );
1253
1254    coorx = relCoordinate.x;
1255    coory = relCoordinate.y;
1256    coorz = relCoordinate.z;
1257  }
1258
1259  if ( flags & __FLAG_ROT )
1260  {
1261
1262    PRINTF(0)("SEND QUAT: %f %f %f %f\n", this->absDirection.w, this->absDirection.v.x, this->absDirection.v.y, this->absDirection.v.z);
1263
1264    SYNCHELP_WRITE_FLOAT( this->absDirection.w );
1265    SYNCHELP_WRITE_FLOAT( this->absDirection.v.x );
1266    SYNCHELP_WRITE_FLOAT( this->absDirection.v.y );
1267    SYNCHELP_WRITE_FLOAT( this->absDirection.v.z );
1268
1269    rotw = absDirection.w;
1270    rotx = absDirection.v.x;
1271    roty = absDirection.v.y;
1272    rotz = absDirection.v.z;
1273  }
1274
1275  return SYNCHELP_WRITE_N;
1276}
Note: See TracBrowser for help on using the repository browser.