camera_matrix.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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 determinant = 1.0f;
  362. for (k = 0; k < 4; k++) {
  363. /** Locate k'th pivot element **/
  364. pvt_val = matrix[k][k]; /** Initialize for search **/
  365. pvt_i[k] = k;
  366. pvt_j[k] = k;
  367. for (i = k; i < 4; i++) {
  368. for (j = k; j < 4; j++) {
  369. if (Math::abs(matrix[i][j]) > Math::abs(pvt_val)) {
  370. pvt_i[k] = i;
  371. pvt_j[k] = j;
  372. pvt_val = matrix[i][j];
  373. }
  374. }
  375. }
  376. /** Product of pivots, gives determinant when finished **/
  377. determinant *= pvt_val;
  378. if (Math::is_zero_approx(determinant)) {
  379. return; /** Matrix is singular (zero determinant). **/
  380. }
  381. /** "Interchange" rows (with sign change stuff) **/
  382. i = pvt_i[k];
  383. if (i != k) { /** If rows are different **/
  384. for (j = 0; j < 4; j++) {
  385. hold = -matrix[k][j];
  386. matrix[k][j] = matrix[i][j];
  387. matrix[i][j] = hold;
  388. }
  389. }
  390. /** "Interchange" columns **/
  391. j = pvt_j[k];
  392. if (j != k) { /** If columns are different **/
  393. for (i = 0; i < 4; i++) {
  394. hold = -matrix[i][k];
  395. matrix[i][k] = matrix[i][j];
  396. matrix[i][j] = hold;
  397. }
  398. }
  399. /** Divide column by minus pivot value **/
  400. for (i = 0; i < 4; i++) {
  401. if (i != k) {
  402. matrix[i][k] /= (-pvt_val);
  403. }
  404. }
  405. /** Reduce the matrix **/
  406. for (i = 0; i < 4; i++) {
  407. hold = matrix[i][k];
  408. for (j = 0; j < 4; j++) {
  409. if (i != k && j != k) {
  410. matrix[i][j] += hold * matrix[k][j];
  411. }
  412. }
  413. }
  414. /** Divide row by pivot **/
  415. for (j = 0; j < 4; j++) {
  416. if (j != k) {
  417. matrix[k][j] /= pvt_val;
  418. }
  419. }
  420. /** Replace pivot by reciprocal (at last we can touch it). **/
  421. matrix[k][k] = 1.0 / pvt_val;
  422. }
  423. /* That was most of the work, one final pass of row/column interchange */
  424. /* to finish */
  425. for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
  426. i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
  427. if (i != k) { /* If rows are different */
  428. for (j = 0; j < 4; j++) {
  429. hold = matrix[k][j];
  430. matrix[k][j] = -matrix[i][j];
  431. matrix[i][j] = hold;
  432. }
  433. }
  434. j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
  435. if (j != k) { /* If columns are different */
  436. for (i = 0; i < 4; i++) {
  437. hold = matrix[i][k];
  438. matrix[i][k] = -matrix[i][j];
  439. matrix[i][j] = hold;
  440. }
  441. }
  442. }
  443. }
  444. void CameraMatrix::flip_y() {
  445. for (int i = 0; i < 4; i++) {
  446. matrix[1][i] = -matrix[1][i];
  447. }
  448. }
  449. CameraMatrix::CameraMatrix() {
  450. set_identity();
  451. }
  452. CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const {
  453. CameraMatrix new_matrix;
  454. for (int j = 0; j < 4; j++) {
  455. for (int i = 0; i < 4; i++) {
  456. real_t ab = 0;
  457. for (int k = 0; k < 4; k++) {
  458. ab += matrix[k][i] * p_matrix.matrix[j][k];
  459. }
  460. new_matrix.matrix[j][i] = ab;
  461. }
  462. }
  463. return new_matrix;
  464. }
  465. void CameraMatrix::set_depth_correction(bool p_flip_y) {
  466. real_t *m = &matrix[0][0];
  467. m[0] = 1;
  468. m[1] = 0.0;
  469. m[2] = 0.0;
  470. m[3] = 0.0;
  471. m[4] = 0.0;
  472. m[5] = p_flip_y ? -1 : 1;
  473. m[6] = 0.0;
  474. m[7] = 0.0;
  475. m[8] = 0.0;
  476. m[9] = 0.0;
  477. m[10] = 0.5;
  478. m[11] = 0.0;
  479. m[12] = 0.0;
  480. m[13] = 0.0;
  481. m[14] = 0.5;
  482. m[15] = 1.0;
  483. }
  484. void CameraMatrix::set_light_bias() {
  485. real_t *m = &matrix[0][0];
  486. m[0] = 0.5;
  487. m[1] = 0.0;
  488. m[2] = 0.0;
  489. m[3] = 0.0;
  490. m[4] = 0.0;
  491. m[5] = 0.5;
  492. m[6] = 0.0;
  493. m[7] = 0.0;
  494. m[8] = 0.0;
  495. m[9] = 0.0;
  496. m[10] = 0.5;
  497. m[11] = 0.0;
  498. m[12] = 0.5;
  499. m[13] = 0.5;
  500. m[14] = 0.5;
  501. m[15] = 1.0;
  502. }
  503. void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) {
  504. real_t *m = &matrix[0][0];
  505. m[0] = p_rect.size.width;
  506. m[1] = 0.0;
  507. m[2] = 0.0;
  508. m[3] = 0.0;
  509. m[4] = 0.0;
  510. m[5] = p_rect.size.height;
  511. m[6] = 0.0;
  512. m[7] = 0.0;
  513. m[8] = 0.0;
  514. m[9] = 0.0;
  515. m[10] = 1.0;
  516. m[11] = 0.0;
  517. m[12] = p_rect.position.x;
  518. m[13] = p_rect.position.y;
  519. m[14] = 0.0;
  520. m[15] = 1.0;
  521. }
  522. CameraMatrix::operator String() const {
  523. String str;
  524. for (int i = 0; i < 4; i++) {
  525. for (int j = 0; j < 4; j++) {
  526. str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]);
  527. }
  528. }
  529. return str;
  530. }
  531. real_t CameraMatrix::get_aspect() const {
  532. Vector2 vp_he = get_viewport_half_extents();
  533. return vp_he.x / vp_he.y;
  534. }
  535. int CameraMatrix::get_pixels_per_meter(int p_for_pixel_width) const {
  536. Vector3 result = xform(Vector3(1, 0, -1));
  537. return int((result.x * 0.5 + 0.5) * p_for_pixel_width);
  538. }
  539. bool CameraMatrix::is_orthogonal() const {
  540. return matrix[3][3] == 1.0;
  541. }
  542. real_t CameraMatrix::get_fov() const {
  543. const real_t *matrix = (const real_t *)this->matrix;
  544. Plane right_plane = Plane(matrix[3] - matrix[0],
  545. matrix[7] - matrix[4],
  546. matrix[11] - matrix[8],
  547. -matrix[15] + matrix[12]);
  548. right_plane.normalize();
  549. if ((matrix[8] == 0) && (matrix[9] == 0)) {
  550. return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0;
  551. } else {
  552. // our frustum is asymmetrical need to calculate the left planes angle separately..
  553. Plane left_plane = Plane(matrix[3] + matrix[0],
  554. matrix[7] + matrix[4],
  555. matrix[11] + matrix[8],
  556. matrix[15] + matrix[12]);
  557. left_plane.normalize();
  558. return Math::rad2deg(Math::acos(Math::abs(left_plane.normal.x))) + Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x)));
  559. }
  560. }
  561. float CameraMatrix::get_lod_multiplier() const {
  562. if (is_orthogonal()) {
  563. return get_viewport_half_extents().x;
  564. } else {
  565. float zn = get_z_near();
  566. float width = get_viewport_half_extents().x * 2.0;
  567. return 1.0 / (zn / width);
  568. }
  569. //usage is lod_size / (lod_distance * multiplier) < threshold
  570. }
  571. void CameraMatrix::make_scale(const Vector3 &p_scale) {
  572. set_identity();
  573. matrix[0][0] = p_scale.x;
  574. matrix[1][1] = p_scale.y;
  575. matrix[2][2] = p_scale.z;
  576. }
  577. void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) {
  578. Vector3 min = p_aabb.position;
  579. Vector3 max = p_aabb.position + p_aabb.size;
  580. matrix[0][0] = 2 / (max.x - min.x);
  581. matrix[1][0] = 0;
  582. matrix[2][0] = 0;
  583. matrix[3][0] = -(max.x + min.x) / (max.x - min.x);
  584. matrix[0][1] = 0;
  585. matrix[1][1] = 2 / (max.y - min.y);
  586. matrix[2][1] = 0;
  587. matrix[3][1] = -(max.y + min.y) / (max.y - min.y);
  588. matrix[0][2] = 0;
  589. matrix[1][2] = 0;
  590. matrix[2][2] = 2 / (max.z - min.z);
  591. matrix[3][2] = -(max.z + min.z) / (max.z - min.z);
  592. matrix[0][3] = 0;
  593. matrix[1][3] = 0;
  594. matrix[2][3] = 0;
  595. matrix[3][3] = 1;
  596. }
  597. CameraMatrix::operator Transform3D() const {
  598. Transform3D tr;
  599. const real_t *m = &matrix[0][0];
  600. tr.basis.rows[0][0] = m[0];
  601. tr.basis.rows[1][0] = m[1];
  602. tr.basis.rows[2][0] = m[2];
  603. tr.basis.rows[0][1] = m[4];
  604. tr.basis.rows[1][1] = m[5];
  605. tr.basis.rows[2][1] = m[6];
  606. tr.basis.rows[0][2] = m[8];
  607. tr.basis.rows[1][2] = m[9];
  608. tr.basis.rows[2][2] = m[10];
  609. tr.origin.x = m[12];
  610. tr.origin.y = m[13];
  611. tr.origin.z = m[14];
  612. return tr;
  613. }
  614. CameraMatrix::CameraMatrix(const Transform3D &p_transform) {
  615. const Transform3D &tr = p_transform;
  616. real_t *m = &matrix[0][0];
  617. m[0] = tr.basis.rows[0][0];
  618. m[1] = tr.basis.rows[1][0];
  619. m[2] = tr.basis.rows[2][0];
  620. m[3] = 0.0;
  621. m[4] = tr.basis.rows[0][1];
  622. m[5] = tr.basis.rows[1][1];
  623. m[6] = tr.basis.rows[2][1];
  624. m[7] = 0.0;
  625. m[8] = tr.basis.rows[0][2];
  626. m[9] = tr.basis.rows[1][2];
  627. m[10] = tr.basis.rows[2][2];
  628. m[11] = 0.0;
  629. m[12] = tr.origin.x;
  630. m[13] = tr.origin.y;
  631. m[14] = tr.origin.z;
  632. m[15] = 1.0;
  633. }
  634. CameraMatrix::~CameraMatrix() {
  635. }