projection.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*************************************************************************/
  2. /* projection.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef GODOT_PROJECTION_HPP
  31. #define GODOT_PROJECTION_HPP
  32. #include <godot_cpp/core/math.hpp>
  33. #include <godot_cpp/variant/array.hpp>
  34. #include <godot_cpp/variant/vector3.hpp>
  35. #include <godot_cpp/variant/vector4.hpp>
  36. namespace godot {
  37. class AABB;
  38. class Plane;
  39. class Rect2;
  40. class Transform3D;
  41. class Vector2;
  42. class Projection {
  43. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  44. friend class Variant;
  45. public:
  46. enum Planes {
  47. PLANE_NEAR,
  48. PLANE_FAR,
  49. PLANE_LEFT,
  50. PLANE_TOP,
  51. PLANE_RIGHT,
  52. PLANE_BOTTOM
  53. };
  54. Vector4 matrix[4];
  55. _FORCE_INLINE_ const Vector4 &operator[](const int p_axis) const {
  56. return matrix[p_axis];
  57. }
  58. _FORCE_INLINE_ Vector4 &operator[](const int p_axis) {
  59. return matrix[p_axis];
  60. }
  61. float determinant() const;
  62. void set_identity();
  63. void set_zero();
  64. void set_light_bias();
  65. void set_depth_correction(bool p_flip_y = true);
  66. void set_light_atlas_rect(const Rect2 &p_rect);
  67. 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);
  68. 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);
  69. 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);
  70. 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);
  71. void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false);
  72. 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);
  73. 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);
  74. void adjust_perspective_znear(real_t p_new_znear);
  75. static Projection create_depth_correction(bool p_flip_y);
  76. static Projection create_light_atlas_rect(const Rect2 &p_rect);
  77. 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);
  78. 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);
  79. 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);
  80. 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);
  81. 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);
  82. 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);
  83. 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);
  84. static Projection create_fit_aabb(const AABB &p_aabb);
  85. Projection perspective_znear_adjusted(real_t p_new_znear) const;
  86. Plane get_projection_plane(Planes p_plane) const;
  87. Projection flipped_y() const;
  88. Projection jitter_offseted(const Vector2 &p_offset) const;
  89. static real_t get_fovy(real_t p_fovx, real_t p_aspect) {
  90. return Math::rad2deg(Math::atan(p_aspect * Math::tan(Math::deg2rad(p_fovx) * 0.5)) * 2.0);
  91. }
  92. real_t get_z_far() const;
  93. real_t get_z_near() const;
  94. real_t get_aspect() const;
  95. real_t get_fov() const;
  96. bool is_orthogonal() const;
  97. Array get_projection_planes(const Transform3D &p_transform) const;
  98. bool get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const;
  99. Vector2 get_viewport_half_extents() const;
  100. Vector2 get_far_plane_half_extents() const;
  101. void invert();
  102. Projection inverse() const;
  103. Projection operator*(const Projection &p_matrix) const;
  104. Plane xform4(const Plane &p_vec4) const;
  105. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec3) const;
  106. Vector4 xform(const Vector4 &p_vec4) const;
  107. Vector4 xform_inv(const Vector4 &p_vec4) const;
  108. operator String() const;
  109. void scale_translate_to_fit(const AABB &p_aabb);
  110. void add_jitter_offset(const Vector2 &p_offset);
  111. void make_scale(const Vector3 &p_scale);
  112. int get_pixels_per_meter(int p_for_pixel_width) const;
  113. operator Transform3D() const;
  114. void flip_y();
  115. bool operator==(const Projection &p_cam) const {
  116. for (uint32_t i = 0; i < 4; i++) {
  117. for (uint32_t j = 0; j < 4; j++) {
  118. if (matrix[i][j] != p_cam.matrix[i][j]) {
  119. return false;
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. bool operator!=(const Projection &p_cam) const {
  126. return !(*this == p_cam);
  127. }
  128. float get_lod_multiplier() const;
  129. Projection();
  130. Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w);
  131. Projection(const Transform3D &p_transform);
  132. ~Projection();
  133. };
  134. Vector3 Projection::xform(const Vector3 &p_vec3) const {
  135. Vector3 ret;
  136. ret.x = matrix[0][0] * p_vec3.x + matrix[1][0] * p_vec3.y + matrix[2][0] * p_vec3.z + matrix[3][0];
  137. ret.y = matrix[0][1] * p_vec3.x + matrix[1][1] * p_vec3.y + matrix[2][1] * p_vec3.z + matrix[3][1];
  138. ret.z = matrix[0][2] * p_vec3.x + matrix[1][2] * p_vec3.y + matrix[2][2] * p_vec3.z + matrix[3][2];
  139. real_t w = matrix[0][3] * p_vec3.x + matrix[1][3] * p_vec3.y + matrix[2][3] * p_vec3.z + matrix[3][3];
  140. return ret / w;
  141. }
  142. } // namespace godot
  143. #endif // GODOT_PROJECTION_HPP