SceneGraph.cpp 8.0 KB

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