2
0

SceneGraph.cpp 5.6 KB

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