| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/vector3.h" |
| 34 |
#include "core/math/vector4.h" |
| 35 |
|
| 36 |
template <typename T> |
| 37 |
class Vector; |
| 38 |
|
| 39 |
struct AABB; |
| 40 |
struct Plane; |
| 41 |
struct Rect2; |
| 42 |
struct Transform3D; |
| 43 |
struct Vector2; |
| 44 |
|
| 45 |
struct [[nodiscard]] Projection { |
| 46 |
enum Planes { |
| 47 |
PLANE_NEAR, |
| 48 |
PLANE_FAR, |
| 49 |
PLANE_LEFT, |
| 50 |
PLANE_TOP, |
| 51 |
PLANE_RIGHT, |
| 52 |
PLANE_BOTTOM |
| 53 |
}; |
| 54 |
|
| 55 |
Vector4 columns[4] = { |
| 56 |
{ 1, 0, 0, 0 }, |
| 57 |
{ 0, 1, 0, 0 }, |
| 58 |
{ 0, 0, 1, 0 }, |
| 59 |
{ 0, 0, 0, 1 }, |
| 60 |
}; |
| 61 |
|
| 62 |
constexpr const Vector4 &operator[](int p_axis) const { |
| 63 |
DEV_ASSERT((unsigned int)p_axis < 4); |
| 64 |
return columns[p_axis]; |
| 65 |
} |
| 66 |
|
| 67 |
constexpr Vector4 &operator[](int p_axis) { |
| 68 |
DEV_ASSERT((unsigned int)p_axis < 4); |
| 69 |
return columns[p_axis]; |
| 70 |
} |
| 71 |
|
| 72 |
real_t determinant() const; |
| 73 |
void set_identity(); |
| 74 |
void set_zero(); |
| 75 |
void set_light_bias(); |
| 76 |
void set_depth_correction(bool p_flip_y = true, bool p_reverse_z = true, bool p_remap_z = true); |
| 77 |
|
| 78 |
void set_light_atlas_rect(const Rect2 &p_rect); |
| 79 |
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); |
| 80 |
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); |
| 81 |
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); |
| 82 |
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); |
| 83 |
void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false); |
| 84 |
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); |
| 85 |
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); |
| 86 |
void adjust_perspective_znear(real_t p_new_znear); |
| 87 |
|
| 88 |
static Projection create_depth_correction(bool p_flip_y); |
| 89 |
static Projection create_light_atlas_rect(const Rect2 &p_rect); |
| 90 |
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); |
| 91 |
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); |
| 92 |
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); |
| 93 |
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); |
| 94 |
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); |
| 95 |
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); |
| 96 |
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); |
| 97 |
static Projection create_fit_aabb(const AABB &p_aabb); |
| 98 |
Projection perspective_znear_adjusted(real_t p_new_znear) const; |
| 99 |
Plane get_projection_plane(Planes p_plane) const; |
| 100 |
Projection flipped_y() const; |
| 101 |
Projection jitter_offseted(const Vector2 &p_offset) const; |
| 102 |
|
| 103 |
static real_t get_fovy(real_t p_fovx, real_t p_aspect) { |
| 104 |
return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5)) * 2.0); |
| 105 |
} |
| 106 |
|
| 107 |
real_t get_z_far() const; |
| 108 |
real_t get_z_near() const; |
| 109 |
real_t get_aspect() const; |
| 110 |
real_t get_fov() const; |
| 111 |
bool is_orthogonal() const; |
| 112 |
|
| 113 |
Vector<Plane> get_projection_planes(const Transform3D &p_transform) const; |
| 114 |
|
| 115 |
bool get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const; |
| 116 |
Vector2 get_viewport_half_extents() const; |
| 117 |
Vector2 get_far_plane_half_extents() const; |
| 118 |
|
| 119 |
void invert(); |
| 120 |
Projection inverse() const; |
| 121 |
|
| 122 |
constexpr Projection operator*(const Projection &p_matrix) const; |
| 123 |
|
| 124 |
Plane xform4(const Plane &p_vec4) const; |
| 125 |
_FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec3) const; |
| 126 |
|
| 127 |
Vector4 xform(const Vector4 &p_vec4) const; |
| 128 |
Vector4 xform_inv(const Vector4 &p_vec4) const; |
| 129 |
|
| 130 |
explicit operator String() const; |
| 131 |
|
| 132 |
void scale_translate_to_fit(const AABB &p_aabb); |
| 133 |
void add_jitter_offset(const Vector2 &p_offset); |
| 134 |
void make_scale(const Vector3 &p_scale); |
| 135 |
int get_pixels_per_meter(int p_for_pixel_width) const; |
| 136 |
operator Transform3D() const; |
| 137 |
|
| 138 |
void flip_y(); |
| 139 |
|
| 140 |
bool is_same(const Projection &p_cam) const; |
| 141 |
|
| 142 |
constexpr bool operator==(const Projection &p_cam) const { |
| 143 |
for (uint32_t i = 0; i < 4; i++) { |
| 144 |
for (uint32_t j = 0; j < 4; j++) { |
| 145 |
if (columns[i][j] != p_cam.columns[i][j]) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
constexpr bool operator!=(const Projection &p_cam) const { |
| 154 |
return !(*this == p_cam); |
| 155 |
} |
| 156 |
|
| 157 |
real_t get_lod_multiplier() const; |
| 158 |
|
| 159 |
Projection() = default; |
| 160 |
constexpr Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w) : |
| 161 |
columns{ p_x, p_y, p_z, p_w } {} |
| 162 |
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) : |
| 163 |
columns{ |
| 164 |
{ p_xx, p_xy, p_xz, p_xw }, |
| 165 |
{ p_yx, p_yy, p_yz, p_yw }, |
| 166 |
{ p_zx, p_zy, p_zz, p_zw }, |
| 167 |
{ p_wx, p_wy, p_wz, p_ww }, |
| 168 |
} {} |
| 169 |
Projection(const Transform3D &p_transform); |
| 170 |
}; |
| 171 |
|
| 172 |
constexpr Projection Projection::operator*(const Projection &p_matrix) const { |
| 173 |
Projection new_matrix; |
| 174 |
|
| 175 |
for (int j = 0; j < 4; j++) { |
| 176 |
for (int i = 0; i < 4; i++) { |
| 177 |
real_t ab = 0; |
| 178 |
for (int k = 0; k < 4; k++) { |
| 179 |
ab += columns[k][i] * p_matrix.columns[j][k]; |
| 180 |
} |
| 181 |
new_matrix.columns[j][i] = ab; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return new_matrix; |
| 186 |
} |
| 187 |
|
| 188 |
Vector3 Projection::xform(const Vector3 &p_vec3) const { |
| 189 |
Vector3 ret; |
| 190 |
ret.x = columns[0][0] * p_vec3.x + columns[1][0] * p_vec3.y + columns[2][0] * p_vec3.z + columns[3][0]; |
| 191 |
ret.y = columns[0][1] * p_vec3.x + columns[1][1] * p_vec3.y + columns[2][1] * p_vec3.z + columns[3][1]; |
| 192 |
ret.z = columns[0][2] * p_vec3.x + columns[1][2] * p_vec3.y + columns[2][2] * p_vec3.z + columns[3][2]; |
| 193 |
real_t w = columns[0][3] * p_vec3.x + columns[1][3] * p_vec3.y + columns[2][3] * p_vec3.z + columns[3][3]; |
| 194 |
return ret / w; |
| 195 |
} |