projection.cpp 30 KB

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