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