camera_matrix.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*************************************************************************/
  2. /* camera_matrix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "camera_matrix.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/print_string.h"
  37. float CameraMatrix::determinant() const {
  38. return matrix[0][3] * matrix[1][2] * matrix[2][1] * matrix[3][0] - matrix[0][2] * matrix[1][3] * matrix[2][1] * matrix[3][0] -
  39. matrix[0][3] * matrix[1][1] * matrix[2][2] * matrix[3][0] + matrix[0][1] * matrix[1][3] * matrix[2][2] * matrix[3][0] +
  40. matrix[0][2] * matrix[1][1] * matrix[2][3] * matrix[3][0] - matrix[0][1] * matrix[1][2] * matrix[2][3] * matrix[3][0] -
  41. matrix[0][3] * matrix[1][2] * matrix[2][0] * matrix[3][1] + matrix[0][2] * matrix[1][3] * matrix[2][0] * matrix[3][1] +
  42. matrix[0][3] * matrix[1][0] * matrix[2][2] * matrix[3][1] - matrix[0][0] * matrix[1][3] * matrix[2][2] * matrix[3][1] -
  43. matrix[0][2] * matrix[1][0] * matrix[2][3] * matrix[3][1] + matrix[0][0] * matrix[1][2] * matrix[2][3] * matrix[3][1] +
  44. matrix[0][3] * matrix[1][1] * matrix[2][0] * matrix[3][2] - matrix[0][1] * matrix[1][3] * matrix[2][0] * matrix[3][2] -
  45. matrix[0][3] * matrix[1][0] * matrix[2][1] * matrix[3][2] + matrix[0][0] * matrix[1][3] * matrix[2][1] * matrix[3][2] +
  46. matrix[0][1] * matrix[1][0] * matrix[2][3] * matrix[3][2] - matrix[0][0] * matrix[1][1] * matrix[2][3] * matrix[3][2] -
  47. matrix[0][2] * matrix[1][1] * matrix[2][0] * matrix[3][3] + matrix[0][1] * matrix[1][2] * matrix[2][0] * matrix[3][3] +
  48. matrix[0][2] * matrix[1][0] * matrix[2][1] * matrix[3][3] - matrix[0][0] * matrix[1][2] * matrix[2][1] * matrix[3][3] -
  49. matrix[0][1] * matrix[1][0] * matrix[2][2] * matrix[3][3] + matrix[0][0] * matrix[1][1] * matrix[2][2] * matrix[3][3];
  50. }
  51. void CameraMatrix::set_identity() {
  52. for (int i = 0; i < 4; i++) {
  53. for (int j = 0; j < 4; j++) {
  54. matrix[i][j] = (i == j) ? 1 : 0;
  55. }
  56. }
  57. }
  58. void CameraMatrix::set_zero() {
  59. for (int i = 0; i < 4; i++) {
  60. for (int j = 0; j < 4; j++) {
  61. matrix[i][j] = 0;
  62. }
  63. }
  64. }
  65. Plane CameraMatrix::xform4(const Plane &p_vec4) const {
  66. Plane ret;
  67. ret.normal.x = matrix[0][0] * p_vec4.normal.x + matrix[1][0] * p_vec4.normal.y + matrix[2][0] * p_vec4.normal.z + matrix[3][0] * p_vec4.d;
  68. ret.normal.y = matrix[0][1] * p_vec4.normal.x + matrix[1][1] * p_vec4.normal.y + matrix[2][1] * p_vec4.normal.z + matrix[3][1] * p_vec4.d;
  69. ret.normal.z = matrix[0][2] * p_vec4.normal.x + matrix[1][2] * p_vec4.normal.y + matrix[2][2] * p_vec4.normal.z + matrix[3][2] * p_vec4.d;
  70. ret.d = matrix[0][3] * p_vec4.normal.x + matrix[1][3] * p_vec4.normal.y + matrix[2][3] * p_vec4.normal.z + matrix[3][3] * p_vec4.d;
  71. return ret;
  72. }
  73. void CameraMatrix::adjust_perspective_znear(real_t p_new_znear) {
  74. real_t zfar = get_z_far();
  75. real_t znear = p_new_znear;
  76. real_t deltaZ = zfar - znear;
  77. matrix[2][2] = -(zfar + znear) / deltaZ;
  78. matrix[3][2] = -2 * znear * zfar / deltaZ;
  79. }
  80. void CameraMatrix::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) {
  81. if (p_flip_fov) {
  82. p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
  83. }
  84. real_t sine, cotangent, deltaZ;
  85. real_t radians = Math::deg2rad(p_fovy_degrees / 2.0);
  86. deltaZ = p_z_far - p_z_near;
  87. sine = Math::sin(radians);
  88. if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) {
  89. return;
  90. }
  91. cotangent = Math::cos(radians) / sine;
  92. set_identity();
  93. matrix[0][0] = cotangent / p_aspect;
  94. matrix[1][1] = cotangent;
  95. matrix[2][2] = -(p_z_far + p_z_near) / deltaZ;
  96. matrix[2][3] = -1;
  97. matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ;
  98. matrix[3][3] = 0;
  99. }
  100. void CameraMatrix::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) {
  101. if (p_flip_fov) {
  102. p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
  103. }
  104. real_t left, right, modeltranslation, ymax, xmax, frustumshift;
  105. ymax = p_z_near * tan(Math::deg2rad(p_fovy_degrees / 2.0));
  106. xmax = ymax * p_aspect;
  107. frustumshift = (p_intraocular_dist / 2.0) * p_z_near / p_convergence_dist;
  108. switch (p_eye) {
  109. case 1: { // left eye
  110. left = -xmax + frustumshift;
  111. right = xmax + frustumshift;
  112. modeltranslation = p_intraocular_dist / 2.0;
  113. } break;
  114. case 2: { // right eye
  115. left = -xmax - frustumshift;
  116. right = xmax - frustumshift;
  117. modeltranslation = -p_intraocular_dist / 2.0;
  118. } break;
  119. default: { // mono, should give the same result as set_perspective(p_fovy_degrees,p_aspect,p_z_near,p_z_far,p_flip_fov)
  120. left = -xmax;
  121. right = xmax;
  122. modeltranslation = 0.0;
  123. } break;
  124. }
  125. set_frustum(left, right, -ymax, ymax, p_z_near, p_z_far);
  126. // translate matrix by (modeltranslation, 0.0, 0.0)
  127. CameraMatrix cm;
  128. cm.set_identity();
  129. cm.matrix[3][0] = modeltranslation;
  130. *this = *this * cm;
  131. }
  132. void CameraMatrix::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) {
  133. // we first calculate our base frustum on our values without taking our lens magnification into account.
  134. real_t f1 = (p_intraocular_dist * 0.5) / p_display_to_lens;
  135. real_t f2 = ((p_display_width - p_intraocular_dist) * 0.5) / p_display_to_lens;
  136. real_t f3 = (p_display_width / 4.0) / p_display_to_lens;
  137. // 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
  138. // we're willing to sacrifice in FOV.
  139. real_t add = ((f1 + f2) * (p_oversample - 1.0)) / 2.0;
  140. f1 += add;
  141. f2 += add;
  142. f3 *= p_oversample;
  143. // always apply KEEP_WIDTH aspect ratio
  144. f3 /= p_aspect;
  145. switch (p_eye) {
  146. case 1: { // left eye
  147. set_frustum(-f2 * p_z_near, f1 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far);
  148. } break;
  149. case 2: { // right eye
  150. set_frustum(-f1 * p_z_near, f2 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far);
  151. } break;
  152. default: { // mono, does not apply here!
  153. } break;
  154. }
  155. }
  156. void CameraMatrix::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) {
  157. set_identity();
  158. matrix[0][0] = 2.0 / (p_right - p_left);
  159. matrix[3][0] = -((p_right + p_left) / (p_right - p_left));
  160. matrix[1][1] = 2.0 / (p_top - p_bottom);
  161. matrix[3][1] = -((p_top + p_bottom) / (p_top - p_bottom));
  162. matrix[2][2] = -2.0 / (p_zfar - p_znear);
  163. matrix[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear));
  164. matrix[3][3] = 1.0;
  165. }
  166. void CameraMatrix::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) {
  167. if (!p_flip_fov) {
  168. p_size *= p_aspect;
  169. }
  170. set_orthogonal(-p_size / 2, +p_size / 2, -p_size / p_aspect / 2, +p_size / p_aspect / 2, p_znear, p_zfar);
  171. }
  172. void CameraMatrix::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) {
  173. ERR_FAIL_COND(p_right <= p_left);
  174. ERR_FAIL_COND(p_top <= p_bottom);
  175. ERR_FAIL_COND(p_far <= p_near);
  176. real_t *te = &matrix[0][0];
  177. real_t x = 2 * p_near / (p_right - p_left);
  178. real_t y = 2 * p_near / (p_top - p_bottom);
  179. real_t a = (p_right + p_left) / (p_right - p_left);
  180. real_t b = (p_top + p_bottom) / (p_top - p_bottom);
  181. real_t c = -(p_far + p_near) / (p_far - p_near);
  182. real_t d = -2 * p_far * p_near / (p_far - p_near);
  183. te[0] = x;
  184. te[1] = 0;
  185. te[2] = 0;
  186. te[3] = 0;
  187. te[4] = 0;
  188. te[5] = y;
  189. te[6] = 0;
  190. te[7] = 0;
  191. te[8] = a;
  192. te[9] = b;
  193. te[10] = c;
  194. te[11] = -1;
  195. te[12] = 0;
  196. te[13] = 0;
  197. te[14] = d;
  198. te[15] = 0;
  199. }
  200. void CameraMatrix::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) {
  201. if (!p_flip_fov) {
  202. p_size *= p_aspect;
  203. }
  204. 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);
  205. }
  206. real_t CameraMatrix::get_z_far() const {
  207. const real_t *matrix = (const real_t *)this->matrix;
  208. Plane new_plane = Plane(matrix[3] - matrix[2],
  209. matrix[7] - matrix[6],
  210. matrix[11] - matrix[10],
  211. matrix[15] - matrix[14]);
  212. new_plane.normal = -new_plane.normal;
  213. new_plane.normalize();
  214. return new_plane.d;
  215. }
  216. real_t CameraMatrix::get_z_near() const {
  217. const real_t *matrix = (const real_t *)this->matrix;
  218. Plane new_plane = Plane(matrix[3] + matrix[2],
  219. matrix[7] + matrix[6],
  220. matrix[11] + matrix[10],
  221. -matrix[15] - matrix[14]);
  222. new_plane.normalize();
  223. return new_plane.d;
  224. }
  225. Vector2 CameraMatrix::get_viewport_half_extents() const {
  226. const real_t *matrix = (const real_t *)this->matrix;
  227. ///////--- Near Plane ---///////
  228. Plane near_plane = Plane(matrix[3] + matrix[2],
  229. matrix[7] + matrix[6],
  230. matrix[11] + matrix[10],
  231. -matrix[15] - matrix[14]);
  232. near_plane.normalize();
  233. ///////--- Right Plane ---///////
  234. Plane right_plane = Plane(matrix[3] - matrix[0],
  235. matrix[7] - matrix[4],
  236. matrix[11] - matrix[8],
  237. -matrix[15] + matrix[12]);
  238. right_plane.normalize();
  239. Plane top_plane = Plane(matrix[3] - matrix[1],
  240. matrix[7] - matrix[5],
  241. matrix[11] - matrix[9],
  242. -matrix[15] + matrix[13]);
  243. top_plane.normalize();
  244. Vector3 res;
  245. near_plane.intersect_3(right_plane, top_plane, &res);
  246. return Vector2(res.x, res.y);
  247. }
  248. Vector2 CameraMatrix::get_far_plane_half_extents() const {
  249. const real_t *matrix = (const real_t *)this->matrix;
  250. ///////--- Far Plane ---///////
  251. Plane far_plane = Plane(matrix[3] - matrix[2],
  252. matrix[7] - matrix[6],
  253. matrix[11] - matrix[10],
  254. -matrix[15] + matrix[14]);
  255. far_plane.normalize();
  256. ///////--- Right Plane ---///////
  257. Plane right_plane = Plane(matrix[3] - matrix[0],
  258. matrix[7] - matrix[4],
  259. matrix[11] - matrix[8],
  260. -matrix[15] + matrix[12]);
  261. right_plane.normalize();
  262. Plane top_plane = Plane(matrix[3] - matrix[1],
  263. matrix[7] - matrix[5],
  264. matrix[11] - matrix[9],
  265. -matrix[15] + matrix[13]);
  266. top_plane.normalize();
  267. Vector3 res;
  268. far_plane.intersect_3(right_plane, top_plane, &res);
  269. return Vector2(res.x, res.y);
  270. }
  271. bool CameraMatrix::get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const {
  272. Vector<Plane> planes = get_projection_planes(Transform3D());
  273. const Planes intersections[8][3] = {
  274. { PLANE_FAR, PLANE_LEFT, PLANE_TOP },
  275. { PLANE_FAR, PLANE_LEFT, PLANE_BOTTOM },
  276. { PLANE_FAR, PLANE_RIGHT, PLANE_TOP },
  277. { PLANE_FAR, PLANE_RIGHT, PLANE_BOTTOM },
  278. { PLANE_NEAR, PLANE_LEFT, PLANE_TOP },
  279. { PLANE_NEAR, PLANE_LEFT, PLANE_BOTTOM },
  280. { PLANE_NEAR, PLANE_RIGHT, PLANE_TOP },
  281. { PLANE_NEAR, PLANE_RIGHT, PLANE_BOTTOM },
  282. };
  283. for (int i = 0; i < 8; i++) {
  284. Vector3 point;
  285. bool res = planes[intersections[i][0]].intersect_3(planes[intersections[i][1]], planes[intersections[i][2]], &point);
  286. ERR_FAIL_COND_V(!res, false);
  287. p_8points[i] = p_transform.xform(point);
  288. }
  289. return true;
  290. }
  291. Vector<Plane> CameraMatrix::get_projection_planes(const Transform3D &p_transform) const {
  292. /** Fast Plane Extraction from combined modelview/projection matrices.
  293. * References:
  294. * https://web.archive.org/web/20011221205252/https://www.markmorley.com/opengl/frustumculling.html
  295. * https://web.archive.org/web/20061020020112/https://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
  296. */
  297. Vector<Plane> planes;
  298. planes.resize(6);
  299. const real_t *matrix = (const real_t *)this->matrix;
  300. Plane new_plane;
  301. ///////--- Near Plane ---///////
  302. new_plane = Plane(matrix[3] + matrix[2],
  303. matrix[7] + matrix[6],
  304. matrix[11] + matrix[10],
  305. matrix[15] + matrix[14]);
  306. new_plane.normal = -new_plane.normal;
  307. new_plane.normalize();
  308. planes.write[0] = p_transform.xform(new_plane);
  309. ///////--- Far Plane ---///////
  310. new_plane = Plane(matrix[3] - matrix[2],
  311. matrix[7] - matrix[6],
  312. matrix[11] - matrix[10],
  313. matrix[15] - matrix[14]);
  314. new_plane.normal = -new_plane.normal;
  315. new_plane.normalize();
  316. planes.write[1] = p_transform.xform(new_plane);
  317. ///////--- Left Plane ---///////
  318. new_plane = Plane(matrix[3] + matrix[0],
  319. matrix[7] + matrix[4],
  320. matrix[11] + matrix[8],
  321. matrix[15] + matrix[12]);
  322. new_plane.normal = -new_plane.normal;
  323. new_plane.normalize();
  324. planes.write[2] = p_transform.xform(new_plane);
  325. ///////--- Top Plane ---///////
  326. new_plane = Plane(matrix[3] - matrix[1],
  327. matrix[7] - matrix[5],
  328. matrix[11] - matrix[9],
  329. matrix[15] - matrix[13]);
  330. new_plane.normal = -new_plane.normal;
  331. new_plane.normalize();
  332. planes.write[3] = p_transform.xform(new_plane);
  333. ///////--- Right Plane ---///////
  334. new_plane = Plane(matrix[3] - matrix[0],
  335. matrix[7] - matrix[4],
  336. matrix[11] - matrix[8],
  337. matrix[15] - matrix[12]);
  338. new_plane.normal = -new_plane.normal;
  339. new_plane.normalize();
  340. planes.write[4] = p_transform.xform(new_plane);
  341. ///////--- Bottom Plane ---///////
  342. new_plane = Plane(matrix[3] + matrix[1],
  343. matrix[7] + matrix[5],
  344. matrix[11] + matrix[9],
  345. matrix[15] + matrix[13]);
  346. new_plane.normal = -new_plane.normal;
  347. new_plane.normalize();
  348. planes.write[5] = p_transform.xform(new_plane);
  349. return planes;
  350. }
  351. CameraMatrix CameraMatrix::inverse() const {
  352. CameraMatrix cm = *this;
  353. cm.invert();
  354. return cm;
  355. }
  356. void CameraMatrix::invert() {
  357. int i, j, k;
  358. int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
  359. real_t pvt_val; /* Value of current pivot element */
  360. real_t hold; /* Temporary storage */
  361. real_t determinat; /* Determinant */
  362. determinat = 1.0;
  363. for (k = 0; k < 4; k++) {
  364. /** Locate k'th pivot element **/
  365. pvt_val = matrix[k][k]; /** Initialize for search **/
  366. pvt_i[k] = k;
  367. pvt_j[k] = k;
  368. for (i = k; i < 4; i++) {
  369. for (j = k; j < 4; j++) {
  370. if (Math::absd(matrix[i][j]) > Math::absd(pvt_val)) {
  371. pvt_i[k] = i;
  372. pvt_j[k] = j;
  373. pvt_val = matrix[i][j];
  374. }
  375. }
  376. }
  377. /** Product of pivots, gives determinant when finished **/
  378. determinat *= pvt_val;
  379. if (Math::absd(determinat) < 1e-7) {
  380. return; //(false); /** Matrix is singular (zero determinant). **/
  381. }
  382. /** "Interchange" rows (with sign change stuff) **/
  383. i = pvt_i[k];
  384. if (i != k) { /** If rows are different **/
  385. for (j = 0; j < 4; j++) {
  386. hold = -matrix[k][j];
  387. matrix[k][j] = matrix[i][j];
  388. matrix[i][j] = hold;
  389. }
  390. }
  391. /** "Interchange" columns **/
  392. j = pvt_j[k];
  393. if (j != k) { /** If columns are different **/
  394. for (i = 0; i < 4; i++) {
  395. hold = -matrix[i][k];
  396. matrix[i][k] = matrix[i][j];
  397. matrix[i][j] = hold;
  398. }
  399. }
  400. /** Divide column by minus pivot value **/
  401. for (i = 0; i < 4; i++) {
  402. if (i != k) {
  403. matrix[i][k] /= (-pvt_val);
  404. }
  405. }
  406. /** Reduce the matrix **/
  407. for (i = 0; i < 4; i++) {
  408. hold = matrix[i][k];
  409. for (j = 0; j < 4; j++) {
  410. if (i != k && j != k) {
  411. matrix[i][j] += hold * matrix[k][j];
  412. }
  413. }
  414. }
  415. /** Divide row by pivot **/
  416. for (j = 0; j < 4; j++) {
  417. if (j != k) {
  418. matrix[k][j] /= pvt_val;
  419. }
  420. }
  421. /** Replace pivot by reciprocal (at last we can touch it). **/
  422. matrix[k][k] = 1.0 / pvt_val;
  423. }
  424. /* That was most of the work, one final pass of row/column interchange */
  425. /* to finish */
  426. for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
  427. i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
  428. if (i != k) { /* If rows are different */
  429. for (j = 0; j < 4; j++) {
  430. hold = matrix[k][j];
  431. matrix[k][j] = -matrix[i][j];
  432. matrix[i][j] = hold;
  433. }
  434. }
  435. j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
  436. if (j != k) { /* If columns are different */
  437. for (i = 0; i < 4; i++) {
  438. hold = matrix[i][k];
  439. matrix[i][k] = -matrix[i][j];
  440. matrix[i][j] = hold;
  441. }
  442. }
  443. }
  444. }
  445. void CameraMatrix::flip_y() {
  446. for (int i = 0; i < 4; i++) {
  447. matrix[1][i] = -matrix[1][i];
  448. }
  449. }
  450. CameraMatrix::CameraMatrix() {
  451. set_identity();
  452. }
  453. CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const {
  454. CameraMatrix new_matrix;
  455. for (int j = 0; j < 4; j++) {
  456. for (int i = 0; i < 4; i++) {
  457. real_t ab = 0;
  458. for (int k = 0; k < 4; k++) {
  459. ab += matrix[k][i] * p_matrix.matrix[j][k];
  460. }
  461. new_matrix.matrix[j][i] = ab;
  462. }
  463. }
  464. return new_matrix;
  465. }
  466. void CameraMatrix::set_depth_correction(bool p_flip_y) {
  467. real_t *m = &matrix[0][0];
  468. m[0] = 1;
  469. m[1] = 0.0;
  470. m[2] = 0.0;
  471. m[3] = 0.0;
  472. m[4] = 0.0;
  473. m[5] = p_flip_y ? -1 : 1;
  474. m[6] = 0.0;
  475. m[7] = 0.0;
  476. m[8] = 0.0;
  477. m[9] = 0.0;
  478. m[10] = 0.5;
  479. m[11] = 0.0;
  480. m[12] = 0.0;
  481. m[13] = 0.0;
  482. m[14] = 0.5;
  483. m[15] = 1.0;
  484. }
  485. void CameraMatrix::set_light_bias() {
  486. real_t *m = &matrix[0][0];
  487. m[0] = 0.5;
  488. m[1] = 0.0;
  489. m[2] = 0.0;
  490. m[3] = 0.0;
  491. m[4] = 0.0;
  492. m[5] = 0.5;
  493. m[6] = 0.0;
  494. m[7] = 0.0;
  495. m[8] = 0.0;
  496. m[9] = 0.0;
  497. m[10] = 0.5;
  498. m[11] = 0.0;
  499. m[12] = 0.5;
  500. m[13] = 0.5;
  501. m[14] = 0.5;
  502. m[15] = 1.0;
  503. }
  504. void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) {
  505. real_t *m = &matrix[0][0];
  506. m[0] = p_rect.size.width;
  507. m[1] = 0.0;
  508. m[2] = 0.0;
  509. m[3] = 0.0;
  510. m[4] = 0.0;
  511. m[5] = p_rect.size.height;
  512. m[6] = 0.0;
  513. m[7] = 0.0;
  514. m[8] = 0.0;
  515. m[9] = 0.0;
  516. m[10] = 1.0;
  517. m[11] = 0.0;
  518. m[12] = p_rect.position.x;
  519. m[13] = p_rect.position.y;
  520. m[14] = 0.0;
  521. m[15] = 1.0;
  522. }
  523. CameraMatrix::operator String() const {
  524. String str;
  525. for (int i = 0; i < 4; i++) {
  526. for (int j = 0; j < 4; j++) {
  527. str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]);
  528. }
  529. }
  530. return str;
  531. }
  532. real_t CameraMatrix::get_aspect() const {
  533. Vector2 vp_he = get_viewport_half_extents();
  534. return vp_he.x / vp_he.y;
  535. }
  536. int CameraMatrix::get_pixels_per_meter(int p_for_pixel_width) const {
  537. Vector3 result = xform(Vector3(1, 0, -1));
  538. return int((result.x * 0.5 + 0.5) * p_for_pixel_width);
  539. }
  540. bool CameraMatrix::is_orthogonal() const {
  541. return matrix[3][3] == 1.0;
  542. }
  543. real_t CameraMatrix::get_fov() const {
  544. const real_t *matrix = (const real_t *)this->matrix;
  545. Plane right_plane = Plane(matrix[3] - matrix[0],
  546. matrix[7] - matrix[4],
  547. matrix[11] - matrix[8],
  548. -matrix[15] + matrix[12]);
  549. right_plane.normalize();
  550. if ((matrix[8] == 0) && (matrix[9] == 0)) {
  551. return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0;
  552. } else {
  553. // our frustum is asymmetrical need to calculate the left planes angle separately..
  554. Plane left_plane = Plane(matrix[3] + matrix[0],
  555. matrix[7] + matrix[4],
  556. matrix[11] + matrix[8],
  557. matrix[15] + matrix[12]);
  558. left_plane.normalize();
  559. return Math::rad2deg(Math::acos(Math::abs(left_plane.normal.x))) + Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x)));
  560. }
  561. }
  562. float CameraMatrix::get_lod_multiplier() const {
  563. if (is_orthogonal()) {
  564. return get_viewport_half_extents().x;
  565. } else {
  566. float zn = get_z_near();
  567. float width = get_viewport_half_extents().x * 2.0;
  568. return 1.0 / (zn / width);
  569. }
  570. //usage is lod_size / (lod_distance * multiplier) < threshold
  571. }
  572. void CameraMatrix::make_scale(const Vector3 &p_scale) {
  573. set_identity();
  574. matrix[0][0] = p_scale.x;
  575. matrix[1][1] = p_scale.y;
  576. matrix[2][2] = p_scale.z;
  577. }
  578. void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) {
  579. Vector3 min = p_aabb.position;
  580. Vector3 max = p_aabb.position + p_aabb.size;
  581. matrix[0][0] = 2 / (max.x - min.x);
  582. matrix[1][0] = 0;
  583. matrix[2][0] = 0;
  584. matrix[3][0] = -(max.x + min.x) / (max.x - min.x);
  585. matrix[0][1] = 0;
  586. matrix[1][1] = 2 / (max.y - min.y);
  587. matrix[2][1] = 0;
  588. matrix[3][1] = -(max.y + min.y) / (max.y - min.y);
  589. matrix[0][2] = 0;
  590. matrix[1][2] = 0;
  591. matrix[2][2] = 2 / (max.z - min.z);
  592. matrix[3][2] = -(max.z + min.z) / (max.z - min.z);
  593. matrix[0][3] = 0;
  594. matrix[1][3] = 0;
  595. matrix[2][3] = 0;
  596. matrix[3][3] = 1;
  597. }
  598. CameraMatrix::operator Transform3D() const {
  599. Transform3D tr;
  600. const real_t *m = &matrix[0][0];
  601. tr.basis.elements[0][0] = m[0];
  602. tr.basis.elements[1][0] = m[1];
  603. tr.basis.elements[2][0] = m[2];
  604. tr.basis.elements[0][1] = m[4];
  605. tr.basis.elements[1][1] = m[5];
  606. tr.basis.elements[2][1] = m[6];
  607. tr.basis.elements[0][2] = m[8];
  608. tr.basis.elements[1][2] = m[9];
  609. tr.basis.elements[2][2] = m[10];
  610. tr.origin.x = m[12];
  611. tr.origin.y = m[13];
  612. tr.origin.z = m[14];
  613. return tr;
  614. }
  615. CameraMatrix::CameraMatrix(const Transform3D &p_transform) {
  616. const Transform3D &tr = p_transform;
  617. real_t *m = &matrix[0][0];
  618. m[0] = tr.basis.elements[0][0];
  619. m[1] = tr.basis.elements[1][0];
  620. m[2] = tr.basis.elements[2][0];
  621. m[3] = 0.0;
  622. m[4] = tr.basis.elements[0][1];
  623. m[5] = tr.basis.elements[1][1];
  624. m[6] = tr.basis.elements[2][1];
  625. m[7] = 0.0;
  626. m[8] = tr.basis.elements[0][2];
  627. m[9] = tr.basis.elements[1][2];
  628. m[10] = tr.basis.elements[2][2];
  629. m[11] = 0.0;
  630. m[12] = tr.origin.x;
  631. m[13] = tr.origin.y;
  632. m[14] = tr.origin.z;
  633. m[15] = 1.0;
  634. }
  635. CameraMatrix::~CameraMatrix() {
  636. }