| 1 | /**************************************************************************/ |
| 2 | /* projection.cpp */ |
| 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 | #include "projection.h" |
| 32 | |
| 33 | #include "core/math/aabb.h" |
| 34 | #include "core/math/math_funcs.h" |
| 35 | #include "core/math/plane.h" |
| 36 | #include "core/math/rect2.h" |
| 37 | #include "core/math/transform_3d.h" |
| 38 | #include "core/string/ustring.h" |
| 39 | |
| 40 | real_t Projection::determinant() const { |
| 41 | return columns[0][3] * columns[1][2] * columns[2][1] * columns[3][0] - columns[0][2] * columns[1][3] * columns[2][1] * columns[3][0] - |
| 42 | columns[0][3] * columns[1][1] * columns[2][2] * columns[3][0] + columns[0][1] * columns[1][3] * columns[2][2] * columns[3][0] + |
| 43 | columns[0][2] * columns[1][1] * columns[2][3] * columns[3][0] - columns[0][1] * columns[1][2] * columns[2][3] * columns[3][0] - |
| 44 | columns[0][3] * columns[1][2] * columns[2][0] * columns[3][1] + columns[0][2] * columns[1][3] * columns[2][0] * columns[3][1] + |
| 45 | columns[0][3] * columns[1][0] * columns[2][2] * columns[3][1] - columns[0][0] * columns[1][3] * columns[2][2] * columns[3][1] - |
| 46 | columns[0][2] * columns[1][0] * columns[2][3] * columns[3][1] + columns[0][0] * columns[1][2] * columns[2][3] * columns[3][1] + |
| 47 | columns[0][3] * columns[1][1] * columns[2][0] * columns[3][2] - columns[0][1] * columns[1][3] * columns[2][0] * columns[3][2] - |
| 48 | columns[0][3] * columns[1][0] * columns[2][1] * columns[3][2] + columns[0][0] * columns[1][3] * columns[2][1] * columns[3][2] + |
| 49 | columns[0][1] * columns[1][0] * columns[2][3] * columns[3][2] - columns[0][0] * columns[1][1] * columns[2][3] * columns[3][2] - |
| 50 | columns[0][2] * columns[1][1] * columns[2][0] * columns[3][3] + columns[0][1] * columns[1][2] * columns[2][0] * columns[3][3] + |
| 51 | columns[0][2] * columns[1][0] * columns[2][1] * columns[3][3] - columns[0][0] * columns[1][2] * columns[2][1] * columns[3][3] - |
| 52 | columns[0][1] * columns[1][0] * columns[2][2] * columns[3][3] + columns[0][0] * columns[1][1] * columns[2][2] * columns[3][3]; |
| 53 | } |
| 54 | |
| 55 | void Projection::set_identity() { |
| 56 | for (int i = 0; i < 4; i++) { |
| 57 | for (int j = 0; j < 4; j++) { |
| 58 | columns[i][j] = (i == j) ? 1 : 0; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Projection::set_zero() { |
| 64 | for (int i = 0; i < 4; i++) { |
| 65 | for (int j = 0; j < 4; j++) { |
| 66 | columns[i][j] = 0; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Plane Projection::xform4(const Plane &p_vec4) const { |
| 72 | Plane ret; |
| 73 | |
| 74 | ret.normal.x = columns[0][0] * p_vec4.normal.x + columns[1][0] * p_vec4.normal.y + columns[2][0] * p_vec4.normal.z + columns[3][0] * p_vec4.d; |
| 75 | ret.normal.y = columns[0][1] * p_vec4.normal.x + columns[1][1] * p_vec4.normal.y + columns[2][1] * p_vec4.normal.z + columns[3][1] * p_vec4.d; |
| 76 | ret.normal.z = columns[0][2] * p_vec4.normal.x + columns[1][2] * p_vec4.normal.y + columns[2][2] * p_vec4.normal.z + columns[3][2] * p_vec4.d; |
| 77 | ret.d = columns[0][3] * p_vec4.normal.x + columns[1][3] * p_vec4.normal.y + columns[2][3] * p_vec4.normal.z + columns[3][3] * p_vec4.d; |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | Vector4 Projection::xform(const Vector4 &p_vec4) const { |
| 82 | return Vector4( |
| 83 | columns[0][0] * p_vec4.x + columns[1][0] * p_vec4.y + columns[2][0] * p_vec4.z + columns[3][0] * p_vec4.w, |
| 84 | columns[0][1] * p_vec4.x + columns[1][1] * p_vec4.y + columns[2][1] * p_vec4.z + columns[3][1] * p_vec4.w, |
| 85 | columns[0][2] * p_vec4.x + columns[1][2] * p_vec4.y + columns[2][2] * p_vec4.z + columns[3][2] * p_vec4.w, |
| 86 | columns[0][3] * p_vec4.x + columns[1][3] * p_vec4.y + columns[2][3] * p_vec4.z + columns[3][3] * p_vec4.w); |
| 87 | } |
| 88 | Vector4 Projection::xform_inv(const Vector4 &p_vec4) const { |
| 89 | return Vector4( |
| 90 | columns[0][0] * p_vec4.x + columns[0][1] * p_vec4.y + columns[0][2] * p_vec4.z + columns[0][3] * p_vec4.w, |
| 91 | columns[1][0] * p_vec4.x + columns[1][1] * p_vec4.y + columns[1][2] * p_vec4.z + columns[1][3] * p_vec4.w, |
| 92 | columns[2][0] * p_vec4.x + columns[2][1] * p_vec4.y + columns[2][2] * p_vec4.z + columns[2][3] * p_vec4.w, |
| 93 | columns[3][0] * p_vec4.x + columns[3][1] * p_vec4.y + columns[3][2] * p_vec4.z + columns[3][3] * p_vec4.w); |
| 94 | } |
| 95 | |
| 96 | void Projection::adjust_perspective_znear(real_t p_new_znear) { |
| 97 | real_t zfar = get_z_far(); |
| 98 | real_t znear = p_new_znear; |
| 99 | |
| 100 | real_t deltaZ = zfar - znear; |
| 101 | columns[2][2] = -(zfar + znear) / deltaZ; |
| 102 | columns[3][2] = -2 * znear * zfar / deltaZ; |
| 103 | } |
| 104 | |
| 105 | Projection Projection::create_depth_correction(bool p_flip_y) { |
| 106 | Projection proj; |
| 107 | proj.set_depth_correction(p_flip_y); |
| 108 | return proj; |
| 109 | } |
| 110 | |
| 111 | Projection Projection::create_light_atlas_rect(const Rect2 &p_rect) { |
| 112 | Projection proj; |
| 113 | proj.set_light_atlas_rect(p_rect); |
| 114 | return proj; |
| 115 | } |
| 116 | |
| 117 | Projection 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) { |
| 118 | Projection proj; |
| 119 | proj.set_perspective(p_fovy_degrees, p_aspect, p_z_near, p_z_far, p_flip_fov); |
| 120 | return proj; |
| 121 | } |
| 122 | |
| 123 | Projection 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) { |
| 124 | Projection proj; |
| 125 | proj.set_perspective(p_fovy_degrees, p_aspect, p_z_near, p_z_far, p_flip_fov, p_eye, p_intraocular_dist, p_convergence_dist); |
| 126 | return proj; |
| 127 | } |
| 128 | |
| 129 | Projection 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) { |
| 130 | Projection proj; |
| 131 | proj.set_for_hmd(p_eye, p_aspect, p_intraocular_dist, p_display_width, p_display_to_lens, p_oversample, p_z_near, p_z_far); |
| 132 | return proj; |
| 133 | } |
| 134 | |
| 135 | Projection 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) { |
| 136 | Projection proj; |
| 137 | proj.set_orthogonal(p_left, p_right, p_bottom, p_top, p_znear, p_zfar); |
| 138 | return proj; |
| 139 | } |
| 140 | |
| 141 | Projection Projection::create_orthogonal_aspect(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) { |
| 142 | Projection proj; |
| 143 | proj.set_orthogonal(p_size, p_aspect, p_znear, p_zfar, p_flip_fov); |
| 144 | return proj; |
| 145 | } |
| 146 | |
| 147 | Projection 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) { |
| 148 | Projection proj; |
| 149 | proj.set_frustum(p_left, p_right, p_bottom, p_top, p_near, p_far); |
| 150 | return proj; |
| 151 | } |
| 152 | |
| 153 | Projection 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) { |
| 154 | Projection proj; |
| 155 | proj.set_frustum(p_size, p_aspect, p_offset, p_near, p_far, p_flip_fov); |
| 156 | return proj; |
| 157 | } |
| 158 | |
| 159 | Projection Projection::create_fit_aabb(const AABB &p_aabb) { |
| 160 | Projection proj; |
| 161 | proj.scale_translate_to_fit(p_aabb); |
| 162 | return proj; |
| 163 | } |
| 164 | |
| 165 | Projection Projection::perspective_znear_adjusted(real_t p_new_znear) const { |
| 166 | Projection proj = *this; |
| 167 | proj.adjust_perspective_znear(p_new_znear); |
| 168 | return proj; |
| 169 | } |
| 170 | |
| 171 | Plane Projection::get_projection_plane(Planes p_plane) const { |
| 172 | const real_t *matrix = (const real_t *)columns; |
| 173 | |
| 174 | switch (p_plane) { |
| 175 | case PLANE_NEAR: { |
| 176 | Plane new_plane = Plane(matrix[3] + matrix[2], |
| 177 | matrix[7] + matrix[6], |
| 178 | matrix[11] + matrix[10], |
| 179 | matrix[15] + matrix[14]); |
| 180 | |
| 181 | new_plane.normal = -new_plane.normal; |
| 182 | new_plane.normalize(); |
| 183 | return new_plane; |
| 184 | } |
| 185 | case PLANE_FAR: { |
| 186 | Plane new_plane = Plane(matrix[3] - matrix[2], |
| 187 | matrix[7] - matrix[6], |
| 188 | matrix[11] - matrix[10], |
| 189 | matrix[15] - matrix[14]); |
| 190 | |
| 191 | new_plane.normal = -new_plane.normal; |
| 192 | new_plane.normalize(); |
| 193 | return new_plane; |
| 194 | } |
| 195 | case PLANE_LEFT: { |
| 196 | Plane new_plane = Plane(matrix[3] + matrix[0], |
| 197 | matrix[7] + matrix[4], |
| 198 | matrix[11] + matrix[8], |
| 199 | matrix[15] + matrix[12]); |
| 200 | |
| 201 | new_plane.normal = -new_plane.normal; |
| 202 | new_plane.normalize(); |
| 203 | return new_plane; |
| 204 | } |
| 205 | case PLANE_TOP: { |
| 206 | Plane new_plane = Plane(matrix[3] - matrix[1], |
| 207 | matrix[7] - matrix[5], |
| 208 | matrix[11] - matrix[9], |
| 209 | matrix[15] - matrix[13]); |
| 210 | |
| 211 | new_plane.normal = -new_plane.normal; |
| 212 | new_plane.normalize(); |
| 213 | return new_plane; |
| 214 | } |
| 215 | case PLANE_RIGHT: { |
| 216 | Plane new_plane = Plane(matrix[3] - matrix[0], |
| 217 | matrix[7] - matrix[4], |
| 218 | matrix[11] - matrix[8], |
| 219 | matrix[15] - matrix[12]); |
| 220 | |
| 221 | new_plane.normal = -new_plane.normal; |
| 222 | new_plane.normalize(); |
| 223 | return new_plane; |
| 224 | } |
| 225 | case PLANE_BOTTOM: { |
| 226 | Plane new_plane = Plane(matrix[3] + matrix[1], |
| 227 | matrix[7] + matrix[5], |
| 228 | matrix[11] + matrix[9], |
| 229 | matrix[15] + matrix[13]); |
| 230 | |
| 231 | new_plane.normal = -new_plane.normal; |
| 232 | new_plane.normalize(); |
| 233 | return new_plane; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return Plane(); |
| 238 | } |
| 239 | |
| 240 | Projection Projection::flipped_y() const { |
| 241 | Projection proj = *this; |
| 242 | proj.flip_y(); |
| 243 | return proj; |
| 244 | } |
| 245 | |
| 246 | Projection Projection ::jitter_offseted(const Vector2 &p_offset) const { |
| 247 | Projection proj = *this; |
| 248 | proj.add_jitter_offset(p_offset); |
| 249 | return proj; |
| 250 | } |
| 251 | |
| 252 | void Projection::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) { |
| 253 | if (p_flip_fov) { |
| 254 | p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect); |
| 255 | } |
| 256 | |
| 257 | real_t sine, cotangent, deltaZ; |
| 258 | real_t radians = Math::deg_to_rad(p_fovy_degrees / 2.0); |
| 259 | |
| 260 | deltaZ = p_z_far - p_z_near; |
| 261 | sine = Math::sin(radians); |
| 262 | |
| 263 | if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) { |
| 264 | return; |
| 265 | } |
| 266 | cotangent = Math::cos(radians) / sine; |
| 267 | |
| 268 | set_identity(); |
| 269 | |
| 270 | columns[0][0] = cotangent / p_aspect; |
| 271 | columns[1][1] = cotangent; |
| 272 | columns[2][2] = -(p_z_far + p_z_near) / deltaZ; |
| 273 | columns[2][3] = -1; |
| 274 | columns[3][2] = -2 * p_z_near * p_z_far / deltaZ; |
| 275 | columns[3][3] = 0; |
| 276 | } |
| 277 | |
| 278 | void Projection::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) { |
| 279 | if (p_flip_fov) { |
| 280 | p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect); |
| 281 | } |
| 282 | |
| 283 | real_t left, right, modeltranslation, ymax, xmax, frustumshift; |
| 284 | |
| 285 | ymax = p_z_near * std::tan(Math::deg_to_rad(p_fovy_degrees / 2.0)); |
| 286 | xmax = ymax * p_aspect; |
| 287 | frustumshift = (p_intraocular_dist / 2.0) * p_z_near / p_convergence_dist; |
| 288 | |
| 289 | switch (p_eye) { |
| 290 | case 1: { // left eye |
| 291 | left = -xmax + frustumshift; |
| 292 | right = xmax + frustumshift; |
| 293 | modeltranslation = p_intraocular_dist / 2.0; |
| 294 | } break; |
| 295 | case 2: { // right eye |
| 296 | left = -xmax - frustumshift; |
| 297 | right = xmax - frustumshift; |
| 298 | modeltranslation = -p_intraocular_dist / 2.0; |
| 299 | } break; |
| 300 | default: { // mono, should give the same result as set_perspective(p_fovy_degrees,p_aspect,p_z_near,p_z_far,p_flip_fov) |
| 301 | left = -xmax; |
| 302 | right = xmax; |
| 303 | modeltranslation = 0.0; |
| 304 | } break; |
| 305 | } |
| 306 | |
| 307 | set_frustum(left, right, -ymax, ymax, p_z_near, p_z_far); |
| 308 | |
| 309 | // translate matrix by (modeltranslation, 0.0, 0.0) |
| 310 | Projection cm; |
| 311 | cm.set_identity(); |
| 312 | cm.columns[3][0] = modeltranslation; |
| 313 | *this = *this * cm; |
| 314 | } |
| 315 | |
| 316 | void Projection::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) { |
| 317 | // we first calculate our base frustum on our values without taking our lens magnification into account. |
| 318 | real_t f1 = (p_intraocular_dist * 0.5) / p_display_to_lens; |
| 319 | real_t f2 = ((p_display_width - p_intraocular_dist) * 0.5) / p_display_to_lens; |
| 320 | real_t f3 = (p_display_width / 4.0) / p_display_to_lens; |
| 321 | |
| 322 | // now we apply our oversample factor to increase our FOV. how much we oversample is always a balance we strike between performance and how much |
| 323 | // we're willing to sacrifice in FOV. |
| 324 | real_t add = ((f1 + f2) * (p_oversample - 1.0)) / 2.0; |
| 325 | f1 += add; |
| 326 | f2 += add; |
| 327 | f3 *= p_oversample; |
| 328 | |
| 329 | // always apply KEEP_WIDTH aspect ratio |
| 330 | f3 /= p_aspect; |
| 331 | |
| 332 | switch (p_eye) { |
| 333 | case 1: { // left eye |
| 334 | set_frustum(-f2 * p_z_near, f1 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far); |
| 335 | } break; |
| 336 | case 2: { // right eye |
| 337 | set_frustum(-f1 * p_z_near, f2 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far); |
| 338 | } break; |
| 339 | default: { // mono, does not apply here! |
| 340 | } break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void Projection::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) { |
| 345 | set_identity(); |
| 346 | |
| 347 | columns[0][0] = 2.0 / (p_right - p_left); |
| 348 | columns[3][0] = -((p_right + p_left) / (p_right - p_left)); |
| 349 | columns[1][1] = 2.0 / (p_top - p_bottom); |
| 350 | columns[3][1] = -((p_top + p_bottom) / (p_top - p_bottom)); |
| 351 | columns[2][2] = -2.0 / (p_zfar - p_znear); |
| 352 | columns[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear)); |
| 353 | columns[3][3] = 1.0; |
| 354 | } |
| 355 | |
| 356 | void Projection::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) { |
| 357 | if (!p_flip_fov) { |
| 358 | p_size *= p_aspect; |
| 359 | } |
| 360 | |
| 361 | set_orthogonal(-p_size / 2, +p_size / 2, -p_size / p_aspect / 2, +p_size / p_aspect / 2, p_znear, p_zfar); |
| 362 | } |
| 363 | |
| 364 | void Projection::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) { |
| 365 | ERR_FAIL_COND(p_right <= p_left); |
| 366 | ERR_FAIL_COND(p_top <= p_bottom); |
| 367 | ERR_FAIL_COND(p_far <= p_near); |
| 368 | |
| 369 | real_t *te = &columns[0][0]; |
| 370 | real_t x = 2 * p_near / (p_right - p_left); |
| 371 | real_t y = 2 * p_near / (p_top - p_bottom); |
| 372 | |
| 373 | real_t a = (p_right + p_left) / (p_right - p_left); |
| 374 | real_t b = (p_top + p_bottom) / (p_top - p_bottom); |
| 375 | real_t c = -(p_far + p_near) / (p_far - p_near); |
| 376 | real_t d = -2 * p_far * p_near / (p_far - p_near); |
| 377 | |
| 378 | te[0] = x; |
| 379 | te[1] = 0; |
| 380 | te[2] = 0; |
| 381 | te[3] = 0; |
| 382 | te[4] = 0; |
| 383 | te[5] = y; |
| 384 | te[6] = 0; |
| 385 | te[7] = 0; |
| 386 | te[8] = a; |
| 387 | te[9] = b; |
| 388 | te[10] = c; |
| 389 | te[11] = -1; |
| 390 | te[12] = 0; |
| 391 | te[13] = 0; |
| 392 | te[14] = d; |
| 393 | te[15] = 0; |
| 394 | } |
| 395 | |
| 396 | void Projection::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) { |
| 397 | if (!p_flip_fov) { |
| 398 | p_size *= p_aspect; |
| 399 | } |
| 400 | |
| 401 | set_frustum(-p_size / 2 + p_offset.x, +p_size / 2 + p_offset.x, -p_size / p_aspect / 2 + p_offset.y, +p_size / p_aspect / 2 + p_offset.y, p_near, p_far); |
| 402 | } |
| 403 | |
| 404 | real_t Projection::get_z_far() const { |
| 405 | // NOTE: This assumes z-facing near and far planes, i.e. that : |
| 406 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 407 | // - near and far planes are z-facing (i.e. columns[0][2] and [1][2] == 0) |
| 408 | return (columns[3][3] - columns[3][2]) / (columns[2][3] - columns[2][2]); |
| 409 | } |
| 410 | |
| 411 | real_t Projection::get_z_near() const { |
| 412 | // NOTE: This assumes z-facing near and far planes, i.e. that : |
| 413 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 414 | // - near and far planes are z-facing (i.e. columns[0][2] and [1][2] == 0) |
| 415 | return (columns[3][3] + columns[3][2]) / (columns[2][3] + columns[2][2]); |
| 416 | } |
| 417 | |
| 418 | Vector2 Projection::get_viewport_half_extents() const { |
| 419 | // NOTE: This assumes a symmetrical frustum, i.e. that : |
| 420 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 421 | // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0) |
| 422 | // - there is no offset / skew (i.e. columns[2][0] == columns[2][1] == 0) |
| 423 | real_t w = -get_z_near() * columns[2][3] + columns[3][3]; |
| 424 | return Vector2(w / columns[0][0], w / columns[1][1]); |
| 425 | } |
| 426 | |
| 427 | Vector2 Projection::get_far_plane_half_extents() const { |
| 428 | // NOTE: This assumes a symmetrical frustum, i.e. that : |
| 429 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 430 | // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0) |
| 431 | // - there is no offset / skew (i.e. columns[2][0] == columns[2][1] == 0) |
| 432 | real_t w = -get_z_far() * columns[2][3] + columns[3][3]; |
| 433 | return Vector2(w / columns[0][0], w / columns[1][1]); |
| 434 | } |
| 435 | |
| 436 | bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const { |
| 437 | Vector<Plane> planes = get_projection_planes(Transform3D()); |
| 438 | const Planes intersections[8][3] = { |
| 439 | { PLANE_FAR, PLANE_LEFT, PLANE_TOP }, |
| 440 | { PLANE_FAR, PLANE_LEFT, PLANE_BOTTOM }, |
| 441 | { PLANE_FAR, PLANE_RIGHT, PLANE_TOP }, |
| 442 | { PLANE_FAR, PLANE_RIGHT, PLANE_BOTTOM }, |
| 443 | { PLANE_NEAR, PLANE_LEFT, PLANE_TOP }, |
| 444 | { PLANE_NEAR, PLANE_LEFT, PLANE_BOTTOM }, |
| 445 | { PLANE_NEAR, PLANE_RIGHT, PLANE_TOP }, |
| 446 | { PLANE_NEAR, PLANE_RIGHT, PLANE_BOTTOM }, |
| 447 | }; |
| 448 | |
| 449 | for (int i = 0; i < 8; i++) { |
| 450 | Vector3 point; |
| 451 | Plane a = planes[intersections[i][0]]; |
| 452 | Plane b = planes[intersections[i][1]]; |
| 453 | Plane c = planes[intersections[i][2]]; |
| 454 | bool res = a.intersect_3(b, c, &point); |
| 455 | ERR_FAIL_COND_V(!res, false); |
| 456 | p_8points[i] = p_transform.xform(point); |
| 457 | } |
| 458 | |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | Vector<Plane> Projection::get_projection_planes(const Transform3D &p_transform) const { |
| 463 | /** Fast Plane Extraction from combined modelview/projection matrices. |
| 464 | * References: |
| 465 | * https://web.archive.org/web/20011221205252/https://www.markmorley.com/opengl/frustumculling.html |
| 466 | * https://web.archive.org/web/20061020020112/https://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf |
| 467 | */ |
| 468 | |
| 469 | Vector<Plane> planes; |
| 470 | planes.resize(6); |
| 471 | |
| 472 | const real_t *matrix = (const real_t *)columns; |
| 473 | |
| 474 | Plane new_plane; |
| 475 | |
| 476 | ///////--- Near Plane ---/////// |
| 477 | new_plane = Plane(matrix[3] + matrix[2], |
| 478 | matrix[7] + matrix[6], |
| 479 | matrix[11] + matrix[10], |
| 480 | matrix[15] + matrix[14]); |
| 481 | |
| 482 | new_plane.normal = -new_plane.normal; |
| 483 | new_plane.normalize(); |
| 484 | |
| 485 | planes.write[0] = p_transform.xform(new_plane); |
| 486 | |
| 487 | ///////--- Far Plane ---/////// |
| 488 | new_plane = Plane(matrix[3] - matrix[2], |
| 489 | matrix[7] - matrix[6], |
| 490 | matrix[11] - matrix[10], |
| 491 | matrix[15] - matrix[14]); |
| 492 | |
| 493 | new_plane.normal = -new_plane.normal; |
| 494 | new_plane.normalize(); |
| 495 | |
| 496 | planes.write[1] = p_transform.xform(new_plane); |
| 497 | |
| 498 | ///////--- Left Plane ---/////// |
| 499 | new_plane = Plane(matrix[3] + matrix[0], |
| 500 | matrix[7] + matrix[4], |
| 501 | matrix[11] + matrix[8], |
| 502 | matrix[15] + matrix[12]); |
| 503 | |
| 504 | new_plane.normal = -new_plane.normal; |
| 505 | new_plane.normalize(); |
| 506 | |
| 507 | planes.write[2] = p_transform.xform(new_plane); |
| 508 | |
| 509 | ///////--- Top Plane ---/////// |
| 510 | new_plane = Plane(matrix[3] - matrix[1], |
| 511 | matrix[7] - matrix[5], |
| 512 | matrix[11] - matrix[9], |
| 513 | matrix[15] - matrix[13]); |
| 514 | |
| 515 | new_plane.normal = -new_plane.normal; |
| 516 | new_plane.normalize(); |
| 517 | |
| 518 | planes.write[3] = p_transform.xform(new_plane); |
| 519 | |
| 520 | ///////--- Right Plane ---/////// |
| 521 | new_plane = Plane(matrix[3] - matrix[0], |
| 522 | matrix[7] - matrix[4], |
| 523 | matrix[11] - matrix[8], |
| 524 | matrix[15] - matrix[12]); |
| 525 | |
| 526 | new_plane.normal = -new_plane.normal; |
| 527 | new_plane.normalize(); |
| 528 | |
| 529 | planes.write[4] = p_transform.xform(new_plane); |
| 530 | |
| 531 | ///////--- Bottom Plane ---/////// |
| 532 | new_plane = Plane(matrix[3] + matrix[1], |
| 533 | matrix[7] + matrix[5], |
| 534 | matrix[11] + matrix[9], |
| 535 | matrix[15] + matrix[13]); |
| 536 | |
| 537 | new_plane.normal = -new_plane.normal; |
| 538 | new_plane.normalize(); |
| 539 | |
| 540 | planes.write[5] = p_transform.xform(new_plane); |
| 541 | |
| 542 | return planes; |
| 543 | } |
| 544 | |
| 545 | Projection Projection::inverse() const { |
| 546 | Projection cm = *this; |
| 547 | cm.invert(); |
| 548 | return cm; |
| 549 | } |
| 550 | |
| 551 | void Projection::invert() { |
| 552 | // Adapted from Mesa's `src/util/u_math.c` `util_invert_mat4x4`. |
| 553 | // MIT licensed. Copyright 2008 VMware, Inc. Authored by Jacques Leroy. |
| 554 | Projection temp; |
| 555 | real_t *out = (real_t *)temp.columns; |
| 556 | real_t *m = (real_t *)columns; |
| 557 | |
| 558 | real_t wtmp[4][8]; |
| 559 | real_t m0, m1, m2, m3, s; |
| 560 | real_t *r0, *r1, *r2, *r3; |
| 561 | |
| 562 | #define MAT(m, r, c) (m)[(c) * 4 + (r)] |
| 563 | |
| 564 | r0 = wtmp[0]; |
| 565 | r1 = wtmp[1]; |
| 566 | r2 = wtmp[2]; |
| 567 | r3 = wtmp[3]; |
| 568 | |
| 569 | r0[0] = MAT(m, 0, 0); |
| 570 | r0[1] = MAT(m, 0, 1); |
| 571 | r0[2] = MAT(m, 0, 2); |
| 572 | r0[3] = MAT(m, 0, 3); |
| 573 | r0[4] = 1.0; |
| 574 | r0[5] = 0.0; |
| 575 | r0[6] = 0.0; |
| 576 | r0[7] = 0.0; |
| 577 | |
| 578 | r1[0] = MAT(m, 1, 0); |
| 579 | r1[1] = MAT(m, 1, 1); |
| 580 | r1[2] = MAT(m, 1, 2); |
| 581 | r1[3] = MAT(m, 1, 3); |
| 582 | r1[5] = 1.0; |
| 583 | r1[4] = 0.0; |
| 584 | r1[6] = 0.0; |
| 585 | r1[7] = 0.0; |
| 586 | |
| 587 | r2[0] = MAT(m, 2, 0); |
| 588 | r2[1] = MAT(m, 2, 1); |
| 589 | r2[2] = MAT(m, 2, 2); |
| 590 | r2[3] = MAT(m, 2, 3); |
| 591 | r2[6] = 1.0; |
| 592 | r2[4] = 0.0; |
| 593 | r2[5] = 0.0; |
| 594 | r2[7] = 0.0; |
| 595 | |
| 596 | r3[0] = MAT(m, 3, 0); |
| 597 | r3[1] = MAT(m, 3, 1); |
| 598 | r3[2] = MAT(m, 3, 2); |
| 599 | r3[3] = MAT(m, 3, 3); |
| 600 | |
| 601 | r3[7] = 1.0; |
| 602 | r3[4] = 0.0; |
| 603 | r3[5] = 0.0; |
| 604 | r3[6] = 0.0; |
| 605 | |
| 606 | /* choose pivot - or die */ |
| 607 | if (Math::abs(r3[0]) > Math::abs(r2[0])) { |
| 608 | SWAP(r3, r2); |
| 609 | } |
| 610 | if (Math::abs(r2[0]) > Math::abs(r1[0])) { |
| 611 | SWAP(r2, r1); |
| 612 | } |
| 613 | if (Math::abs(r1[0]) > Math::abs(r0[0])) { |
| 614 | SWAP(r1, r0); |
| 615 | } |
| 616 | ERR_FAIL_COND(0.0 == r0[0]); |
| 617 | |
| 618 | /* eliminate first variable */ |
| 619 | m1 = r1[0] / r0[0]; |
| 620 | m2 = r2[0] / r0[0]; |
| 621 | m3 = r3[0] / r0[0]; |
| 622 | s = r0[1]; |
| 623 | r1[1] -= m1 * s; |
| 624 | r2[1] -= m2 * s; |
| 625 | r3[1] -= m3 * s; |
| 626 | s = r0[2]; |
| 627 | r1[2] -= m1 * s; |
| 628 | r2[2] -= m2 * s; |
| 629 | r3[2] -= m3 * s; |
| 630 | s = r0[3]; |
| 631 | r1[3] -= m1 * s; |
| 632 | r2[3] -= m2 * s; |
| 633 | r3[3] -= m3 * s; |
| 634 | s = r0[4]; |
| 635 | if (s != 0.0) { |
| 636 | r1[4] -= m1 * s; |
| 637 | r2[4] -= m2 * s; |
| 638 | r3[4] -= m3 * s; |
| 639 | } |
| 640 | s = r0[5]; |
| 641 | if (s != 0.0) { |
| 642 | r1[5] -= m1 * s; |
| 643 | r2[5] -= m2 * s; |
| 644 | r3[5] -= m3 * s; |
| 645 | } |
| 646 | s = r0[6]; |
| 647 | if (s != 0.0) { |
| 648 | r1[6] -= m1 * s; |
| 649 | r2[6] -= m2 * s; |
| 650 | r3[6] -= m3 * s; |
| 651 | } |
| 652 | s = r0[7]; |
| 653 | if (s != 0.0) { |
| 654 | r1[7] -= m1 * s; |
| 655 | r2[7] -= m2 * s; |
| 656 | r3[7] -= m3 * s; |
| 657 | } |
| 658 | |
| 659 | /* choose pivot - or die */ |
| 660 | if (Math::abs(r3[1]) > Math::abs(r2[1])) { |
| 661 | SWAP(r3, r2); |
| 662 | } |
| 663 | if (Math::abs(r2[1]) > Math::abs(r1[1])) { |
| 664 | SWAP(r2, r1); |
| 665 | } |
| 666 | ERR_FAIL_COND(0.0 == r1[1]); |
| 667 | |
| 668 | /* eliminate second variable */ |
| 669 | m2 = r2[1] / r1[1]; |
| 670 | m3 = r3[1] / r1[1]; |
| 671 | r2[2] -= m2 * r1[2]; |
| 672 | r3[2] -= m3 * r1[2]; |
| 673 | r2[3] -= m2 * r1[3]; |
| 674 | r3[3] -= m3 * r1[3]; |
| 675 | s = r1[4]; |
| 676 | if (0.0 != s) { |
| 677 | r2[4] -= m2 * s; |
| 678 | r3[4] -= m3 * s; |
| 679 | } |
| 680 | s = r1[5]; |
| 681 | if (0.0 != s) { |
| 682 | r2[5] -= m2 * s; |
| 683 | r3[5] -= m3 * s; |
| 684 | } |
| 685 | s = r1[6]; |
| 686 | if (0.0 != s) { |
| 687 | r2[6] -= m2 * s; |
| 688 | r3[6] -= m3 * s; |
| 689 | } |
| 690 | s = r1[7]; |
| 691 | if (0.0 != s) { |
| 692 | r2[7] -= m2 * s; |
| 693 | r3[7] -= m3 * s; |
| 694 | } |
| 695 | |
| 696 | /* choose pivot - or die */ |
| 697 | if (Math::abs(r3[2]) > Math::abs(r2[2])) { |
| 698 | SWAP(r3, r2); |
| 699 | } |
| 700 | ERR_FAIL_COND(0.0 == r2[2]); |
| 701 | |
| 702 | /* eliminate third variable */ |
| 703 | m3 = r3[2] / r2[2]; |
| 704 | r3[3] -= m3 * r2[3]; |
| 705 | r3[4] -= m3 * r2[4]; |
| 706 | r3[5] -= m3 * r2[5]; |
| 707 | r3[6] -= m3 * r2[6]; |
| 708 | r3[7] -= m3 * r2[7]; |
| 709 | |
| 710 | /* last check */ |
| 711 | ERR_FAIL_COND(0.0 == r3[3]); |
| 712 | |
| 713 | s = 1.0 / r3[3]; /* now back substitute row 3 */ |
| 714 | r3[4] *= s; |
| 715 | r3[5] *= s; |
| 716 | r3[6] *= s; |
| 717 | r3[7] *= s; |
| 718 | |
| 719 | m2 = r2[3]; /* now back substitute row 2 */ |
| 720 | s = 1.0 / r2[2]; |
| 721 | r2[4] = s * (r2[4] - r3[4] * m2); |
| 722 | r2[5] = s * (r2[5] - r3[5] * m2); |
| 723 | r2[6] = s * (r2[6] - r3[6] * m2); |
| 724 | r2[7] = s * (r2[7] - r3[7] * m2); |
| 725 | m1 = r1[3]; |
| 726 | r1[4] -= r3[4] * m1; |
| 727 | r1[5] -= r3[5] * m1; |
| 728 | r1[6] -= r3[6] * m1; |
| 729 | r1[7] -= r3[7] * m1; |
| 730 | m0 = r0[3]; |
| 731 | r0[4] -= r3[4] * m0; |
| 732 | r0[5] -= r3[5] * m0; |
| 733 | r0[6] -= r3[6] * m0; |
| 734 | r0[7] -= r3[7] * m0; |
| 735 | |
| 736 | m1 = r1[2]; /* now back substitute row 1 */ |
| 737 | s = 1.0 / r1[1]; |
| 738 | r1[4] = s * (r1[4] - r2[4] * m1); |
| 739 | r1[5] = s * (r1[5] - r2[5] * m1), |
| 740 | r1[6] = s * (r1[6] - r2[6] * m1); |
| 741 | r1[7] = s * (r1[7] - r2[7] * m1); |
| 742 | m0 = r0[2]; |
| 743 | r0[4] -= r2[4] * m0; |
| 744 | r0[5] -= r2[5] * m0; |
| 745 | r0[6] -= r2[6] * m0; |
| 746 | r0[7] -= r2[7] * m0; |
| 747 | |
| 748 | m0 = r0[1]; /* now back substitute row 0 */ |
| 749 | s = 1.0 / r0[0]; |
| 750 | r0[4] = s * (r0[4] - r1[4] * m0); |
| 751 | r0[5] = s * (r0[5] - r1[5] * m0), |
| 752 | r0[6] = s * (r0[6] - r1[6] * m0); |
| 753 | r0[7] = s * (r0[7] - r1[7] * m0); |
| 754 | |
| 755 | MAT(out, 0, 0) = r0[4]; |
| 756 | MAT(out, 0, 1) = r0[5]; |
| 757 | MAT(out, 0, 2) = r0[6]; |
| 758 | MAT(out, 0, 3) = r0[7]; |
| 759 | MAT(out, 1, 0) = r1[4]; |
| 760 | MAT(out, 1, 1) = r1[5]; |
| 761 | MAT(out, 1, 2) = r1[6]; |
| 762 | MAT(out, 1, 3) = r1[7]; |
| 763 | MAT(out, 2, 0) = r2[4]; |
| 764 | MAT(out, 2, 1) = r2[5]; |
| 765 | MAT(out, 2, 2) = r2[6]; |
| 766 | MAT(out, 2, 3) = r2[7]; |
| 767 | MAT(out, 3, 0) = r3[4]; |
| 768 | MAT(out, 3, 1) = r3[5]; |
| 769 | MAT(out, 3, 2) = r3[6]; |
| 770 | MAT(out, 3, 3) = r3[7]; |
| 771 | |
| 772 | #undef MAT |
| 773 | |
| 774 | *this = temp; |
| 775 | } |
| 776 | |
| 777 | void Projection::flip_y() { |
| 778 | for (int i = 0; i < 4; i++) { |
| 779 | columns[1][i] = -columns[1][i]; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | bool Projection::is_same(const Projection &p_cam) const { |
| 784 | return columns[0].is_same(p_cam.columns[0]) && columns[1].is_same(p_cam.columns[1]) && columns[2].is_same(p_cam.columns[2]) && columns[3].is_same(p_cam.columns[3]); |
| 785 | } |
| 786 | |
| 787 | void Projection::set_depth_correction(bool p_flip_y, bool p_reverse_z, bool p_remap_z) { |
| 788 | // p_remap_z is used to convert from OpenGL-style clip space (-1 - 1) to Vulkan style (0 - 1). |
| 789 | real_t *m = &columns[0][0]; |
| 790 | |
| 791 | m[0] = 1; |
| 792 | m[1] = 0.0; |
| 793 | m[2] = 0.0; |
| 794 | m[3] = 0.0; |
| 795 | m[4] = 0.0; |
| 796 | m[5] = p_flip_y ? -1 : 1; |
| 797 | m[6] = 0.0; |
| 798 | m[7] = 0.0; |
| 799 | m[8] = 0.0; |
| 800 | m[9] = 0.0; |
| 801 | m[10] = p_remap_z ? (p_reverse_z ? -0.5 : 0.5) : (p_reverse_z ? -1.0 : 1.0); |
| 802 | m[11] = 0.0; |
| 803 | m[12] = 0.0; |
| 804 | m[13] = 0.0; |
| 805 | m[14] = p_remap_z ? 0.5 : 0.0; |
| 806 | m[15] = 1.0; |
| 807 | } |
| 808 | |
| 809 | void Projection::set_light_bias() { |
| 810 | real_t *m = &columns[0][0]; |
| 811 | |
| 812 | m[0] = 0.5; |
| 813 | m[1] = 0.0; |
| 814 | m[2] = 0.0; |
| 815 | m[3] = 0.0; |
| 816 | m[4] = 0.0; |
| 817 | m[5] = 0.5; |
| 818 | m[6] = 0.0; |
| 819 | m[7] = 0.0; |
| 820 | m[8] = 0.0; |
| 821 | m[9] = 0.0; |
| 822 | m[10] = 0.5; |
| 823 | m[11] = 0.0; |
| 824 | m[12] = 0.5; |
| 825 | m[13] = 0.5; |
| 826 | m[14] = 0.5; |
| 827 | m[15] = 1.0; |
| 828 | } |
| 829 | |
| 830 | void Projection::set_light_atlas_rect(const Rect2 &p_rect) { |
| 831 | real_t *m = &columns[0][0]; |
| 832 | |
| 833 | m[0] = p_rect.size.width; |
| 834 | m[1] = 0.0; |
| 835 | m[2] = 0.0; |
| 836 | m[3] = 0.0; |
| 837 | m[4] = 0.0; |
| 838 | m[5] = p_rect.size.height; |
| 839 | m[6] = 0.0; |
| 840 | m[7] = 0.0; |
| 841 | m[8] = 0.0; |
| 842 | m[9] = 0.0; |
| 843 | m[10] = 1.0; |
| 844 | m[11] = 0.0; |
| 845 | m[12] = p_rect.position.x; |
| 846 | m[13] = p_rect.position.y; |
| 847 | m[14] = 0.0; |
| 848 | m[15] = 1.0; |
| 849 | } |
| 850 | |
| 851 | Projection::operator String() const { |
| 852 | return "[X: " + columns[0].operator String() + |
| 853 | ", Y: " + columns[1].operator String() + |
| 854 | ", Z: " + columns[2].operator String() + |
| 855 | ", W: " + columns[3].operator String() + "]"; |
| 856 | } |
| 857 | |
| 858 | real_t Projection::get_aspect() const { |
| 859 | // NOTE: This assumes a rectangular projection plane, i.e. that : |
| 860 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 861 | // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0) |
| 862 | return columns[1][1] / columns[0][0]; |
| 863 | } |
| 864 | |
| 865 | int Projection::get_pixels_per_meter(int p_for_pixel_width) const { |
| 866 | // NOTE: This assumes a rectangular projection plane, i.e. that : |
| 867 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 868 | // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0) |
| 869 | real_t width = 2 * (-get_z_near() * columns[2][3] + columns[3][3]) / columns[0][0]; |
| 870 | return p_for_pixel_width / width; // Note : return type should be real_t (kept as int for compatibility for now). |
| 871 | } |
| 872 | |
| 873 | bool Projection::is_orthogonal() const { |
| 874 | // NOTE: This assumes that the matrix is a projection across z-axis |
| 875 | // i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0 |
| 876 | return columns[2][3] == 0.0; |
| 877 | } |
| 878 | |
| 879 | real_t Projection::get_fov() const { |
| 880 | // NOTE: This assumes a rectangular projection plane, i.e. that : |
| 881 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 882 | // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0) |
| 883 | if (columns[2][0] == 0) { |
| 884 | return Math::rad_to_deg(2 * Math::atan2(1, columns[0][0])); |
| 885 | } else { |
| 886 | // The frustum is asymmetrical so we need to calculate the left and right angles separately. |
| 887 | real_t right = Math::atan2(columns[2][0] + 1, columns[0][0]); |
| 888 | real_t left = Math::atan2(columns[2][0] - 1, columns[0][0]); |
| 889 | return Math::rad_to_deg(right - left); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | real_t Projection::get_lod_multiplier() const { |
| 894 | // NOTE: This assumes a rectangular projection plane, i.e. that : |
| 895 | // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0) |
| 896 | // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0) |
| 897 | return 2 / columns[0][0]; |
| 898 | } |
| 899 | |
| 900 | void Projection::make_scale(const Vector3 &p_scale) { |
| 901 | set_identity(); |
| 902 | columns[0][0] = p_scale.x; |
| 903 | columns[1][1] = p_scale.y; |
| 904 | columns[2][2] = p_scale.z; |
| 905 | } |
| 906 | |
| 907 | void Projection::scale_translate_to_fit(const AABB &p_aabb) { |
| 908 | Vector3 min = p_aabb.position; |
| 909 | Vector3 max = p_aabb.position + p_aabb.size; |
| 910 | |
| 911 | columns[0][0] = 2 / (max.x - min.x); |
| 912 | columns[1][0] = 0; |
| 913 | columns[2][0] = 0; |
| 914 | columns[3][0] = -(max.x + min.x) / (max.x - min.x); |
| 915 | |
| 916 | columns[0][1] = 0; |
| 917 | columns[1][1] = 2 / (max.y - min.y); |
| 918 | columns[2][1] = 0; |
| 919 | columns[3][1] = -(max.y + min.y) / (max.y - min.y); |
| 920 | |
| 921 | columns[0][2] = 0; |
| 922 | columns[1][2] = 0; |
| 923 | columns[2][2] = 2 / (max.z - min.z); |
| 924 | columns[3][2] = -(max.z + min.z) / (max.z - min.z); |
| 925 | |
| 926 | columns[0][3] = 0; |
| 927 | columns[1][3] = 0; |
| 928 | columns[2][3] = 0; |
| 929 | columns[3][3] = 1; |
| 930 | } |
| 931 | |
| 932 | void Projection::add_jitter_offset(const Vector2 &p_offset) { |
| 933 | columns[3][0] += p_offset.x; |
| 934 | columns[3][1] += p_offset.y; |
| 935 | } |
| 936 | |
| 937 | Projection::operator Transform3D() const { |
| 938 | Transform3D tr; |
| 939 | const real_t *m = &columns[0][0]; |
| 940 | |
| 941 | tr.basis.rows[0][0] = m[0]; |
| 942 | tr.basis.rows[1][0] = m[1]; |
| 943 | tr.basis.rows[2][0] = m[2]; |
| 944 | |
| 945 | tr.basis.rows[0][1] = m[4]; |
| 946 | tr.basis.rows[1][1] = m[5]; |
| 947 | tr.basis.rows[2][1] = m[6]; |
| 948 | |
| 949 | tr.basis.rows[0][2] = m[8]; |
| 950 | tr.basis.rows[1][2] = m[9]; |
| 951 | tr.basis.rows[2][2] = m[10]; |
| 952 | |
| 953 | tr.origin.x = m[12]; |
| 954 | tr.origin.y = m[13]; |
| 955 | tr.origin.z = m[14]; |
| 956 | |
| 957 | return tr; |
| 958 | } |
| 959 | |
| 960 | Projection::Projection(const Transform3D &p_transform) { |
| 961 | const Transform3D &tr = p_transform; |
| 962 | real_t *m = &columns[0][0]; |
| 963 | |
| 964 | m[0] = tr.basis.rows[0][0]; |
| 965 | m[1] = tr.basis.rows[1][0]; |
| 966 | m[2] = tr.basis.rows[2][0]; |
| 967 | m[3] = 0.0; |
| 968 | m[4] = tr.basis.rows[0][1]; |
| 969 | m[5] = tr.basis.rows[1][1]; |
| 970 | m[6] = tr.basis.rows[2][1]; |
| 971 | m[7] = 0.0; |
| 972 | m[8] = tr.basis.rows[0][2]; |
| 973 | m[9] = tr.basis.rows[1][2]; |
| 974 | m[10] = tr.basis.rows[2][2]; |
| 975 | m[11] = 0.0; |
| 976 | m[12] = tr.origin.x; |
| 977 | m[13] = tr.origin.y; |
| 978 | m[14] = tr.origin.z; |
| 979 | m[15] = 1.0; |
| 980 | } |