💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
6.7 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_CAMERA_HPP
  20. #define ANTKEEPER_CAMERA_HPP
  21. #include "scene/scene-object.hpp"
  22. #include "geometry/view-frustum.hpp"
  23. #include "utility/fundamental-types.hpp"
  24. class compositor;
  25. /**
  26. *
  27. */
  28. class camera: public scene_object<camera>
  29. {
  30. public:
  31. camera();
  32. /**
  33. * Maps object coordinates to window coordinates.
  34. *
  35. * @param object Object coordinates.
  36. * @param viewport Vector containing the `x`, `y`, `w`, and `h` of the viewport.
  37. * @return Projected window coordinates.
  38. */
  39. float3 project(const float3& object, const float4& viewport) const;
  40. /**
  41. * Maps window coordinates to object coordinates.
  42. *
  43. * @param window Window coordinates.
  44. * @param viewport Vector containing the `x`, `y`, `w`, and `h` of the viewport.
  45. * @return Unprojected object coordinates.
  46. */
  47. float3 unproject(const float3& window, const float4& viewport) const;
  48. /**
  49. * Sets the camera's projection matrix using perspective projection.
  50. *
  51. * @param fov Vertical field of view.
  52. * @param aspect_ratio Aspect ratio.
  53. * @param clip_near Distance to near clipping plane.
  54. * @param clip_far Distance to far clipping plane.
  55. */
  56. void set_perspective(float fov, float aspect_ratio, float clip_near, float clip_far);
  57. /**
  58. * Sets the camera's projection matrix using orthographic projection.
  59. *
  60. * @param clip_left Signed distance to left clipping plane.
  61. * @param clip_right Signed distance to right clipping plane.
  62. * @param clip_bottom Signed distance to bottom clipping plane.
  63. * @param clip_top Signed distance to top clipping plane.
  64. * @param clip_near Signed distance to near clipping plane.
  65. * @param clip_far Signed distance to far clipping plane.
  66. */
  67. void set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far);
  68. void set_compositor(compositor* compositor);
  69. void set_composite_index(int index);
  70. virtual const bounding_volume<float>& get_bounds() const;
  71. float is_orthographic() const;
  72. float get_clip_left() const;
  73. float get_clip_right() const;
  74. float get_clip_bottom() const;
  75. float get_clip_top() const;
  76. float get_clip_near() const;
  77. float get_clip_far() const;
  78. float get_fov() const;
  79. float get_aspect_ratio() const;
  80. /// Returns the camera's view matrix.
  81. const float4x4& get_view() const;
  82. /// Returns the camera's projection matrix.
  83. const float4x4& get_projection() const;
  84. /// Returns the camera's view-projection matrix.
  85. const float4x4& get_view_projection() const;
  86. /// Returns the camera's view frustum.
  87. const view_frustum<float>& get_view_frustum() const;
  88. const compositor* get_compositor() const;
  89. compositor* get_compositor();
  90. int get_composite_index() const;
  91. const tween<float>& get_clip_left_tween() const;
  92. const tween<float>& get_clip_right_tween() const;
  93. const tween<float>& get_clip_bottom_tween() const;
  94. const tween<float>& get_clip_top_tween() const;
  95. const tween<float>& get_clip_near_tween() const;
  96. const tween<float>& get_clip_far_tween() const;
  97. const tween<float>& get_fov_tween() const;
  98. const tween<float>& get_aspect_ratio_tween() const;
  99. const tween<float4x4>& get_view_tween() const;
  100. const tween<float4x4>& get_projection_tween() const;
  101. const tween<float4x4>& get_view_projection_tween() const;
  102. /// @copydoc scene_object_base::update_tweens();
  103. virtual void update_tweens();
  104. private:
  105. virtual void transformed();
  106. compositor* compositor;
  107. int composite_index;
  108. bool orthographic;
  109. tween<float> clip_left;
  110. tween<float> clip_right;
  111. tween<float> clip_bottom;
  112. tween<float> clip_top;
  113. tween<float> clip_near;
  114. tween<float> clip_far;
  115. tween<float> fov;
  116. tween<float> aspect_ratio;
  117. tween<float4x4> view;
  118. tween<float4x4> projection;
  119. tween<float4x4> view_projection;
  120. view_frustum<float> view_frustum;
  121. };
  122. inline const bounding_volume<float>& camera::get_bounds() const
  123. {
  124. return view_frustum.get_bounds();
  125. }
  126. inline float camera::is_orthographic() const
  127. {
  128. return orthographic;
  129. }
  130. inline float camera::get_clip_left() const
  131. {
  132. return clip_left[1];
  133. }
  134. inline float camera::get_clip_right() const
  135. {
  136. return clip_right[1];
  137. }
  138. inline float camera::get_clip_bottom() const
  139. {
  140. return clip_bottom[1];
  141. }
  142. inline float camera::get_clip_top() const
  143. {
  144. return clip_top[1];
  145. }
  146. inline float camera::get_clip_near() const
  147. {
  148. return clip_near[1];
  149. }
  150. inline float camera::get_clip_far() const
  151. {
  152. return clip_far[1];
  153. }
  154. inline float camera::get_fov() const
  155. {
  156. return fov[1];
  157. }
  158. inline float camera::get_aspect_ratio() const
  159. {
  160. return aspect_ratio[1];
  161. }
  162. inline const float4x4& camera::get_view() const
  163. {
  164. return view[1];
  165. }
  166. inline const float4x4& camera::get_projection() const
  167. {
  168. return projection[1];
  169. }
  170. inline const float4x4& camera::get_view_projection() const
  171. {
  172. return view_projection[1];
  173. }
  174. inline const view_frustum<float>& camera::get_view_frustum() const
  175. {
  176. return view_frustum;
  177. }
  178. inline const compositor* camera::get_compositor() const
  179. {
  180. return compositor;
  181. }
  182. inline compositor* camera::get_compositor()
  183. {
  184. return compositor;
  185. }
  186. inline int camera::get_composite_index() const
  187. {
  188. return composite_index;
  189. }
  190. inline const tween<float>& camera::get_clip_left_tween() const
  191. {
  192. return clip_left;
  193. }
  194. inline const tween<float>& camera::get_clip_right_tween() const
  195. {
  196. return clip_right;
  197. }
  198. inline const tween<float>& camera::get_clip_bottom_tween() const
  199. {
  200. return clip_bottom;
  201. }
  202. inline const tween<float>& camera::get_clip_top_tween() const
  203. {
  204. return clip_top;
  205. }
  206. inline const tween<float>& camera::get_clip_near_tween() const
  207. {
  208. return clip_near;
  209. }
  210. inline const tween<float>& camera::get_clip_far_tween() const
  211. {
  212. return clip_far;
  213. }
  214. inline const tween<float>& camera::get_fov_tween() const
  215. {
  216. return fov;
  217. }
  218. inline const tween<float>& camera::get_aspect_ratio_tween() const
  219. {
  220. return aspect_ratio;
  221. }
  222. inline const tween<float4x4>& camera::get_view_tween() const
  223. {
  224. return view;
  225. }
  226. inline const tween<float4x4>& camera::get_projection_tween() const
  227. {
  228. return projection;
  229. }
  230. inline const tween<float4x4>& camera::get_view_projection_tween() const
  231. {
  232. return view_projection;
  233. }
  234. #endif // ANTKEEPER_CAMERA_HPP