scene_graph.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "scene_graph.h"
  6. #include "quaternion.h"
  7. #include "vector3.h"
  8. #include "matrix4x4.h"
  9. #include "allocator.h"
  10. #include "string_utils.h"
  11. #include "murmur.h"
  12. #include <string.h>
  13. #define CLEAN 0
  14. #define LOCAL_DIRTY 1
  15. #define WORLD_DIRTY 1 << 2
  16. namespace crown
  17. {
  18. using namespace matrix4x4;
  19. SceneGraph::SceneGraph(Allocator& a, uint32_t index)
  20. : m_allocator(&a)
  21. , m_index(index)
  22. , m_num_nodes(0)
  23. , m_flags(NULL)
  24. , m_world_poses(NULL)
  25. , m_local_poses(NULL)
  26. , m_parents(NULL)
  27. , m_names(NULL)
  28. {
  29. }
  30. void SceneGraph::create(const Matrix4x4& root, uint32_t count, const UnitNode* nodes)
  31. {
  32. m_num_nodes = count;
  33. m_flags = (uint8_t*) m_allocator->allocate(sizeof(uint8_t) * count);
  34. m_world_poses = (Matrix4x4*) m_allocator->allocate(sizeof(Matrix4x4) * count);
  35. m_local_poses = (Matrix4x4*) m_allocator->allocate(sizeof(Matrix4x4) * count);
  36. m_parents = (int32_t*) m_allocator->allocate(sizeof(int32_t) * count);
  37. m_names = (StringId32*) m_allocator->allocate(sizeof(StringId32) * count);
  38. for (uint32_t i = 0; i < count; i++)
  39. {
  40. m_flags[i] = (int) CLEAN;
  41. m_local_poses[i] = nodes[i].pose;
  42. m_parents[i] = -1;
  43. m_names[i] = nodes[i].name;
  44. }
  45. // Compute initial world poses
  46. for (uint32_t i = 1; i < m_num_nodes; i++)
  47. {
  48. m_world_poses[i] = root * m_local_poses[i];
  49. }
  50. m_world_poses[0] = root;
  51. m_flags[0] = WORLD_DIRTY;
  52. update();
  53. }
  54. void SceneGraph::destroy()
  55. {
  56. m_allocator->deallocate(m_flags);
  57. m_allocator->deallocate(m_world_poses);
  58. m_allocator->deallocate(m_local_poses);
  59. m_allocator->deallocate(m_parents);
  60. m_allocator->deallocate(m_names);
  61. }
  62. int32_t SceneGraph::node(const char* name) const
  63. {
  64. return node(murmur2_32(name, strlen(name)));
  65. }
  66. int32_t SceneGraph::node(StringId32 name) const
  67. {
  68. for (uint32_t i = 0; i < m_num_nodes; i++)
  69. {
  70. if (m_names[i] == name)
  71. {
  72. return i;
  73. }
  74. }
  75. CE_FATAL("Node not found");
  76. return 0;
  77. }
  78. bool SceneGraph::has_node(const char* name) const
  79. {
  80. StringId32 name_hash = murmur2_32(name, strlen(name), 0);
  81. for (uint32_t i = 0; i < m_num_nodes; i++)
  82. {
  83. if (m_names[i] == name_hash)
  84. {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. uint32_t SceneGraph::num_nodes() const
  91. {
  92. return m_num_nodes;
  93. }
  94. bool SceneGraph::can_link(int32_t child, int32_t parent) const
  95. {
  96. return parent < child;
  97. }
  98. void SceneGraph::link(int32_t child, int32_t parent)
  99. {
  100. CE_ASSERT(child < (int32_t) m_num_nodes, "Child node does not exist");
  101. CE_ASSERT(parent < (int32_t) m_num_nodes, "Parent node does not exist");
  102. CE_ASSERT(can_link(child, parent), "Parent must be < child");
  103. m_world_poses[child] = matrix4x4::IDENTITY;
  104. m_local_poses[child] = matrix4x4::IDENTITY;
  105. m_parents[child] = parent;
  106. }
  107. void SceneGraph::unlink(int32_t child)
  108. {
  109. CE_ASSERT(child < (int32_t) m_num_nodes, "Child node does not exist");
  110. if (m_parents[child] != -1)
  111. {
  112. // Copy world pose before unlinking from parent
  113. m_local_poses[child] = m_world_poses[child];
  114. m_parents[child] = -1;
  115. }
  116. }
  117. void SceneGraph::set_local_position(int32_t node, const Vector3& pos)
  118. {
  119. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  120. m_flags[node] |= LOCAL_DIRTY;
  121. set_translation(m_local_poses[node], pos);
  122. }
  123. void SceneGraph::set_local_rotation(int32_t node, const Quaternion& rot)
  124. {
  125. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  126. m_flags[node] |= LOCAL_DIRTY;
  127. set_rotation(m_local_poses[node], rot);
  128. }
  129. void SceneGraph::set_local_pose(int32_t node, const Matrix4x4& pose)
  130. {
  131. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  132. m_flags[node] |= LOCAL_DIRTY;
  133. m_local_poses[node] = pose;
  134. }
  135. Vector3 SceneGraph::local_position(int32_t node) const
  136. {
  137. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  138. return translation(m_local_poses[node]);
  139. }
  140. Quaternion SceneGraph::local_rotation(int32_t node) const
  141. {
  142. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  143. return rotation(m_local_poses[node]);
  144. }
  145. Matrix4x4 SceneGraph::local_pose(int32_t node) const
  146. {
  147. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  148. return m_local_poses[node];
  149. }
  150. void SceneGraph::set_world_position(int32_t node, const Vector3& pos)
  151. {
  152. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  153. m_flags[node] |= WORLD_DIRTY;
  154. set_translation(m_world_poses[node], pos);
  155. }
  156. void SceneGraph::set_world_rotation(int32_t node, const Quaternion& rot)
  157. {
  158. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  159. m_flags[node] |= WORLD_DIRTY;
  160. set_rotation(m_world_poses[node], rot);
  161. }
  162. void SceneGraph::set_world_pose(int32_t node, const Matrix4x4& pose)
  163. {
  164. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  165. m_flags[node] |= WORLD_DIRTY;
  166. m_world_poses[node] = pose;
  167. }
  168. Vector3 SceneGraph::world_position(int32_t node) const
  169. {
  170. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  171. return translation(m_world_poses[node]);
  172. }
  173. Quaternion SceneGraph::world_rotation(int32_t node) const
  174. {
  175. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  176. return rotation(m_world_poses[node]);
  177. }
  178. Matrix4x4 SceneGraph::world_pose(int32_t node) const
  179. {
  180. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  181. return m_world_poses[node];
  182. }
  183. void SceneGraph::update()
  184. {
  185. for (uint32_t i = 0; i < m_num_nodes; i++)
  186. {
  187. const uint8_t my_flags = m_flags[i];
  188. const uint8_t parent_flags = m_flags[m_parents[i]];
  189. if (my_flags & LOCAL_DIRTY || parent_flags & WORLD_DIRTY)
  190. {
  191. if (m_parents[i] == -1)
  192. {
  193. m_world_poses[i] = m_local_poses[i];
  194. }
  195. else
  196. {
  197. m_world_poses[i] = m_world_poses[m_parents[i]] * m_local_poses[i];
  198. }
  199. m_flags[i] = CLEAN;
  200. }
  201. }
  202. }
  203. } // namespace crown