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

169 lines
5.3 KiB

  1. /*
  2. * Copyright (C) 2023 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 <engine/scene/camera.hpp>
  20. #include <engine/math/quaternion.hpp>
  21. #include <engine/math/projection.hpp>
  22. namespace scene {
  23. geom::ray<float, 3> camera::pick(const math::fvec2& ndc) const
  24. {
  25. const auto near = m_inv_view_projection * math::fvec4{ndc[0], ndc[1], 1.0f, 1.0f};
  26. const auto far = m_inv_view_projection * math::fvec4{ndc[0], ndc[1], 0.0f, 1.0f};
  27. const auto origin = math::fvec3{near[0], near[1], near[2]} / near[3];
  28. const auto direction = math::normalize(math::fvec3{far[0], far[1], far[2]} / far[3] - origin);
  29. return {origin, direction};
  30. }
  31. math::fvec3 camera::project(const math::fvec3& object, const math::fvec4& viewport) const
  32. {
  33. math::fvec4 result = m_view_projection * math::fvec4{object[0], object[1], object[2], 1.0f};
  34. result[0] = (result[0] / result[3]) * 0.5f + 0.5f;
  35. result[1] = (result[1] / result[3]) * 0.5f + 0.5f;
  36. result[2] = (result[2] / result[3]) * 0.5f + 0.5f;
  37. result[0] = result[0] * viewport[2] + viewport[0];
  38. result[1] = result[1] * viewport[3] + viewport[1];
  39. return math::fvec3(result);
  40. }
  41. math::fvec3 camera::unproject(const math::fvec3& window, const math::fvec4& viewport) const
  42. {
  43. math::fvec4 result;
  44. result[0] = ((window[0] - viewport[0]) / viewport[2]) * 2.0f - 1.0f;
  45. result[1] = ((window[1] - viewport[1]) / viewport[3]) * 2.0f - 1.0f;
  46. //result[2] = window[2] * 2.0f - 1.0f; z: [-1, 1]
  47. //result[2] = window[2]; // z: [0, 1]
  48. result[2] = 1.0f - window[2]; // z: [1, 0]
  49. result[3] = 1.0f;
  50. result = m_inv_view_projection * result;
  51. return math::fvec3(result) * (1.0f / result[3]);
  52. }
  53. void camera::set_perspective(float vertical_fov, float aspect_ratio, float clip_near, float clip_far)
  54. {
  55. // Set projection mode to perspective
  56. m_orthographic = false;
  57. // Update perspective projection parameters
  58. m_vertical_fov = vertical_fov;
  59. m_aspect_ratio = aspect_ratio;
  60. m_clip_near = clip_near;
  61. m_clip_far = clip_far;
  62. // Recalculate projection matrix and its inverse
  63. std::tie(m_projection, m_inv_projection) = math::perspective_half_z_inv(m_vertical_fov, m_aspect_ratio, m_clip_far, m_clip_near);
  64. // Recalculate view-projection matrix
  65. m_view_projection = m_projection * m_view;
  66. m_inv_view_projection = m_inv_view * m_inv_projection;
  67. // Recalculate view frustum
  68. update_frustum();
  69. }
  70. void camera::set_vertical_fov(float vertical_fov)
  71. {
  72. if (!m_orthographic)
  73. {
  74. set_perspective(vertical_fov, m_aspect_ratio, m_clip_near, m_clip_far);
  75. }
  76. }
  77. void camera::set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far)
  78. {
  79. // Set projection mode to orthographic
  80. m_orthographic = true;
  81. // Update signed distances to clipping planes
  82. m_clip_left = clip_left;
  83. m_clip_right = clip_right;
  84. m_clip_bottom = clip_bottom;
  85. m_clip_top = clip_top;
  86. m_clip_near = clip_near;
  87. m_clip_far = clip_far;
  88. // Recalculate projection matrix and its inverse
  89. std::tie(m_projection, m_inv_projection) = math::ortho_half_z_inv(m_clip_left, m_clip_right, m_clip_bottom, m_clip_top, m_clip_far, m_clip_near);
  90. // Update view-projection matrix and its inverse
  91. m_view_projection = m_projection * m_view;
  92. m_inv_view_projection = m_inv_view * m_inv_projection;
  93. // Update view frustum
  94. update_frustum();
  95. }
  96. void camera::set_exposure_value(float ev100)
  97. {
  98. m_exposure_value = ev100;
  99. // m_exposure_normalization = 1.0f / (std::exp2(m_exposure_value) * 1.2f);
  100. m_exposure_normalization = 1.0f / (std::exp2(m_exposure_value));
  101. }
  102. void camera::transformed()
  103. {
  104. // Update basis vectors
  105. m_forward = get_rotation() * math::fvec3{0.0f, 0.0f, -1.0f};
  106. m_up = get_rotation() * math::fvec3{0.0f, 1.0f, 0.0f};
  107. // Recalculate view matrix and its inverse
  108. std::tie(m_view, m_inv_view) = math::look_at_rh_inv(get_translation(), get_translation() + m_forward, m_up);
  109. // Update view-projection matrix and its inverse
  110. m_view_projection = m_projection * m_view;
  111. m_inv_view_projection = m_inv_view * m_inv_projection;
  112. // Update view frustum
  113. update_frustum();
  114. }
  115. void camera::update_frustum()
  116. {
  117. // Recalculate view frustum
  118. m_view_frustum.extract(m_view_projection);
  119. // Reverse half z clip-space coordinates of a cube
  120. constexpr math::fvec4 clip_space_cube[8] =
  121. {
  122. {-1, -1, 1, 1}, // NBL
  123. { 1, -1, 1, 1}, // NBR
  124. {-1, 1, 1, 1}, // NTL
  125. { 1, 1, 1, 1}, // NTR
  126. {-1, -1, 0, 1}, // FBL
  127. { 1, -1, 0, 1}, // FBR
  128. {-1, 1, 0, 1}, // FTL
  129. { 1, 1, 0, 1} // FTR
  130. };
  131. // Update bounds
  132. m_bounds = {math::fvec3::infinity(), -math::fvec3::infinity()};
  133. for (std::size_t i = 0; i < 8; ++i)
  134. {
  135. const math::fvec4 frustum_corner = m_inv_view_projection * clip_space_cube[i];
  136. m_bounds.extend(math::fvec3(frustum_corner) / frustum_corner[3]);
  137. }
  138. }
  139. } // namespace scene