3 #include <msp/geometry/affinetransformation.h>
13 Base(Base::identity())
16 Matrix::Matrix(const float *m):
20 Matrix::Matrix(const LinAl::Matrix<float, 4, 4> &other):
24 Matrix &Matrix::translate(const Vector3 &t)
26 return multiply(translation(t));
29 Matrix &Matrix::rotate(const Angle &a, const Vector3 &x)
31 return multiply(rotation(a, x));
34 Matrix &Matrix::scale(const Vector3 &s)
36 return multiply(scaling(s));
39 float Matrix::operator[](unsigned i) const
42 throw out_of_range("Matrix::operator[]");
43 return operator()(i%4, i/4);
46 Matrix Matrix::translation(const Vector3 &t)
48 return Geometry::AffineTransformation<float, 3>::translation(t).get_matrix();
51 Matrix Matrix::rotation(const Angle &a, const Vector3 &x)
53 return Geometry::AffineTransformation<float, 3>::rotation(a, x).get_matrix();
56 Matrix Matrix::scaling(const Vector3 &s)
58 return Geometry::AffineTransformation<float, 3>::scaling(s).get_matrix();
61 Matrix Matrix::ortho(float l, float r, float b, float t, float n, float f)
63 if(l==r || b==t || n==f)
64 throw invalid_argument("Matrix::ortho");
67 result(0, 0) = 2/(r-l);
68 result(1, 1) = 2/(t-b);
69 result(2, 2) = -2/(f-n);
70 result(0, 3) = -(r+l)/(r-l);
71 result(1, 3) = -(t+b)/(t-b);
72 result(2, 3) = -(f+n)/(f-n);
76 Matrix Matrix::ortho_centered(float w, float h)
78 return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
81 Matrix Matrix::ortho_bottomleft(float w, float h)
83 return ortho(0, w, 0, h, -1, 1);
86 Matrix Matrix::ortho_topleft(float w, float h)
88 return ortho(0, w, h, 0, -1, 1);
91 Matrix Matrix::frustum(float l, float r, float b, float t, float n, float f)
93 if(l==r || b==t || n<=0 || f<=n)
94 throw invalid_argument("Matrix::frustum");
97 result(0, 0) = 2*n/(r-l);
98 result(1, 1) = 2*n/(t-b);
99 result(0, 2) = (r+l)/(r-l);
100 result(1, 2) = (t+b)/(t-b);
101 result(2, 2) = -(f+n)/(f-n);
103 result(2, 3) = -2*f*n/(f-n);
108 Matrix Matrix::frustum_centered(float w, float h, float n, float f)
110 return frustum(-w/2, w/2, -h/2, h/2, n, f);
113 Matrix Matrix::perspective(const Angle &h, float a, float n, float f)
115 float hh = tan(h/2.0f)*n;
116 return frustum(-hh*a, hh*a, -hh, hh, n, f);