Node.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <algorithm>
  2. #include "Node.h"
  3. #include "renderer.h"
  4. #include "collision.h"
  5. #include "Controller.h"
  6. namespace scene
  7. {
  8. extern void registerNode( Node* );
  9. }
  10. //=====================================================================================================================================
  11. // commonConstructorCode =
  12. //=====================================================================================================================================
  13. void Node::commonConstructorCode()
  14. {
  15. parent = NULL;
  16. isGroupNode = false;
  17. translationLspace = Vec3( 0.0 );
  18. scaleLspace = 1.0;
  19. rotationLspace = Mat3::getIdentity();
  20. translationWspace = Vec3( 0.0 );
  21. scaleWspace = 1.0;
  22. rotationWspace = Mat3::getIdentity();
  23. bvolumeLspace = NULL;
  24. scene::registerNode( this );
  25. }
  26. //=====================================================================================================================================
  27. // ~Node =
  28. //=====================================================================================================================================
  29. Node::~Node()
  30. {
  31. }
  32. //=====================================================================================================================================
  33. // updateWorldTransform =
  34. //=====================================================================================================================================
  35. void Node::updateWorldTransform()
  36. {
  37. if( parent )
  38. {
  39. /* the original code:
  40. scaleWspace = parent->scaleWspace * scaleLspace;
  41. rotationWspace = parent->rotationWspace * rotationLspace;
  42. translationWspace = translationLspace.Transformed( parent->translationWspace, parent->rotationWspace, parent->scaleWspace ); */
  43. combineTransformations( parent->translationWspace, parent->rotationWspace, parent->scaleWspace,
  44. translationLspace, rotationLspace, scaleLspace,
  45. translationWspace, rotationWspace, scaleWspace );
  46. }
  47. else // else copy
  48. {
  49. scaleWspace = scaleLspace;
  50. rotationWspace = rotationLspace;
  51. translationWspace = translationLspace;
  52. }
  53. transformationWspace = Mat4( translationWspace, rotationWspace, scaleWspace );
  54. // transform the bvolume
  55. /*if( bvolumeLspace != NULL )
  56. {
  57. DEBUG_ERR( bvolumeLspace->type!=bvolume_t::BSPHERE && bvolumeLspace->type!=bvolume_t::AABB && bvolumeLspace->type!=bvolume_t::OBB );
  58. switch( bvolumeLspace->type )
  59. {
  60. case bvolume_t::BSPHERE:
  61. {
  62. bsphere_t sphere = static_cast<bsphere_t*>(bvolumeLspace)->Transformed( translationWspace, rotationWspace, scaleWspace );
  63. *static_cast<bsphere_t*>(bvolumeLspace) = sphere;
  64. break;
  65. }
  66. case bvolume_t::AABB:
  67. {
  68. aabb_t aabb = static_cast<aabb_t*>(bvolumeLspace)->Transformed( translationWspace, rotationWspace, scaleWspace );
  69. *static_cast<aabb_t*>(bvolumeLspace) = aabb;
  70. break;
  71. }
  72. case bvolume_t::OBB:
  73. {
  74. obb_t obb = static_cast<obb_t*>(bvolumeLspace)->Transformed( translationWspace, rotationWspace, scaleWspace );
  75. *static_cast<obb_t*>(bvolumeLspace) = obb;
  76. break;
  77. }
  78. default:
  79. FATAL( "What the fuck" );
  80. }
  81. }*/
  82. }
  83. //=====================================================================================================================================
  84. // Move(s) =
  85. // Move the object according to it's local axis =
  86. //=====================================================================================================================================
  87. void Node::moveLocalX( float distance )
  88. {
  89. Vec3 x_axis = rotationLspace.getColumn(0);
  90. translationLspace += x_axis * distance;
  91. }
  92. void Node::moveLocalY( float distance )
  93. {
  94. Vec3 y_axis = rotationLspace.getColumn(1);
  95. translationLspace += y_axis * distance;
  96. }
  97. void Node::moveLocalZ( float distance )
  98. {
  99. Vec3 z_axis = rotationLspace.getColumn(2);
  100. translationLspace += z_axis * distance;
  101. }
  102. //=====================================================================================================================================
  103. // addChild =
  104. //=====================================================================================================================================
  105. void Node::addChild( Node* node )
  106. {
  107. if( node->parent != NULL )
  108. {
  109. ERROR( "Node already has parent" );
  110. return;
  111. }
  112. node->parent = this;
  113. childs.push_back( node );
  114. }
  115. //=====================================================================================================================================
  116. // removeChild =
  117. //=====================================================================================================================================
  118. void Node::removeChild( Node* node )
  119. {
  120. Vec<Node*>::iterator it = find( childs.begin(), childs.end(), node );
  121. if( it == childs.end() )
  122. {
  123. ERROR( "Child not found" );
  124. return;
  125. }
  126. node->parent = NULL;
  127. childs.erase( it );
  128. }