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

208 lines
6.9 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. #include "scene/camera.hpp"
  20. #include "config.hpp"
  21. #include "math/constants.hpp"
  22. #include "math/interpolation.hpp"
  23. namespace scene {
  24. static float4x4 interpolate_view(const camera* camera, const float4x4& x, const float4x4& y, float a)
  25. {
  26. math::transform<float> transform = camera->get_transform_tween().interpolate(a);
  27. float3 forward = transform.rotation * config::global_forward;
  28. float3 up = transform.rotation * config::global_up;
  29. return math::look_at(transform.translation, transform.translation + forward, up);
  30. }
  31. static float4x4 interpolate_projection(const camera* camera, const float4x4& x, const float4x4& y, float a)
  32. {
  33. if (camera->is_orthographic())
  34. {
  35. return math::ortho(
  36. camera->get_clip_left_tween().interpolate(a),
  37. camera->get_clip_right_tween().interpolate(a),
  38. camera->get_clip_bottom_tween().interpolate(a),
  39. camera->get_clip_top_tween().interpolate(a),
  40. camera->get_clip_far_tween().interpolate(a),
  41. camera->get_clip_near_tween().interpolate(a));
  42. }
  43. else
  44. {
  45. return math::perspective(
  46. camera->get_fov_tween().interpolate(a),
  47. camera->get_aspect_ratio_tween().interpolate(a),
  48. camera->get_clip_far_tween().interpolate(a),
  49. camera->get_clip_near_tween().interpolate(a));
  50. }
  51. }
  52. static float4x4 interpolate_view_projection(const camera* camera, const float4x4& x, const float4x4& y, float a)
  53. {
  54. return camera->get_projection_tween().interpolate(a) * camera->get_view_tween().interpolate(a);
  55. }
  56. camera::camera():
  57. compositor(nullptr),
  58. composite_index(0),
  59. orthographic(true),
  60. clip_left(-1.0f, math::lerp<float, float>),
  61. clip_right(1.0f, math::lerp<float, float>),
  62. clip_bottom(-1.0f, math::lerp<float, float>),
  63. clip_top(1.0f, math::lerp<float, float>),
  64. clip_near(-1.0f, math::lerp<float, float>),
  65. clip_far(1.0f, math::lerp<float, float>),
  66. fov(math::half_pi<float>, math::lerp<float, float>),
  67. aspect_ratio(1.0f, math::lerp<float, float>),
  68. view(math::matrix4<float>::identity, std::bind(&interpolate_view, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
  69. projection(math::matrix4<float>::identity, std::bind(&interpolate_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
  70. view_projection(math::matrix4<float>::identity, std::bind(&interpolate_view_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
  71. exposure(0.0f, math::lerp<float, float>)
  72. {}
  73. geom::ray<float> camera::pick(const float2& ndc) const
  74. {
  75. const float4x4 inverse_view_projection = math::inverse(view_projection[1]);
  76. const float4 near = inverse_view_projection * float4{ndc[0], ndc[1], 1.0f, 1.0f};
  77. const float4 far = inverse_view_projection * float4{ndc[0], ndc[1], 0.0f, 1.0f};
  78. const float3 origin = float3{near[0], near[1], near[2]} / near[3];
  79. const float3 direction = math::normalize(float3{far[0], far[1], far[2]} / far[3] - origin);
  80. return {origin, direction};
  81. }
  82. float3 camera::project(const float3& object, const float4& viewport) const
  83. {
  84. float4 result = view_projection[1] * float4{object[0], object[1], object[2], 1.0f};
  85. result[0] = (result[0] / result[3]) * 0.5f + 0.5f;
  86. result[1] = (result[1] / result[3]) * 0.5f + 0.5f;
  87. result[2] = (result[2] / result[3]) * 0.5f + 0.5f;
  88. result[0] = result[0] * viewport[2] + viewport[0];
  89. result[1] = result[1] * viewport[3] + viewport[1];
  90. return math::resize<3>(result);
  91. }
  92. float3 camera::unproject(const float3& window, const float4& viewport) const
  93. {
  94. float4 result;
  95. result[0] = ((window[0] - viewport[0]) / viewport[2]) * 2.0f - 1.0f;
  96. result[1] = ((window[1] - viewport[1]) / viewport[3]) * 2.0f - 1.0f;
  97. //result[2] = window[2] * 2.0f - 1.0f; z: [-1, 1]
  98. //result[2] = window[2]; // z: [0, 1]
  99. result[2] = 1.0f - window[2]; // z: [1, 0]
  100. result[3] = 1.0f;
  101. result = math::inverse(view_projection[1]) * result;
  102. return math::resize<3>(result) * (1.0f / result[3]);
  103. }
  104. void camera::set_perspective(float fov, float aspect_ratio, float clip_near, float clip_far)
  105. {
  106. orthographic = false;
  107. this->fov[1] = fov;
  108. this->aspect_ratio[1] = aspect_ratio;
  109. this->clip_near[1] = clip_near;
  110. this->clip_far[1] = clip_far;
  111. projection[1] = math::perspective_half_z(fov, aspect_ratio, clip_far, clip_near);
  112. // Recalculate view-projection matrix
  113. view_projection[1] = projection[1] * view[1];
  114. // Recalculate view frustum
  115. /// @TODO: this is a hack to fix the half z projection matrix view frustum
  116. view_frustum.set_matrix(math::perspective(this->fov[1], this->aspect_ratio[1], this->clip_near[1], this->clip_far[1]) * view[1]);
  117. }
  118. void camera::set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far)
  119. {
  120. orthographic = true;
  121. this->clip_left[1] = clip_left;
  122. this->clip_right[1] = clip_right;
  123. this->clip_bottom[1] = clip_bottom;
  124. this->clip_top[1] = clip_top;
  125. this->clip_near[1] = clip_near;
  126. this->clip_far[1] = clip_far;
  127. projection[1] = math::ortho_half_z(clip_left, clip_right, clip_bottom, clip_top, clip_far, clip_near);
  128. // Recalculate view-projection matrix
  129. view_projection[1] = projection[1] * view[1];
  130. // Recalculate view frustum
  131. view_frustum.set_matrix(view_projection[1]);
  132. }
  133. void camera::set_exposure(float ev100)
  134. {
  135. exposure[1] = ev100;
  136. }
  137. void camera::set_compositor(render::compositor* compositor)
  138. {
  139. this->compositor = compositor;
  140. }
  141. void camera::set_composite_index(int index)
  142. {
  143. composite_index = index;
  144. }
  145. void camera::update_tweens()
  146. {
  147. object_base::update_tweens();
  148. clip_left.update();
  149. clip_right.update();
  150. clip_bottom.update();
  151. clip_top.update();
  152. clip_near.update();
  153. clip_far.update();
  154. fov.update();
  155. aspect_ratio.update();
  156. view.update();
  157. projection.update();
  158. view_projection.update();
  159. exposure.update();
  160. }
  161. void camera::transformed()
  162. {
  163. // Recalculate view and view-projection matrices
  164. float3 forward = get_rotation() * config::global_forward;
  165. float3 up = get_rotation() * config::global_up;
  166. view[1] = math::look_at(get_translation(), get_translation() + forward, up);
  167. view_projection[1] = projection[1] * view[1];
  168. // Recalculate view frustum
  169. /// @TODO: this is a hack to fix the half z projection matrix view frustum
  170. if (orthographic)
  171. view_frustum.set_matrix(view_projection[1]);
  172. else
  173. view_frustum.set_matrix(math::perspective(fov[1], aspect_ratio[1], clip_near[1], clip_far[1]) * view[1]);
  174. }
  175. } // namespace scene