CameraMatrix.cpp 19 KB

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