projection.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**************************************************************************/
  2. /* projection.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/math/vector3.h"
  32. #include "core/math/vector4.h"
  33. template <typename T>
  34. class Vector;
  35. struct AABB;
  36. struct Plane;
  37. struct Rect2;
  38. struct Transform3D;
  39. struct Vector2;
  40. struct [[nodiscard]] Projection {
  41. enum Planes {
  42. PLANE_NEAR,
  43. PLANE_FAR,
  44. PLANE_LEFT,
  45. PLANE_TOP,
  46. PLANE_RIGHT,
  47. PLANE_BOTTOM
  48. };
  49. Vector4 columns[4] = {
  50. { 1, 0, 0, 0 },
  51. { 0, 1, 0, 0 },
  52. { 0, 0, 1, 0 },
  53. { 0, 0, 0, 1 },
  54. };
  55. constexpr const Vector4 &operator[](int p_axis) const {
  56. DEV_ASSERT((unsigned int)p_axis < 4);
  57. return columns[p_axis];
  58. }
  59. constexpr Vector4 &operator[](int p_axis) {
  60. DEV_ASSERT((unsigned int)p_axis < 4);
  61. return columns[p_axis];
  62. }
  63. real_t determinant() const;
  64. void set_identity();
  65. void set_zero();
  66. void set_light_bias();
  67. void set_depth_correction(bool p_flip_y = true, bool p_reverse_z = true, bool p_remap_z = true);
  68. void set_light_atlas_rect(const Rect2 &p_rect);
  69. void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false);
  70. void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov, int p_eye, real_t p_intraocular_dist, real_t p_convergence_dist);
  71. void set_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_dist, real_t p_display_width, real_t p_display_to_lens, real_t p_oversample, real_t p_z_near, real_t p_z_far);
  72. void set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar);
  73. void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false);
  74. void set_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far);
  75. void set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, real_t p_near, real_t p_far, bool p_flip_fov = false);
  76. void adjust_perspective_znear(real_t p_new_znear);
  77. static Projection create_depth_correction(bool p_flip_y);
  78. static Projection create_light_atlas_rect(const Rect2 &p_rect);
  79. static Projection create_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false);
  80. static Projection create_perspective_hmd(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov, int p_eye, real_t p_intraocular_dist, real_t p_convergence_dist);
  81. static Projection create_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_dist, real_t p_display_width, real_t p_display_to_lens, real_t p_oversample, real_t p_z_near, real_t p_z_far);
  82. static Projection create_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar);
  83. static Projection create_orthogonal_aspect(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false);
  84. static Projection create_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far);
  85. static Projection create_frustum_aspect(real_t p_size, real_t p_aspect, Vector2 p_offset, real_t p_near, real_t p_far, bool p_flip_fov = false);
  86. static Projection create_fit_aabb(const AABB &p_aabb);
  87. Projection perspective_znear_adjusted(real_t p_new_znear) const;
  88. Plane get_projection_plane(Planes p_plane) const;
  89. Projection flipped_y() const;
  90. Projection jitter_offseted(const Vector2 &p_offset) const;
  91. static real_t get_fovy(real_t p_fovx, real_t p_aspect) {
  92. return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5)) * 2.0);
  93. }
  94. real_t get_z_far() const;
  95. real_t get_z_near() const;
  96. real_t get_aspect() const;
  97. real_t get_fov() const;
  98. bool is_orthogonal() const;
  99. Vector<Plane> get_projection_planes(const Transform3D &p_transform) const;
  100. bool get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const;
  101. Vector2 get_viewport_half_extents() const;
  102. Vector2 get_far_plane_half_extents() const;
  103. void invert();
  104. Projection inverse() const;
  105. constexpr Projection operator*(const Projection &p_matrix) const;
  106. Plane xform4(const Plane &p_vec4) const;
  107. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec3) const;
  108. Vector4 xform(const Vector4 &p_vec4) const;
  109. Vector4 xform_inv(const Vector4 &p_vec4) const;
  110. operator String() const;
  111. void scale_translate_to_fit(const AABB &p_aabb);
  112. void add_jitter_offset(const Vector2 &p_offset);
  113. void make_scale(const Vector3 &p_scale);
  114. int get_pixels_per_meter(int p_for_pixel_width) const;
  115. operator Transform3D() const;
  116. void flip_y();
  117. bool is_same(const Projection &p_cam) const;
  118. constexpr bool operator==(const Projection &p_cam) const {
  119. for (uint32_t i = 0; i < 4; i++) {
  120. for (uint32_t j = 0; j < 4; j++) {
  121. if (columns[i][j] != p_cam.columns[i][j]) {
  122. return false;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. constexpr bool operator!=(const Projection &p_cam) const {
  129. return !(*this == p_cam);
  130. }
  131. real_t get_lod_multiplier() const;
  132. Projection() = default;
  133. constexpr Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w) :
  134. columns{ p_x, p_y, p_z, p_w } {}
  135. constexpr Projection(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_xw, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_yw, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_zw, real_t p_wx, real_t p_wy, real_t p_wz, real_t p_ww) :
  136. columns{
  137. { p_xx, p_xy, p_xz, p_xw },
  138. { p_yx, p_yy, p_yz, p_yw },
  139. { p_zx, p_zy, p_zz, p_zw },
  140. { p_wx, p_wy, p_wz, p_ww },
  141. } {}
  142. Projection(const Transform3D &p_transform);
  143. };
  144. constexpr Projection Projection::operator*(const Projection &p_matrix) const {
  145. Projection new_matrix;
  146. for (int j = 0; j < 4; j++) {
  147. for (int i = 0; i < 4; i++) {
  148. real_t ab = 0;
  149. for (int k = 0; k < 4; k++) {
  150. ab += columns[k][i] * p_matrix.columns[j][k];
  151. }
  152. new_matrix.columns[j][i] = ab;
  153. }
  154. }
  155. return new_matrix;
  156. }
  157. Vector3 Projection::xform(const Vector3 &p_vec3) const {
  158. Vector3 ret;
  159. ret.x = columns[0][0] * p_vec3.x + columns[1][0] * p_vec3.y + columns[2][0] * p_vec3.z + columns[3][0];
  160. ret.y = columns[0][1] * p_vec3.x + columns[1][1] * p_vec3.y + columns[2][1] * p_vec3.z + columns[3][1];
  161. ret.z = columns[0][2] * p_vec3.x + columns[1][2] * p_vec3.y + columns[2][2] * p_vec3.z + columns[3][2];
  162. real_t w = columns[0][3] * p_vec3.x + columns[1][3] * p_vec3.y + columns[2][3] * p_vec3.z + columns[3][3];
  163. return ret / w;
  164. }