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 void Matrix::multiply(const Matrix &other)
29 void Matrix::translate(const Vector3 &t)
31 multiply(translation(t));
34 void Matrix::rotate(const Angle &a, const Vector3 &x)
36 multiply(rotation(a, x));
39 void Matrix::scale(const Vector3 &s)
44 Matrix Matrix::operator*(const Matrix &other) const
46 return static_cast<const Base &>(*this)*static_cast<const Base &>(other);
49 Matrix &Matrix::operator*=(const Matrix &other)
55 Vector4 Matrix::operator*(const Vector4 &vec) const
57 return static_cast<const Base &>(*this)*LinAl::Vector<float, 4>(vec);
60 Vector3 Matrix::operator*(const Vector3 &vec) const
62 return Vector3((*this)*Vector4(vec, 1.0f));
65 float Matrix::operator[](unsigned i) const
68 throw out_of_range("Matrix::operator[]");
69 return operator()(i%4, i/4);
72 Matrix Matrix::translation(const Vector3 &t)
74 return Geometry::AffineTransformation<float, 3>::translation(t).get_matrix();
77 Matrix Matrix::rotation(const Angle &a, const Vector3 &x)
79 return Geometry::AffineTransformation<float, 3>::rotation(a, x).get_matrix();
82 Matrix Matrix::scaling(const Vector3 &s)
84 return Geometry::AffineTransformation<float, 3>::scaling(s).get_matrix();
87 Matrix Matrix::ortho(float l, float r, float b, float t, float n, float f)
89 if(l==r || b==t || n==f)
90 throw invalid_argument("Matrix::ortho");
93 result(0, 0) = 2/(r-l);
94 result(1, 1) = 2/(t-b);
95 result(2, 2) = -2/(f-n);
96 result(0, 3) = -(r+l)/(r-l);
97 result(1, 3) = -(t+b)/(t-b);
98 result(2, 3) = -(f+n)/(f-n);
102 Matrix Matrix::ortho_centered(float w, float h)
104 return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
107 Matrix Matrix::ortho_bottomleft(float w, float h)
109 return ortho(0, w, 0, h, -1, 1);
112 Matrix Matrix::ortho_topleft(float w, float h)
114 return ortho(0, w, h, 0, -1, 1);
117 Matrix Matrix::frustum(float l, float r, float b, float t, float n, float f)
119 if(l==r || b==t || n<=0 || f<=n)
120 throw invalid_argument("Matrix::frustum");
123 result(0, 0) = 2*n/(r-l);
124 result(1, 1) = 2*n/(t-b);
125 result(0, 2) = (r+l)/(r-l);
126 result(1, 2) = (t+b)/(t-b);
127 result(2, 2) = -(f+n)/(f-n);
129 result(2, 3) = -2*f*n/(f-n);
134 Matrix Matrix::frustum_centered(float w, float h, float n, float f)
136 return frustum(-w/2, w/2, -h/2, h/2, n, f);
139 Matrix Matrix::perspective(const Angle &h, float a, float n, float f)
141 float hh = tan(h/2.0f)*n;
142 return frustum(-hh*a, hh*a, -hh, hh, n, f);
146 GLenum MatrixStack::current_mode = GL_MODELVIEW;
148 MatrixStack::MatrixStack(GLenum m):
151 matrices.reserve(mode==GL_MODELVIEW ? 32 : 4);
152 matrices.push_back(Matrix());
155 MatrixStack::MatrixStack():
158 matrices.reserve(32);
159 matrices.push_back(Matrix());
162 const Matrix &MatrixStack::top() const
164 return matrices.back();
167 void MatrixStack::load(const Matrix &m)
173 void MatrixStack::multiply(const Matrix &m)
175 matrices.back() *= m;
179 void MatrixStack::push()
181 matrices.push_back(top());
184 void MatrixStack::pop()
186 if(matrices.size()==1)
187 throw stack_underflow("MatrixStack::pop()");
193 void MatrixStack::update()
198 if(mode!=current_mode)
204 glLoadMatrixf(matrices.back().data());
207 MatrixStack &MatrixStack::operator=(const Matrix &m)
213 MatrixStack &MatrixStack::operator*=(const Matrix &m)
219 MatrixStack &MatrixStack::modelview()
221 static MatrixStack ms(GL_MODELVIEW);
225 MatrixStack &MatrixStack::projection()
227 static MatrixStack ms(GL_PROJECTION);