camera_matrix.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 "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) {
  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(float p_fovy_degrees, float p_aspect, float p_z_near, float 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. float sine, cotangent, deltaZ;
  60. float 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_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float p_zfar) {
  76. set_identity();
  77. matrix[0][0] = 2.0 / (p_right - p_left);
  78. matrix[3][0] = -((p_right + p_left) / (p_right - p_left));
  79. matrix[1][1] = 2.0 / (p_top - p_bottom);
  80. matrix[3][1] = -((p_top + p_bottom) / (p_top - p_bottom));
  81. matrix[2][2] = -2.0 / (p_zfar - p_znear);
  82. matrix[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear));
  83. matrix[3][3] = 1.0;
  84. }
  85. void CameraMatrix::set_orthogonal(float p_size, float p_aspect, float p_znear, float p_zfar, bool p_flip_fov) {
  86. if (!p_flip_fov) {
  87. p_size *= p_aspect;
  88. }
  89. set_orthogonal(-p_size / 2, +p_size / 2, -p_size / p_aspect / 2, +p_size / p_aspect / 2, p_znear, p_zfar);
  90. }
  91. void CameraMatrix::set_frustum(float p_left, float p_right, float p_bottom, float p_top, float p_near, float p_far) {
  92. #if 0
  93. ///@TODO, give a check to this. I'm not sure if it's working.
  94. set_identity();
  95. matrix[0][0]=(2*p_near) / (p_right-p_left);
  96. matrix[0][2]=(p_right+p_left) / (p_right-p_left);
  97. matrix[1][1]=(2*p_near) / (p_top-p_bottom);
  98. matrix[1][2]=(p_top+p_bottom) / (p_top-p_bottom);
  99. matrix[2][2]=-(p_far+p_near) / ( p_far-p_near);
  100. matrix[2][3]=-(2*p_far*p_near) / (p_far-p_near);
  101. matrix[3][2]=-1;
  102. matrix[3][3]=0;
  103. #else
  104. float *te = &matrix[0][0];
  105. float x = 2 * p_near / (p_right - p_left);
  106. float y = 2 * p_near / (p_top - p_bottom);
  107. float a = (p_right + p_left) / (p_right - p_left);
  108. float b = (p_top + p_bottom) / (p_top - p_bottom);
  109. float c = -(p_far + p_near) / (p_far - p_near);
  110. float d = -2 * p_far * p_near / (p_far - p_near);
  111. te[0] = x;
  112. te[1] = 0;
  113. te[2] = 0;
  114. te[3] = 0;
  115. te[4] = 0;
  116. te[5] = y;
  117. te[6] = 0;
  118. te[7] = 0;
  119. te[8] = a;
  120. te[9] = b;
  121. te[10] = c;
  122. te[11] = -1;
  123. te[12] = 0;
  124. te[13] = 0;
  125. te[14] = d;
  126. te[15] = 0;
  127. #endif
  128. }
  129. float CameraMatrix::get_z_far() const {
  130. const float *matrix = (const float *)this->matrix;
  131. Plane new_plane = Plane(matrix[3] - matrix[2],
  132. matrix[7] - matrix[6],
  133. matrix[11] - matrix[10],
  134. matrix[15] - matrix[14]);
  135. new_plane.normal = -new_plane.normal;
  136. new_plane.normalize();
  137. return new_plane.d;
  138. }
  139. float CameraMatrix::get_z_near() const {
  140. const float *matrix = (const float *)this->matrix;
  141. Plane new_plane = Plane(matrix[3] + matrix[2],
  142. matrix[7] + matrix[6],
  143. matrix[11] + matrix[10],
  144. -matrix[15] - matrix[14]);
  145. new_plane.normalize();
  146. return new_plane.d;
  147. }
  148. void CameraMatrix::get_viewport_size(float &r_width, float &r_height) const {
  149. const float *matrix = (const float *)this->matrix;
  150. ///////--- Near Plane ---///////
  151. Plane near_plane = Plane(matrix[3] + matrix[2],
  152. matrix[7] + matrix[6],
  153. matrix[11] + matrix[10],
  154. -matrix[15] - matrix[14]);
  155. near_plane.normalize();
  156. ///////--- Right Plane ---///////
  157. Plane right_plane = Plane(matrix[3] - matrix[0],
  158. matrix[7] - matrix[4],
  159. matrix[11] - matrix[8],
  160. -matrix[15] + matrix[12]);
  161. right_plane.normalize();
  162. Plane top_plane = Plane(matrix[3] - matrix[1],
  163. matrix[7] - matrix[5],
  164. matrix[11] - matrix[9],
  165. -matrix[15] + matrix[13]);
  166. top_plane.normalize();
  167. Vector3 res;
  168. near_plane.intersect_3(right_plane, top_plane, &res);
  169. r_width = res.x;
  170. r_height = res.y;
  171. }
  172. bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const {
  173. const float *matrix = (const float *)this->matrix;
  174. ///////--- Near Plane ---///////
  175. Plane near_plane = Plane(matrix[3] + matrix[2],
  176. matrix[7] + matrix[6],
  177. matrix[11] + matrix[10],
  178. -matrix[15] - matrix[14]);
  179. near_plane.normalize();
  180. ///////--- Far Plane ---///////
  181. Plane far_plane = Plane(matrix[2] - matrix[3],
  182. matrix[6] - matrix[7],
  183. matrix[10] - matrix[11],
  184. matrix[15] - matrix[14]);
  185. far_plane.normalize();
  186. ///////--- Right Plane ---///////
  187. Plane right_plane = Plane(matrix[0] - matrix[3],
  188. matrix[4] - matrix[7],
  189. matrix[8] - matrix[11],
  190. -matrix[15] + matrix[12]);
  191. right_plane.normalize();
  192. ///////--- Top Plane ---///////
  193. Plane top_plane = Plane(matrix[1] - matrix[3],
  194. matrix[5] - matrix[7],
  195. matrix[9] - matrix[11],
  196. -matrix[15] + matrix[13]);
  197. top_plane.normalize();
  198. Vector3 near_endpoint;
  199. Vector3 far_endpoint;
  200. bool res = near_plane.intersect_3(right_plane, top_plane, &near_endpoint);
  201. ERR_FAIL_COND_V(!res, false);
  202. res = far_plane.intersect_3(right_plane, top_plane, &far_endpoint);
  203. ERR_FAIL_COND_V(!res, false);
  204. p_8points[0] = p_transform.xform(Vector3(near_endpoint.x, near_endpoint.y, near_endpoint.z));
  205. p_8points[1] = p_transform.xform(Vector3(near_endpoint.x, -near_endpoint.y, near_endpoint.z));
  206. p_8points[2] = p_transform.xform(Vector3(-near_endpoint.x, near_endpoint.y, near_endpoint.z));
  207. p_8points[3] = p_transform.xform(Vector3(-near_endpoint.x, -near_endpoint.y, near_endpoint.z));
  208. p_8points[4] = p_transform.xform(Vector3(far_endpoint.x, far_endpoint.y, far_endpoint.z));
  209. p_8points[5] = p_transform.xform(Vector3(far_endpoint.x, -far_endpoint.y, far_endpoint.z));
  210. p_8points[6] = p_transform.xform(Vector3(-far_endpoint.x, far_endpoint.y, far_endpoint.z));
  211. p_8points[7] = p_transform.xform(Vector3(-far_endpoint.x, -far_endpoint.y, far_endpoint.z));
  212. return true;
  213. }
  214. Vector<Plane> CameraMatrix::get_projection_planes(const Transform &p_transform) const {
  215. /** Fast Plane Extraction from combined modelview/projection matrices.
  216. * References:
  217. * http://www.markmorley.com/opengl/frustumculling.html
  218. * http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
  219. */
  220. Vector<Plane> planes;
  221. const float *matrix = (const float *)this->matrix;
  222. Plane new_plane;
  223. ///////--- Near Plane ---///////
  224. new_plane = Plane(matrix[3] + matrix[2],
  225. matrix[7] + matrix[6],
  226. matrix[11] + matrix[10],
  227. matrix[15] + matrix[14]);
  228. new_plane.normal = -new_plane.normal;
  229. new_plane.normalize();
  230. planes.push_back(p_transform.xform(new_plane));
  231. ///////--- Far Plane ---///////
  232. new_plane = Plane(matrix[3] - matrix[2],
  233. matrix[7] - matrix[6],
  234. matrix[11] - matrix[10],
  235. matrix[15] - matrix[14]);
  236. new_plane.normal = -new_plane.normal;
  237. new_plane.normalize();
  238. planes.push_back(p_transform.xform(new_plane));
  239. ///////--- Left Plane ---///////
  240. new_plane = Plane(matrix[3] + matrix[0],
  241. matrix[7] + matrix[4],
  242. matrix[11] + matrix[8],
  243. matrix[15] + matrix[12]);
  244. new_plane.normal = -new_plane.normal;
  245. new_plane.normalize();
  246. planes.push_back(p_transform.xform(new_plane));
  247. ///////--- Top Plane ---///////
  248. new_plane = Plane(matrix[3] - matrix[1],
  249. matrix[7] - matrix[5],
  250. matrix[11] - matrix[9],
  251. matrix[15] - matrix[13]);
  252. new_plane.normal = -new_plane.normal;
  253. new_plane.normalize();
  254. planes.push_back(p_transform.xform(new_plane));
  255. ///////--- Right Plane ---///////
  256. new_plane = Plane(matrix[3] - matrix[0],
  257. matrix[7] - matrix[4],
  258. matrix[11] - matrix[8],
  259. matrix[15] - matrix[12]);
  260. new_plane.normal = -new_plane.normal;
  261. new_plane.normalize();
  262. planes.push_back(p_transform.xform(new_plane));
  263. ///////--- Bottom Plane ---///////
  264. new_plane = Plane(matrix[3] + matrix[1],
  265. matrix[7] + matrix[5],
  266. matrix[11] + matrix[9],
  267. matrix[15] + matrix[13]);
  268. new_plane.normal = -new_plane.normal;
  269. new_plane.normalize();
  270. planes.push_back(p_transform.xform(new_plane));
  271. return planes;
  272. }
  273. CameraMatrix CameraMatrix::inverse() const {
  274. CameraMatrix cm = *this;
  275. cm.invert();
  276. return cm;
  277. }
  278. void CameraMatrix::invert() {
  279. int i, j, k;
  280. int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
  281. float pvt_val; /* Value of current pivot element */
  282. float hold; /* Temporary storage */
  283. float determinat; /* Determinant */
  284. determinat = 1.0;
  285. for (k = 0; k < 4; k++) {
  286. /** Locate k'th pivot element **/
  287. pvt_val = matrix[k][k]; /** Initialize for search **/
  288. pvt_i[k] = k;
  289. pvt_j[k] = k;
  290. for (i = k; i < 4; i++) {
  291. for (j = k; j < 4; j++) {
  292. if (Math::absd(matrix[i][j]) > Math::absd(pvt_val)) {
  293. pvt_i[k] = i;
  294. pvt_j[k] = j;
  295. pvt_val = matrix[i][j];
  296. }
  297. }
  298. }
  299. /** Product of pivots, gives determinant when finished **/
  300. determinat *= pvt_val;
  301. if (Math::absd(determinat) < 1e-7) {
  302. return; //(false); /** Matrix is singular (zero determinant). **/
  303. }
  304. /** "Interchange" rows (with sign change stuff) **/
  305. i = pvt_i[k];
  306. if (i != k) { /** If rows are different **/
  307. for (j = 0; j < 4; j++) {
  308. hold = -matrix[k][j];
  309. matrix[k][j] = matrix[i][j];
  310. matrix[i][j] = hold;
  311. }
  312. }
  313. /** "Interchange" columns **/
  314. j = pvt_j[k];
  315. if (j != k) { /** If columns are different **/
  316. for (i = 0; i < 4; i++) {
  317. hold = -matrix[i][k];
  318. matrix[i][k] = matrix[i][j];
  319. matrix[i][j] = hold;
  320. }
  321. }
  322. /** Divide column by minus pivot value **/
  323. for (i = 0; i < 4; i++) {
  324. if (i != k) matrix[i][k] /= (-pvt_val);
  325. }
  326. /** Reduce the matrix **/
  327. for (i = 0; i < 4; i++) {
  328. hold = matrix[i][k];
  329. for (j = 0; j < 4; j++) {
  330. if (i != k && j != k) matrix[i][j] += hold * matrix[k][j];
  331. }
  332. }
  333. /** Divide row by pivot **/
  334. for (j = 0; j < 4; j++) {
  335. if (j != k) matrix[k][j] /= pvt_val;
  336. }
  337. /** Replace pivot by reciprocal (at last we can touch it). **/
  338. matrix[k][k] = 1.0 / pvt_val;
  339. }
  340. /* That was most of the work, one final pass of row/column interchange */
  341. /* to finish */
  342. for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
  343. i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
  344. if (i != k) { /* If rows are different */
  345. for (j = 0; j < 4; j++) {
  346. hold = matrix[k][j];
  347. matrix[k][j] = -matrix[i][j];
  348. matrix[i][j] = hold;
  349. }
  350. }
  351. j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
  352. if (j != k) /* If columns are different */
  353. for (i = 0; i < 4; i++) {
  354. hold = matrix[i][k];
  355. matrix[i][k] = -matrix[i][j];
  356. matrix[i][j] = hold;
  357. }
  358. }
  359. }
  360. CameraMatrix::CameraMatrix() {
  361. set_identity();
  362. }
  363. CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const {
  364. CameraMatrix new_matrix;
  365. for (int j = 0; j < 4; j++) {
  366. for (int i = 0; i < 4; i++) {
  367. real_t ab = 0;
  368. for (int k = 0; k < 4; k++)
  369. ab += matrix[k][i] * p_matrix.matrix[j][k];
  370. new_matrix.matrix[j][i] = ab;
  371. }
  372. }
  373. return new_matrix;
  374. }
  375. void CameraMatrix::set_light_bias() {
  376. float *m = &matrix[0][0];
  377. m[0] = 0.5,
  378. m[1] = 0.0,
  379. m[2] = 0.0,
  380. m[3] = 0.0,
  381. m[4] = 0.0,
  382. m[5] = 0.5,
  383. m[6] = 0.0,
  384. m[7] = 0.0,
  385. m[8] = 0.0,
  386. m[9] = 0.0,
  387. m[10] = 0.5,
  388. m[11] = 0.0,
  389. m[12] = 0.5,
  390. m[13] = 0.5,
  391. m[14] = 0.5,
  392. m[15] = 1.0;
  393. }
  394. CameraMatrix::operator String() const {
  395. String str;
  396. for (int i = 0; i < 4; i++)
  397. for (int j = 0; j < 4; j++)
  398. str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]);
  399. return str;
  400. }
  401. float CameraMatrix::get_aspect() const {
  402. float w, h;
  403. get_viewport_size(w, h);
  404. return w / h;
  405. }
  406. float CameraMatrix::get_fov() const {
  407. const float *matrix = (const float *)this->matrix;
  408. Plane right_plane = Plane(matrix[3] - matrix[0],
  409. matrix[7] - matrix[4],
  410. matrix[11] - matrix[8],
  411. -matrix[15] + matrix[12]);
  412. right_plane.normalize();
  413. return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0;
  414. }
  415. void CameraMatrix::make_scale(const Vector3 &p_scale) {
  416. set_identity();
  417. matrix[0][0] = p_scale.x;
  418. matrix[1][1] = p_scale.y;
  419. matrix[2][2] = p_scale.z;
  420. }
  421. void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) {
  422. Vector3 min = p_aabb.pos;
  423. Vector3 max = p_aabb.pos + p_aabb.size;
  424. matrix[0][0] = 2 / (max.x - min.x);
  425. matrix[1][0] = 0;
  426. matrix[2][0] = 0;
  427. matrix[3][0] = -(max.x + min.x) / (max.x - min.x);
  428. matrix[0][1] = 0;
  429. matrix[1][1] = 2 / (max.y - min.y);
  430. matrix[2][1] = 0;
  431. matrix[3][1] = -(max.y + min.y) / (max.y - min.y);
  432. matrix[0][2] = 0;
  433. matrix[1][2] = 0;
  434. matrix[2][2] = 2 / (max.z - min.z);
  435. matrix[3][2] = -(max.z + min.z) / (max.z - min.z);
  436. matrix[0][3] = 0;
  437. matrix[1][3] = 0;
  438. matrix[2][3] = 0;
  439. matrix[3][3] = 1;
  440. }
  441. CameraMatrix::operator Transform() const {
  442. Transform tr;
  443. const float *m = &matrix[0][0];
  444. tr.basis.elements[0][0] = m[0];
  445. tr.basis.elements[1][0] = m[1];
  446. tr.basis.elements[2][0] = m[2];
  447. tr.basis.elements[0][1] = m[4];
  448. tr.basis.elements[1][1] = m[5];
  449. tr.basis.elements[2][1] = m[6];
  450. tr.basis.elements[0][2] = m[8];
  451. tr.basis.elements[1][2] = m[9];
  452. tr.basis.elements[2][2] = m[10];
  453. tr.origin.x = m[12];
  454. tr.origin.y = m[13];
  455. tr.origin.z = m[14];
  456. return tr;
  457. }
  458. CameraMatrix::CameraMatrix(const Transform &p_transform) {
  459. const Transform &tr = p_transform;
  460. float *m = &matrix[0][0];
  461. m[0] = tr.basis.elements[0][0];
  462. m[1] = tr.basis.elements[1][0];
  463. m[2] = tr.basis.elements[2][0];
  464. m[3] = 0.0;
  465. m[4] = tr.basis.elements[0][1];
  466. m[5] = tr.basis.elements[1][1];
  467. m[6] = tr.basis.elements[2][1];
  468. m[7] = 0.0;
  469. m[8] = tr.basis.elements[0][2];
  470. m[9] = tr.basis.elements[1][2];
  471. m[10] = tr.basis.elements[2][2];
  472. m[11] = 0.0;
  473. m[12] = tr.origin.x;
  474. m[13] = tr.origin.y;
  475. m[14] = tr.origin.z;
  476. m[15] = 1.0;
  477. }
  478. CameraMatrix::~CameraMatrix() {
  479. }