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

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