1 /*************************************************************************/
2 /* camera_matrix.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 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
set_identity()34 void CameraMatrix::set_identity() {
35
36 for (int i = 0; i < 4; i++) {
37
38 for (int j = 0; j < 4; j++) {
39
40 matrix[i][j] = (i == j) ? 1 : 0;
41 }
42 }
43 }
44
set_zero()45 void CameraMatrix::set_zero() {
46
47 for (int i = 0; i < 4; i++) {
48
49 for (int j = 0; j < 4; j++) {
50
51 matrix[i][j] = 0;
52 }
53 }
54 }
55
xform4(const Plane & p_vec4)56 Plane CameraMatrix::xform4(const Plane &p_vec4) {
57
58 Plane ret;
59
60 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;
61 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;
62 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;
63 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;
64 return ret;
65 }
66
set_perspective(float p_fovy_degrees,float p_aspect,float p_z_near,float p_z_far,bool p_flip_fov)67 void CameraMatrix::set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float p_z_far, bool p_flip_fov) {
68
69 if (p_flip_fov) {
70 p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
71 }
72
73 float sine, cotangent, deltaZ;
74 float radians = p_fovy_degrees / 2.0 * Math_PI / 180.0;
75
76 deltaZ = p_z_far - p_z_near;
77 sine = Math::sin(radians);
78
79 if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) {
80 return;
81 }
82 cotangent = Math::cos(radians) / sine;
83
84 set_identity();
85
86 matrix[0][0] = cotangent / p_aspect;
87 matrix[1][1] = cotangent;
88 matrix[2][2] = -(p_z_far + p_z_near) / deltaZ;
89 matrix[2][3] = -1;
90 matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ;
91 matrix[3][3] = 0;
92 }
93
set_orthogonal(float p_left,float p_right,float p_bottom,float p_top,float p_znear,float p_zfar)94 void CameraMatrix::set_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float p_zfar) {
95
96 set_identity();
97
98 matrix[0][0] = 2.0 / (p_right - p_left);
99 matrix[3][0] = -((p_right + p_left) / (p_right - p_left));
100 matrix[1][1] = 2.0 / (p_top - p_bottom);
101 matrix[3][1] = -((p_top + p_bottom) / (p_top - p_bottom));
102 matrix[2][2] = -2.0 / (p_zfar - p_znear);
103 matrix[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear));
104 matrix[3][3] = 1.0;
105 }
106
set_orthogonal(float p_size,float p_aspect,float p_znear,float p_zfar,bool p_flip_fov)107 void CameraMatrix::set_orthogonal(float p_size, float p_aspect, float p_znear, float p_zfar, bool p_flip_fov) {
108
109 if (!p_flip_fov) {
110 p_size *= p_aspect;
111 }
112
113 set_orthogonal(-p_size / 2, +p_size / 2, -p_size / p_aspect / 2, +p_size / p_aspect / 2, p_znear, p_zfar);
114 }
115
set_frustum(float p_left,float p_right,float p_bottom,float p_top,float p_near,float p_far)116 void CameraMatrix::set_frustum(float p_left, float p_right, float p_bottom, float p_top, float p_near, float p_far) {
117 #if 0
118 ///@TODO, give a check to this. I'm not sure if it's working.
119 set_identity();
120
121 matrix[0][0]=(2*p_near) / (p_right-p_left);
122 matrix[0][2]=(p_right+p_left) / (p_right-p_left);
123 matrix[1][1]=(2*p_near) / (p_top-p_bottom);
124 matrix[1][2]=(p_top+p_bottom) / (p_top-p_bottom);
125 matrix[2][2]=-(p_far+p_near) / ( p_far-p_near);
126 matrix[2][3]=-(2*p_far*p_near) / (p_far-p_near);
127 matrix[3][2]=-1;
128 matrix[3][3]=0;
129 #else
130 float *te = &matrix[0][0];
131 float x = 2 * p_near / (p_right - p_left);
132 float y = 2 * p_near / (p_top - p_bottom);
133
134 float a = (p_right + p_left) / (p_right - p_left);
135 float b = (p_top + p_bottom) / (p_top - p_bottom);
136 float c = -(p_far + p_near) / (p_far - p_near);
137 float d = -2 * p_far * p_near / (p_far - p_near);
138
139 te[0] = x;
140 te[1] = 0;
141 te[2] = 0;
142 te[3] = 0;
143 te[4] = 0;
144 te[5] = y;
145 te[6] = 0;
146 te[7] = 0;
147 te[8] = a;
148 te[9] = b;
149 te[10] = c;
150 te[11] = -1;
151 te[12] = 0;
152 te[13] = 0;
153 te[14] = d;
154 te[15] = 0;
155
156 #endif
157 }
158
get_z_far() const159 float CameraMatrix::get_z_far() const {
160
161 const float *matrix = (const float *)this->matrix;
162 Plane new_plane = Plane(matrix[3] - matrix[2],
163 matrix[7] - matrix[6],
164 matrix[11] - matrix[10],
165 matrix[15] - matrix[14]);
166
167 new_plane.normal = -new_plane.normal;
168 new_plane.normalize();
169
170 return new_plane.d;
171 }
get_z_near() const172 float CameraMatrix::get_z_near() const {
173
174 const float *matrix = (const float *)this->matrix;
175 Plane new_plane = Plane(matrix[3] + matrix[2],
176 matrix[7] + matrix[6],
177 matrix[11] + matrix[10],
178 -matrix[15] - matrix[14]);
179
180 new_plane.normalize();
181 return new_plane.d;
182 }
183
get_viewport_size(float & r_width,float & r_height) const184 void CameraMatrix::get_viewport_size(float &r_width, float &r_height) const {
185
186 const float *matrix = (const float *)this->matrix;
187 ///////--- Near Plane ---///////
188 Plane near_plane = Plane(matrix[3] + matrix[2],
189 matrix[7] + matrix[6],
190 matrix[11] + matrix[10],
191 -matrix[15] - matrix[14]);
192 near_plane.normalize();
193
194 ///////--- Right Plane ---///////
195 Plane right_plane = Plane(matrix[3] - matrix[0],
196 matrix[7] - matrix[4],
197 matrix[11] - matrix[8],
198 -matrix[15] + matrix[12]);
199 right_plane.normalize();
200
201 Plane top_plane = Plane(matrix[3] - matrix[1],
202 matrix[7] - matrix[5],
203 matrix[11] - matrix[9],
204 -matrix[15] + matrix[13]);
205 top_plane.normalize();
206
207 Vector3 res;
208 near_plane.intersect_3(right_plane, top_plane, &res);
209
210 r_width = res.x;
211 r_height = res.y;
212 }
213
get_endpoints(const Transform & p_transform,Vector3 * p_8points) const214 bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const {
215
216 const float *matrix = (const float *)this->matrix;
217
218 ///////--- Near Plane ---///////
219 Plane near_plane = Plane(matrix[3] + matrix[2],
220 matrix[7] + matrix[6],
221 matrix[11] + matrix[10],
222 -matrix[15] - matrix[14]);
223 near_plane.normalize();
224
225 ///////--- Far Plane ---///////
226 Plane far_plane = Plane(matrix[2] - matrix[3],
227 matrix[6] - matrix[7],
228 matrix[10] - matrix[11],
229 matrix[15] - matrix[14]);
230 far_plane.normalize();
231
232 ///////--- Right Plane ---///////
233 Plane right_plane = Plane(matrix[0] - matrix[3],
234 matrix[4] - matrix[7],
235 matrix[8] - matrix[11],
236 -matrix[15] + matrix[12]);
237 right_plane.normalize();
238
239 ///////--- Top Plane ---///////
240 Plane top_plane = Plane(matrix[1] - matrix[3],
241 matrix[5] - matrix[7],
242 matrix[9] - matrix[11],
243 -matrix[15] + matrix[13]);
244 top_plane.normalize();
245
246 Vector3 near_endpoint;
247 Vector3 far_endpoint;
248
249 bool res = near_plane.intersect_3(right_plane, top_plane, &near_endpoint);
250 ERR_FAIL_COND_V(!res, false);
251
252 res = far_plane.intersect_3(right_plane, top_plane, &far_endpoint);
253 ERR_FAIL_COND_V(!res, false);
254
255 p_8points[0] = p_transform.xform(Vector3(near_endpoint.x, near_endpoint.y, near_endpoint.z));
256 p_8points[1] = p_transform.xform(Vector3(near_endpoint.x, -near_endpoint.y, near_endpoint.z));
257 p_8points[2] = p_transform.xform(Vector3(-near_endpoint.x, near_endpoint.y, near_endpoint.z));
258 p_8points[3] = p_transform.xform(Vector3(-near_endpoint.x, -near_endpoint.y, near_endpoint.z));
259 p_8points[4] = p_transform.xform(Vector3(far_endpoint.x, far_endpoint.y, far_endpoint.z));
260 p_8points[5] = p_transform.xform(Vector3(far_endpoint.x, -far_endpoint.y, far_endpoint.z));
261 p_8points[6] = p_transform.xform(Vector3(-far_endpoint.x, far_endpoint.y, far_endpoint.z));
262 p_8points[7] = p_transform.xform(Vector3(-far_endpoint.x, -far_endpoint.y, far_endpoint.z));
263
264 return true;
265 }
266
get_projection_planes(const Transform & p_transform) const267 Vector<Plane> CameraMatrix::get_projection_planes(const Transform &p_transform) const {
268
269 /** Fast Plane Extraction from combined modelview/projection matrices.
270 * References:
271 * http://www.markmorley.com/opengl/frustumculling.html
272 * http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
273 */
274
275 Vector<Plane> planes;
276
277 const float *matrix = (const float *)this->matrix;
278
279 Plane new_plane;
280
281 ///////--- Near Plane ---///////
282 new_plane = Plane(matrix[3] + matrix[2],
283 matrix[7] + matrix[6],
284 matrix[11] + matrix[10],
285 matrix[15] + matrix[14]);
286
287 new_plane.normal = -new_plane.normal;
288 new_plane.normalize();
289
290 planes.push_back(p_transform.xform(new_plane));
291
292 ///////--- Far Plane ---///////
293 new_plane = Plane(matrix[3] - matrix[2],
294 matrix[7] - matrix[6],
295 matrix[11] - matrix[10],
296 matrix[15] - matrix[14]);
297
298 new_plane.normal = -new_plane.normal;
299 new_plane.normalize();
300
301 planes.push_back(p_transform.xform(new_plane));
302
303 ///////--- Left Plane ---///////
304 new_plane = Plane(matrix[3] + matrix[0],
305 matrix[7] + matrix[4],
306 matrix[11] + matrix[8],
307 matrix[15] + matrix[12]);
308
309 new_plane.normal = -new_plane.normal;
310 new_plane.normalize();
311
312 planes.push_back(p_transform.xform(new_plane));
313
314 ///////--- Top Plane ---///////
315 new_plane = Plane(matrix[3] - matrix[1],
316 matrix[7] - matrix[5],
317 matrix[11] - matrix[9],
318 matrix[15] - matrix[13]);
319
320 new_plane.normal = -new_plane.normal;
321 new_plane.normalize();
322
323 planes.push_back(p_transform.xform(new_plane));
324
325 ///////--- Right Plane ---///////
326 new_plane = Plane(matrix[3] - matrix[0],
327 matrix[7] - matrix[4],
328 matrix[11] - matrix[8],
329 matrix[15] - matrix[12]);
330
331 new_plane.normal = -new_plane.normal;
332 new_plane.normalize();
333
334 planes.push_back(p_transform.xform(new_plane));
335
336 ///////--- Bottom Plane ---///////
337 new_plane = Plane(matrix[3] + matrix[1],
338 matrix[7] + matrix[5],
339 matrix[11] + matrix[9],
340 matrix[15] + matrix[13]);
341
342 new_plane.normal = -new_plane.normal;
343 new_plane.normalize();
344
345 planes.push_back(p_transform.xform(new_plane));
346
347 return planes;
348 }
349
inverse() const350 CameraMatrix CameraMatrix::inverse() const {
351
352 CameraMatrix cm = *this;
353 cm.invert();
354 return cm;
355 }
356
invert()357 void CameraMatrix::invert() {
358
359 int i, j, k;
360 int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
361 float pvt_val; /* Value of current pivot element */
362 float hold; /* Temporary storage */
363 float determinat; /* Determinant */
364
365 determinat = 1.0;
366 for (k = 0; k < 4; k++) {
367 /** Locate k'th pivot element **/
368 pvt_val = matrix[k][k]; /** Initialize for search **/
369 pvt_i[k] = k;
370 pvt_j[k] = k;
371 for (i = k; i < 4; i++) {
372 for (j = k; j < 4; j++) {
373 if (Math::absd(matrix[i][j]) > Math::absd(pvt_val)) {
374 pvt_i[k] = i;
375 pvt_j[k] = j;
376 pvt_val = matrix[i][j];
377 }
378 }
379 }
380
381 /** Product of pivots, gives determinant when finished **/
382 determinat *= pvt_val;
383 if (Math::absd(determinat) < 1e-7) {
384 return; //(false); /** Matrix is singular (zero determinant). **/
385 }
386
387 /** "Interchange" rows (with sign change stuff) **/
388 i = pvt_i[k];
389 if (i != k) { /** If rows are different **/
390 for (j = 0; j < 4; j++) {
391 hold = -matrix[k][j];
392 matrix[k][j] = matrix[i][j];
393 matrix[i][j] = hold;
394 }
395 }
396
397 /** "Interchange" columns **/
398 j = pvt_j[k];
399 if (j != k) { /** If columns are different **/
400 for (i = 0; i < 4; i++) {
401 hold = -matrix[i][k];
402 matrix[i][k] = matrix[i][j];
403 matrix[i][j] = hold;
404 }
405 }
406
407 /** Divide column by minus pivot value **/
408 for (i = 0; i < 4; i++) {
409 if (i != k) matrix[i][k] /= (-pvt_val);
410 }
411
412 /** Reduce the matrix **/
413 for (i = 0; i < 4; i++) {
414 hold = matrix[i][k];
415 for (j = 0; j < 4; j++) {
416 if (i != k && j != k) matrix[i][j] += hold * matrix[k][j];
417 }
418 }
419
420 /** Divide row by pivot **/
421 for (j = 0; j < 4; j++) {
422 if (j != k) matrix[k][j] /= pvt_val;
423 }
424
425 /** Replace pivot by reciprocal (at last we can touch it). **/
426 matrix[k][k] = 1.0 / pvt_val;
427 }
428
429 /* That was most of the work, one final pass of row/column interchange */
430 /* to finish */
431 for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
432 i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
433 if (i != k) { /* If rows are different */
434 for (j = 0; j < 4; j++) {
435 hold = matrix[k][j];
436 matrix[k][j] = -matrix[i][j];
437 matrix[i][j] = hold;
438 }
439 }
440
441 j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
442 if (j != k) /* If columns are different */
443 for (i = 0; i < 4; i++) {
444 hold = matrix[i][k];
445 matrix[i][k] = -matrix[i][j];
446 matrix[i][j] = hold;
447 }
448 }
449 }
450
CameraMatrix()451 CameraMatrix::CameraMatrix() {
452
453 set_identity();
454 }
455
operator *(const CameraMatrix & p_matrix) const456 CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const {
457
458 CameraMatrix new_matrix;
459
460 for (int j = 0; j < 4; j++) {
461 for (int i = 0; i < 4; i++) {
462 real_t ab = 0;
463 for (int k = 0; k < 4; k++)
464 ab += matrix[k][i] * p_matrix.matrix[j][k];
465 new_matrix.matrix[j][i] = ab;
466 }
467 }
468
469 return new_matrix;
470 }
471
set_light_bias()472 void CameraMatrix::set_light_bias() {
473
474 float *m = &matrix[0][0];
475
476 m[0] = 0.5,
477 m[1] = 0.0,
478 m[2] = 0.0,
479 m[3] = 0.0,
480 m[4] = 0.0,
481 m[5] = 0.5,
482 m[6] = 0.0,
483 m[7] = 0.0,
484 m[8] = 0.0,
485 m[9] = 0.0,
486 m[10] = 0.5,
487 m[11] = 0.0,
488 m[12] = 0.5,
489 m[13] = 0.5,
490 m[14] = 0.5,
491 m[15] = 1.0;
492 }
493
operator String() const494 CameraMatrix::operator String() const {
495
496 String str;
497 for (int i = 0; i < 4; i++)
498 for (int j = 0; j < 4; j++)
499 str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]);
500
501 return str;
502 }
503
get_aspect() const504 float CameraMatrix::get_aspect() const {
505
506 float w, h;
507 get_viewport_size(w, h);
508 return w / h;
509 }
510
get_fov() const511 float CameraMatrix::get_fov() const {
512 const float *matrix = (const float *)this->matrix;
513
514 Plane right_plane = Plane(matrix[3] - matrix[0],
515 matrix[7] - matrix[4],
516 matrix[11] - matrix[8],
517 -matrix[15] + matrix[12]);
518 right_plane.normalize();
519
520 return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0;
521 }
522
make_scale(const Vector3 & p_scale)523 void CameraMatrix::make_scale(const Vector3 &p_scale) {
524
525 set_identity();
526 matrix[0][0] = p_scale.x;
527 matrix[1][1] = p_scale.y;
528 matrix[2][2] = p_scale.z;
529 }
530
scale_translate_to_fit(const AABB & p_aabb)531 void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) {
532
533 Vector3 min = p_aabb.pos;
534 Vector3 max = p_aabb.pos + p_aabb.size;
535
536 matrix[0][0] = 2 / (max.x - min.x);
537 matrix[1][0] = 0;
538 matrix[2][0] = 0;
539 matrix[3][0] = -(max.x + min.x) / (max.x - min.x);
540
541 matrix[0][1] = 0;
542 matrix[1][1] = 2 / (max.y - min.y);
543 matrix[2][1] = 0;
544 matrix[3][1] = -(max.y + min.y) / (max.y - min.y);
545
546 matrix[0][2] = 0;
547 matrix[1][2] = 0;
548 matrix[2][2] = 2 / (max.z - min.z);
549 matrix[3][2] = -(max.z + min.z) / (max.z - min.z);
550
551 matrix[0][3] = 0;
552 matrix[1][3] = 0;
553 matrix[2][3] = 0;
554 matrix[3][3] = 1;
555 }
556
operator Transform() const557 CameraMatrix::operator Transform() const {
558
559 Transform tr;
560 const float *m = &matrix[0][0];
561
562 tr.basis.elements[0][0] = m[0];
563 tr.basis.elements[1][0] = m[1];
564 tr.basis.elements[2][0] = m[2];
565
566 tr.basis.elements[0][1] = m[4];
567 tr.basis.elements[1][1] = m[5];
568 tr.basis.elements[2][1] = m[6];
569
570 tr.basis.elements[0][2] = m[8];
571 tr.basis.elements[1][2] = m[9];
572 tr.basis.elements[2][2] = m[10];
573
574 tr.origin.x = m[12];
575 tr.origin.y = m[13];
576 tr.origin.z = m[14];
577
578 return tr;
579 }
580
CameraMatrix(const Transform & p_transform)581 CameraMatrix::CameraMatrix(const Transform &p_transform) {
582
583 const Transform &tr = p_transform;
584 float *m = &matrix[0][0];
585
586 m[0] = tr.basis.elements[0][0];
587 m[1] = tr.basis.elements[1][0];
588 m[2] = tr.basis.elements[2][0];
589 m[3] = 0.0;
590 m[4] = tr.basis.elements[0][1];
591 m[5] = tr.basis.elements[1][1];
592 m[6] = tr.basis.elements[2][1];
593 m[7] = 0.0;
594 m[8] = tr.basis.elements[0][2];
595 m[9] = tr.basis.elements[1][2];
596 m[10] = tr.basis.elements[2][2];
597 m[11] = 0.0;
598 m[12] = tr.origin.x;
599 m[13] = tr.origin.y;
600 m[14] = tr.origin.z;
601 m[15] = 1.0;
602 }
603
~CameraMatrix()604 CameraMatrix::~CameraMatrix() {
605 }
606