| 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 | |
| 31 | #ifndef PROJECTION_H |
| 32 | #define PROJECTION_H |
| 33 | |
| 34 | #include "core/math/vector3.h" |
| 35 | #include "core/math/vector4.h" |
| 36 | |
| 37 | template <class T> |
| 38 | class Vector; |
| 39 | |
| 40 | struct AABB; |
| 41 | struct Plane; |
| 42 | struct Rect2; |
| 43 | struct Transform3D; |
| 44 | struct Vector2; |
| 45 | |
| 46 | struct _NO_DISCARD_ Projection { |
| 47 | enum Planes { |
| 48 | PLANE_NEAR, |
| 49 | PLANE_FAR, |
| 50 | PLANE_LEFT, |
| 51 | PLANE_TOP, |
| 52 | PLANE_RIGHT, |
| 53 | PLANE_BOTTOM |
| 54 | }; |
| 55 | |
| 56 | Vector4 columns[4]; |
| 57 | |
| 58 | _FORCE_INLINE_ const Vector4 &operator[](const int p_axis) const { |
| 59 | DEV_ASSERT((unsigned int)p_axis < 4); |
| 60 | return columns[p_axis]; |
| 61 | } |
| 62 | |
| 63 | _FORCE_INLINE_ Vector4 &operator[](const int p_axis) { |
| 64 | DEV_ASSERT((unsigned int)p_axis < 4); |
| 65 | return columns[p_axis]; |
| 66 | } |
| 67 | |
| 68 | float determinant() const; |
| 69 | void set_identity(); |
| 70 | void set_zero(); |
| 71 | void set_light_bias(); |
| 72 | void set_depth_correction(bool p_flip_y = true); |
| 73 | |
| 74 | void set_light_atlas_rect(const Rect2 &p_rect); |
| 75 | 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); |
| 76 | 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); |
| 77 | 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); |
| 78 | 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); |
| 79 | void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false); |
| 80 | 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); |
| 81 | 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); |
| 82 | void adjust_perspective_znear(real_t p_new_znear); |
| 83 | |
| 84 | static Projection create_depth_correction(bool p_flip_y); |
| 85 | static Projection create_light_atlas_rect(const Rect2 &p_rect); |
| 86 | 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); |
| 87 | 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); |
| 88 | 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); |
| 89 | 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); |
| 90 | 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); |
| 91 | 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); |
| 92 | 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); |
| 93 | static Projection create_fit_aabb(const AABB &p_aabb); |
| 94 | Projection perspective_znear_adjusted(real_t p_new_znear) const; |
| 95 | Plane get_projection_plane(Planes p_plane) const; |
| 96 | Projection flipped_y() const; |
| 97 | Projection jitter_offseted(const Vector2 &p_offset) const; |
| 98 | |
| 99 | static real_t get_fovy(real_t p_fovx, real_t p_aspect) { |
| 100 | return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5)) * 2.0); |
| 101 | } |
| 102 | |
| 103 | real_t get_z_far() const; |
| 104 | real_t get_z_near() const; |
| 105 | real_t get_aspect() const; |
| 106 | real_t get_fov() const; |
| 107 | bool is_orthogonal() const; |
| 108 | |
| 109 | Vector<Plane> get_projection_planes(const Transform3D &p_transform) const; |
| 110 | |
| 111 | bool get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const; |
| 112 | Vector2 get_viewport_half_extents() const; |
| 113 | Vector2 get_far_plane_half_extents() const; |
| 114 | |
| 115 | void invert(); |
| 116 | Projection inverse() const; |
| 117 | |
| 118 | Projection operator*(const Projection &p_matrix) const; |
| 119 | |
| 120 | Plane xform4(const Plane &p_vec4) const; |
| 121 | _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec3) const; |
| 122 | |
| 123 | Vector4 xform(const Vector4 &p_vec4) const; |
| 124 | Vector4 xform_inv(const Vector4 &p_vec4) const; |
| 125 | |
| 126 | operator String() const; |
| 127 | |
| 128 | void scale_translate_to_fit(const AABB &p_aabb); |
| 129 | void add_jitter_offset(const Vector2 &p_offset); |
| 130 | void make_scale(const Vector3 &p_scale); |
| 131 | int get_pixels_per_meter(int p_for_pixel_width) const; |
| 132 | operator Transform3D() const; |
| 133 | |
| 134 | void flip_y(); |
| 135 | |
| 136 | bool operator==(const Projection &p_cam) const { |
| 137 | for (uint32_t i = 0; i < 4; i++) { |
| 138 | for (uint32_t j = 0; j < 4; j++) { |
| 139 | if (columns[i][j] != p_cam.columns[i][j]) { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | bool operator!=(const Projection &p_cam) const { |
| 148 | return !(*this == p_cam); |
| 149 | } |
| 150 | |
| 151 | float get_lod_multiplier() const; |
| 152 | |
| 153 | Projection(); |
| 154 | Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w); |
| 155 | Projection(const Transform3D &p_transform); |
| 156 | ~Projection(); |
| 157 | }; |
| 158 | |
| 159 | Vector3 Projection::xform(const Vector3 &p_vec3) const { |
| 160 | Vector3 ret; |
| 161 | ret.x = columns[0][0] * p_vec3.x + columns[1][0] * p_vec3.y + columns[2][0] * p_vec3.z + columns[3][0]; |
| 162 | ret.y = columns[0][1] * p_vec3.x + columns[1][1] * p_vec3.y + columns[2][1] * p_vec3.z + columns[3][1]; |
| 163 | ret.z = columns[0][2] * p_vec3.x + columns[1][2] * p_vec3.y + columns[2][2] * p_vec3.z + columns[3][2]; |
| 164 | real_t w = columns[0][3] * p_vec3.x + columns[1][3] * p_vec3.y + columns[2][3] * p_vec3.z + columns[3][3]; |
| 165 | return ret / w; |
| 166 | } |
| 167 | |
| 168 | #endif // PROJECTION_H |
| 169 | |