projection.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 * std::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. // NOTE: This assumes z-facing near and far planes, i.e. that :
  347. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  348. // - near and far planes are z-facing (i.e. columns[0][2] and [1][2] == 0)
  349. return (columns[3][3] - columns[3][2]) / (columns[2][3] - columns[2][2]);
  350. }
  351. real_t Projection::get_z_near() const {
  352. // NOTE: This assumes z-facing near and far planes, i.e. that :
  353. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  354. // - near and far planes are z-facing (i.e. columns[0][2] and [1][2] == 0)
  355. return (columns[3][3] + columns[3][2]) / (columns[2][3] + columns[2][2]);
  356. }
  357. Vector2 Projection::get_viewport_half_extents() const {
  358. // NOTE: This assumes a symmetrical frustum, i.e. that :
  359. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  360. // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0)
  361. // - there is no offset / skew (i.e. columns[2][0] == columns[2][1] == 0)
  362. real_t w = -get_z_near() * columns[2][3] + columns[3][3];
  363. return Vector2(w / columns[0][0], w / columns[1][1]);
  364. }
  365. Vector2 Projection::get_far_plane_half_extents() const {
  366. // NOTE: This assumes a symmetrical frustum, i.e. that :
  367. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  368. // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0)
  369. // - there is no offset / skew (i.e. columns[2][0] == columns[2][1] == 0)
  370. real_t w = -get_z_far() * columns[2][3] + columns[3][3];
  371. return Vector2(w / columns[0][0], w / columns[1][1]);
  372. }
  373. bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const {
  374. Vector<Plane> planes = get_projection_planes(Transform3D());
  375. const Planes intersections[8][3] = {
  376. { PLANE_FAR, PLANE_LEFT, PLANE_TOP },
  377. { PLANE_FAR, PLANE_LEFT, PLANE_BOTTOM },
  378. { PLANE_FAR, PLANE_RIGHT, PLANE_TOP },
  379. { PLANE_FAR, PLANE_RIGHT, PLANE_BOTTOM },
  380. { PLANE_NEAR, PLANE_LEFT, PLANE_TOP },
  381. { PLANE_NEAR, PLANE_LEFT, PLANE_BOTTOM },
  382. { PLANE_NEAR, PLANE_RIGHT, PLANE_TOP },
  383. { PLANE_NEAR, PLANE_RIGHT, PLANE_BOTTOM },
  384. };
  385. for (int i = 0; i < 8; i++) {
  386. Vector3 point;
  387. Plane a = planes[intersections[i][0]];
  388. Plane b = planes[intersections[i][1]];
  389. Plane c = planes[intersections[i][2]];
  390. bool res = a.intersect_3(b, c, &point);
  391. ERR_FAIL_COND_V(!res, false);
  392. p_8points[i] = p_transform.xform(point);
  393. }
  394. return true;
  395. }
  396. Vector<Plane> Projection::get_projection_planes(const Transform3D &p_transform) const {
  397. /** Fast Plane Extraction from combined modelview/projection matrices.
  398. * References:
  399. * https://web.archive.org/web/20011221205252/https://www.markmorley.com/opengl/frustumculling.html
  400. * https://web.archive.org/web/20061020020112/https://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
  401. */
  402. Vector<Plane> planes;
  403. planes.resize(6);
  404. const real_t *matrix = (const real_t *)columns;
  405. Plane new_plane;
  406. ///////--- Near Plane ---///////
  407. new_plane = Plane(matrix[3] + matrix[2],
  408. matrix[7] + matrix[6],
  409. matrix[11] + matrix[10],
  410. matrix[15] + matrix[14]);
  411. new_plane.normal = -new_plane.normal;
  412. new_plane.normalize();
  413. planes.write[0] = p_transform.xform(new_plane);
  414. ///////--- Far Plane ---///////
  415. new_plane = Plane(matrix[3] - matrix[2],
  416. matrix[7] - matrix[6],
  417. matrix[11] - matrix[10],
  418. matrix[15] - matrix[14]);
  419. new_plane.normal = -new_plane.normal;
  420. new_plane.normalize();
  421. planes.write[1] = p_transform.xform(new_plane);
  422. ///////--- Left Plane ---///////
  423. new_plane = Plane(matrix[3] + matrix[0],
  424. matrix[7] + matrix[4],
  425. matrix[11] + matrix[8],
  426. matrix[15] + matrix[12]);
  427. new_plane.normal = -new_plane.normal;
  428. new_plane.normalize();
  429. planes.write[2] = p_transform.xform(new_plane);
  430. ///////--- Top Plane ---///////
  431. new_plane = Plane(matrix[3] - matrix[1],
  432. matrix[7] - matrix[5],
  433. matrix[11] - matrix[9],
  434. matrix[15] - matrix[13]);
  435. new_plane.normal = -new_plane.normal;
  436. new_plane.normalize();
  437. planes.write[3] = p_transform.xform(new_plane);
  438. ///////--- Right Plane ---///////
  439. new_plane = Plane(matrix[3] - matrix[0],
  440. matrix[7] - matrix[4],
  441. matrix[11] - matrix[8],
  442. matrix[15] - matrix[12]);
  443. new_plane.normal = -new_plane.normal;
  444. new_plane.normalize();
  445. planes.write[4] = p_transform.xform(new_plane);
  446. ///////--- Bottom Plane ---///////
  447. new_plane = Plane(matrix[3] + matrix[1],
  448. matrix[7] + matrix[5],
  449. matrix[11] + matrix[9],
  450. matrix[15] + matrix[13]);
  451. new_plane.normal = -new_plane.normal;
  452. new_plane.normalize();
  453. planes.write[5] = p_transform.xform(new_plane);
  454. return planes;
  455. }
  456. Projection Projection::inverse() const {
  457. Projection cm = *this;
  458. cm.invert();
  459. return cm;
  460. }
  461. void Projection::invert() {
  462. // Adapted from Mesa's `src/util/u_math.c` `util_invert_mat4x4`.
  463. // MIT licensed. Copyright 2008 VMware, Inc. Authored by Jacques Leroy.
  464. Projection temp;
  465. real_t *out = (real_t *)temp.columns;
  466. real_t *m = (real_t *)columns;
  467. real_t wtmp[4][8];
  468. real_t m0, m1, m2, m3, s;
  469. real_t *r0, *r1, *r2, *r3;
  470. #define MAT(m, r, c) (m)[(c) * 4 + (r)]
  471. r0 = wtmp[0];
  472. r1 = wtmp[1];
  473. r2 = wtmp[2];
  474. r3 = wtmp[3];
  475. r0[0] = MAT(m, 0, 0);
  476. r0[1] = MAT(m, 0, 1);
  477. r0[2] = MAT(m, 0, 2);
  478. r0[3] = MAT(m, 0, 3);
  479. r0[4] = 1.0;
  480. r0[5] = 0.0;
  481. r0[6] = 0.0;
  482. r0[7] = 0.0;
  483. r1[0] = MAT(m, 1, 0);
  484. r1[1] = MAT(m, 1, 1);
  485. r1[2] = MAT(m, 1, 2);
  486. r1[3] = MAT(m, 1, 3);
  487. r1[5] = 1.0;
  488. r1[4] = 0.0;
  489. r1[6] = 0.0;
  490. r1[7] = 0.0;
  491. r2[0] = MAT(m, 2, 0);
  492. r2[1] = MAT(m, 2, 1);
  493. r2[2] = MAT(m, 2, 2);
  494. r2[3] = MAT(m, 2, 3);
  495. r2[6] = 1.0;
  496. r2[4] = 0.0;
  497. r2[5] = 0.0;
  498. r2[7] = 0.0;
  499. r3[0] = MAT(m, 3, 0);
  500. r3[1] = MAT(m, 3, 1);
  501. r3[2] = MAT(m, 3, 2);
  502. r3[3] = MAT(m, 3, 3);
  503. r3[7] = 1.0;
  504. r3[4] = 0.0;
  505. r3[5] = 0.0;
  506. r3[6] = 0.0;
  507. /* choose pivot - or die */
  508. if (Math::abs(r3[0]) > Math::abs(r2[0])) {
  509. SWAP(r3, r2);
  510. }
  511. if (Math::abs(r2[0]) > Math::abs(r1[0])) {
  512. SWAP(r2, r1);
  513. }
  514. if (Math::abs(r1[0]) > Math::abs(r0[0])) {
  515. SWAP(r1, r0);
  516. }
  517. ERR_FAIL_COND(0.0 == r0[0]);
  518. /* eliminate first variable */
  519. m1 = r1[0] / r0[0];
  520. m2 = r2[0] / r0[0];
  521. m3 = r3[0] / r0[0];
  522. s = r0[1];
  523. r1[1] -= m1 * s;
  524. r2[1] -= m2 * s;
  525. r3[1] -= m3 * s;
  526. s = r0[2];
  527. r1[2] -= m1 * s;
  528. r2[2] -= m2 * s;
  529. r3[2] -= m3 * s;
  530. s = r0[3];
  531. r1[3] -= m1 * s;
  532. r2[3] -= m2 * s;
  533. r3[3] -= m3 * s;
  534. s = r0[4];
  535. if (s != 0.0) {
  536. r1[4] -= m1 * s;
  537. r2[4] -= m2 * s;
  538. r3[4] -= m3 * s;
  539. }
  540. s = r0[5];
  541. if (s != 0.0) {
  542. r1[5] -= m1 * s;
  543. r2[5] -= m2 * s;
  544. r3[5] -= m3 * s;
  545. }
  546. s = r0[6];
  547. if (s != 0.0) {
  548. r1[6] -= m1 * s;
  549. r2[6] -= m2 * s;
  550. r3[6] -= m3 * s;
  551. }
  552. s = r0[7];
  553. if (s != 0.0) {
  554. r1[7] -= m1 * s;
  555. r2[7] -= m2 * s;
  556. r3[7] -= m3 * s;
  557. }
  558. /* choose pivot - or die */
  559. if (Math::abs(r3[1]) > Math::abs(r2[1])) {
  560. SWAP(r3, r2);
  561. }
  562. if (Math::abs(r2[1]) > Math::abs(r1[1])) {
  563. SWAP(r2, r1);
  564. }
  565. ERR_FAIL_COND(0.0 == r1[1]);
  566. /* eliminate second variable */
  567. m2 = r2[1] / r1[1];
  568. m3 = r3[1] / r1[1];
  569. r2[2] -= m2 * r1[2];
  570. r3[2] -= m3 * r1[2];
  571. r2[3] -= m2 * r1[3];
  572. r3[3] -= m3 * r1[3];
  573. s = r1[4];
  574. if (0.0 != s) {
  575. r2[4] -= m2 * s;
  576. r3[4] -= m3 * s;
  577. }
  578. s = r1[5];
  579. if (0.0 != s) {
  580. r2[5] -= m2 * s;
  581. r3[5] -= m3 * s;
  582. }
  583. s = r1[6];
  584. if (0.0 != s) {
  585. r2[6] -= m2 * s;
  586. r3[6] -= m3 * s;
  587. }
  588. s = r1[7];
  589. if (0.0 != s) {
  590. r2[7] -= m2 * s;
  591. r3[7] -= m3 * s;
  592. }
  593. /* choose pivot - or die */
  594. if (Math::abs(r3[2]) > Math::abs(r2[2])) {
  595. SWAP(r3, r2);
  596. }
  597. ERR_FAIL_COND(0.0 == r2[2]);
  598. /* eliminate third variable */
  599. m3 = r3[2] / r2[2];
  600. r3[3] -= m3 * r2[3];
  601. r3[4] -= m3 * r2[4];
  602. r3[5] -= m3 * r2[5];
  603. r3[6] -= m3 * r2[6];
  604. r3[7] -= m3 * r2[7];
  605. /* last check */
  606. ERR_FAIL_COND(0.0 == r3[3]);
  607. s = 1.0 / r3[3]; /* now back substitute row 3 */
  608. r3[4] *= s;
  609. r3[5] *= s;
  610. r3[6] *= s;
  611. r3[7] *= s;
  612. m2 = r2[3]; /* now back substitute row 2 */
  613. s = 1.0 / r2[2];
  614. r2[4] = s * (r2[4] - r3[4] * m2);
  615. r2[5] = s * (r2[5] - r3[5] * m2);
  616. r2[6] = s * (r2[6] - r3[6] * m2);
  617. r2[7] = s * (r2[7] - r3[7] * m2);
  618. m1 = r1[3];
  619. r1[4] -= r3[4] * m1;
  620. r1[5] -= r3[5] * m1;
  621. r1[6] -= r3[6] * m1;
  622. r1[7] -= r3[7] * m1;
  623. m0 = r0[3];
  624. r0[4] -= r3[4] * m0;
  625. r0[5] -= r3[5] * m0;
  626. r0[6] -= r3[6] * m0;
  627. r0[7] -= r3[7] * m0;
  628. m1 = r1[2]; /* now back substitute row 1 */
  629. s = 1.0 / r1[1];
  630. r1[4] = s * (r1[4] - r2[4] * m1);
  631. r1[5] = s * (r1[5] - r2[5] * m1),
  632. r1[6] = s * (r1[6] - r2[6] * m1);
  633. r1[7] = s * (r1[7] - r2[7] * m1);
  634. m0 = r0[2];
  635. r0[4] -= r2[4] * m0;
  636. r0[5] -= r2[5] * m0;
  637. r0[6] -= r2[6] * m0;
  638. r0[7] -= r2[7] * m0;
  639. m0 = r0[1]; /* now back substitute row 0 */
  640. s = 1.0 / r0[0];
  641. r0[4] = s * (r0[4] - r1[4] * m0);
  642. r0[5] = s * (r0[5] - r1[5] * m0),
  643. r0[6] = s * (r0[6] - r1[6] * m0);
  644. r0[7] = s * (r0[7] - r1[7] * m0);
  645. MAT(out, 0, 0) = r0[4];
  646. MAT(out, 0, 1) = r0[5];
  647. MAT(out, 0, 2) = r0[6];
  648. MAT(out, 0, 3) = r0[7];
  649. MAT(out, 1, 0) = r1[4];
  650. MAT(out, 1, 1) = r1[5];
  651. MAT(out, 1, 2) = r1[6];
  652. MAT(out, 1, 3) = r1[7];
  653. MAT(out, 2, 0) = r2[4];
  654. MAT(out, 2, 1) = r2[5];
  655. MAT(out, 2, 2) = r2[6];
  656. MAT(out, 2, 3) = r2[7];
  657. MAT(out, 3, 0) = r3[4];
  658. MAT(out, 3, 1) = r3[5];
  659. MAT(out, 3, 2) = r3[6];
  660. MAT(out, 3, 3) = r3[7];
  661. #undef MAT
  662. *this = temp;
  663. }
  664. void Projection::flip_y() {
  665. for (int i = 0; i < 4; i++) {
  666. columns[1][i] = -columns[1][i];
  667. }
  668. }
  669. bool Projection::is_same(const Projection &p_cam) const {
  670. 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]);
  671. }
  672. void Projection::set_depth_correction(bool p_flip_y, bool p_reverse_z, bool p_remap_z) {
  673. // p_remap_z is used to convert from OpenGL-style clip space (-1 - 1) to Vulkan style (0 - 1).
  674. real_t *m = &columns[0][0];
  675. m[0] = 1;
  676. m[1] = 0.0;
  677. m[2] = 0.0;
  678. m[3] = 0.0;
  679. m[4] = 0.0;
  680. m[5] = p_flip_y ? -1 : 1;
  681. m[6] = 0.0;
  682. m[7] = 0.0;
  683. m[8] = 0.0;
  684. m[9] = 0.0;
  685. m[10] = p_remap_z ? (p_reverse_z ? -0.5 : 0.5) : (p_reverse_z ? -1.0 : 1.0);
  686. m[11] = 0.0;
  687. m[12] = 0.0;
  688. m[13] = 0.0;
  689. m[14] = p_remap_z ? 0.5 : 0.0;
  690. m[15] = 1.0;
  691. }
  692. void Projection::set_light_bias() {
  693. real_t *m = &columns[0][0];
  694. m[0] = 0.5;
  695. m[1] = 0.0;
  696. m[2] = 0.0;
  697. m[3] = 0.0;
  698. m[4] = 0.0;
  699. m[5] = 0.5;
  700. m[6] = 0.0;
  701. m[7] = 0.0;
  702. m[8] = 0.0;
  703. m[9] = 0.0;
  704. m[10] = 0.5;
  705. m[11] = 0.0;
  706. m[12] = 0.5;
  707. m[13] = 0.5;
  708. m[14] = 0.5;
  709. m[15] = 1.0;
  710. }
  711. void Projection::set_light_atlas_rect(const Rect2 &p_rect) {
  712. real_t *m = &columns[0][0];
  713. m[0] = p_rect.size.width;
  714. m[1] = 0.0;
  715. m[2] = 0.0;
  716. m[3] = 0.0;
  717. m[4] = 0.0;
  718. m[5] = p_rect.size.height;
  719. m[6] = 0.0;
  720. m[7] = 0.0;
  721. m[8] = 0.0;
  722. m[9] = 0.0;
  723. m[10] = 1.0;
  724. m[11] = 0.0;
  725. m[12] = p_rect.position.x;
  726. m[13] = p_rect.position.y;
  727. m[14] = 0.0;
  728. m[15] = 1.0;
  729. }
  730. Projection::operator String() const {
  731. return "[X: " + columns[0].operator String() +
  732. ", Y: " + columns[1].operator String() +
  733. ", Z: " + columns[2].operator String() +
  734. ", W: " + columns[3].operator String() + "]";
  735. }
  736. real_t Projection::get_aspect() const {
  737. // NOTE: This assumes a rectangular projection plane, i.e. that :
  738. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  739. // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0)
  740. return columns[1][1] / columns[0][0];
  741. }
  742. int Projection::get_pixels_per_meter(int p_for_pixel_width) const {
  743. // NOTE: This assumes a rectangular projection plane, i.e. that :
  744. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  745. // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0)
  746. real_t width = 2 * (-get_z_near() * columns[2][3] + columns[3][3]) / columns[0][0];
  747. return p_for_pixel_width / width; // Note : return type should be real_t (kept as int for compatibility for now).
  748. }
  749. bool Projection::is_orthogonal() const {
  750. // NOTE: This assumes that the matrix is a projection across z-axis
  751. // i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0
  752. return columns[2][3] == 0.0;
  753. }
  754. real_t Projection::get_fov() const {
  755. // NOTE: This assumes a rectangular projection plane, i.e. that :
  756. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  757. // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0)
  758. if (columns[2][0] == 0) {
  759. return Math::rad_to_deg(2 * Math::atan2(1, columns[0][0]));
  760. } else {
  761. // The frustum is asymmetrical so we need to calculate the left and right angles separately.
  762. real_t right = Math::atan2(columns[2][0] + 1, columns[0][0]);
  763. real_t left = Math::atan2(columns[2][0] - 1, columns[0][0]);
  764. return Math::rad_to_deg(right - left);
  765. }
  766. }
  767. real_t Projection::get_lod_multiplier() const {
  768. // NOTE: This assumes a rectangular projection plane, i.e. that :
  769. // - the matrix is a projection across z-axis (i.e. is invertible and columns[0][1], [0][3], [1][0] and [1][3] == 0)
  770. // - the projection plane is rectangular (i.e. columns[0][2] and [1][2] == 0 if columns[2][3] != 0)
  771. return 2 / columns[0][0];
  772. }
  773. void Projection::make_scale(const Vector3 &p_scale) {
  774. set_identity();
  775. columns[0][0] = p_scale.x;
  776. columns[1][1] = p_scale.y;
  777. columns[2][2] = p_scale.z;
  778. }
  779. void Projection::scale_translate_to_fit(const AABB &p_aabb) {
  780. Vector3 min = p_aabb.position;
  781. Vector3 max = p_aabb.position + p_aabb.size;
  782. columns[0][0] = 2 / (max.x - min.x);
  783. columns[1][0] = 0;
  784. columns[2][0] = 0;
  785. columns[3][0] = -(max.x + min.x) / (max.x - min.x);
  786. columns[0][1] = 0;
  787. columns[1][1] = 2 / (max.y - min.y);
  788. columns[2][1] = 0;
  789. columns[3][1] = -(max.y + min.y) / (max.y - min.y);
  790. columns[0][2] = 0;
  791. columns[1][2] = 0;
  792. columns[2][2] = 2 / (max.z - min.z);
  793. columns[3][2] = -(max.z + min.z) / (max.z - min.z);
  794. columns[0][3] = 0;
  795. columns[1][3] = 0;
  796. columns[2][3] = 0;
  797. columns[3][3] = 1;
  798. }
  799. void Projection::add_jitter_offset(const Vector2 &p_offset) {
  800. columns[3][0] += p_offset.x;
  801. columns[3][1] += p_offset.y;
  802. }
  803. Projection::operator Transform3D() const {
  804. Transform3D tr;
  805. const real_t *m = &columns[0][0];
  806. tr.basis.rows[0][0] = m[0];
  807. tr.basis.rows[1][0] = m[1];
  808. tr.basis.rows[2][0] = m[2];
  809. tr.basis.rows[0][1] = m[4];
  810. tr.basis.rows[1][1] = m[5];
  811. tr.basis.rows[2][1] = m[6];
  812. tr.basis.rows[0][2] = m[8];
  813. tr.basis.rows[1][2] = m[9];
  814. tr.basis.rows[2][2] = m[10];
  815. tr.origin.x = m[12];
  816. tr.origin.y = m[13];
  817. tr.origin.z = m[14];
  818. return tr;
  819. }
  820. Projection::Projection(const Transform3D &p_transform) {
  821. const Transform3D &tr = p_transform;
  822. real_t *m = &columns[0][0];
  823. m[0] = tr.basis.rows[0][0];
  824. m[1] = tr.basis.rows[1][0];
  825. m[2] = tr.basis.rows[2][0];
  826. m[3] = 0.0;
  827. m[4] = tr.basis.rows[0][1];
  828. m[5] = tr.basis.rows[1][1];
  829. m[6] = tr.basis.rows[2][1];
  830. m[7] = 0.0;
  831. m[8] = tr.basis.rows[0][2];
  832. m[9] = tr.basis.rows[1][2];
  833. m[10] = tr.basis.rows[2][2];
  834. m[11] = 0.0;
  835. m[12] = tr.origin.x;
  836. m[13] = tr.origin.y;
  837. m[14] = tr.origin.z;
  838. m[15] = 1.0;
  839. }