SceneGraph.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "Device.h"
  25. #include "DebugRenderer.h"
  26. #include "Quat.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. SceneGraph::SceneGraph() :
  31. m_nodes(m_allocator, MAX_NODES),
  32. m_world_poses(m_allocator),
  33. m_local_poses(m_allocator),
  34. m_parents(m_allocator)
  35. {
  36. }
  37. //-----------------------------------------------------------------------------
  38. NodeId SceneGraph::create_node(const Vec3& pos, const Quat& rot)
  39. {
  40. const NodeId node = m_nodes.create();
  41. Mat4 pose(rot, pos);
  42. if (m_world_poses.size() <= node.index)
  43. {
  44. m_world_poses.push_back(pose);
  45. m_local_poses.push_back(pose);
  46. m_parents.push_back(-1);
  47. m_sparse_to_packed[node.index] = m_world_poses.size() - 1;
  48. }
  49. else
  50. {
  51. m_world_poses[node.index] = pose;
  52. m_local_poses[node.index] = pose;
  53. m_parents[node.index] = -1;
  54. m_sparse_to_packed[node.index] = node.index;
  55. }
  56. return node;
  57. }
  58. //-----------------------------------------------------------------------------
  59. void SceneGraph::destroy_node(NodeId id)
  60. {
  61. (void)id;
  62. }
  63. //-----------------------------------------------------------------------------
  64. void SceneGraph::link(NodeId child, NodeId parent)
  65. {
  66. CE_ASSERT(m_nodes.has(child), "Child node does not exist");
  67. CE_ASSERT(m_nodes.has(parent), "Parent node does not exist");
  68. m_parents[m_sparse_to_packed[child.index]] = m_sparse_to_packed[parent.index];
  69. }
  70. //-----------------------------------------------------------------------------
  71. void SceneGraph::unlink(NodeId child)
  72. {
  73. CE_ASSERT(m_nodes.has(child), "Node does not exist");
  74. m_local_poses[m_sparse_to_packed[child.index]] = m_world_poses[m_sparse_to_packed[child.index]];
  75. m_parents[m_sparse_to_packed[child.index]] = -1;
  76. }
  77. //-----------------------------------------------------------------------------
  78. void SceneGraph::set_local_position(NodeId node, const Vec3& pos)
  79. {
  80. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  81. Mat4& local_pose = m_local_poses[m_sparse_to_packed[node.index]];
  82. local_pose.set_translation(pos);
  83. }
  84. //-----------------------------------------------------------------------------
  85. void SceneGraph::set_local_rotation(NodeId node, const Quat& rot)
  86. {
  87. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  88. Mat4& local_pose = m_local_poses[m_sparse_to_packed[node.index]];
  89. Vec3 local_translation = local_pose.translation();
  90. local_pose = rot.to_mat4();
  91. local_pose.set_translation(local_translation);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void SceneGraph::set_local_pose(NodeId node, const Mat4& pose)
  95. {
  96. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  97. m_local_poses[m_sparse_to_packed[node.index]] = pose;
  98. }
  99. //-----------------------------------------------------------------------------
  100. Vec3 SceneGraph::local_position(NodeId node) const
  101. {
  102. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  103. return m_local_poses[m_sparse_to_packed[node.index]].translation();
  104. }
  105. //-----------------------------------------------------------------------------
  106. Quat SceneGraph::local_rotation(NodeId node) const
  107. {
  108. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  109. return Quat(Vec3(1, 0, 0), 0.0f);
  110. }
  111. //-----------------------------------------------------------------------------
  112. Mat4 SceneGraph::local_pose(NodeId node) const
  113. {
  114. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  115. return m_local_poses[m_sparse_to_packed[node.index]];
  116. }
  117. //-----------------------------------------------------------------------------
  118. Vec3 SceneGraph::world_position(NodeId node) const
  119. {
  120. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  121. return m_world_poses[m_sparse_to_packed[node.index]].translation();
  122. }
  123. //-----------------------------------------------------------------------------
  124. Quat SceneGraph::world_rotation(NodeId node) const
  125. {
  126. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  127. return Quat(Vec3(1, 0, 0), 0.0f);
  128. }
  129. //-----------------------------------------------------------------------------
  130. Mat4 SceneGraph::world_pose(NodeId node) const
  131. {
  132. CE_ASSERT(m_nodes.has(node), "Node does not exist");
  133. return m_world_poses[m_sparse_to_packed[node.index]];
  134. }
  135. //-----------------------------------------------------------------------------
  136. void SceneGraph::update()
  137. {
  138. for (uint32_t i = 0; i < m_world_poses.size(); i++)
  139. {
  140. if (m_parents[i] == -1)
  141. {
  142. m_world_poses[i] = m_local_poses[i];
  143. }
  144. else
  145. {
  146. m_world_poses[i] = m_local_poses[m_parents[i]] * m_local_poses[i];
  147. }
  148. }
  149. // Draw debug
  150. for (uint32_t i = 0; i < m_world_poses.size(); i++)
  151. {
  152. device()->debug_renderer()->add_pose(m_world_poses[i], true);
  153. }
  154. }
  155. } // namespace crown