projection.cpp 30 KB

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