Camera.cpp 7.0 KB

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