17 #ifndef _VECTORMATH_MAT_AOS_CPP_H
18 #define _VECTORMATH_MAT_AOS_CPP_H
20 namespace Vectormath {
26 #define _VECTORMATH_PI_OVER_2 1.570796327f
40 mCol0 = Vector3( scalar );
41 mCol1 = Vector3( scalar );
42 mCol2 = Vector3( scalar );
47 float qx, qy, qz, qw, qx2, qy2, qz2, qxqx2, qyqy2, qzqz2, qxqy2, qyqz2, qzqw2, qxqz2, qyqw2, qxqw2;
64 mCol0 = Vector3( ( ( 1.0f - qyqy2 ) - qzqz2 ), ( qxqy2 + qzqw2 ), ( qxqz2 - qyqw2 ) );
65 mCol1 = Vector3( ( qxqy2 - qzqw2 ), ( ( 1.0f - qxqx2 ) - qzqz2 ), ( qyqz2 + qxqw2 ) );
66 mCol2 = Vector3( ( qxqz2 + qyqw2 ), ( qyqz2 - qxqw2 ), ( ( 1.0f - qxqx2 ) - qyqy2 ) );
69 inline Matrix3::Matrix3(
const Vector3 & _col0,
const Vector3 & _col1,
const Vector3 & _col2 )
96 *(&
mCol0 + col) = vec;
111 tmpV3_0 = this->
getCol( col );
112 tmpV3_0.setElem( row, val );
113 this->
setCol( col, tmpV3_0 );
139 return *(&
mCol0 + col);
149 return *(&
mCol0 + col);
154 return *(&
mCol0 + col);
165 inline const Matrix3
transpose(
const Matrix3 & mat )
168 Vector3( mat.getCol0().getX(), mat.getCol1().getX(), mat.getCol2().getX() ),
169 Vector3( mat.getCol0().getY(), mat.getCol1().getY(), mat.getCol2().getY() ),
170 Vector3( mat.getCol0().getZ(), mat.getCol1().getZ(), mat.getCol2().getZ() )
174 inline const Matrix3
inverse(
const Matrix3 & mat )
176 Vector3 tmp0, tmp1, tmp2;
178 tmp0 =
cross( mat.getCol1(), mat.getCol2() );
179 tmp1 =
cross( mat.getCol2(), mat.getCol0() );
180 tmp2 =
cross( mat.getCol0(), mat.getCol1() );
181 detinv = ( 1.0f /
dot( mat.getCol2(), tmp2 ) );
183 Vector3( ( tmp0.getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) ),
184 Vector3( ( tmp0.getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) ),
185 Vector3( ( tmp0.getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) )
191 return dot( mat.getCol2(),
cross( mat.getCol0(), mat.getCol1() ) );
197 (
mCol0 + mat.mCol0 ),
198 (
mCol1 + mat.mCol1 ),
199 (
mCol2 + mat.mCol2 )
206 (
mCol0 - mat.mCol0 ),
207 (
mCol1 - mat.mCol1 ),
208 (
mCol2 - mat.mCol2 )
233 inline const Matrix3
absPerElem(
const Matrix3 & mat )
253 *
this = *
this * scalar;
257 inline const Matrix3
operator *(
float scalar,
const Matrix3 & mat )
274 ( *
this * mat.mCol0 ),
275 ( *
this * mat.mCol1 ),
276 ( *
this * mat.mCol2 )
286 inline const Matrix3
mulPerElem(
const Matrix3 & mat0,
const Matrix3 & mat1 )
311 Vector3( 0.0f, c, s ),
312 Vector3( 0.0f, -s, c )
322 Vector3( c, 0.0f, -s ),
324 Vector3( s, 0.0f, c )
334 Vector3( c, s, 0.0f ),
335 Vector3( -s, c, 0.0f ),
342 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
343 sX = sinf( radiansXYZ.getX() );
344 cX = cosf( radiansXYZ.getX() );
345 sY = sinf( radiansXYZ.getY() );
346 cY = cosf( radiansXYZ.getY() );
347 sZ = sinf( radiansXYZ.getZ() );
348 cZ = cosf( radiansXYZ.getZ() );
352 Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
353 Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
354 Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) )
360 float x, y, z, s, c, oneMinusC, xy, yz, zx;
369 oneMinusC = ( 1.0f - c );
371 Vector3( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ) ),
372 Vector3( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ) ),
373 Vector3( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ) )
385 Vector3( scaleVec.getX(), 0.0f, 0.0f ),
386 Vector3( 0.0f, scaleVec.getY(), 0.0f ),
387 Vector3( 0.0f, 0.0f, scaleVec.getZ() )
391 inline const Matrix3
appendScale(
const Matrix3 & mat,
const Vector3 & scaleVec )
394 ( mat.getCol0() * scaleVec.getX( ) ),
395 ( mat.getCol1() * scaleVec.getY( ) ),
396 ( mat.getCol2() * scaleVec.getZ( ) )
400 inline const Matrix3
prependScale(
const Vector3 & scaleVec,
const Matrix3 & mat )
409 inline const Matrix3
select(
const Matrix3 & mat0,
const Matrix3 & mat1,
bool select1 )
412 select( mat0.getCol0(), mat1.getCol0(), select1 ),
413 select( mat0.getCol1(), mat1.getCol1(), select1 ),
414 select( mat0.getCol2(), mat1.getCol2(), select1 )
418 #ifdef _VECTORMATH_DEBUG
420 inline void print(
const Matrix3 & mat )
422 print( mat.getRow( 0 ) );
423 print( mat.getRow( 1 ) );
424 print( mat.getRow( 2 ) );
427 inline void print(
const Matrix3 & mat,
const char * name )
429 printf(
"%s:\n", name);
445 mCol0 = Vector4( scalar );
446 mCol1 = Vector4( scalar );
447 mCol2 = Vector4( scalar );
448 mCol3 = Vector4( scalar );
453 mCol0 = Vector4( mat.getCol0(), 0.0f );
454 mCol1 = Vector4( mat.getCol1(), 0.0f );
455 mCol2 = Vector4( mat.getCol2(), 0.0f );
456 mCol3 = Vector4( mat.getCol3(), 1.0f );
459 inline Matrix4::Matrix4(
const Vector4 & _col0,
const Vector4 & _col1,
const Vector4 & _col2,
const Vector4 & _col3 )
467 inline Matrix4::Matrix4(
const Matrix3 & mat,
const Vector3 & translateVec )
469 mCol0 = Vector4( mat.getCol0(), 0.0f );
470 mCol1 = Vector4( mat.getCol1(), 0.0f );
471 mCol2 = Vector4( mat.getCol2(), 0.0f );
472 mCol3 = Vector4( translateVec, 1.0f );
475 inline Matrix4::Matrix4(
const Quat & unitQuat,
const Vector3 & translateVec )
478 mat = Matrix3( unitQuat );
479 mCol0 = Vector4( mat.getCol0(), 0.0f );
480 mCol1 = Vector4( mat.getCol1(), 0.0f );
481 mCol2 = Vector4( mat.getCol2(), 0.0f );
482 mCol3 = Vector4( translateVec, 1.0f );
511 *(&
mCol0 + col) = vec;
518 mCol1.
setElem( row, vec.getElem( 1 ) );
519 mCol2.
setElem( row, vec.getElem( 2 ) );
520 mCol3.
setElem( row, vec.getElem( 3 ) );
527 tmpV3_0 = this->
getCol( col );
528 tmpV3_0.setElem( row, val );
529 this->
setCol( col, tmpV3_0 );
560 return *(&
mCol0 + col);
565 return Vector4(
mCol0.
getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ), mCol3.getElem( row ) );
570 return *(&
mCol0 + col);
575 return *(&
mCol0 + col);
587 inline const Matrix4
transpose(
const Matrix4 & mat )
590 Vector4( mat.getCol0().getX(), mat.getCol1().getX(), mat.getCol2().getX(), mat.getCol3().getX() ),
591 Vector4( mat.getCol0().getY(), mat.getCol1().getY(), mat.getCol2().getY(), mat.getCol3().getY() ),
592 Vector4( mat.getCol0().getZ(), mat.getCol1().getZ(), mat.getCol2().getZ(), mat.getCol3().getZ() ),
593 Vector4( mat.getCol0().getW(), mat.getCol1().getW(), mat.getCol2().getW(), mat.getCol3().getW() )
597 inline const Matrix4
inverse(
const Matrix4 & mat )
599 Vector4 res0, res1, res2, res3;
600 float mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, detInv;
602 mB = mat.getCol0().getY();
603 mC = mat.getCol0().getZ();
604 mD = mat.getCol0().getW();
605 mE = mat.getCol1().getX();
606 mF = mat.getCol1().getY();
607 mG = mat.getCol1().getZ();
608 mH = mat.getCol1().getW();
609 mI = mat.getCol2().getX();
610 mJ = mat.getCol2().getY();
611 mK = mat.getCol2().getZ();
612 mL = mat.getCol2().getW();
613 mM = mat.getCol3().getX();
614 mN = mat.getCol3().getY();
615 mO = mat.getCol3().getZ();
616 mP = mat.getCol3().getW();
617 tmp0 = ( ( mK * mD ) - ( mC * mL ) );
618 tmp1 = ( ( mO * mH ) - ( mG * mP ) );
619 tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
620 tmp3 = ( ( mF * mO ) - ( mN * mG ) );
621 tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
622 tmp5 = ( ( mN * mH ) - ( mF * mP ) );
623 res0.setX( ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) ) );
624 res0.setY( ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) ) );
625 res0.setZ( ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) ) );
626 res0.setW( ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) ) );
627 detInv = ( 1.0f / ( ( ( ( mA * res0.getX() ) + ( mE * res0.getY() ) ) + ( mI * res0.getZ() ) ) + ( mM * res0.getW() ) ) );
628 res1.setX( ( mI * tmp1 ) );
629 res1.setY( ( mM * tmp0 ) );
630 res1.setZ( ( mA * tmp1 ) );
631 res1.setW( ( mE * tmp0 ) );
632 res3.setX( ( mI * tmp3 ) );
633 res3.setY( ( mM * tmp2 ) );
634 res3.setZ( ( mA * tmp3 ) );
635 res3.setW( ( mE * tmp2 ) );
636 res2.setX( ( mI * tmp5 ) );
637 res2.setY( ( mM * tmp4 ) );
638 res2.setZ( ( mA * tmp5 ) );
639 res2.setW( ( mE * tmp4 ) );
640 tmp0 = ( ( mI * mB ) - ( mA * mJ ) );
641 tmp1 = ( ( mM * mF ) - ( mE * mN ) );
642 tmp2 = ( ( mI * mD ) - ( mA * mL ) );
643 tmp3 = ( ( mM * mH ) - ( mE * mP ) );
644 tmp4 = ( ( mI * mC ) - ( mA * mK ) );
645 tmp5 = ( ( mM * mG ) - ( mE * mO ) );
646 res2.setX( ( ( ( mL * tmp1 ) - ( mJ * tmp3 ) ) + res2.getX() ) );
647 res2.setY( ( ( ( mP * tmp0 ) - ( mN * tmp2 ) ) + res2.getY() ) );
648 res2.setZ( ( ( ( mB * tmp3 ) - ( mD * tmp1 ) ) - res2.getZ() ) );
649 res2.setW( ( ( ( mF * tmp2 ) - ( mH * tmp0 ) ) - res2.getW() ) );
650 res3.setX( ( ( ( mJ * tmp5 ) - ( mK * tmp1 ) ) + res3.getX() ) );
651 res3.setY( ( ( ( mN * tmp4 ) - ( mO * tmp0 ) ) + res3.getY() ) );
652 res3.setZ( ( ( ( mC * tmp1 ) - ( mB * tmp5 ) ) - res3.getZ() ) );
653 res3.setW( ( ( ( mG * tmp0 ) - ( mF * tmp4 ) ) - res3.getW() ) );
654 res1.setX( ( ( ( mK * tmp3 ) - ( mL * tmp5 ) ) - res1.getX() ) );
655 res1.setY( ( ( ( mO * tmp2 ) - ( mP * tmp4 ) ) - res1.getY() ) );
656 res1.setZ( ( ( ( mD * tmp5 ) - ( mC * tmp3 ) ) + res1.getZ() ) );
657 res1.setW( ( ( ( mH * tmp4 ) - ( mG * tmp2 ) ) + res1.getW() ) );
668 Transform3 affineMat;
669 affineMat.
setCol0( mat.getCol0().getXYZ( ) );
670 affineMat.
setCol1( mat.getCol1().getXYZ( ) );
671 affineMat.
setCol2( mat.getCol2().getXYZ( ) );
672 affineMat.
setCol3( mat.getCol3().getXYZ( ) );
673 return Matrix4(
inverse( affineMat ) );
676 inline const Matrix4
orthoInverse(
const Matrix4 & mat )
678 Transform3 affineMat;
679 affineMat.
setCol0( mat.getCol0().getXYZ( ) );
680 affineMat.
setCol1( mat.getCol1().getXYZ( ) );
681 affineMat.
setCol2( mat.getCol2().getXYZ( ) );
682 affineMat.
setCol3( mat.getCol3().getXYZ( ) );
688 float dx, dy, dz, dw, mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
689 mA = mat.getCol0().getX();
690 mB = mat.getCol0().getY();
691 mC = mat.getCol0().getZ();
692 mD = mat.getCol0().getW();
693 mE = mat.getCol1().getX();
694 mF = mat.getCol1().getY();
695 mG = mat.getCol1().getZ();
696 mH = mat.getCol1().getW();
697 mI = mat.getCol2().getX();
698 mJ = mat.getCol2().getY();
699 mK = mat.getCol2().getZ();
700 mL = mat.getCol2().getW();
701 mM = mat.getCol3().getX();
702 mN = mat.getCol3().getY();
703 mO = mat.getCol3().getZ();
704 mP = mat.getCol3().getW();
705 tmp0 = ( ( mK * mD ) - ( mC * mL ) );
706 tmp1 = ( ( mO * mH ) - ( mG * mP ) );
707 tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
708 tmp3 = ( ( mF * mO ) - ( mN * mG ) );
709 tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
710 tmp5 = ( ( mN * mH ) - ( mF * mP ) );
711 dx = ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) );
712 dy = ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) );
713 dz = ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) );
714 dw = ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) );
715 return ( ( ( ( mA * dx ) + ( mE * dy ) ) + ( mI * dz ) ) + ( mM * dw ) );
721 (
mCol0 + mat.mCol0 ),
722 ( mCol1 + mat.mCol1 ),
723 ( mCol2 + mat.mCol2 ),
724 ( mCol3 + mat.mCol3 )
731 (
mCol0 - mat.mCol0 ),
732 ( mCol1 - mat.mCol1 ),
733 ( mCol2 - mat.mCol2 ),
734 ( mCol3 - mat.mCol3 )
760 inline const Matrix4
absPerElem(
const Matrix4 & mat )
782 *
this = *
this * scalar;
786 inline const Matrix4
operator *(
float scalar,
const Matrix4 & mat )
794 ( ( ( (
mCol0.
getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ) + ( mCol3.getX() * vec.getW() ) ),
795 ( ( ( (
mCol0.
getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ) + ( mCol3.getY() * vec.getW() ) ),
796 ( ( ( (
mCol0.
getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) ) + ( mCol3.getZ() * vec.getW() ) ),
797 ( ( ( (
mCol0.
getW() * vec.getX() ) + ( mCol1.getW() * vec.getY() ) ) + ( mCol2.getW() * vec.getZ() ) ) + ( mCol3.getW() * vec.getW() ) )
804 ( ( (
mCol0.
getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
805 ( ( (
mCol0.
getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
806 ( ( (
mCol0.
getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) ),
807 ( ( (
mCol0.
getW() * vec.getX() ) + ( mCol1.getW() * vec.getY() ) ) + ( mCol2.getW() * vec.getZ() ) )
814 ( ( ( (
mCol0.
getX() * pnt.getX() ) + ( mCol1.getX() * pnt.getY() ) ) + ( mCol2.getX() * pnt.getZ() ) ) + mCol3.getX() ),
815 ( ( ( (
mCol0.
getY() * pnt.getX() ) + ( mCol1.getY() * pnt.getY() ) ) + ( mCol2.getY() * pnt.getZ() ) ) + mCol3.getY() ),
816 ( ( ( (
mCol0.
getZ() * pnt.getX() ) + ( mCol1.getZ() * pnt.getY() ) ) + ( mCol2.getZ() * pnt.getZ() ) ) + mCol3.getZ() ),
817 ( ( ( (
mCol0.
getW() * pnt.getX() ) + ( mCol1.getW() * pnt.getY() ) ) + ( mCol2.getW() * pnt.getZ() ) ) + mCol3.getW() )
824 ( *
this * mat.mCol0 ),
825 ( *
this * mat.mCol1 ),
826 ( *
this * mat.mCol2 ),
827 ( *
this * mat.mCol3 )
840 ( *
this * tfrm.getCol0() ),
841 ( *
this * tfrm.getCol1() ),
842 ( *
this * tfrm.getCol2() ),
843 ( *
this * Point3( tfrm.getCol3() ) )
849 *
this = *
this * tfrm;
853 inline const Matrix4
mulPerElem(
const Matrix4 & mat0,
const Matrix4 & mat1 )
876 mCol1.
setXYZ( mat3.getCol1() );
877 mCol2.
setXYZ( mat3.getCol2() );
892 mCol3.setXYZ( translateVec );
898 return mCol3.getXYZ( );
908 Vector4( 0.0f, c, s, 0.0f ),
909 Vector4( 0.0f, -s, c, 0.0f ),
920 Vector4( c, 0.0f, -s, 0.0f ),
922 Vector4( s, 0.0f, c, 0.0f ),
933 Vector4( c, s, 0.0f, 0.0f ),
934 Vector4( -s, c, 0.0f, 0.0f ),
942 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
943 sX = sinf( radiansXYZ.getX() );
944 cX = cosf( radiansXYZ.getX() );
945 sY = sinf( radiansXYZ.getY() );
946 cY = cosf( radiansXYZ.getY() );
947 sZ = sinf( radiansXYZ.getZ() );
948 cZ = cosf( radiansXYZ.getZ() );
952 Vector4( ( cZ * cY ), ( sZ * cY ), -sY, 0.0f ),
953 Vector4( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ), 0.0f ),
954 Vector4( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ), 0.0f ),
961 float x, y, z, s, c, oneMinusC, xy, yz, zx;
970 oneMinusC = ( 1.0f - c );
972 Vector4( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ), 0.0f ),
973 Vector4( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ), 0.0f ),
974 Vector4( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ), 0.0f ),
987 Vector4( scaleVec.getX(), 0.0f, 0.0f, 0.0f ),
988 Vector4( 0.0f, scaleVec.getY(), 0.0f, 0.0f ),
989 Vector4( 0.0f, 0.0f, scaleVec.getZ(), 0.0f ),
994 inline const Matrix4
appendScale(
const Matrix4 & mat,
const Vector3 & scaleVec )
997 ( mat.getCol0() * scaleVec.getX( ) ),
998 ( mat.getCol1() * scaleVec.getY( ) ),
999 ( mat.getCol2() * scaleVec.getZ( ) ),
1004 inline const Matrix4
prependScale(
const Vector3 & scaleVec,
const Matrix4 & mat )
1007 scale4 = Vector4( scaleVec, 1.0f );
1022 Vector4( translateVec, 1.0f )
1026 inline const Matrix4
Matrix4::lookAt(
const Point3 & eyePos,
const Point3 & lookAtPos,
const Vector3 & upVec )
1029 Vector3 v3X, v3Y, v3Z;
1031 v3Z =
normalize( ( eyePos - lookAtPos ) );
1033 v3Y =
cross( v3Z, v3X );
1034 m4EyeFrame =
Matrix4( Vector4( v3X ), Vector4( v3Y ), Vector4( v3Z ), Vector4( eyePos ) );
1038 inline const Matrix4
Matrix4::perspective(
float fovyRadians,
float aspect,
float zNear,
float zFar )
1042 rangeInv = ( 1.0f / ( zNear - zFar ) );
1044 Vector4( ( f / aspect ), 0.0f, 0.0f, 0.0f ),
1045 Vector4( 0.0f, f, 0.0f, 0.0f ),
1046 Vector4( 0.0f, 0.0f, ( ( zNear + zFar ) * rangeInv ), -1.0f ),
1047 Vector4( 0.0f, 0.0f, ( ( ( zNear * zFar ) * rangeInv ) * 2.0f ), 0.0f )
1051 inline const Matrix4
Matrix4::frustum(
float left,
float right,
float bottom,
float top,
float zNear,
float zFar )
1053 float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf, n2;
1054 sum_rl = ( right + left );
1055 sum_tb = ( top + bottom );
1056 sum_nf = ( zNear + zFar );
1057 inv_rl = ( 1.0f / ( right - left ) );
1058 inv_tb = ( 1.0f / ( top - bottom ) );
1059 inv_nf = ( 1.0f / ( zNear - zFar ) );
1060 n2 = ( zNear + zNear );
1062 Vector4( ( n2 * inv_rl ), 0.0f, 0.0f, 0.0f ),
1063 Vector4( 0.0f, ( n2 * inv_tb ), 0.0f, 0.0f ),
1064 Vector4( ( sum_rl * inv_rl ), ( sum_tb * inv_tb ), ( sum_nf * inv_nf ), -1.0f ),
1065 Vector4( 0.0f, 0.0f, ( ( n2 * inv_nf ) * zFar ), 0.0f )
1069 inline const Matrix4
Matrix4::orthographic(
float left,
float right,
float bottom,
float top,
float zNear,
float zFar )
1071 float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf;
1072 sum_rl = ( right + left );
1073 sum_tb = ( top + bottom );
1074 sum_nf = ( zNear + zFar );
1075 inv_rl = ( 1.0f / ( right - left ) );
1076 inv_tb = ( 1.0f / ( top - bottom ) );
1077 inv_nf = ( 1.0f / ( zNear - zFar ) );
1079 Vector4( ( inv_rl + inv_rl ), 0.0f, 0.0f, 0.0f ),
1080 Vector4( 0.0f, ( inv_tb + inv_tb ), 0.0f, 0.0f ),
1081 Vector4( 0.0f, 0.0f, ( inv_nf + inv_nf ), 0.0f ),
1082 Vector4( ( -sum_rl * inv_rl ), ( -sum_tb * inv_tb ), ( sum_nf * inv_nf ), 1.0f )
1086 inline const Matrix4
select(
const Matrix4 & mat0,
const Matrix4 & mat1,
bool select1 )
1089 select( mat0.getCol0(), mat1.getCol0(), select1 ),
1090 select( mat0.getCol1(), mat1.getCol1(), select1 ),
1091 select( mat0.getCol2(), mat1.getCol2(), select1 ),
1092 select( mat0.getCol3(), mat1.getCol3(), select1 )
1096 #ifdef _VECTORMATH_DEBUG
1098 inline void print(
const Matrix4 & mat )
1100 print( mat.getRow( 0 ) );
1101 print( mat.getRow( 1 ) );
1102 print( mat.getRow( 2 ) );
1103 print( mat.getRow( 3 ) );
1106 inline void print(
const Matrix4 & mat,
const char * name )
1108 printf(
"%s:\n", name);
1124 mCol0 = Vector3( scalar );
1125 mCol1 = Vector3( scalar );
1126 mCol2 = Vector3( scalar );
1127 mCol3 = Vector3( scalar );
1130 inline Transform3::Transform3(
const Vector3 & _col0,
const Vector3 & _col1,
const Vector3 & _col2,
const Vector3 & _col3 )
1176 *(&
mCol0 + col) = vec;
1183 mCol1.
setElem( row, vec.getElem( 1 ) );
1184 mCol2.
setElem( row, vec.getElem( 2 ) );
1185 mCol3.
setElem( row, vec.getElem( 3 ) );
1192 tmpV3_0 = this->
getCol( col );
1193 tmpV3_0.setElem( row, val );
1194 this->
setCol( col, tmpV3_0 );
1225 return *(&
mCol0 + col);
1230 return Vector4(
mCol0.
getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ), mCol3.getElem( row ) );
1235 return *(&
mCol0 + col);
1240 return *(&
mCol0 + col);
1252 inline const Transform3
inverse(
const Transform3 & tfrm )
1254 Vector3 tmp0, tmp1, tmp2, inv0, inv1, inv2;
1256 tmp0 =
cross( tfrm.getCol1(), tfrm.getCol2() );
1257 tmp1 =
cross( tfrm.getCol2(), tfrm.getCol0() );
1258 tmp2 =
cross( tfrm.getCol0(), tfrm.getCol1() );
1259 detinv = ( 1.0f /
dot( tfrm.getCol2(), tmp2 ) );
1260 inv0 = Vector3( ( tmp0.getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) );
1261 inv1 = Vector3( ( tmp0.getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) );
1262 inv2 = Vector3( ( tmp0.getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) );
1267 Vector3( ( -( ( inv0 * tfrm.getCol3().getX() ) + ( ( inv1 * tfrm.getCol3().getY() ) + ( inv2 * tfrm.getCol3().getZ() ) ) ) ) )
1271 inline const Transform3
orthoInverse(
const Transform3 & tfrm )
1273 Vector3 inv0, inv1, inv2;
1274 inv0 = Vector3( tfrm.getCol0().getX(), tfrm.getCol1().getX(), tfrm.getCol2().getX() );
1275 inv1 = Vector3( tfrm.getCol0().getY(), tfrm.getCol1().getY(), tfrm.getCol2().getY() );
1276 inv2 = Vector3( tfrm.getCol0().getZ(), tfrm.getCol1().getZ(), tfrm.getCol2().getZ() );
1281 Vector3( ( -( ( inv0 * tfrm.getCol3().getX() ) + ( ( inv1 * tfrm.getCol3().getY() ) + ( inv2 * tfrm.getCol3().getZ() ) ) ) ) )
1285 inline const Transform3
absPerElem(
const Transform3 & tfrm )
1298 ( ( (
mCol0.
getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
1299 ( ( (
mCol0.
getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
1300 ( ( (
mCol0.
getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) )
1307 ( ( ( (
mCol0.
getX() * pnt.getX() ) + ( mCol1.getX() * pnt.getY() ) ) + ( mCol2.getX() * pnt.getZ() ) ) + mCol3.getX() ),
1308 ( ( ( (
mCol0.
getY() * pnt.getX() ) + ( mCol1.getY() * pnt.getY() ) ) + ( mCol2.getY() * pnt.getZ() ) ) + mCol3.getY() ),
1309 ( ( ( (
mCol0.
getZ() * pnt.getX() ) + ( mCol1.getZ() * pnt.getY() ) ) + ( mCol2.getZ() * pnt.getZ() ) ) + mCol3.getZ() )
1316 ( *
this * tfrm.mCol0 ),
1317 ( *
this * tfrm.mCol1 ),
1318 ( *
this * tfrm.mCol2 ),
1319 Vector3( ( *
this * Point3( tfrm.mCol3 ) ) )
1325 *
this = *
this * tfrm;
1329 inline const Transform3
mulPerElem(
const Transform3 & tfrm0,
const Transform3 & tfrm1 )
1332 mulPerElem( tfrm0.getCol0(), tfrm1.getCol0() ),
1333 mulPerElem( tfrm0.getCol1(), tfrm1.getCol1() ),
1334 mulPerElem( tfrm0.getCol2(), tfrm1.getCol2() ),
1335 mulPerElem( tfrm0.getCol3(), tfrm1.getCol3() )
1351 mCol0 = tfrm.getCol0();
1352 mCol1 = tfrm.getCol1();
1353 mCol2 = tfrm.getCol2();
1359 return Matrix3(
mCol0, mCol1, mCol2 );
1364 mCol3 = translateVec;
1376 s = sinf( radians );
1377 c = cosf( radians );
1380 Vector3( 0.0f, c, s ),
1381 Vector3( 0.0f, -s, c ),
1389 s = sinf( radians );
1390 c = cosf( radians );
1392 Vector3( c, 0.0f, -s ),
1394 Vector3( s, 0.0f, c ),
1402 s = sinf( radians );
1403 c = cosf( radians );
1405 Vector3( c, s, 0.0f ),
1406 Vector3( -s, c, 0.0f ),
1414 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
1415 sX = sinf( radiansXYZ.getX() );
1416 cX = cosf( radiansXYZ.getX() );
1417 sY = sinf( radiansXYZ.getY() );
1418 cY = cosf( radiansXYZ.getY() );
1419 sZ = sinf( radiansXYZ.getZ() );
1420 cZ = cosf( radiansXYZ.getZ() );
1424 Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
1425 Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
1426 Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) ),
1438 return Transform3( Matrix3( unitQuat ), Vector3( 0.0f ) );
1444 Vector3( scaleVec.getX(), 0.0f, 0.0f ),
1445 Vector3( 0.0f, scaleVec.getY(), 0.0f ),
1446 Vector3( 0.0f, 0.0f, scaleVec.getZ() ),
1451 inline const Transform3
appendScale(
const Transform3 & tfrm,
const Vector3 & scaleVec )
1454 ( tfrm.getCol0() * scaleVec.getX( ) ),
1455 ( tfrm.getCol1() * scaleVec.getY( ) ),
1456 ( tfrm.getCol2() * scaleVec.getZ( ) ),
1461 inline const Transform3
prependScale(
const Vector3 & scaleVec,
const Transform3 & tfrm )
1481 inline const Transform3
select(
const Transform3 & tfrm0,
const Transform3 & tfrm1,
bool select1 )
1484 select( tfrm0.getCol0(), tfrm1.getCol0(), select1 ),
1485 select( tfrm0.getCol1(), tfrm1.getCol1(), select1 ),
1486 select( tfrm0.getCol2(), tfrm1.getCol2(), select1 ),
1487 select( tfrm0.getCol3(), tfrm1.getCol3(), select1 )
1491 #ifdef _VECTORMATH_DEBUG
1493 inline void print(
const Transform3 & tfrm )
1495 print( tfrm.getRow( 0 ) );
1496 print( tfrm.getRow( 1 ) );
1497 print( tfrm.getRow( 2 ) );
1500 inline void print(
const Transform3 & tfrm,
const char * name )
1502 printf(
"%s:\n", name);
1510 float trace, radicand,
scale, xx, yx, zx, xy, yy, zy, xz, yz, zz, tmpx, tmpy, tmpz, tmpw, qx, qy, qz, qw;
1511 int negTrace, ZgtX, ZgtY, YgtX;
1512 int largestXorY, largestYorZ, largestZorX;
1514 xx = tfrm.getCol0().
getX();
1515 yx = tfrm.getCol0().getY();
1516 zx = tfrm.getCol0().getZ();
1517 xy = tfrm.getCol1().getX();
1518 yy = tfrm.getCol1().getY();
1519 zy = tfrm.getCol1().getZ();
1520 xz = tfrm.getCol2().getX();
1521 yz = tfrm.getCol2().getY();
1522 zz = tfrm.getCol2().getZ();
1524 trace = ( ( xx + yy ) + zz );
1526 negTrace = ( trace < 0.0f );
1530 largestXorY = ( !ZgtX || !ZgtY ) && negTrace;
1531 largestYorZ = ( YgtX || ZgtX ) && negTrace;
1532 largestZorX = ( ZgtY || !YgtX ) && negTrace;
1550 radicand = ( ( ( xx + yy ) + zz ) + 1.0f );
1551 scale = ( 0.5f * ( 1.0f / sqrtf( radicand ) ) );
1553 tmpx = ( ( zy - yz ) * scale );
1554 tmpy = ( ( xz - zx ) * scale );
1555 tmpz = ( ( yx - xy ) * scale );
1556 tmpw = ( radicand *
scale );
1585 inline const Matrix3
outer(
const Vector3 & tfrm0,
const Vector3 & tfrm1 )
1588 ( tfrm0 * tfrm1.getX( ) ),
1589 ( tfrm0 * tfrm1.getY( ) ),
1590 ( tfrm0 * tfrm1.getZ( ) )
1594 inline const Matrix4
outer(
const Vector4 & tfrm0,
const Vector4 & tfrm1 )
1597 ( tfrm0 * tfrm1.getX( ) ),
1598 ( tfrm0 * tfrm1.getY( ) ),
1599 ( tfrm0 * tfrm1.getZ( ) ),
1600 ( tfrm0 * tfrm1.getW( ) )
1604 inline const Vector3
rowMul(
const Vector3 & vec,
const Matrix3 & mat )
1607 ( ( ( vec.getX() * mat.getCol0().getX() ) + ( vec.getY() * mat.getCol0().getY() ) ) + ( vec.getZ() * mat.getCol0().getZ() ) ),
1608 ( ( ( vec.getX() * mat.getCol1().getX() ) + ( vec.getY() * mat.getCol1().getY() ) ) + ( vec.getZ() * mat.getCol1().getZ() ) ),
1609 ( ( ( vec.getX() * mat.getCol2().getX() ) + ( vec.getY() * mat.getCol2().getY() ) ) + ( vec.getZ() * mat.getCol2().getZ() ) )
1613 inline const Matrix3
crossMatrix(
const Vector3 & vec )
1616 Vector3( 0.0f, vec.getZ(), -vec.getY() ),
1617 Vector3( -vec.getZ(), 0.0f, vec.getX() ),
1618 Vector3( vec.getY(), -vec.getX(), 0.0f )
1622 inline const Matrix3
crossMatrixMul(
const Vector3 & vec,
const Matrix3 & mat )
1624 return Matrix3(
cross( vec, mat.getCol0() ),
cross( vec, mat.getCol1() ),
cross( vec, mat.getCol2() ) );
const Quat normalize(const Quat &quat)
Matrix4 & setCol1(const Vector4 &col1)
float determinant(const Matrix3 &mat)
const Vector3 rowMul(const Vector3 &vec, const Matrix3 &mat)
static const Matrix4 scale(const Vector3 &scaleVec)
const Vector3 getXYZ() const
const Matrix3 crossMatrixMul(const Vector3 &vec, const Matrix3 &mat)
static const Vector4 yAxis()
Vector4 & setXYZ(const Vector3 &vec)
static const Matrix4 rotationX(float radians)
static const Matrix4 translation(const Vector3 &translateVec)
static const Vector3 xAxis()
static const Vector3 zAxis()
const Matrix4 operator+(const Matrix4 &mat) const
const Matrix3 operator*(float scalar) const
Matrix3 & setElem(int col, int row, float val)
static const Vector4 xAxis()
const Matrix3 appendScale(const Matrix3 &mat, const Vector3 &scaleVec)
Matrix3 & setCol0(const Vector3 &col0)
Matrix3 & setCol2(const Vector3 &col2)
Matrix3 & operator-=(const Matrix3 &mat)
const Matrix3 inverse(const Matrix3 &mat)
static const Matrix4 rotation(float radians, const Vector3 &unitVec)
static const Matrix4 rotationY(float radians)
const Matrix3 crossMatrix(const Vector3 &vec)
float getElem(int col, int row) const
Matrix4 & operator-=(const Matrix4 &mat)
static const Matrix4 orthographic(float left, float right, float bottom, float top, float zNear, float zFar)
static const Matrix3 rotationZ(float radians)
static const Vector3 yAxis()
static const Matrix3 rotationZYX(const Vector3 &radiansXYZ)
const Matrix4 operator*(float scalar) const
const Vector4 getCol3() const
static const Matrix4 identity()
static const Vector4 wAxis()
static const Matrix3 identity()
const Vector4 getCol1() const
Matrix4 & setTranslation(const Vector3 &translateVec)
const Vector4 getCol0() const
const Matrix4 affineInverse(const Matrix4 &mat)
static const Matrix3 scale(const Vector3 &scaleVec)
const Point3 scale(const Point3 &pnt, float scaleVal)
Vector4 & operator[](int col)
const Vector3 getCol0() const
const Matrix3 getUpper3x3() const
Matrix4 & setElem(int col, int row, float val)
#define _VECTORMATH_PI_OVER_2
const Matrix3 outer(const Vector3 &tfrm0, const Vector3 &tfrm1)
const Matrix4 operator-() const
Matrix4 & operator*=(float scalar)
static const Matrix4 frustum(float left, float right, float bottom, float top, float zNear, float zFar)
Vector3 & operator[](int col)
Vector3 & setElem(int idx, float value)
const Vector3 getTranslation() const
const Matrix3 prependScale(const Vector3 &scaleVec, const Matrix3 &mat)
const Vector4 getCol2() const
const Matrix3 operator+(const Matrix3 &mat) const
Matrix3 & operator+=(const Matrix3 &mat)
const Matrix3 operator-() const
Matrix3 & setCol1(const Vector3 &col1)
static const Matrix3 rotationX(float radians)
const Matrix4 orthoInverse(const Matrix4 &mat)
float getElem(int idx) const
Matrix4 & setCol3(const Vector4 &col3)
static const Matrix3 rotation(float radians, const Vector3 &unitVec)
const Vector4 getCol(int col) const
Matrix4 & setCol(int col, const Vector4 &vec)
Matrix4 & setUpper3x3(const Matrix3 &mat3)
Matrix4 & setCol2(const Vector4 &col2)
static const Matrix4 lookAt(const Point3 &eyePos, const Point3 &lookAtPos, const Vector3 &upVec)
const Matrix3 select(const Matrix3 &mat0, const Matrix3 &mat1, bool select1)
Matrix4 & operator+=(const Matrix4 &mat)
static const Matrix4 perspective(float fovyRadians, float aspect, float zNear, float zFar)
float getElem(int idx) const
static const Matrix3 rotationY(float radians)
const Matrix3 transpose(const Matrix3 &mat)
Matrix3 & operator*=(float scalar)
const Vector3 getRow(int row) const
Matrix4 & setCol0(const Vector4 &col0)
Matrix4 & setRow(int row, const Vector4 &vec)
Matrix3 & setCol(int col, const Vector3 &vec)
float getElem(int col, int row) const
const Vector3 getCol(int col) const
static const Matrix4 rotationZYX(const Vector3 &radiansXYZ)
static const Vector4 zAxis()
float dot(const Quat &quat0, const Quat &quat1)
const Vector3 cross(const Vector3 &vec0, const Vector3 &vec1)
const Matrix3 mulPerElem(const Matrix3 &mat0, const Matrix3 &mat1)
static const Matrix4 rotationZ(float radians)
Matrix3 & operator=(const Matrix3 &mat)
const Vector4 getRow(int row) const
const Vector3 getCol2() const
const Matrix3 absPerElem(const Matrix3 &mat)
Vector4 & setElem(int idx, float value)
const Matrix3 operator*(float scalar, const Matrix3 &mat)
Matrix3 & setRow(int row, const Vector3 &vec)
Matrix4 & operator=(const Matrix4 &mat)
const Vector3 getCol1() const