camera_matrix.cpp 17 KB

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