SceneGraph.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "Quat.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 Vec3& pos, const Quat& rot)
  37. {
  38. Mat4 pose(rot, pos);
  39. m_world_poses.push_back(pose);
  40. m_local_poses.push_back(pose);
  41. m_parents.push_back(parent);
  42. return m_local_poses.size() - 1;
  43. }
  44. //-----------------------------------------------------------------------------
  45. void SceneGraph::destroy_node(int32_t id)
  46. {
  47. (void)id;
  48. }
  49. //-----------------------------------------------------------------------------
  50. void SceneGraph::link(int32_t child, int32_t parent)
  51. {
  52. m_parents[child] = parent;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void SceneGraph::unlink(int32_t child)
  56. {
  57. // Copy world pose before unlinking from parent
  58. m_local_poses[child] = m_world_poses[m_parents[child]];
  59. m_parents[child] = -1;
  60. }
  61. //-----------------------------------------------------------------------------
  62. void SceneGraph::set_local_position(int32_t node, const Vec3& pos)
  63. {
  64. Mat4& local_pose = m_local_poses[node];
  65. local_pose.set_translation(pos);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void SceneGraph::set_local_rotation(int32_t node, const Quat& rot)
  69. {
  70. Mat4& local_pose = m_local_poses[node];
  71. Vec3 local_translation = local_pose.translation();
  72. local_pose = rot.to_mat4();
  73. local_pose.set_translation(local_translation);
  74. }
  75. //-----------------------------------------------------------------------------
  76. void SceneGraph::set_local_pose(int32_t node, const Mat4& pose)
  77. {
  78. m_local_poses[node] = pose;
  79. }
  80. //-----------------------------------------------------------------------------
  81. Vec3 SceneGraph::local_position(int32_t node) const
  82. {
  83. return m_local_poses[node].translation();
  84. }
  85. //-----------------------------------------------------------------------------
  86. Quat SceneGraph::local_rotation(int32_t /*node*/) const
  87. {
  88. return Quat(Vec3(1, 0, 0), 0.0f);
  89. }
  90. //-----------------------------------------------------------------------------
  91. Mat4 SceneGraph::local_pose(int32_t node) const
  92. {
  93. return m_local_poses[node];
  94. }
  95. //-----------------------------------------------------------------------------
  96. Vec3 SceneGraph::world_position(int32_t node) const
  97. {
  98. return m_world_poses[node].translation();
  99. }
  100. //-----------------------------------------------------------------------------
  101. Quat SceneGraph::world_rotation(int32_t /*node*/) const
  102. {
  103. return Quat(Vec3(1, 0, 0), 0.0f);
  104. }
  105. //-----------------------------------------------------------------------------
  106. Mat4 SceneGraph::world_pose(int32_t node) const
  107. {
  108. return m_world_poses[node];
  109. }
  110. //-----------------------------------------------------------------------------
  111. void SceneGraph::update()
  112. {
  113. for (uint32_t i = 0; i < m_world_poses.size(); i++)
  114. {
  115. if (m_parents[i] == -1)
  116. {
  117. m_world_poses[i] = m_local_poses[i];
  118. }
  119. else
  120. {
  121. m_world_poses[i] = m_local_poses[m_parents[i]] * m_local_poses[i];
  122. }
  123. }
  124. }
  125. } // namespace crown