2
0

Camera.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "Quaternion.h"
  27. #include "Unit.h"
  28. #include "Assert.h"
  29. #include "Vector4.h"
  30. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. void Camera::create(int32_t node, const Vector3& /*pos*/, const Quaternion& /*rot*/)
  34. {
  35. m_node = node;
  36. m_projection_type = ProjectionType::PERSPECTIVE;
  37. }
  38. //-----------------------------------------------------------------------------
  39. Vector3 Camera::local_position() const
  40. {
  41. return m_local_pose.translation();
  42. }
  43. //-----------------------------------------------------------------------------
  44. Quaternion Camera::local_rotation() const
  45. {
  46. return m_local_pose.to_quaternion();
  47. }
  48. //-----------------------------------------------------------------------------
  49. Matrix4x4 Camera::local_pose() const
  50. {
  51. return m_local_pose;
  52. }
  53. //-----------------------------------------------------------------------------
  54. Vector3 Camera::world_position() const
  55. {
  56. return m_world_pose.translation();
  57. }
  58. //-----------------------------------------------------------------------------
  59. Quaternion Camera::world_rotation() const
  60. {
  61. return m_world_pose.to_quaternion();
  62. }
  63. //-----------------------------------------------------------------------------
  64. Matrix4x4 Camera::world_pose() const
  65. {
  66. return m_world_pose;
  67. }
  68. //-----------------------------------------------------------------------------
  69. void Camera::set_local_position(Unit* unit, const Vector3& pos)
  70. {
  71. m_local_pose.set_translation(pos);
  72. unit->set_local_position(pos, m_node);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void Camera::set_local_rotation(Unit* unit, const Quaternion& rot)
  76. {
  77. Matrix4x4& local_pose = m_local_pose;
  78. Vector3 local_translation = local_pose.translation();
  79. local_pose = rot.to_matrix4x4();
  80. local_pose.set_translation(local_translation);
  81. unit->set_local_rotation(rot, m_node);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void Camera::set_local_pose(Unit* unit, const Matrix4x4& pose)
  85. {
  86. m_local_pose = pose;
  87. unit->set_local_pose(pose, m_node);
  88. }
  89. //-----------------------------------------------------------------------
  90. void Camera::set_projection_type(ProjectionType::Enum type)
  91. {
  92. m_projection_type = type;
  93. update_projection_matrix();
  94. }
  95. //-----------------------------------------------------------------------
  96. ProjectionType::Enum Camera::projection_type() const
  97. {
  98. return m_projection_type;
  99. }
  100. //-----------------------------------------------------------------------------
  101. float Camera::fov() const
  102. {
  103. return m_FOV;
  104. }
  105. //-----------------------------------------------------------------------------
  106. void Camera::set_fov(float fov)
  107. {
  108. m_FOV = fov;
  109. update_projection_matrix();
  110. }
  111. //-----------------------------------------------------------------------------
  112. float Camera::aspect() const
  113. {
  114. return m_aspect;
  115. }
  116. //-----------------------------------------------------------------------------
  117. void Camera::set_aspect(float aspect)
  118. {
  119. m_aspect = aspect;
  120. update_projection_matrix();
  121. }
  122. //-----------------------------------------------------------------------------
  123. float Camera::near_clip_distance() const
  124. {
  125. return m_near;
  126. }
  127. //-----------------------------------------------------------------------------
  128. void Camera::set_near_clip_distance(float near)
  129. {
  130. m_near = near;
  131. update_projection_matrix();
  132. }
  133. //-----------------------------------------------------------------------------
  134. float Camera::far_clip_distance() const
  135. {
  136. return m_far;
  137. }
  138. //-----------------------------------------------------------------------------
  139. void Camera::set_far_clip_distance(float far)
  140. {
  141. m_far = far;
  142. update_projection_matrix();
  143. }
  144. //-----------------------------------------------------------------------------
  145. void Camera::set_orthographic_metrics(float left, float right, float bottom, float top)
  146. {
  147. m_left = left;
  148. m_right = right;
  149. m_bottom = bottom;
  150. m_top = top;
  151. update_projection_matrix();
  152. }
  153. //-----------------------------------------------------------------------------
  154. void Camera::set_viewport_metrics(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  155. {
  156. m_view_x = x;
  157. m_view_y = y;
  158. m_view_width = width;
  159. m_view_height = height;
  160. }
  161. //-----------------------------------------------------------------------------
  162. Vector3 Camera::screen_to_world(const Vector3& pos)
  163. {
  164. Matrix4x4 world_inv = m_world_pose;
  165. world_inv.invert();
  166. Matrix4x4 mvp = m_projection * world_inv;
  167. mvp.invert();
  168. Vector4 ndc( (2 * (pos.x - 0)) / m_view_width - 1,
  169. (2 * (pos.y - 0)) / m_view_height - 1,
  170. (2 * pos.z) - 1, 1);
  171. Vector4 tmp = mvp * ndc;
  172. tmp *= 1.0 / tmp.w;
  173. return Vector3(tmp.x, tmp.y, tmp.z);
  174. }
  175. //-----------------------------------------------------------------------------
  176. Vector3 Camera::world_to_screen(const Vector3& pos)
  177. {
  178. Matrix4x4 world_inv = m_world_pose;
  179. world_inv.invert();
  180. Vector3 ndc = (m_projection * world_inv) * pos;
  181. return Vector3( (m_view_x + m_view_width * (ndc.x + 1.0)) / 2.0,
  182. (m_view_y + m_view_height * (ndc.y + 1.0)) / 2.0,
  183. (ndc.z + 1.0) / 2.0);
  184. }
  185. //-----------------------------------------------------------------------------
  186. void Camera::update_projection_matrix()
  187. {
  188. switch (m_projection_type)
  189. {
  190. case ProjectionType::ORTHOGRAPHIC:
  191. {
  192. m_projection.build_projection_ortho_rh(m_left, m_right, m_bottom, m_top, m_near, m_far);
  193. break;
  194. }
  195. case ProjectionType::PERSPECTIVE:
  196. {
  197. m_projection.build_projection_perspective_rh(m_FOV, m_aspect, m_near, m_far);
  198. break;
  199. }
  200. default:
  201. {
  202. CE_FATAL("Oops, unknown projection type");
  203. break;
  204. }
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. void Camera::update_frustum()
  209. {
  210. // TODO
  211. //m_frustum.from_matrix(m_projection * m_view);
  212. }
  213. } // namespace crown