3 #include <msp/geometry/affinetransformation.h>
12 Base(Base::identity())
15 Matrix::Matrix(const float *m):
19 Matrix::Matrix(const LinAl::Matrix<float, 4, 4> &other):
23 Matrix &Matrix::translate(const Vector3 &t)
25 return multiply(translation(t));
28 Matrix &Matrix::rotate(const Angle &a, const Vector3 &x)
30 return multiply(rotation(a, x));
33 Matrix &Matrix::scale(const Vector3 &s)
35 return multiply(scaling(s));
38 float Matrix::operator[](unsigned i) const
41 throw out_of_range("Matrix::operator[]");
42 return operator()(i%4, i/4);
45 Matrix Matrix::translation(const Vector3 &t)
47 return Geometry::AffineTransformation<float, 3>::translation(t).get_matrix();
50 Matrix Matrix::rotation(const Angle &a, const Vector3 &x)
52 return Geometry::AffineTransformation<float, 3>::rotation(a, x).get_matrix();
55 Matrix Matrix::scaling(const Vector3 &s)
57 return Geometry::AffineTransformation<float, 3>::scaling(s).get_matrix();
60 Matrix Matrix::ortho(float l, float r, float b, float t, float n, float f)
62 if(l==r || b==t || n==f)
63 throw invalid_argument("Matrix::ortho");
66 result(0, 0) = 2/(r-l);
67 result(1, 1) = 2/(t-b);
68 result(2, 2) = -2/(f-n);
69 result(0, 3) = -(r+l)/(r-l);
70 result(1, 3) = -(t+b)/(t-b);
71 result(2, 3) = -(f+n)/(f-n);
75 Matrix Matrix::ortho_centered(float w, float h)
77 return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
80 Matrix Matrix::ortho_bottomleft(float w, float h)
82 return ortho(0, w, 0, h, -1, 1);
85 Matrix Matrix::ortho_topleft(float w, float h)
87 return ortho(0, w, h, 0, -1, 1);
90 Matrix Matrix::frustum(float l, float r, float b, float t, float n, float f)
92 if(l==r || b==t || n<=0 || f<=n)
93 throw invalid_argument("Matrix::frustum");
96 result(0, 0) = 2*n/(r-l);
97 result(1, 1) = 2*n/(t-b);
98 result(0, 2) = (r+l)/(r-l);
99 result(1, 2) = (t+b)/(t-b);
100 result(2, 2) = -(f+n)/(f-n);
102 result(2, 3) = -2*f*n/(f-n);
107 Matrix Matrix::frustum_centered(float w, float h, float n, float f)
109 return frustum(-w/2, w/2, -h/2, h/2, n, f);
112 Matrix Matrix::perspective(const Angle &h, float a, float n, float f)
114 float hh = tan(h/2.0f)*n;
115 return frustum(-hh*a, hh*a, -hh, hh, n, f);