Camera.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "Camera.h"
  24. #include "Types.h"
  25. #include "MathUtils.h"
  26. #include "Quat.h"
  27. #include "Unit.h"
  28. #include "Assert.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------
  32. void Camera::create(Unit& parent, int32_t node)
  33. {
  34. m_parent = &parent;
  35. m_node = node;
  36. m_projection_type = ProjectionType::PERSPECTIVE;
  37. update_projection_matrix();
  38. }
  39. //-----------------------------------------------------------------------
  40. Vec3 Camera::local_position() const
  41. {
  42. return m_parent->local_position(m_node);
  43. }
  44. //-----------------------------------------------------------------------
  45. Quat Camera::local_rotation() const
  46. {
  47. return m_parent->local_rotation(m_node);
  48. }
  49. //-----------------------------------------------------------------------
  50. Mat4 Camera::local_pose() const
  51. {
  52. return m_parent->local_pose(m_node);
  53. }
  54. //-----------------------------------------------------------------------
  55. Vec3 Camera::world_position() const
  56. {
  57. return m_parent->world_position(m_node);
  58. }
  59. //-----------------------------------------------------------------------
  60. Quat Camera::world_rotation() const
  61. {
  62. return m_parent->world_rotation(m_node);
  63. }
  64. //-----------------------------------------------------------------------
  65. Mat4 Camera::world_pose() const
  66. {
  67. return m_parent->world_pose(m_node);
  68. }
  69. //-----------------------------------------------------------------------
  70. void Camera::set_local_position(const Vec3& pos)
  71. {
  72. m_parent->set_local_position(pos, m_node);
  73. }
  74. //-----------------------------------------------------------------------
  75. void Camera::set_local_rotation(const Quat& rot)
  76. {
  77. m_parent->set_local_rotation(rot, m_node);
  78. }
  79. //-----------------------------------------------------------------------
  80. void Camera::set_local_pose(const Mat4& pose)
  81. {
  82. m_parent->set_local_pose(pose, m_node);
  83. }
  84. //-----------------------------------------------------------------------
  85. void Camera::set_projection_type(ProjectionType::Enum type)
  86. {
  87. m_projection_type = type;
  88. }
  89. //-----------------------------------------------------------------------
  90. ProjectionType::Enum Camera::projection_type() const
  91. {
  92. return m_projection_type;
  93. }
  94. //-----------------------------------------------------------------------------
  95. float Camera::fov() const
  96. {
  97. return m_FOV;
  98. }
  99. //-----------------------------------------------------------------------------
  100. void Camera::set_fov(float fov)
  101. {
  102. m_FOV = fov;
  103. update_projection_matrix();
  104. }
  105. //-----------------------------------------------------------------------------
  106. float Camera::aspect() const
  107. {
  108. return m_aspect;
  109. }
  110. //-----------------------------------------------------------------------------
  111. void Camera::set_aspect(float aspect)
  112. {
  113. m_aspect = aspect;
  114. update_projection_matrix();
  115. }
  116. //-----------------------------------------------------------------------------
  117. float Camera::near_clip_distance() const
  118. {
  119. return m_near;
  120. }
  121. //-----------------------------------------------------------------------------
  122. void Camera::set_near_clip_distance(float near)
  123. {
  124. m_near = near;
  125. update_projection_matrix();
  126. }
  127. //-----------------------------------------------------------------------------
  128. float Camera::far_clip_distance() const
  129. {
  130. return m_far;
  131. }
  132. //-----------------------------------------------------------------------------
  133. void Camera::set_far_clip_distance(float far)
  134. {
  135. m_far = far;
  136. update_projection_matrix();
  137. }
  138. //-----------------------------------------------------------------------------
  139. void Camera::update_projection_matrix()
  140. {
  141. switch (m_projection_type)
  142. {
  143. case ProjectionType::ORTHOGRAPHIC:
  144. {
  145. CE_FATAL("TODO");
  146. break;
  147. }
  148. case ProjectionType::PERSPECTIVE:
  149. {
  150. m_projection.build_projection_perspective_rh(m_FOV, m_aspect, m_near, m_far);
  151. break;
  152. }
  153. default:
  154. {
  155. CE_FATAL("Oops, unknown projection type");
  156. break;
  157. }
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. void Camera::update_frustum()
  162. {
  163. // TODO
  164. //m_frustum.from_matrix(m_projection * m_view);
  165. }
  166. } // namespace crown