💿🐜 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.

288 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_SCENE_CAMERA_HPP
  20. #define ANTKEEPER_SCENE_CAMERA_HPP
  21. #include "scene/object.hpp"
  22. #include "geometry/view-frustum.hpp"
  23. #include "utility/fundamental-types.hpp"
  24. class compositor;
  25. namespace scene {
  26. /**
  27. *
  28. */
  29. class camera: public object<camera>
  30. {
  31. public:
  32. camera();
  33. /**
  34. * Maps object coordinates to window coordinates.
  35. *
  36. * @param object Object coordinates.
  37. * @param viewport Vector containing the `x`, `y`, `w`, and `h` of the viewport.
  38. * @return Projected window coordinates.
  39. */
  40. float3 project(const float3& object, const float4& viewport) const;
  41. /**
  42. * Maps window coordinates to object coordinates.
  43. *
  44. * @param window Window coordinates.
  45. * @param viewport Vector containing the `x`, `y`, `w`, and `h` of the viewport.
  46. * @return Unprojected object coordinates.
  47. */
  48. float3 unproject(const float3& window, const float4& viewport) const;
  49. /**
  50. * Sets the camera's projection matrix using perspective projection.
  51. *
  52. * @param fov Vertical field of view.
  53. * @param aspect_ratio Aspect ratio.
  54. * @param clip_near Distance to near clipping plane.
  55. * @param clip_far Distance to far clipping plane.
  56. */
  57. void set_perspective(float fov, float aspect_ratio, float clip_near, float clip_far);
  58. /**
  59. * Sets the camera's projection matrix using orthographic projection.
  60. *
  61. * @param clip_left Signed distance to left clipping plane.
  62. * @param clip_right Signed distance to right clipping plane.
  63. * @param clip_bottom Signed distance to bottom clipping plane.
  64. * @param clip_top Signed distance to top clipping plane.
  65. * @param clip_near Signed distance to near clipping plane.
  66. * @param clip_far Signed distance to far clipping plane.
  67. */
  68. void set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far);
  69. void set_compositor(compositor* compositor);
  70. void set_composite_index(int index);
  71. virtual const bounding_volume<float>& get_bounds() const;
  72. float is_orthographic() const;
  73. float get_clip_left() const;
  74. float get_clip_right() const;
  75. float get_clip_bottom() const;
  76. float get_clip_top() const;
  77. float get_clip_near() const;
  78. float get_clip_far() const;
  79. float get_fov() const;
  80. float get_aspect_ratio() const;
  81. /// Returns the camera's view matrix.
  82. const float4x4& get_view() const;
  83. /// Returns the camera's projection matrix.
  84. const float4x4& get_projection() const;
  85. /// Returns the camera's view-projection matrix.
  86. const float4x4& get_view_projection() const;
  87. /// Returns the camera's view frustum.
  88. const view_frustum<float>& get_view_frustum() const;
  89. const compositor* get_compositor() const;
  90. compositor* get_compositor();
  91. int get_composite_index() const;
  92. const tween<float>& get_clip_left_tween() const;
  93. const tween<float>& get_clip_right_tween() const;
  94. const tween<float>& get_clip_bottom_tween() const;
  95. const tween<float>& get_clip_top_tween() const;
  96. const tween<float>& get_clip_near_tween() const;
  97. const tween<float>& get_clip_far_tween() const;
  98. const tween<float>& get_fov_tween() const;
  99. const tween<float>& get_aspect_ratio_tween() const;
  100. const tween<float4x4>& get_view_tween() const;
  101. const tween<float4x4>& get_projection_tween() const;
  102. const tween<float4x4>& get_view_projection_tween() const;
  103. /// @copydoc object_base::update_tweens();
  104. virtual void update_tweens();
  105. private:
  106. virtual void transformed();
  107. compositor* compositor;
  108. int composite_index;
  109. bool orthographic;
  110. tween<float> clip_left;
  111. tween<float> clip_right;
  112. tween<float> clip_bottom;
  113. tween<float> clip_top;
  114. tween<float> clip_near;
  115. tween<float> clip_far;
  116. tween<float> fov;
  117. tween<float> aspect_ratio;
  118. tween<float4x4> view;
  119. tween<float4x4> projection;
  120. tween<float4x4> view_projection;
  121. view_frustum<float> view_frustum;
  122. };
  123. inline const bounding_volume<float>& camera::get_bounds() const
  124. {
  125. return view_frustum.get_bounds();
  126. }
  127. inline float camera::is_orthographic() const
  128. {
  129. return orthographic;
  130. }
  131. inline float camera::get_clip_left() const
  132. {
  133. return clip_left[1];
  134. }
  135. inline float camera::get_clip_right() const
  136. {
  137. return clip_right[1];
  138. }
  139. inline float camera::get_clip_bottom() const
  140. {
  141. return clip_bottom[1];
  142. }
  143. inline float camera::get_clip_top() const
  144. {
  145. return clip_top[1];
  146. }
  147. inline float camera::get_clip_near() const
  148. {
  149. return clip_near[1];
  150. }
  151. inline float camera::get_clip_far() const
  152. {
  153. return clip_far[1];
  154. }
  155. inline float camera::get_fov() const
  156. {
  157. return fov[1];
  158. }
  159. inline float camera::get_aspect_ratio() const
  160. {
  161. return aspect_ratio[1];
  162. }
  163. inline const float4x4& camera::get_view() const
  164. {
  165. return view[1];
  166. }
  167. inline const float4x4& camera::get_projection() const
  168. {
  169. return projection[1];
  170. }
  171. inline const float4x4& camera::get_view_projection() const
  172. {
  173. return view_projection[1];
  174. }
  175. inline const view_frustum<float>& camera::get_view_frustum() const
  176. {
  177. return view_frustum;
  178. }
  179. inline const compositor* camera::get_compositor() const
  180. {
  181. return compositor;
  182. }
  183. inline compositor* camera::get_compositor()
  184. {
  185. return compositor;
  186. }
  187. inline int camera::get_composite_index() const
  188. {
  189. return composite_index;
  190. }
  191. inline const tween<float>& camera::get_clip_left_tween() const
  192. {
  193. return clip_left;
  194. }
  195. inline const tween<float>& camera::get_clip_right_tween() const
  196. {
  197. return clip_right;
  198. }
  199. inline const tween<float>& camera::get_clip_bottom_tween() const
  200. {
  201. return clip_bottom;
  202. }
  203. inline const tween<float>& camera::get_clip_top_tween() const
  204. {
  205. return clip_top;
  206. }
  207. inline const tween<float>& camera::get_clip_near_tween() const
  208. {
  209. return clip_near;
  210. }
  211. inline const tween<float>& camera::get_clip_far_tween() const
  212. {
  213. return clip_far;
  214. }
  215. inline const tween<float>& camera::get_fov_tween() const
  216. {
  217. return fov;
  218. }
  219. inline const tween<float>& camera::get_aspect_ratio_tween() const
  220. {
  221. return aspect_ratio;
  222. }
  223. inline const tween<float4x4>& camera::get_view_tween() const
  224. {
  225. return view;
  226. }
  227. inline const tween<float4x4>& camera::get_projection_tween() const
  228. {
  229. return projection;
  230. }
  231. inline const tween<float4x4>& camera::get_view_projection_tween() const
  232. {
  233. return view_projection;
  234. }
  235. } // namespace scene
  236. #endif // ANTKEEPER_SCENE_CAMERA_HPP