node.cpp 5.6 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_t* );
  9. }
  10. //=====================================================================================================================================
  11. // CommonConstructorCode =
  12. //=====================================================================================================================================
  13. void node_t::CommonConstructorCode()
  14. {
  15. parent = NULL;
  16. is_group_node = false;
  17. translation_lspace = vec3_t( 0.0 );
  18. scale_lspace = 1.0;
  19. rotation_lspace = mat3_t::GetIdentity();
  20. translation_wspace = vec3_t( 0.0 );
  21. scale_wspace = 1.0;
  22. rotation_wspace = mat3_t::GetIdentity();
  23. bvolume_lspace = NULL;
  24. scene::RegisterNode( this );
  25. }
  26. //=====================================================================================================================================
  27. // ~node_t =
  28. //=====================================================================================================================================
  29. node_t::~node_t()
  30. {
  31. }
  32. //=====================================================================================================================================
  33. // UpdateWorldTransform =
  34. //=====================================================================================================================================
  35. void node_t::UpdateWorldTransform()
  36. {
  37. if( parent )
  38. {
  39. /* the original code:
  40. scale_wspace = parent->scale_wspace * scale_lspace;
  41. rotation_wspace = parent->rotation_wspace * rotation_lspace;
  42. translation_wspace = translation_lspace.Transformed( parent->translation_wspace, parent->rotation_wspace, parent->scale_wspace ); */
  43. CombineTransformations( parent->translation_wspace, parent->rotation_wspace, parent->scale_wspace,
  44. translation_lspace, rotation_lspace, scale_lspace,
  45. translation_wspace, rotation_wspace, scale_wspace );
  46. }
  47. else // else copy
  48. {
  49. scale_wspace = scale_lspace;
  50. rotation_wspace = rotation_lspace;
  51. translation_wspace = translation_lspace;
  52. }
  53. transformation_wspace = mat4_t( translation_wspace, rotation_wspace, scale_wspace );
  54. // transform the bvolume
  55. /*if( bvolume_lspace != NULL )
  56. {
  57. DEBUG_ERR( bvolume_lspace->type!=bvolume_t::BSPHERE && bvolume_lspace->type!=bvolume_t::AABB && bvolume_lspace->type!=bvolume_t::OBB );
  58. switch( bvolume_lspace->type )
  59. {
  60. case bvolume_t::BSPHERE:
  61. {
  62. bsphere_t sphere = static_cast<bsphere_t*>(bvolume_lspace)->Transformed( translation_wspace, rotation_wspace, scale_wspace );
  63. *static_cast<bsphere_t*>(bvolume_wspace) = sphere;
  64. break;
  65. }
  66. case bvolume_t::AABB:
  67. {
  68. aabb_t aabb = static_cast<aabb_t*>(bvolume_lspace)->Transformed( translation_wspace, rotation_wspace, scale_wspace );
  69. *static_cast<aabb_t*>(bvolume_wspace) = aabb;
  70. break;
  71. }
  72. case bvolume_t::OBB:
  73. {
  74. obb_t obb = static_cast<obb_t*>(bvolume_lspace)->Transformed( translation_wspace, rotation_wspace, scale_wspace );
  75. *static_cast<obb_t*>(bvolume_wspace) = 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_t::MoveLocalX( float distance )
  88. {
  89. vec3_t x_axis = rotation_lspace.GetColumn(0);
  90. translation_lspace += x_axis * distance;
  91. }
  92. void node_t::MoveLocalY( float distance )
  93. {
  94. vec3_t y_axis = rotation_lspace.GetColumn(1);
  95. translation_lspace += y_axis * distance;
  96. }
  97. void node_t::MoveLocalZ( float distance )
  98. {
  99. vec3_t z_axis = rotation_lspace.GetColumn(2);
  100. translation_lspace += z_axis * distance;
  101. }
  102. //=====================================================================================================================================
  103. // AddChild =
  104. //=====================================================================================================================================
  105. void node_t::AddChild( node_t* node )
  106. {
  107. if( node->parent != NULL )
  108. {
  109. ERROR( "Node allready have parent" );
  110. return;
  111. }
  112. node->parent = this;
  113. childs.push_back( node );
  114. }
  115. //=====================================================================================================================================
  116. // RemoveChild =
  117. //=====================================================================================================================================
  118. void node_t::RemoveChild( node_t* node )
  119. {
  120. Vec<node_t*>::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. }