scene_graph.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "scene_graph.h"
  24. #include "quaternion.h"
  25. #include "vector3.h"
  26. #include "matrix4x4.h"
  27. #include "allocator.h"
  28. #include "string_utils.h"
  29. #include <string.h>
  30. #define CLEAN 0
  31. #define LOCAL_DIRTY 1
  32. #define WORLD_DIRTY 1 << 2
  33. namespace crown
  34. {
  35. using namespace matrix4x4;
  36. SceneGraph::SceneGraph(Allocator& a, uint32_t index)
  37. : m_allocator(&a)
  38. , m_index(index)
  39. , m_num_nodes(0)
  40. , m_flags(NULL)
  41. , m_world_poses(NULL)
  42. , m_local_poses(NULL)
  43. , m_parents(NULL)
  44. , m_names(NULL)
  45. {
  46. }
  47. void SceneGraph::create(const Matrix4x4& root, uint32_t count, const UnitNode* nodes)
  48. {
  49. m_num_nodes = count;
  50. m_flags = (uint8_t*) m_allocator->allocate(sizeof(uint8_t) * count);
  51. m_world_poses = (Matrix4x4*) m_allocator->allocate(sizeof(Matrix4x4) * count);
  52. m_local_poses = (Matrix4x4*) m_allocator->allocate(sizeof(Matrix4x4) * count);
  53. m_parents = (int32_t*) m_allocator->allocate(sizeof(int32_t) * count);
  54. m_names = (StringId32*) m_allocator->allocate(sizeof(StringId32) * count);
  55. for (uint32_t i = 0; i < count; i++)
  56. {
  57. m_flags[i] = (int) CLEAN;
  58. m_local_poses[i] = nodes[i].pose;
  59. m_parents[i] = -1;
  60. m_names[i] = nodes[i].name;
  61. }
  62. // Compute initial world poses
  63. for (uint32_t i = 1; i < m_num_nodes; i++)
  64. {
  65. m_world_poses[i] = root * m_local_poses[i];
  66. }
  67. m_world_poses[0] = root;
  68. m_flags[0] = WORLD_DIRTY;
  69. update();
  70. }
  71. void SceneGraph::destroy()
  72. {
  73. m_allocator->deallocate(m_flags);
  74. m_allocator->deallocate(m_world_poses);
  75. m_allocator->deallocate(m_local_poses);
  76. m_allocator->deallocate(m_parents);
  77. m_allocator->deallocate(m_names);
  78. }
  79. int32_t SceneGraph::node(const char* name) const
  80. {
  81. return node(murmur2_32(name, strlen(name)));
  82. }
  83. int32_t SceneGraph::node(StringId32 name) const
  84. {
  85. for (uint32_t i = 0; i < m_num_nodes; i++)
  86. {
  87. if (m_names[i] == name)
  88. {
  89. return i;
  90. }
  91. }
  92. CE_FATAL("Node not found");
  93. return 0;
  94. }
  95. bool SceneGraph::has_node(const char* name) const
  96. {
  97. StringId32 name_hash = murmur2_32(name, strlen(name), 0);
  98. for (uint32_t i = 0; i < m_num_nodes; i++)
  99. {
  100. if (m_names[i] == name_hash)
  101. {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. uint32_t SceneGraph::num_nodes() const
  108. {
  109. return m_num_nodes;
  110. }
  111. bool SceneGraph::can_link(int32_t child, int32_t parent) const
  112. {
  113. return parent < child;
  114. }
  115. void SceneGraph::link(int32_t child, int32_t parent)
  116. {
  117. CE_ASSERT(child < (int32_t) m_num_nodes, "Child node does not exist");
  118. CE_ASSERT(parent < (int32_t) m_num_nodes, "Parent node does not exist");
  119. CE_ASSERT(can_link(child, parent), "Parent must be < child");
  120. m_world_poses[child] = matrix4x4::IDENTITY;
  121. m_local_poses[child] = matrix4x4::IDENTITY;
  122. m_parents[child] = parent;
  123. }
  124. void SceneGraph::unlink(int32_t child)
  125. {
  126. CE_ASSERT(child < (int32_t) m_num_nodes, "Child node does not exist");
  127. if (m_parents[child] != -1)
  128. {
  129. // Copy world pose before unlinking from parent
  130. m_local_poses[child] = m_world_poses[child];
  131. m_parents[child] = -1;
  132. }
  133. }
  134. void SceneGraph::set_local_position(int32_t node, const Vector3& pos)
  135. {
  136. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  137. m_flags[node] |= LOCAL_DIRTY;
  138. set_translation(m_local_poses[node], pos);
  139. }
  140. void SceneGraph::set_local_rotation(int32_t node, const Quaternion& rot)
  141. {
  142. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  143. m_flags[node] |= LOCAL_DIRTY;
  144. set_rotation(m_local_poses[node], rot);
  145. }
  146. void SceneGraph::set_local_pose(int32_t node, const Matrix4x4& pose)
  147. {
  148. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  149. m_flags[node] |= LOCAL_DIRTY;
  150. m_local_poses[node] = pose;
  151. }
  152. Vector3 SceneGraph::local_position(int32_t node) const
  153. {
  154. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  155. return translation(m_local_poses[node]);
  156. }
  157. Quaternion SceneGraph::local_rotation(int32_t node) const
  158. {
  159. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  160. return rotation(m_local_poses[node]);
  161. }
  162. Matrix4x4 SceneGraph::local_pose(int32_t node) const
  163. {
  164. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  165. return m_local_poses[node];
  166. }
  167. void SceneGraph::set_world_position(int32_t node, const Vector3& pos)
  168. {
  169. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  170. m_flags[node] |= WORLD_DIRTY;
  171. set_translation(m_world_poses[node], pos);
  172. }
  173. void SceneGraph::set_world_rotation(int32_t node, const Quaternion& rot)
  174. {
  175. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  176. m_flags[node] |= WORLD_DIRTY;
  177. set_rotation(m_world_poses[node], rot);
  178. }
  179. void SceneGraph::set_world_pose(int32_t node, const Matrix4x4& pose)
  180. {
  181. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  182. m_flags[node] |= WORLD_DIRTY;
  183. m_world_poses[node] = pose;
  184. }
  185. Vector3 SceneGraph::world_position(int32_t node) const
  186. {
  187. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  188. return translation(m_world_poses[node]);
  189. }
  190. Quaternion SceneGraph::world_rotation(int32_t node) const
  191. {
  192. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  193. return rotation(m_world_poses[node]);
  194. }
  195. Matrix4x4 SceneGraph::world_pose(int32_t node) const
  196. {
  197. CE_ASSERT(node < (int32_t) m_num_nodes, "Node does not exist");
  198. return m_world_poses[node];
  199. }
  200. void SceneGraph::update()
  201. {
  202. for (uint32_t i = 0; i < m_num_nodes; i++)
  203. {
  204. const uint8_t my_flags = m_flags[i];
  205. const uint8_t parent_flags = m_flags[m_parents[i]];
  206. if (my_flags & LOCAL_DIRTY || parent_flags & WORLD_DIRTY)
  207. {
  208. if (m_parents[i] == -1)
  209. {
  210. m_world_poses[i] = m_local_poses[i];
  211. }
  212. else
  213. {
  214. m_world_poses[i] = m_world_poses[m_parents[i]] * m_local_poses[i];
  215. }
  216. m_flags[i] = CLEAN;
  217. }
  218. }
  219. }
  220. } // namespace crown