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

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