camera_matrix.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*************************************************************************/
  2. /* camera_matrix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "math_funcs.h"
  32. #include "print_string.h"
  33. void CameraMatrix::set_identity() {
  34. for (int i = 0; i < 4; i++) {
  35. for (int j = 0; j < 4; j++) {
  36. matrix[i][j] = (i == j) ? 1 : 0;
  37. }
  38. }
  39. }
  40. void CameraMatrix::set_zero() {
  41. for (int i = 0; i < 4; i++) {
  42. for (int j = 0; j < 4; j++) {
  43. matrix[i][j] = 0;
  44. }
  45. }
  46. }
  47. Plane CameraMatrix::xform4(const Plane &p_vec4) const {
  48. Plane ret;
  49. 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;
  50. 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;
  51. 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;
  52. 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;
  53. return ret;
  54. }
  55. 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) {
  56. if (p_flip_fov) {
  57. p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
  58. }
  59. real_t sine, cotangent, deltaZ;
  60. real_t radians = p_fovy_degrees / 2.0 * Math_PI / 180.0;
  61. deltaZ = p_z_far - p_z_near;
  62. sine = Math::sin(radians);
  63. if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) {
  64. return;
  65. }
  66. cotangent = Math::cos(radians) / sine;
  67. set_identity();
  68. matrix[0][0] = cotangent / p_aspect;
  69. matrix[1][1] = cotangent;
  70. matrix[2][2] = -(p_z_far + p_z_near) / deltaZ;
  71. matrix[2][3] = -1;
  72. matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ;
  73. matrix[3][3] = 0;
  74. }
  75. 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) {
  76. if (p_flip_fov) {
  77. p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
  78. }
  79. real_t left, right, modeltranslation, ymax, xmax, frustumshift;
  80. ymax = p_z_near * tan(p_fovy_degrees * Math_PI / 360.0f);
  81. xmax = ymax * p_aspect;
  82. frustumshift = (p_intraocular_dist / 2.0) * p_z_near / p_convergence_dist;
  83. switch (p_eye) {
  84. case 1: { // left eye
  85. left = -xmax + frustumshift;
  86. right = xmax + frustumshift;
  87. modeltranslation = p_intraocular_dist / 2.0;
  88. }; break;
  89. case 2: { // right eye
  90. left = -xmax - frustumshift;
  91. right = xmax - frustumshift;
  92. modeltranslation = -p_intraocular_dist / 2.0;
  93. }; break;
  94. default: { // mono, should give the same result as set_perspective(p_fovy_degrees,p_aspect,p_z_near,p_z_far,p_flip_fov)
  95. left = -xmax;
  96. right = xmax;
  97. modeltranslation = 0.0;
  98. }; break;
  99. };
  100. set_frustum(left, right, -ymax, ymax, p_z_near, p_z_far);
  101. // translate matrix by (modeltranslation, 0.0, 0.0)
  102. CameraMatrix cm;
  103. cm.set_identity();
  104. cm.matrix[3][0] = modeltranslation;
  105. *this = *this * cm;
  106. }
  107. 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) {
  108. // we first calculate our base frustum on our values without taking our lens magnification into account.
  109. real_t display_to_eye = 2.0 * p_display_to_lens;
  110. real_t f1 = (p_intraocular_dist * 0.5) / p_display_to_lens;
  111. real_t f2 = ((p_display_width - p_intraocular_dist) * 0.5) / p_display_to_lens;
  112. real_t f3 = (p_display_width / 4.0) / p_display_to_lens;
  113. // 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
  114. // we're willing to sacrifice in FOV.
  115. real_t add = ((f1 + f2) * (p_oversample - 1.0)) / 2.0;
  116. f1 += add;
  117. f2 += add;
  118. // always apply KEEP_WIDTH aspect ratio
  119. f3 *= p_aspect;
  120. switch (p_eye) {
  121. case 1: { // left eye
  122. set_frustum(-f2 * p_z_near, f1 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far);
  123. }; break;
  124. case 2: { // right eye
  125. set_frustum(-f1 * p_z_near, f2 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far);
  126. }; break;
  127. default: { // mono, does not apply here!
  128. }; break;
  129. };
  130. };
  131. 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) {
  132. set_identity();
  133. matrix[0][0] = 2.0 / (p_right - p_left);
  134. matrix[3][0] = -((p_right + p_left) / (p_right - p_left));
  135. matrix[1][1] = 2.0 / (p_top - p_bottom);
  136. matrix[3][1] = -((p_top + p_bottom) / (p_top - p_bottom));
  137. matrix[2][2] = -2.0 / (p_zfar - p_znear);
  138. matrix[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear));
  139. matrix[3][3] = 1.0;
  140. }
  141. void CameraMatrix::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) {
  142. if (!p_flip_fov) {
  143. p_size *= p_aspect;
  144. }
  145. set_orthogonal(-p_size / 2, +p_size / 2, -p_size / p_aspect / 2, +p_size / p_aspect / 2, p_znear, p_zfar);
  146. }
  147. 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) {
  148. #if 0
  149. ///@TODO, give a check to this. I'm not sure if it's working.
  150. set_identity();
  151. matrix[0][0]=(2*p_near) / (p_right-p_left);
  152. matrix[0][2]=(p_right+p_left) / (p_right-p_left);
  153. matrix[1][1]=(2*p_near) / (p_top-p_bottom);
  154. matrix[1][2]=(p_top+p_bottom) / (p_top-p_bottom);
  155. matrix[2][2]=-(p_far+p_near) / ( p_far-p_near);
  156. matrix[2][3]=-(2*p_far*p_near) / (p_far-p_near);
  157. matrix[3][2]=-1;
  158. matrix[3][3]=0;
  159. #else
  160. real_t *te = &matrix[0][0];
  161. real_t x = 2 * p_near / (p_right - p_left);
  162. real_t y = 2 * p_near / (p_top - p_bottom);
  163. real_t a = (p_right + p_left) / (p_right - p_left);
  164. real_t b = (p_top + p_bottom) / (p_top - p_bottom);
  165. real_t c = -(p_far + p_near) / (p_far - p_near);
  166. real_t d = -2 * p_far * p_near / (p_far - p_near);
  167. te[0] = x;
  168. te[1] = 0;
  169. te[2] = 0;
  170. te[3] = 0;
  171. te[4] = 0;
  172. te[5] = y;
  173. te[6] = 0;
  174. te[7] = 0;
  175. te[8] = a;
  176. te[9] = b;
  177. te[10] = c;
  178. te[11] = -1;
  179. te[12] = 0;
  180. te[13] = 0;
  181. te[14] = d;
  182. te[15] = 0;
  183. #endif
  184. }
  185. real_t CameraMatrix::get_z_far() const {
  186. const real_t *matrix = (const real_t *)this->matrix;
  187. Plane new_plane = Plane(matrix[3] - matrix[2],
  188. matrix[7] - matrix[6],
  189. matrix[11] - matrix[10],
  190. matrix[15] - matrix[14]);
  191. new_plane.normal = -new_plane.normal;
  192. new_plane.normalize();
  193. return new_plane.d;
  194. }
  195. real_t CameraMatrix::get_z_near() 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.normalize();
  202. return new_plane.d;
  203. }
  204. void CameraMatrix::get_viewport_size(real_t &r_width, real_t &r_height) const {
  205. const real_t *matrix = (const real_t *)this->matrix;
  206. ///////--- Near Plane ---///////
  207. Plane near_plane = Plane(matrix[3] + matrix[2],
  208. matrix[7] + matrix[6],
  209. matrix[11] + matrix[10],
  210. -matrix[15] - matrix[14]);
  211. near_plane.normalize();
  212. ///////--- Right Plane ---///////
  213. Plane right_plane = Plane(matrix[3] - matrix[0],
  214. matrix[7] - matrix[4],
  215. matrix[11] - matrix[8],
  216. -matrix[15] + matrix[12]);
  217. right_plane.normalize();
  218. Plane top_plane = Plane(matrix[3] - matrix[1],
  219. matrix[7] - matrix[5],
  220. matrix[11] - matrix[9],
  221. -matrix[15] + matrix[13]);
  222. top_plane.normalize();
  223. Vector3 res;
  224. near_plane.intersect_3(right_plane, top_plane, &res);
  225. r_width = res.x;
  226. r_height = res.y;
  227. }
  228. bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const {
  229. const real_t *matrix = (const real_t *)this->matrix;
  230. ///////--- Near Plane ---///////
  231. Plane near_plane = Plane(matrix[3] + matrix[2],
  232. matrix[7] + matrix[6],
  233. matrix[11] + matrix[10],
  234. -matrix[15] - matrix[14]);
  235. near_plane.normalize();
  236. ///////--- Far Plane ---///////
  237. Plane far_plane = Plane(matrix[2] - matrix[3],
  238. matrix[6] - matrix[7],
  239. matrix[10] - matrix[11],
  240. matrix[15] - matrix[14]);
  241. far_plane.normalize();
  242. ///////--- Right Plane ---///////
  243. Plane right_plane = Plane(matrix[0] - matrix[3],
  244. matrix[4] - matrix[7],
  245. matrix[8] - matrix[11],
  246. -matrix[15] + matrix[12]);
  247. right_plane.normalize();
  248. ///////--- Top Plane ---///////
  249. Plane top_plane = Plane(matrix[1] - matrix[3],
  250. matrix[5] - matrix[7],
  251. matrix[9] - matrix[11],
  252. -matrix[15] + matrix[13]);
  253. top_plane.normalize();
  254. Vector3 near_endpoint_left, near_endpoint_right;
  255. Vector3 far_endpoint_left, far_endpoint_right;
  256. bool res = near_plane.intersect_3(right_plane, top_plane, &near_endpoint_right);
  257. ERR_FAIL_COND_V(!res, false);
  258. res = far_plane.intersect_3(right_plane, top_plane, &far_endpoint_right);
  259. ERR_FAIL_COND_V(!res, false);
  260. if ((matrix[8] == 0) && (matrix[9] == 0)) {
  261. near_endpoint_left = near_endpoint_right;
  262. near_endpoint_left.x = -near_endpoint_left.x;
  263. far_endpoint_left = far_endpoint_right;
  264. far_endpoint_left.x = -far_endpoint_left.x;
  265. } else {
  266. ///////--- Left Plane ---///////
  267. Plane left_plane = Plane(matrix[0] + matrix[3],
  268. matrix[4] + matrix[7],
  269. matrix[8] + matrix[11],
  270. -matrix[15] - matrix[12]);
  271. left_plane.normalize();
  272. res = near_plane.intersect_3(left_plane, top_plane, &near_endpoint_left);
  273. ERR_FAIL_COND_V(!res, false);
  274. res = far_plane.intersect_3(left_plane, top_plane, &far_endpoint_left);
  275. ERR_FAIL_COND_V(!res, false);
  276. }
  277. p_8points[0] = p_transform.xform(Vector3(near_endpoint_right.x, near_endpoint_right.y, near_endpoint_right.z));
  278. p_8points[1] = p_transform.xform(Vector3(near_endpoint_right.x, -near_endpoint_right.y, near_endpoint_right.z));
  279. p_8points[2] = p_transform.xform(Vector3(near_endpoint_left.x, near_endpoint_left.y, near_endpoint_left.z));
  280. p_8points[3] = p_transform.xform(Vector3(near_endpoint_left.x, -near_endpoint_left.y, near_endpoint_left.z));
  281. p_8points[4] = p_transform.xform(Vector3(far_endpoint_right.x, far_endpoint_right.y, far_endpoint_right.z));
  282. p_8points[5] = p_transform.xform(Vector3(far_endpoint_right.x, -far_endpoint_right.y, far_endpoint_right.z));
  283. p_8points[6] = p_transform.xform(Vector3(far_endpoint_left.x, far_endpoint_left.y, far_endpoint_left.z));
  284. p_8points[7] = p_transform.xform(Vector3(far_endpoint_left.x, -far_endpoint_left.y, far_endpoint_left.z));
  285. return true;
  286. }
  287. Vector<Plane> CameraMatrix::get_projection_planes(const Transform &p_transform) const {
  288. /** Fast Plane Extraction from combined modelview/projection matrices.
  289. * References:
  290. * http://www.markmorley.com/opengl/frustumculling.html
  291. * http://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) matrix[i][k] /= (-pvt_val);
  398. }
  399. /** Reduce the matrix **/
  400. for (i = 0; i < 4; i++) {
  401. hold = matrix[i][k];
  402. for (j = 0; j < 4; j++) {
  403. if (i != k && j != k) matrix[i][j] += hold * matrix[k][j];
  404. }
  405. }
  406. /** Divide row by pivot **/
  407. for (j = 0; j < 4; j++) {
  408. if (j != k) matrix[k][j] /= pvt_val;
  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. CameraMatrix::CameraMatrix() {
  434. set_identity();
  435. }
  436. CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const {
  437. CameraMatrix new_matrix;
  438. for (int j = 0; j < 4; j++) {
  439. for (int i = 0; i < 4; i++) {
  440. real_t ab = 0;
  441. for (int k = 0; k < 4; k++)
  442. ab += matrix[k][i] * p_matrix.matrix[j][k];
  443. new_matrix.matrix[j][i] = ab;
  444. }
  445. }
  446. return new_matrix;
  447. }
  448. void CameraMatrix::set_light_bias() {
  449. real_t *m = &matrix[0][0];
  450. m[0] = 0.5,
  451. m[1] = 0.0,
  452. m[2] = 0.0,
  453. m[3] = 0.0,
  454. m[4] = 0.0,
  455. m[5] = 0.5,
  456. m[6] = 0.0,
  457. m[7] = 0.0,
  458. m[8] = 0.0,
  459. m[9] = 0.0,
  460. m[10] = 0.5,
  461. m[11] = 0.0,
  462. m[12] = 0.5,
  463. m[13] = 0.5,
  464. m[14] = 0.5,
  465. m[15] = 1.0;
  466. }
  467. void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) {
  468. real_t *m = &matrix[0][0];
  469. m[0] = p_rect.size.width,
  470. m[1] = 0.0,
  471. m[2] = 0.0,
  472. m[3] = 0.0,
  473. m[4] = 0.0,
  474. m[5] = p_rect.size.height,
  475. m[6] = 0.0,
  476. m[7] = 0.0,
  477. m[8] = 0.0,
  478. m[9] = 0.0,
  479. m[10] = 1.0,
  480. m[11] = 0.0,
  481. m[12] = p_rect.position.x,
  482. m[13] = p_rect.position.y,
  483. m[14] = 0.0,
  484. m[15] = 1.0;
  485. }
  486. CameraMatrix::operator String() const {
  487. String str;
  488. for (int i = 0; i < 4; i++)
  489. for (int j = 0; j < 4; j++)
  490. str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]);
  491. return str;
  492. }
  493. real_t CameraMatrix::get_aspect() const {
  494. real_t w, h;
  495. get_viewport_size(w, h);
  496. return w / h;
  497. }
  498. int CameraMatrix::get_pixels_per_meter(int p_for_pixel_width) const {
  499. Vector3 result = xform(Vector3(1, 0, -1));
  500. return int((result.x * 0.5 + 0.5) * p_for_pixel_width);
  501. }
  502. real_t CameraMatrix::get_fov() const {
  503. const real_t *matrix = (const real_t *)this->matrix;
  504. Plane right_plane = Plane(matrix[3] - matrix[0],
  505. matrix[7] - matrix[4],
  506. matrix[11] - matrix[8],
  507. -matrix[15] + matrix[12]);
  508. right_plane.normalize();
  509. if ((matrix[8] == 0) && (matrix[9] == 0)) {
  510. return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0;
  511. } else {
  512. // our frustum is asymetrical need to calculate the left planes angle seperately..
  513. Plane left_plane = Plane(matrix[3] + matrix[0],
  514. matrix[7] + matrix[4],
  515. matrix[11] + matrix[8],
  516. matrix[15] + matrix[12]);
  517. left_plane.normalize();
  518. return Math::rad2deg(Math::acos(Math::abs(left_plane.normal.x))) + Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x)));
  519. }
  520. }
  521. void CameraMatrix::make_scale(const Vector3 &p_scale) {
  522. set_identity();
  523. matrix[0][0] = p_scale.x;
  524. matrix[1][1] = p_scale.y;
  525. matrix[2][2] = p_scale.z;
  526. }
  527. void CameraMatrix::scale_translate_to_fit(const Rect3 &p_aabb) {
  528. Vector3 min = p_aabb.position;
  529. Vector3 max = p_aabb.position + p_aabb.size;
  530. matrix[0][0] = 2 / (max.x - min.x);
  531. matrix[1][0] = 0;
  532. matrix[2][0] = 0;
  533. matrix[3][0] = -(max.x + min.x) / (max.x - min.x);
  534. matrix[0][1] = 0;
  535. matrix[1][1] = 2 / (max.y - min.y);
  536. matrix[2][1] = 0;
  537. matrix[3][1] = -(max.y + min.y) / (max.y - min.y);
  538. matrix[0][2] = 0;
  539. matrix[1][2] = 0;
  540. matrix[2][2] = 2 / (max.z - min.z);
  541. matrix[3][2] = -(max.z + min.z) / (max.z - min.z);
  542. matrix[0][3] = 0;
  543. matrix[1][3] = 0;
  544. matrix[2][3] = 0;
  545. matrix[3][3] = 1;
  546. }
  547. CameraMatrix::operator Transform() const {
  548. Transform tr;
  549. const real_t *m = &matrix[0][0];
  550. tr.basis.elements[0][0] = m[0];
  551. tr.basis.elements[1][0] = m[1];
  552. tr.basis.elements[2][0] = m[2];
  553. tr.basis.elements[0][1] = m[4];
  554. tr.basis.elements[1][1] = m[5];
  555. tr.basis.elements[2][1] = m[6];
  556. tr.basis.elements[0][2] = m[8];
  557. tr.basis.elements[1][2] = m[9];
  558. tr.basis.elements[2][2] = m[10];
  559. tr.origin.x = m[12];
  560. tr.origin.y = m[13];
  561. tr.origin.z = m[14];
  562. return tr;
  563. }
  564. CameraMatrix::CameraMatrix(const Transform &p_transform) {
  565. const Transform &tr = p_transform;
  566. real_t *m = &matrix[0][0];
  567. m[0] = tr.basis.elements[0][0];
  568. m[1] = tr.basis.elements[1][0];
  569. m[2] = tr.basis.elements[2][0];
  570. m[3] = 0.0;
  571. m[4] = tr.basis.elements[0][1];
  572. m[5] = tr.basis.elements[1][1];
  573. m[6] = tr.basis.elements[2][1];
  574. m[7] = 0.0;
  575. m[8] = tr.basis.elements[0][2];
  576. m[9] = tr.basis.elements[1][2];
  577. m[10] = tr.basis.elements[2][2];
  578. m[11] = 0.0;
  579. m[12] = tr.origin.x;
  580. m[13] = tr.origin.y;
  581. m[14] = tr.origin.z;
  582. m[15] = 1.0;
  583. }
  584. CameraMatrix::~CameraMatrix() {
  585. }