SceneNode.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "anki/scene/SceneNode.h"
  2. #include "anki/scene/Scene.h"
  3. #include "anki/util/Exception.h"
  4. #include <algorithm>
  5. #include <boost/lexical_cast.hpp>
  6. #include <boost/foreach.hpp>
  7. namespace anki {
  8. //==============================================================================
  9. // Statics =
  10. //==============================================================================
  11. uint SceneNode::uid = 0;
  12. //==============================================================================
  13. // Constructor =
  14. //==============================================================================
  15. SceneNode::SceneNode(SceneNodeType type_, Scene& scene_, ulong flags_,
  16. SceneNode* parent_)
  17. : type(type_),
  18. flags(flags_),
  19. parent(NULL),
  20. scene(scene_)
  21. {
  22. ++uid;
  23. getWorldTransform().setIdentity();
  24. getLocalTransform().setIdentity();
  25. name = boost::lexical_cast<std::string>(uid);
  26. enableFlag(SNF_ACTIVE);
  27. if(parent_ != NULL)
  28. {
  29. parent_->addChild(*this);
  30. }
  31. // This goes last
  32. scene.registerNode(this);
  33. }
  34. //==============================================================================
  35. // Destructor =
  36. //==============================================================================
  37. SceneNode::~SceneNode()
  38. {
  39. scene.unregisterNode(this);
  40. if(parent != NULL)
  41. {
  42. parent->removeChild(*this);
  43. }
  44. if(isFlagEnabled(SNF_ON_DELETE_DELETE_CHILDREN))
  45. {
  46. BOOST_REVERSE_FOREACH(SceneNode* child, children)
  47. {
  48. delete child;
  49. }
  50. }
  51. else
  52. {
  53. BOOST_REVERSE_FOREACH(SceneNode* child, children)
  54. {
  55. child->parent = NULL;
  56. }
  57. }
  58. }
  59. //==============================================================================
  60. // updateWorldTransform =
  61. //==============================================================================
  62. void SceneNode::updateWorldTransform()
  63. {
  64. prevWTrf = wTrf;
  65. if(parent)
  66. {
  67. if(isFlagEnabled(SNF_INHERIT_PARENT_TRANSFORM))
  68. {
  69. wTrf = parent->getWorldTransform();
  70. }
  71. else
  72. {
  73. wTrf = Transform::combineTransformations(
  74. parent->getWorldTransform(), lTrf);
  75. }
  76. }
  77. else // else copy
  78. {
  79. wTrf = lTrf;
  80. }
  81. }
  82. //==============================================================================
  83. // addChild =
  84. //==============================================================================
  85. void SceneNode::addChild(SceneNode& child)
  86. {
  87. ANKI_ASSERT(child.parent == NULL); // Child already has parent
  88. child.parent = this;
  89. children.push_back(&child);
  90. }
  91. //==============================================================================
  92. // removeChild =
  93. //==============================================================================
  94. void SceneNode::removeChild(SceneNode& child)
  95. {
  96. std::vector<SceneNode*>::iterator it = std::find(children.begin(),
  97. children.end(), &child);
  98. if(it == children.end())
  99. {
  100. throw ANKI_EXCEPTION("Child not found");
  101. }
  102. children.erase(it);
  103. child.parent = NULL;
  104. }
  105. //==============================================================================
  106. // Move(s) =
  107. //==============================================================================
  108. void SceneNode::moveLocalX(float distance)
  109. {
  110. Vec3 x_axis = lTrf.getRotation().getColumn(0);
  111. getLocalTransform().getOrigin() += x_axis * distance;
  112. }
  113. void SceneNode::moveLocalY(float distance)
  114. {
  115. Vec3 y_axis = lTrf.getRotation().getColumn(1);
  116. getLocalTransform().getOrigin() += y_axis * distance;
  117. }
  118. void SceneNode::moveLocalZ(float distance)
  119. {
  120. Vec3 z_axis = lTrf.getRotation().getColumn(2);
  121. getLocalTransform().getOrigin() += z_axis * distance;
  122. }
  123. } // end namespace