1 #ifndef INCLUDE_AL_MATRIX4_HPP 
    2 #define INCLUDE_AL_MATRIX4_HPP 
   46 #include "al/math/al_Constants.hpp" 
   47 #include "al/math/al_Mat.hpp" 
   48 #include "al/math/al_Quat.hpp" 
   49 #include "al/math/al_Vec.hpp" 
   62 template <
typename T = 
double>
 
   68   Matrix4() : 
Base(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) {}
 
   85   Matrix4(
const T& r1c1, 
const T& r1c2, 
const T& r1c3, 
const T& r1c4,
 
   86           const T& r2c1, 
const T& r2c2, 
const T& r2c3, 
const T& r2c4,
 
   87           const T& r3c1, 
const T& r3c2, 
const T& r3c3, 
const T& r3c4,
 
   88           const T& r4c1, 
const T& r4c2, 
const T& r4c3, 
const T& r4c4)
 
   89       : Base(r1c1, r1c2, r1c3, r1c4, r2c1, r2c2, r2c3, r2c4, r3c1, r3c2, r3c3,
 
   90              r3c4, r4c1, r4c2, r4c3, r4c4) {}
 
   92   Matrix4(
const Vec<3, T>& xaxis, 
const Vec<3, T>& yaxis,
 
   93           const Vec<3, T>& zaxis, 
const Vec<3, T>& position)
 
   94       : Base(xaxis[0], yaxis[0], zaxis[0], position[0], xaxis[1], yaxis[1],
 
   95              zaxis[1], position[1], xaxis[2], yaxis[2], zaxis[2], position[2],
 
  117   static Matrix4 rotate(
float angle, 
float x, 
float y, 
float z) {
 
  118     return Matrix4::rotate(
angle, 
Vec3d(x, y, z));
 
  121   static Matrix4 rotate(
float angle, 
const Vec<3, T>& v) {
 
  125     float c = cos(
angle);
 
  126     float s = sin(
angle);
 
  128     Matrix4 m(axis[0] * axis[0] * (1 - c) + c,
 
  129               axis[1] * axis[0] * (1 - c) + axis[2] * s,
 
  130               axis[0] * axis[2] * (1 - c) - axis[1] * s, 0,
 
  132               axis[0] * axis[1] * (1 - c) - axis[2] * s,
 
  133               axis[1] * axis[1] * (1 - c) + c,
 
  134               axis[1] * axis[2] * (1 - c) + axis[0] * s, 0,
 
  136               axis[0] * axis[2] * (1 - c) + axis[1] * s,
 
  137               axis[1] * axis[2] * (1 - c) - axis[0] * s,
 
  138               axis[2] * axis[2] * (1 - c) + c, 0,
 
  147     return Matrix4(1, 0, x, 0, 0, 1, y, 0, 0, 0, 1, 0, 0, 0, 0, 1);
 
  152     return Matrix4(1, 0, 0, 0, y, 1, 0, 0, z, 0, 1, 0, 0, 0, 0, 1);
 
  157     return Matrix4(1, x, 0, 0, 0, 1, 0, 0, 0, z, 1, 0, 0, 0, 0, 1);
 
  177     return Matrix4(n2 / W, 0, W2 / W, 0, 0, n2 / H, H2 / H, 0, 0, 0, -D2 / D,
 
  178                    -fn2 / D, 0, 0, -1, 0);
 
  188     double f = 1. / tan(fovy * M_DEG2RAD / 2.);
 
  191     T fn2 = far * near * 2;
 
  192     return Matrix4(f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, -D2 / D, -fn2 / D, 0,
 
  216     vr = (nearBR - nearBL).normalize();  
 
  217     vu = (nearTL - nearBL).normalize();  
 
  232     l = vr.
dot(va) * nbyd;
 
  233     r = vr.
dot(vb) * nbyd;
 
  234     b = vu.
dot(va) * nbyd;  
 
  235     t = vu.
dot(vc) * nbyd;
 
  256             tan(fovy * M_DEG2RAD * 0.5);  
 
  258     T shift = -xShift * near / focal;
 
  259     T left = -aspect * top + shift;
 
  260     T right = aspect * top + shift;
 
  261     return perspective(left, right, bottom, top, near, far);
 
  275     double tanfovy = tan(fovy * M_DEG2RAD / 2.);
 
  276     T t = near * tanfovy;  
 
  281     T shift = -xShift * near / focal;
 
  284     shift = -yShift * near / focal;
 
  291   static Matrix4 unPerspective(T l, T r, T b, T t, T n, T f) {
 
  300     return Matrix4(W / n2, 0, 0, W2 / n2, 0, H / n2, 0, H2 / n2, 0, 0, 0, -1, 0,
 
  301                    0, -D / fn2, D2 / fn2);
 
  319     return Matrix4(2 / W, 0, 0, -W2 / W, 0, 2 / H, 0, -H2 / H, 0, 0, -2 / D,
 
  320                    -D2 / D, 0, 0, 0, 1);
 
  323   static Matrix4 unOrtho(T l, T r, T b, T t, T n, T f) {
 
  330     return Matrix4(W / 2, 0, 0, W2 / 2, 0, H / 2, 0, H2 / 2, 0, 0, D / -2,
 
  346     return Matrix4(2 / W, 0, 0, -W2 / W, 0, 2 / H, 0, -H2 / H, 0, 0, -1, 0, 0,
 
  358     return Matrix4(ur[0], ur[1], ur[2], -(ur.
dot(eyePos)), uu[0], uu[1], uu[2],
 
  359                    -(uu.
dot(eyePos)), uf[0], uf[1], uf[2], -(uf.
dot(eyePos)), 0,
 
  374     return lookAt(x, y, -z, eyePos);
 
  413     return lookAt(ux, uy, uz, pos + (ux * -eyeShift));
 
  435   static Matrix4 translate(T x, T y, T z) {
 
  439   template <
typename V>
 
  440   static Matrix4 translate(
const Vec<3, V>& v) {
 
  446   template <
typename V>
 
  447   static Matrix4 scale(
const Vec<3, V>& v) {
 
  451   template <
typename V>
 
  452   static Matrix4 scale(
const V& v) {
 
  464   return proj[14] / (proj[10] + T(1));
 
  470   return proj[14] / (proj[10] - T(1));
 
  476   return (T(-2) * proj[14]) / (proj[10] * proj[10] - T(1));
 
  482   return proj[5] / proj[0];
 
Fixed-size n-by-n square matrix.
 
const T * elems() const
Get read-only pointer to elements.
 
static Mat scaling(const Vec< N - 1, V > &v)
Get a scaling transform matrix.
 
static Mat translation(const Vec< N - 1, V > &v)
Get a translation transform matrix.
 
static Mat rotation(double angle, int dim1, int dim2)
Get a rotation transform matrix.
 
static Matrix4 perspective(const Vec< 3, T > &nearBL, const Vec< 3, T > &nearBR, const Vec< 3, T > &nearTL, const Vec< 3, T > &eye, T near, T far)
Calculate perspective projection from near plane and eye coordinates.
 
static Matrix4 lookAtLeft(const Vec< 3, T > &ux, const Vec< 3, T > &uy, const Vec< 3, T > &uz, const Vec< 3, T > &pos, double eyeSep)
Get a left-eye viewing matrix.
 
Matrix4 & fromQuat(Quat< T > &q)
Set from quaternion.
 
static Matrix4 ortho2D(T l, T r, T b, T t)
Get a two-dimensional orthographic projection matrix.
 
static Matrix4 perspectiveOffAxis(T fovy, T aspect, T near, T far, T xShift, T yShift, T focal)
Get an off-axis perspective projection matrix (for stereographics)
 
Matrix4()
Default constructor creates an identity matrix.
 
static Matrix4 perspectiveOffAxis(T fovy, T aspect, T near, T far, T xShift, T focal)
Get an off-axis perspective projection matrix (for stereographics)
 
static Matrix4 perspective(T fovy, T aspect, T near, T far)
Get a perspective projection matrix.
 
static Matrix4 perspectiveLeft(T fovy, T aspect, T near, T far, T eyeSep, T focal)
Get a left-eye perspective projection matrix (for stereographics)
 
static Matrix4 shearYZ(T y, T z)
Get a shear transformation matrix on the yz plane.
 
static Matrix4 inverse(const Mat< 4, T > &m)
Get the inverse of a matrix.
 
static Matrix4 lookAtOffAxis(const Vec< 3, T > &ux, const Vec< 3, T > &uy, const Vec< 3, T > &uz, const Vec< 3, T > &pos, double eyeShift)
Get an off-axis viewing matrix.
 
static Matrix4 perspectiveRight(T fovy, T aspect, T near, T far, T eyeSep, T focal)
Get a right-eye perspective projection matrix (for stereographics)
 
static Matrix4 ortho(T l, T r, T b, T t, T n, T f)
Get an orthographic projection matrix.
 
Vec< 4, T > transform(const Vec< 4, T > &vCol) const
Computes product of matrix multiplied by column vector, r = m * vCol.
 
Vec< 4, T > transform(const Vec< 3, T > &vCol) const
Computes product of matrix multiplied by column vector, r = m * vCol.
 
static Matrix4 shearZX(T z, T x)
Get a shear transformation matrix on the zx plane.
 
static Matrix4 lookAt(const Vec< 3, T > &eyePos, const Vec< 3, T > &at, const Vec< 3, T > &up)
Get a viewing matrix based on look-at parameters.
 
Matrix4(const Mat< 4, T > &src)
 
static Matrix4 perspective(T l, T r, T b, T t, T n, T f)
Get a perspective projection matrix.
 
static Matrix4 lookAtRight(const Vec< 3, T > &ux, const Vec< 3, T > &uy, const Vec< 3, T > &uz, const Vec< 3, T > &pos, double eyeSep)
Get a right-eye viewing matrix.
 
static Matrix4 shearXY(T x, T y)
Get a shear transformation matrix on the xy plane.
 
Quat< T > toQuat() const
Get a quaternion representation.
 
static Matrix4 lookAt(const Vec< 3, T > &ur, const Vec< 3, T > &uu, const Vec< 3, T > &uf, const Vec< 3, T > &eyePos)
Get a viewing matrix based on an eye reference frame.
 
Quat & fromMatrix(const T *matrix)
 
void toMatrix(T2 *matrix) const
Convert to column-major 4-by-4 projective space transformation matrix.
 
void toMatrixTransposed(T2 *matrix) const
Convert to row-major 4-by-4 projective space transformation matrix.
 
Vec & normalize(T scale=T(1))
Set magnitude to one without changing direction.
 
T dot(const Vec< N, U > &v) const
Returns dot (inner) product between vectors.
 
bool invert(Mat< 1, T > &m)
 
T frustumNear(const Mat< 4, T > &proj)
Get frustum near plane distance from a projection matrix.
 
Vec< 3, double > Vec3d
double 3-vector
 
T frustumAspect(const Mat< 4, T > &proj)
Get frustum aspect ratio from a projection matrix.
 
Matrix4< float > Matrix4f
Single-precision 4-by-4 matrix.
 
T angle(const Vec< N, T > &a, const Vec< N, T > &b)
Returns angle, in interval [0, pi], between two vectors.
 
T frustumFar(const Mat< 4, T > &proj)
Get frustum far plane distance from a projection matrix.
 
void cross(Vec< 3, T > &r, const Vec< 3, T > &a, const Vec< 3, T > &b)
Sets r to cross product, a x b.
 
Matrix4< double > Matrix4d
Double-precision 4-by-4 matrix.
 
T frustumDepth(const Mat< 4, T > &proj)
Get frustum depth from a projection matrix.