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