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

320 lines
7.4 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. class compositor;
  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 exposure.
  72. *
  73. * @param exposure Exposure factor.
  74. */
  75. void set_exposure(float exposure);
  76. void set_compositor(compositor* compositor);
  77. void set_composite_index(int index);
  78. virtual const bounding_volume_type& get_local_bounds() const;
  79. virtual const bounding_volume_type& get_world_bounds() const;
  80. float is_orthographic() const;
  81. float get_clip_left() const;
  82. float get_clip_right() const;
  83. float get_clip_bottom() const;
  84. float get_clip_top() const;
  85. float get_clip_near() const;
  86. float get_clip_far() const;
  87. float get_fov() const;
  88. float get_aspect_ratio() const;
  89. /// Returns the camera's view matrix.
  90. const float4x4& get_view() const;
  91. /// Returns the camera's projection matrix.
  92. const float4x4& get_projection() const;
  93. /// Returns the camera's view-projection matrix.
  94. const float4x4& get_view_projection() const;
  95. /// Returns the camera's view frustum.
  96. const view_frustum_type& get_view_frustum() const;
  97. /// Returns the camera's exposure.
  98. float get_exposure() const;
  99. const compositor* get_compositor() const;
  100. compositor* get_compositor();
  101. int get_composite_index() const;
  102. const tween<float>& get_clip_left_tween() const;
  103. const tween<float>& get_clip_right_tween() const;
  104. const tween<float>& get_clip_bottom_tween() const;
  105. const tween<float>& get_clip_top_tween() const;
  106. const tween<float>& get_clip_near_tween() const;
  107. const tween<float>& get_clip_far_tween() const;
  108. const tween<float>& get_fov_tween() const;
  109. const tween<float>& get_aspect_ratio_tween() const;
  110. const tween<float4x4>& get_view_tween() const;
  111. const tween<float4x4>& get_projection_tween() const;
  112. const tween<float4x4>& get_view_projection_tween() const;
  113. const tween<float>& get_exposure_tween() const;
  114. /// @copydoc object_base::update_tweens();
  115. virtual void update_tweens();
  116. private:
  117. virtual void transformed();
  118. compositor* compositor;
  119. int composite_index;
  120. bool orthographic;
  121. tween<float> clip_left;
  122. tween<float> clip_right;
  123. tween<float> clip_bottom;
  124. tween<float> clip_top;
  125. tween<float> clip_near;
  126. tween<float> clip_far;
  127. tween<float> fov;
  128. tween<float> aspect_ratio;
  129. tween<float4x4> view;
  130. tween<float4x4> projection;
  131. tween<float4x4> view_projection;
  132. tween<float> exposure;
  133. view_frustum_type view_frustum;
  134. };
  135. inline const typename object_base::bounding_volume_type& camera::get_local_bounds() const
  136. {
  137. /// @TODO: return local bounds, not world bounds
  138. return view_frustum.get_bounds();
  139. }
  140. inline const typename object_base::bounding_volume_type& camera::get_world_bounds() const
  141. {
  142. return view_frustum.get_bounds();
  143. }
  144. inline float camera::is_orthographic() const
  145. {
  146. return orthographic;
  147. }
  148. inline float camera::get_clip_left() const
  149. {
  150. return clip_left[1];
  151. }
  152. inline float camera::get_clip_right() const
  153. {
  154. return clip_right[1];
  155. }
  156. inline float camera::get_clip_bottom() const
  157. {
  158. return clip_bottom[1];
  159. }
  160. inline float camera::get_clip_top() const
  161. {
  162. return clip_top[1];
  163. }
  164. inline float camera::get_clip_near() const
  165. {
  166. return clip_near[1];
  167. }
  168. inline float camera::get_clip_far() const
  169. {
  170. return clip_far[1];
  171. }
  172. inline float camera::get_fov() const
  173. {
  174. return fov[1];
  175. }
  176. inline float camera::get_aspect_ratio() const
  177. {
  178. return aspect_ratio[1];
  179. }
  180. inline const float4x4& camera::get_view() const
  181. {
  182. return view[1];
  183. }
  184. inline const float4x4& camera::get_projection() const
  185. {
  186. return projection[1];
  187. }
  188. inline const float4x4& camera::get_view_projection() const
  189. {
  190. return view_projection[1];
  191. }
  192. inline const typename camera::view_frustum_type& camera::get_view_frustum() const
  193. {
  194. return view_frustum;
  195. }
  196. inline float camera::get_exposure() const
  197. {
  198. return exposure[1];
  199. }
  200. inline const compositor* camera::get_compositor() const
  201. {
  202. return compositor;
  203. }
  204. inline compositor* camera::get_compositor()
  205. {
  206. return compositor;
  207. }
  208. inline int camera::get_composite_index() const
  209. {
  210. return composite_index;
  211. }
  212. inline const tween<float>& camera::get_clip_left_tween() const
  213. {
  214. return clip_left;
  215. }
  216. inline const tween<float>& camera::get_clip_right_tween() const
  217. {
  218. return clip_right;
  219. }
  220. inline const tween<float>& camera::get_clip_bottom_tween() const
  221. {
  222. return clip_bottom;
  223. }
  224. inline const tween<float>& camera::get_clip_top_tween() const
  225. {
  226. return clip_top;
  227. }
  228. inline const tween<float>& camera::get_clip_near_tween() const
  229. {
  230. return clip_near;
  231. }
  232. inline const tween<float>& camera::get_clip_far_tween() const
  233. {
  234. return clip_far;
  235. }
  236. inline const tween<float>& camera::get_fov_tween() const
  237. {
  238. return fov;
  239. }
  240. inline const tween<float>& camera::get_aspect_ratio_tween() const
  241. {
  242. return aspect_ratio;
  243. }
  244. inline const tween<float4x4>& camera::get_view_tween() const
  245. {
  246. return view;
  247. }
  248. inline const tween<float4x4>& camera::get_projection_tween() const
  249. {
  250. return projection;
  251. }
  252. inline const tween<float4x4>& camera::get_view_projection_tween() const
  253. {
  254. return view_projection;
  255. }
  256. inline const tween<float>& camera::get_exposure_tween() const
  257. {
  258. return exposure;
  259. }
  260. } // namespace scene
  261. #endif // ANTKEEPER_SCENE_CAMERA_HPP