camera_matrix.cpp 22 KB

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