2
0

Camera.cpp 5.2 KB

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