Camera.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. Camera::Camera(const Vector3& position, float fov, float aspect) :
  30. m_position(position),
  31. m_look_at(0, 0, -1),
  32. m_up(0, 1, 0),
  33. m_rot_factor(0.0f, 0.0f),
  34. m_angle_x(0.0f),
  35. m_angle_y(0.0f),
  36. m_FOV(fov),
  37. m_aspect(aspect),
  38. m_near(0.1f),
  39. m_far(1000.0f)
  40. {
  41. update_projection_matrix();
  42. update_view_matrix();
  43. update_frustum();
  44. }
  45. //-----------------------------------------------------------------------------
  46. const Vector3& Camera::position() const
  47. {
  48. return m_position;
  49. }
  50. //-----------------------------------------------------------------------------
  51. void Camera::set_position(const Vector3& position)
  52. {
  53. m_position = position;
  54. update_view_matrix();
  55. }
  56. //-----------------------------------------------------------------------------
  57. const Vector3& Camera::look_at() const
  58. {
  59. return m_look_at;
  60. }
  61. //-----------------------------------------------------------------------------
  62. void Camera::set_look_at(const Vector3& lookat)
  63. {
  64. m_look_at = lookat;
  65. update_view_matrix();
  66. }
  67. //-----------------------------------------------------------------------
  68. void Camera::set_rotation(const float x, const float y)
  69. {
  70. Vector3 right(1, 0, 0);
  71. Vector3 look;
  72. look.x = 0.0f;
  73. look.y = math::sin(x);
  74. look.z = -math::cos(x);
  75. Vector3 up = right.cross(look);
  76. up.normalize();
  77. Matrix3x3 m;
  78. m.build_rotation_y(y);
  79. look = m * look;
  80. m_up = m * up;
  81. set_look_at(look);
  82. }
  83. //-----------------------------------------------------------------------------
  84. const Vector3& Camera::up() const
  85. {
  86. return m_up;
  87. }
  88. //-----------------------------------------------------------------------------
  89. float Camera::fov() const
  90. {
  91. return m_FOV;
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Camera::set_fov(float fov)
  95. {
  96. m_FOV = fov;
  97. update_projection_matrix();
  98. }
  99. //-----------------------------------------------------------------------------
  100. float Camera::aspect() const
  101. {
  102. return m_aspect;
  103. }
  104. //-----------------------------------------------------------------------------
  105. void Camera::set_aspect(float aspect)
  106. {
  107. m_aspect = aspect;
  108. update_projection_matrix();
  109. }
  110. //-----------------------------------------------------------------------------
  111. float Camera::near_clip_distance() const
  112. {
  113. return m_near;
  114. }
  115. //-----------------------------------------------------------------------------
  116. void Camera::set_near_clip_distance(float near)
  117. {
  118. m_near = near;
  119. update_projection_matrix();
  120. }
  121. //-----------------------------------------------------------------------------
  122. float Camera::far_clip_distance() const
  123. {
  124. return m_far;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Camera::set_far_clip_distance(float far)
  128. {
  129. m_far = far;
  130. update_projection_matrix();
  131. }
  132. //-----------------------------------------------------------------------------
  133. const Matrix4x4& Camera::projection_matrix() const
  134. {
  135. return m_projection;
  136. }
  137. //-----------------------------------------------------------------------------
  138. const Matrix4x4& Camera::view_matrix() const
  139. {
  140. return m_view;
  141. }
  142. //-----------------------------------------------------------------------------
  143. const Frustum& Camera::frustum() const
  144. {
  145. return m_frustum;
  146. }
  147. //-----------------------------------------------------------------------------
  148. void Camera::update_projection_matrix()
  149. {
  150. m_projection.build_projection_perspective_rh(m_FOV, m_aspect, m_near, m_far);
  151. }
  152. //-----------------------------------------------------------------------------
  153. void Camera::update_view_matrix()
  154. {
  155. m_view.build_look_at_rh(m_position, m_position + m_look_at, m_up);
  156. }
  157. //-----------------------------------------------------------------------------
  158. void Camera::update_frustum()
  159. {
  160. m_frustum.from_matrix(m_projection * m_view);
  161. }
  162. //-----------------------------------------------------------------------------
  163. void Camera::move_forward(float meters)
  164. {
  165. set_position(m_position + m_look_at * meters);
  166. }
  167. //-----------------------------------------------------------------------
  168. void Camera::move_backward(float meters)
  169. {
  170. set_position(m_position + m_look_at * -meters);
  171. }
  172. //-----------------------------------------------------------------------
  173. void Camera::strafe_left(float meters)
  174. {
  175. Vector3 left = m_up.cross(m_look_at);
  176. left.normalize();
  177. set_position(m_position + left * meters);
  178. }
  179. //-----------------------------------------------------------------------
  180. void Camera::strafe_right(float meters)
  181. {
  182. Vector3 left = m_up.cross(m_look_at);
  183. left.normalize();
  184. set_position(m_position + left * -meters);
  185. }
  186. } // namespace crown