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

179 lines
5.7 KiB

  1. /*
  2. * Copyright (C) 2020 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 "configuration.hpp"
  21. #include "animation/ease.hpp"
  22. using namespace vmq::operators;
  23. static float4x4 interpolate_view(const camera* camera, const float4x4& x, const float4x4& y, float a)
  24. {
  25. transform<float> transform = camera->get_transform_tween().interpolate(a);
  26. float3 forward = transform.rotation * global_forward;
  27. float3 up = transform.rotation * global_up;
  28. return vmq::look_at(transform.translation, transform.translation + forward, up);
  29. }
  30. static float4x4 interpolate_projection(const camera* camera, const float4x4& x, const float4x4& y, float a)
  31. {
  32. if (camera->is_orthographic())
  33. {
  34. return vmq::ortho(
  35. camera->get_clip_left_tween().interpolate(a),
  36. camera->get_clip_right_tween().interpolate(a),
  37. camera->get_clip_bottom_tween().interpolate(a),
  38. camera->get_clip_top_tween().interpolate(a),
  39. camera->get_clip_near_tween().interpolate(a),
  40. camera->get_clip_far_tween().interpolate(a));
  41. }
  42. else
  43. {
  44. return vmq::perspective(
  45. camera->get_fov_tween().interpolate(a),
  46. camera->get_aspect_ratio_tween().interpolate(a),
  47. camera->get_clip_near_tween().interpolate(a),
  48. camera->get_clip_far_tween().interpolate(a));
  49. }
  50. }
  51. static float4x4 interpolate_view_projection(const camera* camera, const float4x4& x, const float4x4& y, float a)
  52. {
  53. return camera->get_projection_tween().interpolate(a) * camera->get_view_tween().interpolate(a);
  54. }
  55. camera::camera():
  56. compositor(nullptr),
  57. composite_index(0),
  58. orthographic(true),
  59. clip_left(-1.0f, ease<float>::linear),
  60. clip_right(1.0f, ease<float>::linear),
  61. clip_bottom(-1.0f, ease<float>::linear),
  62. clip_top(1.0f, ease<float>::linear),
  63. clip_near(-1.0f, ease<float>::linear),
  64. clip_far(1.0f, ease<float>::linear),
  65. fov(vmq::half_pi<float>, ease<float>::linear),
  66. aspect_ratio(1.0f, ease<float>::linear),
  67. view(vmq::identity4x4<float>, std::bind(&interpolate_view, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
  68. projection(vmq::identity4x4<float>, std::bind(&interpolate_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
  69. view_projection(vmq::identity4x4<float>, std::bind(&interpolate_view_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
  70. {}
  71. float3 camera::project(const float3& object, const float4& viewport) const
  72. {
  73. float4 result = view_projection[1] * float4{object[0], object[1], object[2], 1.0f};
  74. result[0] = (result[0] / result[3]) * 0.5f + 0.5f;
  75. result[1] = (result[1] / result[3]) * 0.5f + 0.5f;
  76. result[2] = (result[2] / result[3]) * 0.5f + 0.5f;
  77. result[0] = result[0] * viewport[2] + viewport[0];
  78. result[1] = result[1] * viewport[3] + viewport[1];
  79. return vmq::resize<3>(result);
  80. }
  81. float3 camera::unproject(const float3& window, const float4& viewport) const
  82. {
  83. float4 result;
  84. result[0] = ((window[0] - viewport[0]) / viewport[2]) * 2.0f - 1.0f;
  85. result[1] = ((window[1] - viewport[1]) / viewport[3]) * 2.0f - 1.0f;
  86. result[2] = window[2] * 2.0f - 1.0f;
  87. result[3] = 1.0f;
  88. result = vmq::inverse(view_projection[1]) * result;
  89. return vmq::resize<3>(result) * (1.0f / result[3]);
  90. }
  91. void camera::set_perspective(float fov, float aspect_ratio, float clip_near, float clip_far)
  92. {
  93. orthographic = false;
  94. this->fov[1] = fov;
  95. this->aspect_ratio[1] = aspect_ratio;
  96. this->clip_near[1] = clip_near;
  97. this->clip_far[1] = clip_far;
  98. projection[1] = vmq::perspective(fov, aspect_ratio, clip_near, clip_far);
  99. // Recalculate view-projection matrix
  100. view_projection[1] = projection[1] * view[1];
  101. // Recalculate view frustum
  102. view_frustum.set_matrix(view_projection[1]);
  103. }
  104. void camera::set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far)
  105. {
  106. orthographic = true;
  107. this->clip_left[1] = clip_left;
  108. this->clip_right[1] = clip_right;
  109. this->clip_bottom[1] = clip_bottom;
  110. this->clip_top[1] = clip_top;
  111. this->clip_near[1] = clip_near;
  112. this->clip_far[1] = clip_far;
  113. projection[1] = vmq::ortho(clip_left, clip_right, clip_bottom, clip_top, clip_near, clip_far);
  114. // Recalculate view-projection matrix
  115. view_projection[1] = projection[1] * view[1];
  116. // Recalculate view frustum
  117. view_frustum.set_matrix(view_projection[1]);
  118. }
  119. void camera::set_compositor(::compositor* compositor)
  120. {
  121. this->compositor = compositor;
  122. }
  123. void camera::set_composite_index(int index)
  124. {
  125. composite_index = index;
  126. }
  127. void camera::update_tweens()
  128. {
  129. scene_object_base::update_tweens();
  130. clip_left.update();
  131. clip_right.update();
  132. clip_bottom.update();
  133. clip_top.update();
  134. clip_near.update();
  135. clip_far.update();
  136. fov.update();
  137. aspect_ratio.update();
  138. view.update();
  139. projection.update();
  140. view_projection.update();
  141. }
  142. void camera::transformed()
  143. {
  144. // Recalculate view and view-projection matrices
  145. float3 forward = get_rotation() * global_forward;
  146. float3 up = get_rotation() * global_up;
  147. view[1] = vmq::look_at(get_translation(), get_translation() + forward, up);
  148. view_projection[1] = projection[1] * view[1];
  149. // Recalculate view frustum
  150. view_frustum.set_matrix(view_projection[1]);
  151. }