/usr/include/irrlicht/matrix4.h is in libirrlicht-dev 1.8.1+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 | // Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_MATRIX_H_INCLUDED__
#define __IRR_MATRIX_H_INCLUDED__
#include "irrMath.h"
#include "vector3d.h"
#include "vector2d.h"
#include "plane3d.h"
#include "aabbox3d.h"
#include "rect.h"
#include "irrString.h"
// enable this to keep track of changes to the matrix
// and make simpler identity check for seldomly changing matrices
// otherwise identity check will always compare the elements
//#define USE_MATRIX_TEST
// this is only for debugging purposes
//#define USE_MATRIX_TEST_DEBUG
#if defined( USE_MATRIX_TEST_DEBUG )
struct MatrixTest
{
MatrixTest () : ID(0), Calls(0) {}
char buf[256];
int Calls;
int ID;
};
static MatrixTest MTest;
#endif
namespace irr
{
namespace core
{
//! 4x4 matrix. Mostly used as transformation matrix for 3d calculations.
/** The matrix is a D3D style matrix, row major with translations in the 4th row. */
template <class T>
class CMatrix4
{
public:
//! Constructor Flags
enum eConstructor
{
EM4CONST_NOTHING = 0,
EM4CONST_COPY,
EM4CONST_IDENTITY,
EM4CONST_TRANSPOSED,
EM4CONST_INVERSE,
EM4CONST_INVERSE_TRANSPOSED
};
//! Default constructor
/** \param constructor Choose the initialization style */
CMatrix4( eConstructor constructor = EM4CONST_IDENTITY );
//! Copy constructor
/** \param other Other matrix to copy from
\param constructor Choose the initialization style */
CMatrix4(const CMatrix4<T>& other, eConstructor constructor = EM4CONST_COPY);
//! Simple operator for directly accessing every element of the matrix.
T& operator()(const s32 row, const s32 col)
{
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return M[ row * 4 + col ];
}
//! Simple operator for directly accessing every element of the matrix.
const T& operator()(const s32 row, const s32 col) const { return M[row * 4 + col]; }
//! Simple operator for linearly accessing every element of the matrix.
T& operator[](u32 index)
{
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return M[index];
}
//! Simple operator for linearly accessing every element of the matrix.
const T& operator[](u32 index) const { return M[index]; }
//! Sets this matrix equal to the other matrix.
inline CMatrix4<T>& operator=(const CMatrix4<T> &other);
//! Sets all elements of this matrix to the value.
inline CMatrix4<T>& operator=(const T& scalar);
//! Returns pointer to internal array
const T* pointer() const { return M; }
T* pointer()
{
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return M;
}
//! Returns true if other matrix is equal to this matrix.
bool operator==(const CMatrix4<T> &other) const;
//! Returns true if other matrix is not equal to this matrix.
bool operator!=(const CMatrix4<T> &other) const;
//! Add another matrix.
CMatrix4<T> operator+(const CMatrix4<T>& other) const;
//! Add another matrix.
CMatrix4<T>& operator+=(const CMatrix4<T>& other);
//! Subtract another matrix.
CMatrix4<T> operator-(const CMatrix4<T>& other) const;
//! Subtract another matrix.
CMatrix4<T>& operator-=(const CMatrix4<T>& other);
//! set this matrix to the product of two matrices
/** Calculate b*a */
inline CMatrix4<T>& setbyproduct(const CMatrix4<T>& other_a,const CMatrix4<T>& other_b );
//! Set this matrix to the product of two matrices
/** Calculate b*a, no optimization used,
use it if you know you never have a identity matrix */
CMatrix4<T>& setbyproduct_nocheck(const CMatrix4<T>& other_a,const CMatrix4<T>& other_b );
//! Multiply by another matrix.
/** Calculate other*this */
CMatrix4<T> operator*(const CMatrix4<T>& other) const;
//! Multiply by another matrix.
/** Calculate and return other*this */
CMatrix4<T>& operator*=(const CMatrix4<T>& other);
//! Multiply by scalar.
CMatrix4<T> operator*(const T& scalar) const;
//! Multiply by scalar.
CMatrix4<T>& operator*=(const T& scalar);
//! Set matrix to identity.
inline CMatrix4<T>& makeIdentity();
//! Returns true if the matrix is the identity matrix
inline bool isIdentity() const;
//! Returns true if the matrix is orthogonal
inline bool isOrthogonal() const;
//! Returns true if the matrix is the identity matrix
bool isIdentity_integer_base () const;
//! Set the translation of the current matrix. Will erase any previous values.
CMatrix4<T>& setTranslation( const vector3d<T>& translation );
//! Gets the current translation
vector3d<T> getTranslation() const;
//! Set the inverse translation of the current matrix. Will erase any previous values.
CMatrix4<T>& setInverseTranslation( const vector3d<T>& translation );
//! Make a rotation matrix from Euler angles. The 4th row and column are unmodified.
inline CMatrix4<T>& setRotationRadians( const vector3d<T>& rotation );
//! Make a rotation matrix from Euler angles. The 4th row and column are unmodified.
CMatrix4<T>& setRotationDegrees( const vector3d<T>& rotation );
//! Returns the rotation, as set by setRotation().
/** This code was orginally written by by Chev. */
core::vector3d<T> getRotationDegrees() const;
//! Make an inverted rotation matrix from Euler angles.
/** The 4th row and column are unmodified. */
inline CMatrix4<T>& setInverseRotationRadians( const vector3d<T>& rotation );
//! Make an inverted rotation matrix from Euler angles.
/** The 4th row and column are unmodified. */
inline CMatrix4<T>& setInverseRotationDegrees( const vector3d<T>& rotation );
//! Make a rotation matrix from angle and axis, assuming left handed rotation.
/** The 4th row and column are unmodified. */
inline CMatrix4<T>& setRotationAxisRadians(const T& angle, const vector3d<T>& axis);
//! Set Scale
CMatrix4<T>& setScale( const vector3d<T>& scale );
//! Set Scale
CMatrix4<T>& setScale( const T scale ) { return setScale(core::vector3d<T>(scale,scale,scale)); }
//! Get Scale
core::vector3d<T> getScale() const;
//! Translate a vector by the inverse of the translation part of this matrix.
void inverseTranslateVect( vector3df& vect ) const;
//! Rotate a vector by the inverse of the rotation part of this matrix.
void inverseRotateVect( vector3df& vect ) const;
//! Rotate a vector by the rotation part of this matrix.
void rotateVect( vector3df& vect ) const;
//! An alternate transform vector method, writing into a second vector
void rotateVect(core::vector3df& out, const core::vector3df& in) const;
//! An alternate transform vector method, writing into an array of 3 floats
void rotateVect(T *out,const core::vector3df &in) const;
//! Transforms the vector by this matrix
void transformVect( vector3df& vect) const;
//! Transforms input vector by this matrix and stores result in output vector
void transformVect( vector3df& out, const vector3df& in ) const;
//! An alternate transform vector method, writing into an array of 4 floats
void transformVect(T *out,const core::vector3df &in) const;
//! An alternate transform vector method, reading from and writing to an array of 3 floats
void transformVec3(T *out, const T * in) const;
//! Translate a vector by the translation part of this matrix.
void translateVect( vector3df& vect ) const;
//! Transforms a plane by this matrix
void transformPlane( core::plane3d<f32> &plane) const;
//! Transforms a plane by this matrix
void transformPlane( const core::plane3d<f32> &in, core::plane3d<f32> &out) const;
//! Transforms a axis aligned bounding box
/** The result box of this operation may not be accurate at all. For
correct results, use transformBoxEx() */
void transformBox(core::aabbox3d<f32>& box) const;
//! Transforms a axis aligned bounding box
/** The result box of this operation should by accurate, but this operation
is slower than transformBox(). */
void transformBoxEx(core::aabbox3d<f32>& box) const;
//! Multiplies this matrix by a 1x4 matrix
void multiplyWith1x4Matrix(T* matrix) const;
//! Calculates inverse of matrix. Slow.
/** \return Returns false if there is no inverse matrix.*/
bool makeInverse();
//! Inverts a primitive matrix which only contains a translation and a rotation
/** \param out: where result matrix is written to. */
bool getInversePrimitive ( CMatrix4<T>& out ) const;
//! Gets the inversed matrix of this one
/** \param out: where result matrix is written to.
\return Returns false if there is no inverse matrix. */
bool getInverse(CMatrix4<T>& out) const;
//! Builds a right-handed perspective projection matrix based on a field of view
CMatrix4<T>& buildProjectionMatrixPerspectiveFovRH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar);
//! Builds a left-handed perspective projection matrix based on a field of view
CMatrix4<T>& buildProjectionMatrixPerspectiveFovLH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar);
//! Builds a left-handed perspective projection matrix based on a field of view, with far plane at infinity
CMatrix4<T>& buildProjectionMatrixPerspectiveFovInfinityLH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 epsilon=0);
//! Builds a right-handed perspective projection matrix.
CMatrix4<T>& buildProjectionMatrixPerspectiveRH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar);
//! Builds a left-handed perspective projection matrix.
CMatrix4<T>& buildProjectionMatrixPerspectiveLH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar);
//! Builds a left-handed orthogonal projection matrix.
CMatrix4<T>& buildProjectionMatrixOrthoLH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar);
//! Builds a right-handed orthogonal projection matrix.
CMatrix4<T>& buildProjectionMatrixOrthoRH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar);
//! Builds a left-handed look-at matrix.
CMatrix4<T>& buildCameraLookAtMatrixLH(
const vector3df& position,
const vector3df& target,
const vector3df& upVector);
//! Builds a right-handed look-at matrix.
CMatrix4<T>& buildCameraLookAtMatrixRH(
const vector3df& position,
const vector3df& target,
const vector3df& upVector);
//! Builds a matrix that flattens geometry into a plane.
/** \param light: light source
\param plane: plane into which the geometry if flattened into
\param point: value between 0 and 1, describing the light source.
If this is 1, it is a point light, if it is 0, it is a directional light. */
CMatrix4<T>& buildShadowMatrix(const core::vector3df& light, core::plane3df plane, f32 point=1.0f);
//! Builds a matrix which transforms a normalized Device Coordinate to Device Coordinates.
/** Used to scale <-1,-1><1,1> to viewport, for example from <-1,-1> <1,1> to the viewport <0,0><0,640> */
CMatrix4<T>& buildNDCToDCMatrix( const core::rect<s32>& area, f32 zScale);
//! Creates a new matrix as interpolated matrix from two other ones.
/** \param b: other matrix to interpolate with
\param time: Must be a value between 0 and 1. */
CMatrix4<T> interpolate(const core::CMatrix4<T>& b, f32 time) const;
//! Gets transposed matrix
CMatrix4<T> getTransposed() const;
//! Gets transposed matrix
inline void getTransposed( CMatrix4<T>& dest ) const;
//! Builds a matrix that rotates from one vector to another
/** \param from: vector to rotate from
\param to: vector to rotate to
*/
CMatrix4<T>& buildRotateFromTo(const core::vector3df& from, const core::vector3df& to);
//! Builds a combined matrix which translates to a center before rotation and translates from origin afterwards
/** \param center Position to rotate around
\param translate Translation applied after the rotation
*/
void setRotationCenter(const core::vector3df& center, const core::vector3df& translate);
//! Builds a matrix which rotates a source vector to a look vector over an arbitrary axis
/** \param camPos: viewer position in world coo
\param center: object position in world-coo and rotation pivot
\param translation: object final translation from center
\param axis: axis to rotate about
\param from: source vector to rotate from
*/
void buildAxisAlignedBillboard(const core::vector3df& camPos,
const core::vector3df& center,
const core::vector3df& translation,
const core::vector3df& axis,
const core::vector3df& from);
/*
construct 2D Texture transformations
rotate about center, scale, and transform.
*/
//! Set to a texture transformation matrix with the given parameters.
CMatrix4<T>& buildTextureTransform( f32 rotateRad,
const core::vector2df &rotatecenter,
const core::vector2df &translate,
const core::vector2df &scale);
//! Set texture transformation rotation
/** Rotate about z axis, recenter at (0.5,0.5).
Doesn't clear other elements than those affected
\param radAngle Angle in radians
\return Altered matrix */
CMatrix4<T>& setTextureRotationCenter( f32 radAngle );
//! Set texture transformation translation
/** Doesn't clear other elements than those affected.
\param x Offset on x axis
\param y Offset on y axis
\return Altered matrix */
CMatrix4<T>& setTextureTranslate( f32 x, f32 y );
//! Set texture transformation translation, using a transposed representation
/** Doesn't clear other elements than those affected.
\param x Offset on x axis
\param y Offset on y axis
\return Altered matrix */
CMatrix4<T>& setTextureTranslateTransposed( f32 x, f32 y );
//! Set texture transformation scale
/** Doesn't clear other elements than those affected.
\param sx Scale factor on x axis
\param sy Scale factor on y axis
\return Altered matrix. */
CMatrix4<T>& setTextureScale( f32 sx, f32 sy );
//! Set texture transformation scale, and recenter at (0.5,0.5)
/** Doesn't clear other elements than those affected.
\param sx Scale factor on x axis
\param sy Scale factor on y axis
\return Altered matrix. */
CMatrix4<T>& setTextureScaleCenter( f32 sx, f32 sy );
//! Sets all matrix data members at once
CMatrix4<T>& setM(const T* data);
//! Sets if the matrix is definitely identity matrix
void setDefinitelyIdentityMatrix( bool isDefinitelyIdentityMatrix);
//! Gets if the matrix is definitely identity matrix
bool getDefinitelyIdentityMatrix() const;
//! Compare two matrices using the equal method
bool equals(const core::CMatrix4<T>& other, const T tolerance=(T)ROUNDING_ERROR_f64) const;
private:
//! Matrix data, stored in row-major order
T M[16];
#if defined ( USE_MATRIX_TEST )
//! Flag is this matrix is identity matrix
mutable u32 definitelyIdentityMatrix;
#endif
#if defined ( USE_MATRIX_TEST_DEBUG )
u32 id;
mutable u32 calls;
#endif
};
// Default constructor
template <class T>
inline CMatrix4<T>::CMatrix4( eConstructor constructor )
#if defined ( USE_MATRIX_TEST )
: definitelyIdentityMatrix(BIT_UNTESTED)
#endif
#if defined ( USE_MATRIX_TEST_DEBUG )
,id ( MTest.ID++), calls ( 0 )
#endif
{
switch ( constructor )
{
case EM4CONST_NOTHING:
case EM4CONST_COPY:
break;
case EM4CONST_IDENTITY:
case EM4CONST_INVERSE:
default:
makeIdentity();
break;
}
}
// Copy constructor
template <class T>
inline CMatrix4<T>::CMatrix4( const CMatrix4<T>& other, eConstructor constructor)
#if defined ( USE_MATRIX_TEST )
: definitelyIdentityMatrix(BIT_UNTESTED)
#endif
#if defined ( USE_MATRIX_TEST_DEBUG )
,id ( MTest.ID++), calls ( 0 )
#endif
{
switch ( constructor )
{
case EM4CONST_IDENTITY:
makeIdentity();
break;
case EM4CONST_NOTHING:
break;
case EM4CONST_COPY:
*this = other;
break;
case EM4CONST_TRANSPOSED:
other.getTransposed(*this);
break;
case EM4CONST_INVERSE:
if (!other.getInverse(*this))
memset(M, 0, 16*sizeof(T));
break;
case EM4CONST_INVERSE_TRANSPOSED:
if (!other.getInverse(*this))
memset(M, 0, 16*sizeof(T));
else
*this=getTransposed();
break;
}
}
//! Add another matrix.
template <class T>
inline CMatrix4<T> CMatrix4<T>::operator+(const CMatrix4<T>& other) const
{
CMatrix4<T> temp ( EM4CONST_NOTHING );
temp[0] = M[0]+other[0];
temp[1] = M[1]+other[1];
temp[2] = M[2]+other[2];
temp[3] = M[3]+other[3];
temp[4] = M[4]+other[4];
temp[5] = M[5]+other[5];
temp[6] = M[6]+other[6];
temp[7] = M[7]+other[7];
temp[8] = M[8]+other[8];
temp[9] = M[9]+other[9];
temp[10] = M[10]+other[10];
temp[11] = M[11]+other[11];
temp[12] = M[12]+other[12];
temp[13] = M[13]+other[13];
temp[14] = M[14]+other[14];
temp[15] = M[15]+other[15];
return temp;
}
//! Add another matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::operator+=(const CMatrix4<T>& other)
{
M[0]+=other[0];
M[1]+=other[1];
M[2]+=other[2];
M[3]+=other[3];
M[4]+=other[4];
M[5]+=other[5];
M[6]+=other[6];
M[7]+=other[7];
M[8]+=other[8];
M[9]+=other[9];
M[10]+=other[10];
M[11]+=other[11];
M[12]+=other[12];
M[13]+=other[13];
M[14]+=other[14];
M[15]+=other[15];
return *this;
}
//! Subtract another matrix.
template <class T>
inline CMatrix4<T> CMatrix4<T>::operator-(const CMatrix4<T>& other) const
{
CMatrix4<T> temp ( EM4CONST_NOTHING );
temp[0] = M[0]-other[0];
temp[1] = M[1]-other[1];
temp[2] = M[2]-other[2];
temp[3] = M[3]-other[3];
temp[4] = M[4]-other[4];
temp[5] = M[5]-other[5];
temp[6] = M[6]-other[6];
temp[7] = M[7]-other[7];
temp[8] = M[8]-other[8];
temp[9] = M[9]-other[9];
temp[10] = M[10]-other[10];
temp[11] = M[11]-other[11];
temp[12] = M[12]-other[12];
temp[13] = M[13]-other[13];
temp[14] = M[14]-other[14];
temp[15] = M[15]-other[15];
return temp;
}
//! Subtract another matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::operator-=(const CMatrix4<T>& other)
{
M[0]-=other[0];
M[1]-=other[1];
M[2]-=other[2];
M[3]-=other[3];
M[4]-=other[4];
M[5]-=other[5];
M[6]-=other[6];
M[7]-=other[7];
M[8]-=other[8];
M[9]-=other[9];
M[10]-=other[10];
M[11]-=other[11];
M[12]-=other[12];
M[13]-=other[13];
M[14]-=other[14];
M[15]-=other[15];
return *this;
}
//! Multiply by scalar.
template <class T>
inline CMatrix4<T> CMatrix4<T>::operator*(const T& scalar) const
{
CMatrix4<T> temp ( EM4CONST_NOTHING );
temp[0] = M[0]*scalar;
temp[1] = M[1]*scalar;
temp[2] = M[2]*scalar;
temp[3] = M[3]*scalar;
temp[4] = M[4]*scalar;
temp[5] = M[5]*scalar;
temp[6] = M[6]*scalar;
temp[7] = M[7]*scalar;
temp[8] = M[8]*scalar;
temp[9] = M[9]*scalar;
temp[10] = M[10]*scalar;
temp[11] = M[11]*scalar;
temp[12] = M[12]*scalar;
temp[13] = M[13]*scalar;
temp[14] = M[14]*scalar;
temp[15] = M[15]*scalar;
return temp;
}
//! Multiply by scalar.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::operator*=(const T& scalar)
{
M[0]*=scalar;
M[1]*=scalar;
M[2]*=scalar;
M[3]*=scalar;
M[4]*=scalar;
M[5]*=scalar;
M[6]*=scalar;
M[7]*=scalar;
M[8]*=scalar;
M[9]*=scalar;
M[10]*=scalar;
M[11]*=scalar;
M[12]*=scalar;
M[13]*=scalar;
M[14]*=scalar;
M[15]*=scalar;
return *this;
}
//! Multiply by another matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::operator*=(const CMatrix4<T>& other)
{
#if defined ( USE_MATRIX_TEST )
// do checks on your own in order to avoid copy creation
if ( !other.isIdentity() )
{
if ( this->isIdentity() )
{
return (*this = other);
}
else
{
CMatrix4<T> temp ( *this );
return setbyproduct_nocheck( temp, other );
}
}
return *this;
#else
CMatrix4<T> temp ( *this );
return setbyproduct_nocheck( temp, other );
#endif
}
//! multiply by another matrix
// set this matrix to the product of two other matrices
// goal is to reduce stack use and copy
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setbyproduct_nocheck(const CMatrix4<T>& other_a,const CMatrix4<T>& other_b )
{
const T *m1 = other_a.M;
const T *m2 = other_b.M;
M[0] = m1[0]*m2[0] + m1[4]*m2[1] + m1[8]*m2[2] + m1[12]*m2[3];
M[1] = m1[1]*m2[0] + m1[5]*m2[1] + m1[9]*m2[2] + m1[13]*m2[3];
M[2] = m1[2]*m2[0] + m1[6]*m2[1] + m1[10]*m2[2] + m1[14]*m2[3];
M[3] = m1[3]*m2[0] + m1[7]*m2[1] + m1[11]*m2[2] + m1[15]*m2[3];
M[4] = m1[0]*m2[4] + m1[4]*m2[5] + m1[8]*m2[6] + m1[12]*m2[7];
M[5] = m1[1]*m2[4] + m1[5]*m2[5] + m1[9]*m2[6] + m1[13]*m2[7];
M[6] = m1[2]*m2[4] + m1[6]*m2[5] + m1[10]*m2[6] + m1[14]*m2[7];
M[7] = m1[3]*m2[4] + m1[7]*m2[5] + m1[11]*m2[6] + m1[15]*m2[7];
M[8] = m1[0]*m2[8] + m1[4]*m2[9] + m1[8]*m2[10] + m1[12]*m2[11];
M[9] = m1[1]*m2[8] + m1[5]*m2[9] + m1[9]*m2[10] + m1[13]*m2[11];
M[10] = m1[2]*m2[8] + m1[6]*m2[9] + m1[10]*m2[10] + m1[14]*m2[11];
M[11] = m1[3]*m2[8] + m1[7]*m2[9] + m1[11]*m2[10] + m1[15]*m2[11];
M[12] = m1[0]*m2[12] + m1[4]*m2[13] + m1[8]*m2[14] + m1[12]*m2[15];
M[13] = m1[1]*m2[12] + m1[5]*m2[13] + m1[9]*m2[14] + m1[13]*m2[15];
M[14] = m1[2]*m2[12] + m1[6]*m2[13] + m1[10]*m2[14] + m1[14]*m2[15];
M[15] = m1[3]*m2[12] + m1[7]*m2[13] + m1[11]*m2[14] + m1[15]*m2[15];
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
//! multiply by another matrix
// set this matrix to the product of two other matrices
// goal is to reduce stack use and copy
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setbyproduct(const CMatrix4<T>& other_a, const CMatrix4<T>& other_b )
{
#if defined ( USE_MATRIX_TEST )
if ( other_a.isIdentity () )
return (*this = other_b);
else
if ( other_b.isIdentity () )
return (*this = other_a);
else
return setbyproduct_nocheck(other_a,other_b);
#else
return setbyproduct_nocheck(other_a,other_b);
#endif
}
//! multiply by another matrix
template <class T>
inline CMatrix4<T> CMatrix4<T>::operator*(const CMatrix4<T>& m2) const
{
#if defined ( USE_MATRIX_TEST )
// Testing purpose..
if ( this->isIdentity() )
return m2;
if ( m2.isIdentity() )
return *this;
#endif
CMatrix4<T> m3 ( EM4CONST_NOTHING );
const T *m1 = M;
m3[0] = m1[0]*m2[0] + m1[4]*m2[1] + m1[8]*m2[2] + m1[12]*m2[3];
m3[1] = m1[1]*m2[0] + m1[5]*m2[1] + m1[9]*m2[2] + m1[13]*m2[3];
m3[2] = m1[2]*m2[0] + m1[6]*m2[1] + m1[10]*m2[2] + m1[14]*m2[3];
m3[3] = m1[3]*m2[0] + m1[7]*m2[1] + m1[11]*m2[2] + m1[15]*m2[3];
m3[4] = m1[0]*m2[4] + m1[4]*m2[5] + m1[8]*m2[6] + m1[12]*m2[7];
m3[5] = m1[1]*m2[4] + m1[5]*m2[5] + m1[9]*m2[6] + m1[13]*m2[7];
m3[6] = m1[2]*m2[4] + m1[6]*m2[5] + m1[10]*m2[6] + m1[14]*m2[7];
m3[7] = m1[3]*m2[4] + m1[7]*m2[5] + m1[11]*m2[6] + m1[15]*m2[7];
m3[8] = m1[0]*m2[8] + m1[4]*m2[9] + m1[8]*m2[10] + m1[12]*m2[11];
m3[9] = m1[1]*m2[8] + m1[5]*m2[9] + m1[9]*m2[10] + m1[13]*m2[11];
m3[10] = m1[2]*m2[8] + m1[6]*m2[9] + m1[10]*m2[10] + m1[14]*m2[11];
m3[11] = m1[3]*m2[8] + m1[7]*m2[9] + m1[11]*m2[10] + m1[15]*m2[11];
m3[12] = m1[0]*m2[12] + m1[4]*m2[13] + m1[8]*m2[14] + m1[12]*m2[15];
m3[13] = m1[1]*m2[12] + m1[5]*m2[13] + m1[9]*m2[14] + m1[13]*m2[15];
m3[14] = m1[2]*m2[12] + m1[6]*m2[13] + m1[10]*m2[14] + m1[14]*m2[15];
m3[15] = m1[3]*m2[12] + m1[7]*m2[13] + m1[11]*m2[14] + m1[15]*m2[15];
return m3;
}
template <class T>
inline vector3d<T> CMatrix4<T>::getTranslation() const
{
return vector3d<T>(M[12], M[13], M[14]);
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTranslation( const vector3d<T>& translation )
{
M[12] = translation.X;
M[13] = translation.Y;
M[14] = translation.Z;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setInverseTranslation( const vector3d<T>& translation )
{
M[12] = -translation.X;
M[13] = -translation.Y;
M[14] = -translation.Z;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setScale( const vector3d<T>& scale )
{
M[0] = scale.X;
M[5] = scale.Y;
M[10] = scale.Z;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
//! Returns the absolute values of the scales of the matrix.
/**
Note that this returns the absolute (positive) values unless only scale is set.
Unfortunately it does not appear to be possible to extract any original negative
values. The best that we could do would be to arbitrarily make one scale
negative if one or three of them were negative.
FIXME - return the original values.
*/
template <class T>
inline vector3d<T> CMatrix4<T>::getScale() const
{
// See http://www.robertblum.com/articles/2005/02/14/decomposing-matrices
// Deal with the 0 rotation case first
// Prior to Irrlicht 1.6, we always returned this value.
if(core::iszero(M[1]) && core::iszero(M[2]) &&
core::iszero(M[4]) && core::iszero(M[6]) &&
core::iszero(M[8]) && core::iszero(M[9]))
return vector3d<T>(M[0], M[5], M[10]);
// We have to do the full calculation.
return vector3d<T>(sqrtf(M[0] * M[0] + M[1] * M[1] + M[2] * M[2]),
sqrtf(M[4] * M[4] + M[5] * M[5] + M[6] * M[6]),
sqrtf(M[8] * M[8] + M[9] * M[9] + M[10] * M[10]));
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setRotationDegrees( const vector3d<T>& rotation )
{
return setRotationRadians( rotation * core::DEGTORAD );
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setInverseRotationDegrees( const vector3d<T>& rotation )
{
return setInverseRotationRadians( rotation * core::DEGTORAD );
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setRotationRadians( const vector3d<T>& rotation )
{
const f64 cr = cos( rotation.X );
const f64 sr = sin( rotation.X );
const f64 cp = cos( rotation.Y );
const f64 sp = sin( rotation.Y );
const f64 cy = cos( rotation.Z );
const f64 sy = sin( rotation.Z );
M[0] = (T)( cp*cy );
M[1] = (T)( cp*sy );
M[2] = (T)( -sp );
const f64 srsp = sr*sp;
const f64 crsp = cr*sp;
M[4] = (T)( srsp*cy-cr*sy );
M[5] = (T)( srsp*sy+cr*cy );
M[6] = (T)( sr*cp );
M[8] = (T)( crsp*cy+sr*sy );
M[9] = (T)( crsp*sy-sr*cy );
M[10] = (T)( cr*cp );
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
//! Returns a rotation that is equivalent to that set by setRotationDegrees().
/** This code was sent in by Chev. Note that it does not necessarily return
the *same* Euler angles as those set by setRotationDegrees(), but the rotation will
be equivalent, i.e. will have the same result when used to rotate a vector or node. */
template <class T>
inline core::vector3d<T> CMatrix4<T>::getRotationDegrees() const
{
const CMatrix4<T> &mat = *this;
core::vector3d<T> scale = getScale();
// we need to check for negative scale on to axes, which would bring up wrong results
if (scale.Y<0 && scale.Z<0)
{
scale.Y =-scale.Y;
scale.Z =-scale.Z;
}
else if (scale.X<0 && scale.Z<0)
{
scale.X =-scale.X;
scale.Z =-scale.Z;
}
else if (scale.X<0 && scale.Y<0)
{
scale.X =-scale.X;
scale.Y =-scale.Y;
}
const core::vector3d<f64> invScale(core::reciprocal(scale.X),core::reciprocal(scale.Y),core::reciprocal(scale.Z));
f64 Y = -asin(core::clamp(mat[2]*invScale.X, -1.0, 1.0));
const f64 C = cos(Y);
Y *= RADTODEG64;
f64 rotx, roty, X, Z;
if (!core::iszero(C))
{
const f64 invC = core::reciprocal(C);
rotx = mat[10] * invC * invScale.Z;
roty = mat[6] * invC * invScale.Y;
X = atan2( roty, rotx ) * RADTODEG64;
rotx = mat[0] * invC * invScale.X;
roty = mat[1] * invC * invScale.X;
Z = atan2( roty, rotx ) * RADTODEG64;
}
else
{
X = 0.0;
rotx = mat[5] * invScale.Y;
roty = -mat[4] * invScale.Y;
Z = atan2( roty, rotx ) * RADTODEG64;
}
// fix values that get below zero
if (X < 0.0) X += 360.0;
if (Y < 0.0) Y += 360.0;
if (Z < 0.0) Z += 360.0;
return vector3d<T>((T)X,(T)Y,(T)Z);
}
//! Sets matrix to rotation matrix of inverse angles given as parameters
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setInverseRotationRadians( const vector3d<T>& rotation )
{
f64 cr = cos( rotation.X );
f64 sr = sin( rotation.X );
f64 cp = cos( rotation.Y );
f64 sp = sin( rotation.Y );
f64 cy = cos( rotation.Z );
f64 sy = sin( rotation.Z );
M[0] = (T)( cp*cy );
M[4] = (T)( cp*sy );
M[8] = (T)( -sp );
f64 srsp = sr*sp;
f64 crsp = cr*sp;
M[1] = (T)( srsp*cy-cr*sy );
M[5] = (T)( srsp*sy+cr*cy );
M[9] = (T)( sr*cp );
M[2] = (T)( crsp*cy+sr*sy );
M[6] = (T)( crsp*sy-sr*cy );
M[10] = (T)( cr*cp );
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
//! Sets matrix to rotation matrix defined by axis and angle, assuming LH rotation
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setRotationAxisRadians( const T& angle, const vector3d<T>& axis )
{
const f64 c = cos(angle);
const f64 s = sin(angle);
const f64 t = 1.0 - c;
const f64 tx = t * axis.X;
const f64 ty = t * axis.Y;
const f64 tz = t * axis.Z;
const f64 sx = s * axis.X;
const f64 sy = s * axis.Y;
const f64 sz = s * axis.Z;
M[0] = (T)(tx * axis.X + c);
M[1] = (T)(tx * axis.Y + sz);
M[2] = (T)(tx * axis.Z - sy);
M[4] = (T)(ty * axis.X - sz);
M[5] = (T)(ty * axis.Y + c);
M[6] = (T)(ty * axis.Z + sx);
M[8] = (T)(tz * axis.X + sy);
M[9] = (T)(tz * axis.Y - sx);
M[10] = (T)(tz * axis.Z + c);
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
/*!
*/
template <class T>
inline CMatrix4<T>& CMatrix4<T>::makeIdentity()
{
memset(M, 0, 16*sizeof(T));
M[0] = M[5] = M[10] = M[15] = (T)1;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=true;
#endif
return *this;
}
/*
check identity with epsilon
solve floating range problems..
*/
template <class T>
inline bool CMatrix4<T>::isIdentity() const
{
#if defined ( USE_MATRIX_TEST )
if (definitelyIdentityMatrix)
return true;
#endif
if (!core::equals( M[12], (T)0 ) || !core::equals( M[13], (T)0 ) || !core::equals( M[14], (T)0 ) || !core::equals( M[15], (T)1 ))
return false;
if (!core::equals( M[ 0], (T)1 ) || !core::equals( M[ 1], (T)0 ) || !core::equals( M[ 2], (T)0 ) || !core::equals( M[ 3], (T)0 ))
return false;
if (!core::equals( M[ 4], (T)0 ) || !core::equals( M[ 5], (T)1 ) || !core::equals( M[ 6], (T)0 ) || !core::equals( M[ 7], (T)0 ))
return false;
if (!core::equals( M[ 8], (T)0 ) || !core::equals( M[ 9], (T)0 ) || !core::equals( M[10], (T)1 ) || !core::equals( M[11], (T)0 ))
return false;
/*
if (!core::equals( M[ 0], (T)1 ) ||
!core::equals( M[ 5], (T)1 ) ||
!core::equals( M[10], (T)1 ) ||
!core::equals( M[15], (T)1 ))
return false;
for (s32 i=0; i<4; ++i)
for (s32 j=0; j<4; ++j)
if ((j != i) && (!iszero((*this)(i,j))))
return false;
*/
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=true;
#endif
return true;
}
/* Check orthogonality of matrix. */
template <class T>
inline bool CMatrix4<T>::isOrthogonal() const
{
T dp=M[0] * M[4 ] + M[1] * M[5 ] + M[2 ] * M[6 ] + M[3 ] * M[7 ];
if (!iszero(dp))
return false;
dp = M[0] * M[8 ] + M[1] * M[9 ] + M[2 ] * M[10] + M[3 ] * M[11];
if (!iszero(dp))
return false;
dp = M[0] * M[12] + M[1] * M[13] + M[2 ] * M[14] + M[3 ] * M[15];
if (!iszero(dp))
return false;
dp = M[4] * M[8 ] + M[5] * M[9 ] + M[6 ] * M[10] + M[7 ] * M[11];
if (!iszero(dp))
return false;
dp = M[4] * M[12] + M[5] * M[13] + M[6 ] * M[14] + M[7 ] * M[15];
if (!iszero(dp))
return false;
dp = M[8] * M[12] + M[9] * M[13] + M[10] * M[14] + M[11] * M[15];
return (iszero(dp));
}
/*
doesn't solve floating range problems..
but takes care on +/- 0 on translation because we are changing it..
reducing floating point branches
but it needs the floats in memory..
*/
template <class T>
inline bool CMatrix4<T>::isIdentity_integer_base() const
{
#if defined ( USE_MATRIX_TEST )
if (definitelyIdentityMatrix)
return true;
#endif
if(IR(M[0])!=F32_VALUE_1) return false;
if(IR(M[1])!=0) return false;
if(IR(M[2])!=0) return false;
if(IR(M[3])!=0) return false;
if(IR(M[4])!=0) return false;
if(IR(M[5])!=F32_VALUE_1) return false;
if(IR(M[6])!=0) return false;
if(IR(M[7])!=0) return false;
if(IR(M[8])!=0) return false;
if(IR(M[9])!=0) return false;
if(IR(M[10])!=F32_VALUE_1) return false;
if(IR(M[11])!=0) return false;
if(IR(M[12])!=0) return false;
if(IR(M[13])!=0) return false;
if(IR(M[13])!=0) return false;
if(IR(M[15])!=F32_VALUE_1) return false;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=true;
#endif
return true;
}
template <class T>
inline void CMatrix4<T>::rotateVect( vector3df& vect ) const
{
vector3df tmp = vect;
vect.X = tmp.X*M[0] + tmp.Y*M[4] + tmp.Z*M[8];
vect.Y = tmp.X*M[1] + tmp.Y*M[5] + tmp.Z*M[9];
vect.Z = tmp.X*M[2] + tmp.Y*M[6] + tmp.Z*M[10];
}
//! An alternate transform vector method, writing into a second vector
template <class T>
inline void CMatrix4<T>::rotateVect(core::vector3df& out, const core::vector3df& in) const
{
out.X = in.X*M[0] + in.Y*M[4] + in.Z*M[8];
out.Y = in.X*M[1] + in.Y*M[5] + in.Z*M[9];
out.Z = in.X*M[2] + in.Y*M[6] + in.Z*M[10];
}
//! An alternate transform vector method, writing into an array of 3 floats
template <class T>
inline void CMatrix4<T>::rotateVect(T *out, const core::vector3df& in) const
{
out[0] = in.X*M[0] + in.Y*M[4] + in.Z*M[8];
out[1] = in.X*M[1] + in.Y*M[5] + in.Z*M[9];
out[2] = in.X*M[2] + in.Y*M[6] + in.Z*M[10];
}
template <class T>
inline void CMatrix4<T>::inverseRotateVect( vector3df& vect ) const
{
vector3df tmp = vect;
vect.X = tmp.X*M[0] + tmp.Y*M[1] + tmp.Z*M[2];
vect.Y = tmp.X*M[4] + tmp.Y*M[5] + tmp.Z*M[6];
vect.Z = tmp.X*M[8] + tmp.Y*M[9] + tmp.Z*M[10];
}
template <class T>
inline void CMatrix4<T>::transformVect( vector3df& vect) const
{
f32 vector[3];
vector[0] = vect.X*M[0] + vect.Y*M[4] + vect.Z*M[8] + M[12];
vector[1] = vect.X*M[1] + vect.Y*M[5] + vect.Z*M[9] + M[13];
vector[2] = vect.X*M[2] + vect.Y*M[6] + vect.Z*M[10] + M[14];
vect.X = vector[0];
vect.Y = vector[1];
vect.Z = vector[2];
}
template <class T>
inline void CMatrix4<T>::transformVect( vector3df& out, const vector3df& in) const
{
out.X = in.X*M[0] + in.Y*M[4] + in.Z*M[8] + M[12];
out.Y = in.X*M[1] + in.Y*M[5] + in.Z*M[9] + M[13];
out.Z = in.X*M[2] + in.Y*M[6] + in.Z*M[10] + M[14];
}
template <class T>
inline void CMatrix4<T>::transformVect(T *out, const core::vector3df &in) const
{
out[0] = in.X*M[0] + in.Y*M[4] + in.Z*M[8] + M[12];
out[1] = in.X*M[1] + in.Y*M[5] + in.Z*M[9] + M[13];
out[2] = in.X*M[2] + in.Y*M[6] + in.Z*M[10] + M[14];
out[3] = in.X*M[3] + in.Y*M[7] + in.Z*M[11] + M[15];
}
template <class T>
inline void CMatrix4<T>::transformVec3(T *out, const T * in) const
{
out[0] = in[0]*M[0] + in[1]*M[4] + in[2]*M[8] + M[12];
out[1] = in[0]*M[1] + in[1]*M[5] + in[2]*M[9] + M[13];
out[2] = in[0]*M[2] + in[1]*M[6] + in[2]*M[10] + M[14];
}
//! Transforms a plane by this matrix
template <class T>
inline void CMatrix4<T>::transformPlane( core::plane3d<f32> &plane) const
{
vector3df member;
// Transform the plane member point, i.e. rotate, translate and scale it.
transformVect(member, plane.getMemberPoint());
// Transform the normal by the transposed inverse of the matrix
CMatrix4<T> transposedInverse(*this, EM4CONST_INVERSE_TRANSPOSED);
vector3df normal = plane.Normal;
transposedInverse.transformVect(normal);
plane.setPlane(member, normal);
}
//! Transforms a plane by this matrix
template <class T>
inline void CMatrix4<T>::transformPlane( const core::plane3d<f32> &in, core::plane3d<f32> &out) const
{
out = in;
transformPlane( out );
}
//! Transforms a axis aligned bounding box
template <class T>
inline void CMatrix4<T>::transformBox(core::aabbox3d<f32>& box) const
{
#if defined ( USE_MATRIX_TEST )
if (isIdentity())
return;
#endif
transformVect(box.MinEdge);
transformVect(box.MaxEdge);
box.repair();
}
//! Transforms a axis aligned bounding box more accurately than transformBox()
template <class T>
inline void CMatrix4<T>::transformBoxEx(core::aabbox3d<f32>& box) const
{
#if defined ( USE_MATRIX_TEST )
if (isIdentity())
return;
#endif
const f32 Amin[3] = {box.MinEdge.X, box.MinEdge.Y, box.MinEdge.Z};
const f32 Amax[3] = {box.MaxEdge.X, box.MaxEdge.Y, box.MaxEdge.Z};
f32 Bmin[3];
f32 Bmax[3];
Bmin[0] = Bmax[0] = M[12];
Bmin[1] = Bmax[1] = M[13];
Bmin[2] = Bmax[2] = M[14];
const CMatrix4<T> &m = *this;
for (u32 i = 0; i < 3; ++i)
{
for (u32 j = 0; j < 3; ++j)
{
const f32 a = m(j,i) * Amin[j];
const f32 b = m(j,i) * Amax[j];
if (a < b)
{
Bmin[i] += a;
Bmax[i] += b;
}
else
{
Bmin[i] += b;
Bmax[i] += a;
}
}
}
box.MinEdge.X = Bmin[0];
box.MinEdge.Y = Bmin[1];
box.MinEdge.Z = Bmin[2];
box.MaxEdge.X = Bmax[0];
box.MaxEdge.Y = Bmax[1];
box.MaxEdge.Z = Bmax[2];
}
//! Multiplies this matrix by a 1x4 matrix
template <class T>
inline void CMatrix4<T>::multiplyWith1x4Matrix(T* matrix) const
{
/*
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
*/
T mat[4];
mat[0] = matrix[0];
mat[1] = matrix[1];
mat[2] = matrix[2];
mat[3] = matrix[3];
matrix[0] = M[0]*mat[0] + M[4]*mat[1] + M[8]*mat[2] + M[12]*mat[3];
matrix[1] = M[1]*mat[0] + M[5]*mat[1] + M[9]*mat[2] + M[13]*mat[3];
matrix[2] = M[2]*mat[0] + M[6]*mat[1] + M[10]*mat[2] + M[14]*mat[3];
matrix[3] = M[3]*mat[0] + M[7]*mat[1] + M[11]*mat[2] + M[15]*mat[3];
}
template <class T>
inline void CMatrix4<T>::inverseTranslateVect( vector3df& vect ) const
{
vect.X = vect.X-M[12];
vect.Y = vect.Y-M[13];
vect.Z = vect.Z-M[14];
}
template <class T>
inline void CMatrix4<T>::translateVect( vector3df& vect ) const
{
vect.X = vect.X+M[12];
vect.Y = vect.Y+M[13];
vect.Z = vect.Z+M[14];
}
template <class T>
inline bool CMatrix4<T>::getInverse(CMatrix4<T>& out) const
{
/// Calculates the inverse of this Matrix
/// The inverse is calculated using Cramers rule.
/// If no inverse exists then 'false' is returned.
#if defined ( USE_MATRIX_TEST )
if ( this->isIdentity() )
{
out=*this;
return true;
}
#endif
const CMatrix4<T> &m = *this;
f32 d = (m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0)) * (m(2, 2) * m(3, 3) - m(2, 3) * m(3, 2)) -
(m(0, 0) * m(1, 2) - m(0, 2) * m(1, 0)) * (m(2, 1) * m(3, 3) - m(2, 3) * m(3, 1)) +
(m(0, 0) * m(1, 3) - m(0, 3) * m(1, 0)) * (m(2, 1) * m(3, 2) - m(2, 2) * m(3, 1)) +
(m(0, 1) * m(1, 2) - m(0, 2) * m(1, 1)) * (m(2, 0) * m(3, 3) - m(2, 3) * m(3, 0)) -
(m(0, 1) * m(1, 3) - m(0, 3) * m(1, 1)) * (m(2, 0) * m(3, 2) - m(2, 2) * m(3, 0)) +
(m(0, 2) * m(1, 3) - m(0, 3) * m(1, 2)) * (m(2, 0) * m(3, 1) - m(2, 1) * m(3, 0));
if( core::iszero ( d, FLT_MIN ) )
return false;
d = core::reciprocal ( d );
out(0, 0) = d * (m(1, 1) * (m(2, 2) * m(3, 3) - m(2, 3) * m(3, 2)) +
m(1, 2) * (m(2, 3) * m(3, 1) - m(2, 1) * m(3, 3)) +
m(1, 3) * (m(2, 1) * m(3, 2) - m(2, 2) * m(3, 1)));
out(0, 1) = d * (m(2, 1) * (m(0, 2) * m(3, 3) - m(0, 3) * m(3, 2)) +
m(2, 2) * (m(0, 3) * m(3, 1) - m(0, 1) * m(3, 3)) +
m(2, 3) * (m(0, 1) * m(3, 2) - m(0, 2) * m(3, 1)));
out(0, 2) = d * (m(3, 1) * (m(0, 2) * m(1, 3) - m(0, 3) * m(1, 2)) +
m(3, 2) * (m(0, 3) * m(1, 1) - m(0, 1) * m(1, 3)) +
m(3, 3) * (m(0, 1) * m(1, 2) - m(0, 2) * m(1, 1)));
out(0, 3) = d * (m(0, 1) * (m(1, 3) * m(2, 2) - m(1, 2) * m(2, 3)) +
m(0, 2) * (m(1, 1) * m(2, 3) - m(1, 3) * m(2, 1)) +
m(0, 3) * (m(1, 2) * m(2, 1) - m(1, 1) * m(2, 2)));
out(1, 0) = d * (m(1, 2) * (m(2, 0) * m(3, 3) - m(2, 3) * m(3, 0)) +
m(1, 3) * (m(2, 2) * m(3, 0) - m(2, 0) * m(3, 2)) +
m(1, 0) * (m(2, 3) * m(3, 2) - m(2, 2) * m(3, 3)));
out(1, 1) = d * (m(2, 2) * (m(0, 0) * m(3, 3) - m(0, 3) * m(3, 0)) +
m(2, 3) * (m(0, 2) * m(3, 0) - m(0, 0) * m(3, 2)) +
m(2, 0) * (m(0, 3) * m(3, 2) - m(0, 2) * m(3, 3)));
out(1, 2) = d * (m(3, 2) * (m(0, 0) * m(1, 3) - m(0, 3) * m(1, 0)) +
m(3, 3) * (m(0, 2) * m(1, 0) - m(0, 0) * m(1, 2)) +
m(3, 0) * (m(0, 3) * m(1, 2) - m(0, 2) * m(1, 3)));
out(1, 3) = d * (m(0, 2) * (m(1, 3) * m(2, 0) - m(1, 0) * m(2, 3)) +
m(0, 3) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0)) +
m(0, 0) * (m(1, 2) * m(2, 3) - m(1, 3) * m(2, 2)));
out(2, 0) = d * (m(1, 3) * (m(2, 0) * m(3, 1) - m(2, 1) * m(3, 0)) +
m(1, 0) * (m(2, 1) * m(3, 3) - m(2, 3) * m(3, 1)) +
m(1, 1) * (m(2, 3) * m(3, 0) - m(2, 0) * m(3, 3)));
out(2, 1) = d * (m(2, 3) * (m(0, 0) * m(3, 1) - m(0, 1) * m(3, 0)) +
m(2, 0) * (m(0, 1) * m(3, 3) - m(0, 3) * m(3, 1)) +
m(2, 1) * (m(0, 3) * m(3, 0) - m(0, 0) * m(3, 3)));
out(2, 2) = d * (m(3, 3) * (m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0)) +
m(3, 0) * (m(0, 1) * m(1, 3) - m(0, 3) * m(1, 1)) +
m(3, 1) * (m(0, 3) * m(1, 0) - m(0, 0) * m(1, 3)));
out(2, 3) = d * (m(0, 3) * (m(1, 1) * m(2, 0) - m(1, 0) * m(2, 1)) +
m(0, 0) * (m(1, 3) * m(2, 1) - m(1, 1) * m(2, 3)) +
m(0, 1) * (m(1, 0) * m(2, 3) - m(1, 3) * m(2, 0)));
out(3, 0) = d * (m(1, 0) * (m(2, 2) * m(3, 1) - m(2, 1) * m(3, 2)) +
m(1, 1) * (m(2, 0) * m(3, 2) - m(2, 2) * m(3, 0)) +
m(1, 2) * (m(2, 1) * m(3, 0) - m(2, 0) * m(3, 1)));
out(3, 1) = d * (m(2, 0) * (m(0, 2) * m(3, 1) - m(0, 1) * m(3, 2)) +
m(2, 1) * (m(0, 0) * m(3, 2) - m(0, 2) * m(3, 0)) +
m(2, 2) * (m(0, 1) * m(3, 0) - m(0, 0) * m(3, 1)));
out(3, 2) = d * (m(3, 0) * (m(0, 2) * m(1, 1) - m(0, 1) * m(1, 2)) +
m(3, 1) * (m(0, 0) * m(1, 2) - m(0, 2) * m(1, 0)) +
m(3, 2) * (m(0, 1) * m(1, 0) - m(0, 0) * m(1, 1)));
out(3, 3) = d * (m(0, 0) * (m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1)) +
m(0, 1) * (m(1, 2) * m(2, 0) - m(1, 0) * m(2, 2)) +
m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)));
#if defined ( USE_MATRIX_TEST )
out.definitelyIdentityMatrix = definitelyIdentityMatrix;
#endif
return true;
}
//! Inverts a primitive matrix which only contains a translation and a rotation
//! \param out: where result matrix is written to.
template <class T>
inline bool CMatrix4<T>::getInversePrimitive ( CMatrix4<T>& out ) const
{
out.M[0 ] = M[0];
out.M[1 ] = M[4];
out.M[2 ] = M[8];
out.M[3 ] = 0;
out.M[4 ] = M[1];
out.M[5 ] = M[5];
out.M[6 ] = M[9];
out.M[7 ] = 0;
out.M[8 ] = M[2];
out.M[9 ] = M[6];
out.M[10] = M[10];
out.M[11] = 0;
out.M[12] = (T)-(M[12]*M[0] + M[13]*M[1] + M[14]*M[2]);
out.M[13] = (T)-(M[12]*M[4] + M[13]*M[5] + M[14]*M[6]);
out.M[14] = (T)-(M[12]*M[8] + M[13]*M[9] + M[14]*M[10]);
out.M[15] = 1;
#if defined ( USE_MATRIX_TEST )
out.definitelyIdentityMatrix = definitelyIdentityMatrix;
#endif
return true;
}
/*!
*/
template <class T>
inline bool CMatrix4<T>::makeInverse()
{
#if defined ( USE_MATRIX_TEST )
if (definitelyIdentityMatrix)
return true;
#endif
CMatrix4<T> temp ( EM4CONST_NOTHING );
if (getInverse(temp))
{
*this = temp;
return true;
}
return false;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::operator=(const CMatrix4<T> &other)
{
if (this==&other)
return *this;
memcpy(M, other.M, 16*sizeof(T));
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=other.definitelyIdentityMatrix;
#endif
return *this;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::operator=(const T& scalar)
{
for (s32 i = 0; i < 16; ++i)
M[i]=scalar;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
template <class T>
inline bool CMatrix4<T>::operator==(const CMatrix4<T> &other) const
{
#if defined ( USE_MATRIX_TEST )
if (definitelyIdentityMatrix && other.definitelyIdentityMatrix)
return true;
#endif
for (s32 i = 0; i < 16; ++i)
if (M[i] != other.M[i])
return false;
return true;
}
template <class T>
inline bool CMatrix4<T>::operator!=(const CMatrix4<T> &other) const
{
return !(*this == other);
}
// Builds a right-handed perspective projection matrix based on a field of view
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixPerspectiveFovRH(
f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar)
{
const f64 h = reciprocal(tan(fieldOfViewRadians*0.5));
_IRR_DEBUG_BREAK_IF(aspectRatio==0.f); //divide by zero
const T w = static_cast<T>(h / aspectRatio);
_IRR_DEBUG_BREAK_IF(zNear==zFar); //divide by zero
M[0] = w;
M[1] = 0;
M[2] = 0;
M[3] = 0;
M[4] = 0;
M[5] = (T)h;
M[6] = 0;
M[7] = 0;
M[8] = 0;
M[9] = 0;
M[10] = (T)(zFar/(zNear-zFar)); // DirectX version
// M[10] = (T)(zFar+zNear/(zNear-zFar)); // OpenGL version
M[11] = -1;
M[12] = 0;
M[13] = 0;
M[14] = (T)(zNear*zFar/(zNear-zFar)); // DirectX version
// M[14] = (T)(2.0f*zNear*zFar/(zNear-zFar)); // OpenGL version
M[15] = 0;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a left-handed perspective projection matrix based on a field of view
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixPerspectiveFovLH(
f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar)
{
const f64 h = reciprocal(tan(fieldOfViewRadians*0.5));
_IRR_DEBUG_BREAK_IF(aspectRatio==0.f); //divide by zero
const T w = static_cast<T>(h / aspectRatio);
_IRR_DEBUG_BREAK_IF(zNear==zFar); //divide by zero
M[0] = w;
M[1] = 0;
M[2] = 0;
M[3] = 0;
M[4] = 0;
M[5] = (T)h;
M[6] = 0;
M[7] = 0;
M[8] = 0;
M[9] = 0;
M[10] = (T)(zFar/(zFar-zNear));
M[11] = 1;
M[12] = 0;
M[13] = 0;
M[14] = (T)(-zNear*zFar/(zFar-zNear));
M[15] = 0;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a left-handed perspective projection matrix based on a field of view, with far plane culling at infinity
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixPerspectiveFovInfinityLH(
f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 epsilon)
{
const f64 h = reciprocal(tan(fieldOfViewRadians*0.5));
_IRR_DEBUG_BREAK_IF(aspectRatio==0.f); //divide by zero
const T w = static_cast<T>(h / aspectRatio);
M[0] = w;
M[1] = 0;
M[2] = 0;
M[3] = 0;
M[4] = 0;
M[5] = (T)h;
M[6] = 0;
M[7] = 0;
M[8] = 0;
M[9] = 0;
M[10] = (T)(1.f-epsilon);
M[11] = 1;
M[12] = 0;
M[13] = 0;
M[14] = (T)(zNear*(epsilon-1.f));
M[15] = 0;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a left-handed orthogonal projection matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixOrthoLH(
f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar)
{
_IRR_DEBUG_BREAK_IF(widthOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(heightOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(zNear==zFar); //divide by zero
M[0] = (T)(2/widthOfViewVolume);
M[1] = 0;
M[2] = 0;
M[3] = 0;
M[4] = 0;
M[5] = (T)(2/heightOfViewVolume);
M[6] = 0;
M[7] = 0;
M[8] = 0;
M[9] = 0;
M[10] = (T)(1/(zFar-zNear));
M[11] = 0;
M[12] = 0;
M[13] = 0;
M[14] = (T)(zNear/(zNear-zFar));
M[15] = 1;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a right-handed orthogonal projection matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixOrthoRH(
f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar)
{
_IRR_DEBUG_BREAK_IF(widthOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(heightOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(zNear==zFar); //divide by zero
M[0] = (T)(2/widthOfViewVolume);
M[1] = 0;
M[2] = 0;
M[3] = 0;
M[4] = 0;
M[5] = (T)(2/heightOfViewVolume);
M[6] = 0;
M[7] = 0;
M[8] = 0;
M[9] = 0;
M[10] = (T)(1/(zNear-zFar));
M[11] = 0;
M[12] = 0;
M[13] = 0;
M[14] = (T)(zNear/(zNear-zFar));
M[15] = 1;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a right-handed perspective projection matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixPerspectiveRH(
f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar)
{
_IRR_DEBUG_BREAK_IF(widthOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(heightOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(zNear==zFar); //divide by zero
M[0] = (T)(2*zNear/widthOfViewVolume);
M[1] = 0;
M[2] = 0;
M[3] = 0;
M[4] = 0;
M[5] = (T)(2*zNear/heightOfViewVolume);
M[6] = 0;
M[7] = 0;
M[8] = 0;
M[9] = 0;
M[10] = (T)(zFar/(zNear-zFar));
M[11] = -1;
M[12] = 0;
M[13] = 0;
M[14] = (T)(zNear*zFar/(zNear-zFar));
M[15] = 0;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a left-handed perspective projection matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixPerspectiveLH(
f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar)
{
_IRR_DEBUG_BREAK_IF(widthOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(heightOfViewVolume==0.f); //divide by zero
_IRR_DEBUG_BREAK_IF(zNear==zFar); //divide by zero
M[0] = (T)(2*zNear/widthOfViewVolume);
M[1] = 0;
M[2] = 0;
M[3] = 0;
M[4] = 0;
M[5] = (T)(2*zNear/heightOfViewVolume);
M[6] = 0;
M[7] = 0;
M[8] = 0;
M[9] = 0;
M[10] = (T)(zFar/(zFar-zNear));
M[11] = 1;
M[12] = 0;
M[13] = 0;
M[14] = (T)(zNear*zFar/(zNear-zFar));
M[15] = 0;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a matrix that flattens geometry into a plane.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildShadowMatrix(const core::vector3df& light, core::plane3df plane, f32 point)
{
plane.Normal.normalize();
const f32 d = plane.Normal.dotProduct(light);
M[ 0] = (T)(-plane.Normal.X * light.X + d);
M[ 1] = (T)(-plane.Normal.X * light.Y);
M[ 2] = (T)(-plane.Normal.X * light.Z);
M[ 3] = (T)(-plane.Normal.X * point);
M[ 4] = (T)(-plane.Normal.Y * light.X);
M[ 5] = (T)(-plane.Normal.Y * light.Y + d);
M[ 6] = (T)(-plane.Normal.Y * light.Z);
M[ 7] = (T)(-plane.Normal.Y * point);
M[ 8] = (T)(-plane.Normal.Z * light.X);
M[ 9] = (T)(-plane.Normal.Z * light.Y);
M[10] = (T)(-plane.Normal.Z * light.Z + d);
M[11] = (T)(-plane.Normal.Z * point);
M[12] = (T)(-plane.D * light.X);
M[13] = (T)(-plane.D * light.Y);
M[14] = (T)(-plane.D * light.Z);
M[15] = (T)(-plane.D * point + d);
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a left-handed look-at matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildCameraLookAtMatrixLH(
const vector3df& position,
const vector3df& target,
const vector3df& upVector)
{
vector3df zaxis = target - position;
zaxis.normalize();
vector3df xaxis = upVector.crossProduct(zaxis);
xaxis.normalize();
vector3df yaxis = zaxis.crossProduct(xaxis);
M[0] = (T)xaxis.X;
M[1] = (T)yaxis.X;
M[2] = (T)zaxis.X;
M[3] = 0;
M[4] = (T)xaxis.Y;
M[5] = (T)yaxis.Y;
M[6] = (T)zaxis.Y;
M[7] = 0;
M[8] = (T)xaxis.Z;
M[9] = (T)yaxis.Z;
M[10] = (T)zaxis.Z;
M[11] = 0;
M[12] = (T)-xaxis.dotProduct(position);
M[13] = (T)-yaxis.dotProduct(position);
M[14] = (T)-zaxis.dotProduct(position);
M[15] = 1;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// Builds a right-handed look-at matrix.
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildCameraLookAtMatrixRH(
const vector3df& position,
const vector3df& target,
const vector3df& upVector)
{
vector3df zaxis = position - target;
zaxis.normalize();
vector3df xaxis = upVector.crossProduct(zaxis);
xaxis.normalize();
vector3df yaxis = zaxis.crossProduct(xaxis);
M[0] = (T)xaxis.X;
M[1] = (T)yaxis.X;
M[2] = (T)zaxis.X;
M[3] = 0;
M[4] = (T)xaxis.Y;
M[5] = (T)yaxis.Y;
M[6] = (T)zaxis.Y;
M[7] = 0;
M[8] = (T)xaxis.Z;
M[9] = (T)yaxis.Z;
M[10] = (T)zaxis.Z;
M[11] = 0;
M[12] = (T)-xaxis.dotProduct(position);
M[13] = (T)-yaxis.dotProduct(position);
M[14] = (T)-zaxis.dotProduct(position);
M[15] = 1;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// creates a new matrix as interpolated matrix from this and the passed one.
template <class T>
inline CMatrix4<T> CMatrix4<T>::interpolate(const core::CMatrix4<T>& b, f32 time) const
{
CMatrix4<T> mat ( EM4CONST_NOTHING );
for (u32 i=0; i < 16; i += 4)
{
mat.M[i+0] = (T)(M[i+0] + ( b.M[i+0] - M[i+0] ) * time);
mat.M[i+1] = (T)(M[i+1] + ( b.M[i+1] - M[i+1] ) * time);
mat.M[i+2] = (T)(M[i+2] + ( b.M[i+2] - M[i+2] ) * time);
mat.M[i+3] = (T)(M[i+3] + ( b.M[i+3] - M[i+3] ) * time);
}
return mat;
}
// returns transposed matrix
template <class T>
inline CMatrix4<T> CMatrix4<T>::getTransposed() const
{
CMatrix4<T> t ( EM4CONST_NOTHING );
getTransposed ( t );
return t;
}
// returns transposed matrix
template <class T>
inline void CMatrix4<T>::getTransposed( CMatrix4<T>& o ) const
{
o[ 0] = M[ 0];
o[ 1] = M[ 4];
o[ 2] = M[ 8];
o[ 3] = M[12];
o[ 4] = M[ 1];
o[ 5] = M[ 5];
o[ 6] = M[ 9];
o[ 7] = M[13];
o[ 8] = M[ 2];
o[ 9] = M[ 6];
o[10] = M[10];
o[11] = M[14];
o[12] = M[ 3];
o[13] = M[ 7];
o[14] = M[11];
o[15] = M[15];
#if defined ( USE_MATRIX_TEST )
o.definitelyIdentityMatrix=definitelyIdentityMatrix;
#endif
}
// used to scale <-1,-1><1,1> to viewport
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildNDCToDCMatrix( const core::rect<s32>& viewport, f32 zScale)
{
const f32 scaleX = (viewport.getWidth() - 0.75f ) * 0.5f;
const f32 scaleY = -(viewport.getHeight() - 0.75f ) * 0.5f;
const f32 dx = -0.5f + ( (viewport.UpperLeftCorner.X + viewport.LowerRightCorner.X ) * 0.5f );
const f32 dy = -0.5f + ( (viewport.UpperLeftCorner.Y + viewport.LowerRightCorner.Y ) * 0.5f );
makeIdentity();
M[12] = (T)dx;
M[13] = (T)dy;
return setScale(core::vector3d<T>((T)scaleX, (T)scaleY, (T)zScale));
}
//! Builds a matrix that rotates from one vector to another
/** \param from: vector to rotate from
\param to: vector to rotate to
http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm
*/
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildRotateFromTo(const core::vector3df& from, const core::vector3df& to)
{
// unit vectors
core::vector3df f(from);
core::vector3df t(to);
f.normalize();
t.normalize();
// axis multiplication by sin
core::vector3df vs(t.crossProduct(f));
// axis of rotation
core::vector3df v(vs);
v.normalize();
// cosinus angle
T ca = f.dotProduct(t);
core::vector3df vt(v * (1 - ca));
M[0] = vt.X * v.X + ca;
M[5] = vt.Y * v.Y + ca;
M[10] = vt.Z * v.Z + ca;
vt.X *= v.Y;
vt.Z *= v.X;
vt.Y *= v.Z;
M[1] = vt.X - vs.Z;
M[2] = vt.Z + vs.Y;
M[3] = 0;
M[4] = vt.X + vs.Z;
M[6] = vt.Y - vs.X;
M[7] = 0;
M[8] = vt.Z - vs.Y;
M[9] = vt.Y + vs.X;
M[11] = 0;
M[12] = 0;
M[13] = 0;
M[14] = 0;
M[15] = 1;
return *this;
}
//! Builds a matrix which rotates a source vector to a look vector over an arbitrary axis
/** \param camPos: viewer position in world coord
\param center: object position in world-coord, rotation pivot
\param translation: object final translation from center
\param axis: axis to rotate about
\param from: source vector to rotate from
*/
template <class T>
inline void CMatrix4<T>::buildAxisAlignedBillboard(
const core::vector3df& camPos,
const core::vector3df& center,
const core::vector3df& translation,
const core::vector3df& axis,
const core::vector3df& from)
{
// axis of rotation
core::vector3df up = axis;
up.normalize();
const core::vector3df forward = (camPos - center).normalize();
const core::vector3df right = up.crossProduct(forward).normalize();
// correct look vector
const core::vector3df look = right.crossProduct(up);
// rotate from to
// axis multiplication by sin
const core::vector3df vs = look.crossProduct(from);
// cosinus angle
const f32 ca = from.dotProduct(look);
core::vector3df vt(up * (1.f - ca));
M[0] = static_cast<T>(vt.X * up.X + ca);
M[5] = static_cast<T>(vt.Y * up.Y + ca);
M[10] = static_cast<T>(vt.Z * up.Z + ca);
vt.X *= up.Y;
vt.Z *= up.X;
vt.Y *= up.Z;
M[1] = static_cast<T>(vt.X - vs.Z);
M[2] = static_cast<T>(vt.Z + vs.Y);
M[3] = 0;
M[4] = static_cast<T>(vt.X + vs.Z);
M[6] = static_cast<T>(vt.Y - vs.X);
M[7] = 0;
M[8] = static_cast<T>(vt.Z - vs.Y);
M[9] = static_cast<T>(vt.Y + vs.X);
M[11] = 0;
setRotationCenter(center, translation);
}
//! Builds a combined matrix which translate to a center before rotation and translate afterwards
template <class T>
inline void CMatrix4<T>::setRotationCenter(const core::vector3df& center, const core::vector3df& translation)
{
M[12] = -M[0]*center.X - M[4]*center.Y - M[8]*center.Z + (center.X - translation.X );
M[13] = -M[1]*center.X - M[5]*center.Y - M[9]*center.Z + (center.Y - translation.Y );
M[14] = -M[2]*center.X - M[6]*center.Y - M[10]*center.Z + (center.Z - translation.Z );
M[15] = (T) 1.0;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
}
/*!
Generate texture coordinates as linear functions so that:
u = Ux*x + Uy*y + Uz*z + Uw
v = Vx*x + Vy*y + Vz*z + Vw
The matrix M for this case is:
Ux Vx 0 0
Uy Vy 0 0
Uz Vz 0 0
Uw Vw 0 0
*/
template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildTextureTransform( f32 rotateRad,
const core::vector2df &rotatecenter,
const core::vector2df &translate,
const core::vector2df &scale)
{
const f32 c = cosf(rotateRad);
const f32 s = sinf(rotateRad);
M[0] = (T)(c * scale.X);
M[1] = (T)(s * scale.Y);
M[2] = 0;
M[3] = 0;
M[4] = (T)(-s * scale.X);
M[5] = (T)(c * scale.Y);
M[6] = 0;
M[7] = 0;
M[8] = (T)(c * scale.X * rotatecenter.X + -s * rotatecenter.Y + translate.X);
M[9] = (T)(s * scale.Y * rotatecenter.X + c * rotatecenter.Y + translate.Y);
M[10] = 1;
M[11] = 0;
M[12] = 0;
M[13] = 0;
M[14] = 0;
M[15] = 1;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// rotate about z axis, center ( 0.5, 0.5 )
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTextureRotationCenter( f32 rotateRad )
{
const f32 c = cosf(rotateRad);
const f32 s = sinf(rotateRad);
M[0] = (T)c;
M[1] = (T)s;
M[4] = (T)-s;
M[5] = (T)c;
M[8] = (T)(0.5f * ( s - c) + 0.5f);
M[9] = (T)(-0.5f * ( s + c) + 0.5f);
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix = definitelyIdentityMatrix && (rotateRad==0.0f);
#endif
return *this;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTextureTranslate ( f32 x, f32 y )
{
M[8] = (T)x;
M[9] = (T)y;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix = definitelyIdentityMatrix && (x==0.0f) && (y==0.0f);
#endif
return *this;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTextureTranslateTransposed ( f32 x, f32 y )
{
M[2] = (T)x;
M[6] = (T)y;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix = definitelyIdentityMatrix && (x==0.0f) && (y==0.0f) ;
#endif
return *this;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTextureScale ( f32 sx, f32 sy )
{
M[0] = (T)sx;
M[5] = (T)sy;
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix = definitelyIdentityMatrix && (sx==1.0f) && (sy==1.0f);
#endif
return *this;
}
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTextureScaleCenter( f32 sx, f32 sy )
{
M[0] = (T)sx;
M[5] = (T)sy;
M[8] = (T)(0.5f - 0.5f * sx);
M[9] = (T)(0.5f - 0.5f * sy);
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix = definitelyIdentityMatrix && (sx==1.0f) && (sy==1.0f);
#endif
return *this;
}
// sets all matrix data members at once
template <class T>
inline CMatrix4<T>& CMatrix4<T>::setM(const T* data)
{
memcpy(M,data, 16*sizeof(T));
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}
// sets if the matrix is definitely identity matrix
template <class T>
inline void CMatrix4<T>::setDefinitelyIdentityMatrix( bool isDefinitelyIdentityMatrix)
{
#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix = isDefinitelyIdentityMatrix;
#endif
}
// gets if the matrix is definitely identity matrix
template <class T>
inline bool CMatrix4<T>::getDefinitelyIdentityMatrix() const
{
#if defined ( USE_MATRIX_TEST )
return definitelyIdentityMatrix;
#else
return false;
#endif
}
//! Compare two matrices using the equal method
template <class T>
inline bool CMatrix4<T>::equals(const core::CMatrix4<T>& other, const T tolerance) const
{
#if defined ( USE_MATRIX_TEST )
if (definitelyIdentityMatrix && other.definitelyIdentityMatrix)
return true;
#endif
for (s32 i = 0; i < 16; ++i)
if (!core::equals(M[i],other.M[i], tolerance))
return false;
return true;
}
// Multiply by scalar.
template <class T>
inline CMatrix4<T> operator*(const T scalar, const CMatrix4<T>& mat)
{
return mat*scalar;
}
//! Typedef for f32 matrix
typedef CMatrix4<f32> matrix4;
//! global const identity matrix
IRRLICHT_API extern const matrix4 IdentityMatrix;
} // end namespace core
} // end namespace irr
#endif
|