camera_matrix.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "camera_matrix.h"
  30. #include "math_funcs.h"
  31. #include "print_string.h"
  32. void CameraMatrix::set_identity() {
  33. for (int i=0;i<4;i++) {
  34. for (int j=0;j<4;j++) {
  35. matrix[i][j]=(i==j)?1:0;
  36. }
  37. }
  38. }
  39. void CameraMatrix::set_zero() {
  40. for (int i=0;i<4;i++) {
  41. for (int j=0;j<4;j++) {
  42. matrix[i][j]=0;
  43. }
  44. }
  45. }
  46. Plane CameraMatrix::xform4(const Plane& p_vec4) {
  47. Plane ret;
  48. 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;
  49. 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;
  50. 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;
  51. 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;
  52. return ret;
  53. }
  54. void CameraMatrix::set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float p_z_far,bool p_flip_fov) {
  55. if (p_flip_fov) {
  56. p_fovy_degrees=get_fovy(p_fovy_degrees,1.0/p_aspect);
  57. }
  58. float sine, cotangent, deltaZ;
  59. float radians = p_fovy_degrees / 2.0 * Math_PI / 180.0;
  60. deltaZ = p_z_far - p_z_near;
  61. sine = Math::sin(radians);
  62. if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) {
  63. return ;
  64. }
  65. cotangent = Math::cos(radians) / sine;
  66. set_identity();
  67. matrix[0][0] = cotangent / p_aspect;
  68. matrix[1][1] = cotangent;
  69. matrix[2][2] = -(p_z_far + p_z_near) / deltaZ;
  70. matrix[2][3] = -1;
  71. matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ;
  72. matrix[3][3] = 0;
  73. }
  74. void CameraMatrix::set_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float p_zfar) {
  75. set_identity();
  76. matrix[0][0] = 2.0/(p_right-p_left);
  77. matrix[3][0] = -((p_right+p_left)/(p_right-p_left));
  78. matrix[1][1] = 2.0/(p_top-p_bottom);
  79. matrix[3][1] = -((p_top+p_bottom)/(p_top-p_bottom));
  80. matrix[2][2] = -2.0/(p_zfar-p_znear);
  81. matrix[3][2] = -((p_zfar+p_znear)/(p_zfar-p_znear));
  82. matrix[3][3] = 1.0;
  83. }
  84. void CameraMatrix::set_orthogonal(float p_size, float p_aspect, float p_znear, float p_zfar,bool p_flip_fov) {
  85. if (!p_flip_fov) {
  86. p_size*=p_aspect;
  87. }
  88. set_orthogonal(-p_size/2,+p_size/2,-p_size/p_aspect/2,+p_size/p_aspect/2,p_znear,p_zfar);
  89. }
  90. void CameraMatrix::set_frustum(float p_left, float p_right, float p_bottom, float p_top, float p_near, float p_far) {
  91. #if 0
  92. ///@TODO, give a check to this. I'm not sure if it's working.
  93. set_identity();
  94. matrix[0][0]=(2*p_near) / (p_right-p_left);
  95. matrix[0][2]=(p_right+p_left) / (p_right-p_left);
  96. matrix[1][1]=(2*p_near) / (p_top-p_bottom);
  97. matrix[1][2]=(p_top+p_bottom) / (p_top-p_bottom);
  98. matrix[2][2]=-(p_far+p_near) / ( p_far-p_near);
  99. matrix[2][3]=-(2*p_far*p_near) / (p_far-p_near);
  100. matrix[3][2]=-1;
  101. matrix[3][3]=0;
  102. #else
  103. float *te = &matrix[0][0];
  104. float x = 2 * p_near / ( p_right - p_left );
  105. float y = 2 * p_near / ( p_top - p_bottom );
  106. float a = ( p_right + p_left ) / ( p_right - p_left );
  107. float b = ( p_top + p_bottom ) / ( p_top - p_bottom );
  108. float c = - ( p_far + p_near ) / ( p_far - p_near );
  109. float d = - 2 * p_far * p_near / ( p_far - p_near );
  110. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  111. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  112. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  113. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  114. #endif
  115. }
  116. float CameraMatrix::get_z_far() const {
  117. const float * matrix = (const float*)this->matrix;
  118. Plane new_plane=Plane(matrix[ 3] - matrix[ 2],
  119. matrix[ 7] - matrix[ 6],
  120. matrix[11] - matrix[10],
  121. matrix[15] - matrix[14]);
  122. new_plane.normal=-new_plane.normal;
  123. new_plane.normalize();
  124. return new_plane.d;
  125. }
  126. float CameraMatrix::get_z_near() const {
  127. const float * matrix = (const float*)this->matrix;
  128. Plane new_plane=Plane(matrix[ 3] + matrix[ 2],
  129. matrix[ 7] + matrix[ 6],
  130. matrix[11] + matrix[10],
  131. -matrix[15] - matrix[14]);
  132. new_plane.normalize();
  133. return new_plane.d;
  134. }
  135. void CameraMatrix::get_viewport_size(float& r_width, float& r_height) const {
  136. const float * matrix = (const float*)this->matrix;
  137. ///////--- Near Plane ---///////
  138. Plane near_plane=Plane(matrix[ 3] + matrix[ 2],
  139. matrix[ 7] + matrix[ 6],
  140. matrix[11] + matrix[10],
  141. -matrix[15] - matrix[14]).normalized();
  142. ///////--- Right Plane ---///////
  143. Plane right_plane=Plane(matrix[ 3] - matrix[ 0],
  144. matrix[ 7] - matrix[ 4],
  145. matrix[11] - matrix[ 8],
  146. - matrix[15] + matrix[12]).normalized();
  147. Plane top_plane=Plane(matrix[ 3] - matrix[ 1],
  148. matrix[ 7] - matrix[ 5],
  149. matrix[11] - matrix[ 9],
  150. -matrix[15] + matrix[13]).normalized();
  151. Vector3 res;
  152. near_plane.intersect_3(right_plane,top_plane,&res);
  153. r_width=res.x;
  154. r_height=res.y;
  155. }
  156. bool CameraMatrix::get_endpoints(const Transform& p_transform, Vector3 *p_8points) const {
  157. const float * matrix = (const float*)this->matrix;
  158. ///////--- Near Plane ---///////
  159. Plane near_plane=Plane(matrix[ 3] + matrix[ 2],
  160. matrix[ 7] + matrix[ 6],
  161. matrix[11] + matrix[10],
  162. -matrix[15] - matrix[14]).normalized();
  163. ///////--- Far Plane ---///////
  164. Plane far_plane=Plane(matrix[ 2] - matrix[ 3],
  165. matrix[ 6] - matrix[ 7],
  166. matrix[10] - matrix[11],
  167. matrix[15] - matrix[14]).normalized();
  168. ///////--- Right Plane ---///////
  169. Plane right_plane=Plane(matrix[ 0] - matrix[ 3],
  170. matrix[ 4] - matrix[ 7],
  171. matrix[8] - matrix[ 11],
  172. - matrix[15] + matrix[12]).normalized();
  173. ///////--- Top Plane ---///////
  174. Plane top_plane=Plane(matrix[ 1] - matrix[ 3],
  175. matrix[ 5] - matrix[ 7],
  176. matrix[9] - matrix[ 11],
  177. -matrix[15] + matrix[13]).normalized();
  178. Vector3 near_endpoint;
  179. Vector3 far_endpoint;
  180. bool res=near_plane.intersect_3(right_plane,top_plane,&near_endpoint);
  181. ERR_FAIL_COND_V(!res,false);
  182. res=far_plane.intersect_3(right_plane,top_plane,&far_endpoint);
  183. ERR_FAIL_COND_V(!res,false);
  184. p_8points[0]=p_transform.xform( Vector3( near_endpoint.x, near_endpoint.y, near_endpoint.z ) );
  185. p_8points[1]=p_transform.xform( Vector3( near_endpoint.x,-near_endpoint.y, near_endpoint.z ) );
  186. p_8points[2]=p_transform.xform( Vector3(-near_endpoint.x, near_endpoint.y, near_endpoint.z ) );
  187. p_8points[3]=p_transform.xform( Vector3(-near_endpoint.x,-near_endpoint.y, near_endpoint.z ) );
  188. p_8points[4]=p_transform.xform( Vector3( far_endpoint.x, far_endpoint.y, far_endpoint.z ) );
  189. p_8points[5]=p_transform.xform( Vector3( far_endpoint.x,-far_endpoint.y, far_endpoint.z ) );
  190. p_8points[6]=p_transform.xform( Vector3(-far_endpoint.x, far_endpoint.y, far_endpoint.z ) );
  191. p_8points[7]=p_transform.xform( Vector3(-far_endpoint.x,-far_endpoint.y, far_endpoint.z ) );
  192. return true;
  193. }
  194. Vector<Plane> CameraMatrix::get_projection_planes(const Transform& p_transform) const {
  195. /** Fast Plane Extraction from combined modelview/projection matrices.
  196. * References:
  197. * http://www.markmorley.com/opengl/frustumculling.html
  198. * http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
  199. */
  200. Vector<Plane> planes;
  201. const float * matrix = (const float*)this->matrix;
  202. Plane new_plane;
  203. ///////--- Near Plane ---///////
  204. new_plane=Plane(matrix[ 3] + matrix[ 2],
  205. matrix[ 7] + matrix[ 6],
  206. matrix[11] + matrix[10],
  207. matrix[15] + matrix[14]);
  208. new_plane.normal=-new_plane.normal;
  209. new_plane.normalize();
  210. planes.push_back( p_transform.xform(new_plane) );
  211. ///////--- Far Plane ---///////
  212. new_plane=Plane(matrix[ 3] - matrix[ 2],
  213. matrix[ 7] - matrix[ 6],
  214. matrix[11] - matrix[10],
  215. matrix[15] - matrix[14]);
  216. new_plane.normal=-new_plane.normal;
  217. new_plane.normalize();
  218. planes.push_back( p_transform.xform(new_plane) );
  219. ///////--- Left Plane ---///////
  220. new_plane=Plane(matrix[ 3] + matrix[ 0],
  221. matrix[ 7] + matrix[ 4],
  222. matrix[11] + matrix[ 8],
  223. matrix[15] + matrix[12]);
  224. new_plane.normal=-new_plane.normal;
  225. new_plane.normalize();
  226. planes.push_back( p_transform.xform(new_plane) );
  227. ///////--- Top Plane ---///////
  228. new_plane=Plane(matrix[ 3] - matrix[ 1],
  229. matrix[ 7] - matrix[ 5],
  230. matrix[11] - matrix[ 9],
  231. matrix[15] - matrix[13]);
  232. new_plane.normal=-new_plane.normal;
  233. new_plane.normalize();
  234. planes.push_back( p_transform.xform(new_plane) );
  235. ///////--- Right Plane ---///////
  236. new_plane=Plane(matrix[ 3] - matrix[ 0],
  237. matrix[ 7] - matrix[ 4],
  238. matrix[11] - matrix[ 8],
  239. matrix[15] - matrix[12]);
  240. new_plane.normal=-new_plane.normal;
  241. new_plane.normalize();
  242. planes.push_back( p_transform.xform(new_plane) );
  243. ///////--- Bottom Plane ---///////
  244. new_plane=Plane(matrix[ 3] + matrix[ 1],
  245. matrix[ 7] + matrix[ 5],
  246. matrix[11] + matrix[ 9],
  247. matrix[15] + matrix[13]);
  248. new_plane.normal=-new_plane.normal;
  249. new_plane.normalize();
  250. planes.push_back( p_transform.xform(new_plane) );
  251. return planes;
  252. }
  253. CameraMatrix CameraMatrix::inverse() const {
  254. CameraMatrix cm = *this;
  255. cm.invert();
  256. return cm;
  257. }
  258. void CameraMatrix::invert() {
  259. int i,j,k;
  260. int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
  261. float pvt_val; /* Value of current pivot element */
  262. float hold; /* Temporary storage */
  263. float determinat; /* Determinant */
  264. determinat = 1.0;
  265. for (k=0; k<4; k++) {
  266. /** Locate k'th pivot element **/
  267. pvt_val=matrix[k][k]; /** Initialize for search **/
  268. pvt_i[k]=k;
  269. pvt_j[k]=k;
  270. for (i=k; i<4; i++) {
  271. for (j=k; j<4; j++) {
  272. if (Math::absd(matrix[i][j]) > Math::absd(pvt_val)) {
  273. pvt_i[k]=i;
  274. pvt_j[k]=j;
  275. pvt_val=matrix[i][j];
  276. }
  277. }
  278. }
  279. /** Product of pivots, gives determinant when finished **/
  280. determinat*=pvt_val;
  281. if (Math::absd(determinat)<1e-7) {
  282. return; //(false); /** Matrix is singular (zero determinant). **/
  283. }
  284. /** "Interchange" rows (with sign change stuff) **/
  285. i=pvt_i[k];
  286. if (i!=k) { /** If rows are different **/
  287. for (j=0; j<4; j++) {
  288. hold=-matrix[k][j];
  289. matrix[k][j]=matrix[i][j];
  290. matrix[i][j]=hold;
  291. }
  292. }
  293. /** "Interchange" columns **/
  294. j=pvt_j[k];
  295. if (j!=k) { /** If columns are different **/
  296. for (i=0; i<4; i++) {
  297. hold=-matrix[i][k];
  298. matrix[i][k]=matrix[i][j];
  299. matrix[i][j]=hold;
  300. }
  301. }
  302. /** Divide column by minus pivot value **/
  303. for (i=0; i<4; i++) {
  304. if (i!=k) matrix[i][k]/=( -pvt_val) ;
  305. }
  306. /** Reduce the matrix **/
  307. for (i=0; i<4; i++) {
  308. hold = matrix[i][k];
  309. for (j=0; j<4; j++) {
  310. if (i!=k && j!=k) matrix[i][j]+=hold*matrix[k][j];
  311. }
  312. }
  313. /** Divide row by pivot **/
  314. for (j=0; j<4; j++) {
  315. if (j!=k) matrix[k][j]/=pvt_val;
  316. }
  317. /** Replace pivot by reciprocal (at last we can touch it). **/
  318. matrix[k][k] = 1.0/pvt_val;
  319. }
  320. /* That was most of the work, one final pass of row/column interchange */
  321. /* to finish */
  322. for (k=4-2; k>=0; k--) { /* Don't need to work with 1 by 1 corner*/
  323. i=pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
  324. if (i!=k) { /* If rows are different */
  325. for(j=0; j<4; j++) {
  326. hold = matrix[k][j];
  327. matrix[k][j]=-matrix[i][j];
  328. matrix[i][j]=hold;
  329. }
  330. }
  331. j=pvt_i[k]; /* Columns to swap correspond to pivot ROW */
  332. if (j!=k) /* If columns are different */
  333. for (i=0; i<4; i++) {
  334. hold=matrix[i][k];
  335. matrix[i][k]=-matrix[i][j];
  336. matrix[i][j]=hold;
  337. }
  338. }
  339. }
  340. CameraMatrix::CameraMatrix() {
  341. set_identity();
  342. }
  343. CameraMatrix CameraMatrix::operator*(const CameraMatrix& p_matrix) const {
  344. CameraMatrix new_matrix;
  345. for( int j = 0; j < 4; j++ ) {
  346. for( int i = 0; i < 4; i++ ) {
  347. real_t ab = 0;
  348. for( int k = 0; k < 4; k++ )
  349. ab += matrix[k][i] * p_matrix.matrix[j][k] ;
  350. new_matrix.matrix[j][i] = ab;
  351. }
  352. }
  353. return new_matrix;
  354. }
  355. void CameraMatrix::set_light_bias() {
  356. float *m=&matrix[0][0];
  357. m[0]=0.5,
  358. m[1]=0.0,
  359. m[2]=0.0,
  360. m[3]=0.0,
  361. m[4]=0.0,
  362. m[5]=0.5,
  363. m[6]=0.0,
  364. m[7]=0.0,
  365. m[8]=0.0,
  366. m[9]=0.0,
  367. m[10]=0.5,
  368. m[11]=0.0,
  369. m[12]=0.5,
  370. m[13]=0.5,
  371. m[14]=0.5,
  372. m[15]=1.0;
  373. }
  374. CameraMatrix::operator String() const {
  375. String str;
  376. for (int i=0;i<4;i++)
  377. for (int j=0;j<4;j++)
  378. str+=String((j>0)?", ":"\n")+rtos(matrix[i][j]);
  379. return str;
  380. }
  381. float CameraMatrix::get_aspect() const {
  382. float w,h;
  383. get_viewport_size(w,h);
  384. return w/h;
  385. }
  386. float CameraMatrix::get_fov() const {
  387. const float * matrix = (const float*)this->matrix;
  388. Plane right_plane=Plane(matrix[ 3] - matrix[ 0],
  389. matrix[ 7] - matrix[ 4],
  390. matrix[11] - matrix[ 8],
  391. - matrix[15] + matrix[12]).normalized();
  392. return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x)))*2.0;
  393. }
  394. void CameraMatrix::make_scale(const Vector3 &p_scale) {
  395. set_identity();
  396. matrix[0][0]=p_scale.x;
  397. matrix[1][1]=p_scale.y;
  398. matrix[2][2]=p_scale.z;
  399. }
  400. void CameraMatrix::scale_translate_to_fit(const AABB& p_aabb) {
  401. Vector3 min = p_aabb.pos;
  402. Vector3 max = p_aabb.pos+p_aabb.size;
  403. matrix[0][0]=2/(max.x-min.x);
  404. matrix[1][0]=0;
  405. matrix[2][0]=0;
  406. matrix[3][0]=-(max.x+min.x)/(max.x-min.x);
  407. matrix[0][1]=0;
  408. matrix[1][1]=2/(max.y-min.y);
  409. matrix[2][1]=0;
  410. matrix[3][1]=-(max.y+min.y)/(max.y-min.y);
  411. matrix[0][2]=0;
  412. matrix[1][2]=0;
  413. matrix[2][2]=2/(max.z-min.z);
  414. matrix[3][2]=-(max.z+min.z)/(max.z-min.z);
  415. matrix[0][3]=0;
  416. matrix[1][3]=0;
  417. matrix[2][3]=0;
  418. matrix[3][3]=1;
  419. }
  420. CameraMatrix::operator Transform() const {
  421. Transform tr;
  422. const float *m=&matrix[0][0];
  423. tr.basis.elements[0][0]=m[0];
  424. tr.basis.elements[1][0]=m[1];
  425. tr.basis.elements[2][0]=m[2];
  426. tr.basis.elements[0][1]=m[4];
  427. tr.basis.elements[1][1]=m[5];
  428. tr.basis.elements[2][1]=m[6];
  429. tr.basis.elements[0][2]=m[8];
  430. tr.basis.elements[1][2]=m[9];
  431. tr.basis.elements[2][2]=m[10];
  432. tr.origin.x=m[12];
  433. tr.origin.y=m[13];
  434. tr.origin.z=m[14];
  435. return tr;
  436. }
  437. CameraMatrix::CameraMatrix(const Transform& p_transform) {
  438. const Transform &tr = p_transform;
  439. float *m=&matrix[0][0];
  440. m[0]=tr.basis.elements[0][0];
  441. m[1]=tr.basis.elements[1][0];
  442. m[2]=tr.basis.elements[2][0];
  443. m[3]=0.0;
  444. m[4]=tr.basis.elements[0][1];
  445. m[5]=tr.basis.elements[1][1];
  446. m[6]=tr.basis.elements[2][1];
  447. m[7]=0.0;
  448. m[8]=tr.basis.elements[0][2];
  449. m[9]=tr.basis.elements[1][2];
  450. m[10]=tr.basis.elements[2][2];
  451. m[11]=0.0;
  452. m[12]=tr.origin.x;
  453. m[13]=tr.origin.y;
  454. m[14]=tr.origin.z;
  455. m[15]=1.0;
  456. }
  457. CameraMatrix::~CameraMatrix()
  458. {
  459. }