camera_matrix.cpp 21 KB

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