Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SceneNode.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "SceneNode.h"
  2. namespace BlueCore
  3. {
  4. #define DEBUG_SCENEGRAPH
  5. SceneNode::SceneNode() :
  6. Named("unnamed SceneNode"), _Parent(0)
  7. {
  8. #ifdef DEBUG_SCENEGRAPH
  9. clog << "SceneNode 'Unnamed SceneNode' created." << endline;
  10. #endif
  11. }
  12. SceneNode::SceneNode(const std::string &name) :
  13. Named(name), _Parent(0)
  14. {
  15. #ifdef DEBUG_SCENEGRAPH
  16. clog << "SceneNode '" << name << "' created." << endline;
  17. #endif
  18. }
  19. SceneNode::~SceneNode()
  20. {
  21. detachAll();
  22. #ifdef DEBUG_SCENEGRAPH
  23. clog << "SceneNode '" << getName() << "' deleted." << endline;
  24. #endif
  25. }
  26. void SceneNode::attach(SceneNode *node)
  27. {
  28. if (node == 0)
  29. return;
  30. _Children.push_back(node);
  31. node->_Parent = this;
  32. node->addReference();
  33. #ifdef DEBUG_SCENEGRAPH
  34. clog << "SceneNode '" << node->getName() << "' attached to '"
  35. << this->getName() << "'" << endline;
  36. #endif
  37. }
  38. void SceneNode::detach(SceneNode *node)
  39. {
  40. node->_Parent = 0;
  41. node->removeReference();
  42. _Children.remove(node);
  43. #ifdef DEBUG_SCENEGRAPH
  44. clog << "SceneNode '" << node->getName() << "' detach from '"
  45. << this->getName() << "'" << endline;
  46. #endif
  47. }
  48. void SceneNode::detachAll()
  49. {
  50. SceneNodeList::iterator i;
  51. for (i = _Children.begin(); i != _Children.end(); i++)
  52. {
  53. ( *i )->_Parent = 0;
  54. ( *i )->removeReference();
  55. }
  56. _Children.clear();
  57. }
  58. SceneNode *SceneNode::getParent() const
  59. {
  60. return _Parent;
  61. }
  62. void SceneNode::detachFromParent()
  63. {
  64. if (_Parent)
  65. _Parent->detach( this);
  66. }
  67. const SceneNode::SceneNodeList& SceneNode::getChildren() const
  68. {
  69. return _Children;
  70. }
  71. void SceneNode::update(Scalar time)
  72. {
  73. updateAbsoluteTransformation();
  74. // if (isActive() )
  75. {
  76. SceneNodeList::iterator i;
  77. for (i = _Children.begin(); i != _Children.end(); i++)
  78. ( *i )->update(time);
  79. }
  80. }
  81. void SceneNode::queue(RenderQueue *queue, Camera *camera)
  82. {
  83. if (isActive())
  84. {
  85. if (_Model.valid())
  86. queue->addOpaqueItem(_Model, _AbsoluteTransformation.translation,
  87. _AbsoluteTransformation.rotation);
  88. SceneNodeList::iterator i;
  89. for (i = _Children.begin(); i != _Children.end(); i++)
  90. {
  91. ( *i )->queue(queue, camera);
  92. }
  93. }
  94. }
  95. const Transformation& SceneNode::getRelativeTransformation()
  96. {
  97. return _RelativeTransformation;
  98. }
  99. const Transformation& SceneNode::getAbsoluteTransformation()
  100. {
  101. return _AbsoluteTransformation;
  102. }
  103. void SceneNode::setRelativeTranslation(const Vector3 &translation)
  104. {
  105. _RelativeTransformation.translation = translation;
  106. }
  107. void SceneNode::setRelativeRotation(const Quaternion &rotation)
  108. {
  109. _RelativeTransformation.rotation = rotation;
  110. }
  111. void SceneNode::updateAbsoluteTransformation()
  112. {
  113. if (_Parent)
  114. {
  115. _AbsoluteTransformation.translation = _Parent->getAbsoluteTransformation().translation
  116. +_Parent->getAbsoluteTransformation().rotation.inversed().apply(_RelativeTransformation.translation);
  117. }
  118. else
  119. _AbsoluteTransformation.translation = _RelativeTransformation.translation;
  120. }
  121. void SceneNode::setRigidBody(RigidBody* body)
  122. {
  123. _RigidBody = body;
  124. }
  125. RigidBody* SceneNode::getRigidBody()
  126. {
  127. return _RigidBody.pointer();
  128. }
  129. void SceneNode::setModel(Model* model)
  130. {
  131. _Model = model;
  132. }
  133. Model* SceneNode::getModel()
  134. {
  135. return _Model.pointer();
  136. }
  137. } // namespace BlueCore