camera_matrix.cpp 16 KB

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