14 for(unsigned i=0; i<16; ++i)
15 matrix[i] = (i%5 ? 0 : 1);
18 Matrix::Matrix(const float *m):
21 copy(m, m+16, matrix);
25 Matrix::Matrix(const double *m):
28 copy(m, m+16, matrix);
32 void Matrix::check_flags()
34 const double *m = matrix;
35 if(m[12]!=0 || m[13]!=0 || m[14]!=0)
40 if(m[5]!=m[0] || m[10]!=m[0])
43 if(m[1]!=0 || m[2]!=0 || m[4]!=0 || m[6]!=0 || m[8]!=0 || m[9]!=0)
46 double x_dot_y = m[0]*m[1]+m[4]*m[5]+m[8]*m[9];
47 double x_dot_z = m[0]*m[2]+m[4]*m[6]+m[8]*m[10];
48 double y_dot_z = m[1]*m[2]+m[5]*m[6]+m[9]*m[10];
49 if(x_dot_y!=0 || x_dot_z!=0 || y_dot_z!=0)
52 if(m[3]!=0 || m[7]!=0 || m[11]!=0 || m[15]!=1)
56 void Matrix::multiply(const Matrix &other)
61 void Matrix::translate(double x, double y, double z)
63 multiply(translation(x, y, z));
66 void Matrix::rotate(double a, double x, double y, double z)
68 multiply(rotation(a, x, y, z));
71 void Matrix::rotate_deg(double a, double x, double y, double z)
73 multiply(rotation_deg(a, x, y, z));
76 void Matrix::scale(double s)
81 void Matrix::scale(double x, double y, double z)
83 multiply(scaling(x, y, z));
86 Matrix Matrix::operator*(const Matrix &other) const
90 else if(other.flags==IDENTITY)
92 else if(flags==TRANSLATE && !(other.flags&ARBITARY))
94 Matrix result = other;
95 result.matrix[12] += matrix[12];
96 result.matrix[13] += matrix[13];
97 result.matrix[14] += matrix[14];
98 result.flags |= flags;
101 else if(!(flags&ARBITARY) && other.flags==TRANSLATE)
103 Matrix result = *this;
104 const double *m = other.matrix;
105 result.matrix[12] += matrix[0]*m[12]+matrix[4]*m[13]+matrix[8]*m[14];
106 result.matrix[13] += matrix[1]*m[12]+matrix[5]*m[13]+matrix[9]*m[14];
107 result.matrix[14] += matrix[2]*m[12]+matrix[6]*m[13]+matrix[10]*m[14];
108 result.flags |= other.flags;
114 fill(result.matrix, result.matrix+16, 0.0);
115 for(unsigned i=0; i<4; ++i)
116 for(unsigned j=0; j<4; ++j)
117 for(unsigned k=0; k<4; ++k)
118 result.matrix[i+j*4] += matrix[i+k*4]*other.matrix[k+j*4];
119 result.flags = flags|other.flags;
124 Matrix &Matrix::operator*=(const Matrix &other)
130 Vector4 Matrix::operator*(const Vector4 &vec) const
134 else if(flags==TRANSLATE)
135 return Vector4(vec.x+vec.w*matrix[12], vec.y+vec.w*matrix[13], vec.z+vec.w*matrix[14], vec.w);
136 else if(flags==SCALE)
137 return Vector4(vec.x*matrix[0], vec.y*matrix[5], vec.z*matrix[10], vec.w);
141 result.x = vec.x*matrix[0]+vec.y*matrix[4]+vec.z*matrix[8]+vec.w*matrix[12];
142 result.y = vec.x*matrix[1]+vec.y*matrix[5]+vec.z*matrix[9]+vec.w*matrix[13];
143 result.z = vec.x*matrix[2]+vec.y*matrix[6]+vec.z*matrix[10]+vec.w*matrix[14];
144 result.w = vec.x*matrix[3]+vec.y*matrix[7]+vec.z*matrix[11]+vec.w*matrix[15];
149 double Matrix::operator[](unsigned i) const
152 throw out_of_range("Matrix::operator[]");
156 Matrix Matrix::translation(double x, double y, double z)
159 result.matrix[12] = x;
160 result.matrix[13] = y;
161 result.matrix[14] = z;
162 result.flags |= TRANSLATE;
166 Matrix Matrix::rotation(double a, double x, double y, double z)
168 double l = sqrt(x*x+y*y+z*z);
175 // http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_given_an_axis_and_an_angle
177 result.matrix[0] = c+x*x*(1-c);
178 result.matrix[1] = y*x*(1-c)+z*s;
179 result.matrix[2] = z*x*(1-c)-y*s;
180 result.matrix[4] = x*y*(1-c)-z*s;
181 result.matrix[5] = c+y*y*(1-c);
182 result.matrix[6] = z*y*(1-c)+x*s;
183 result.matrix[8] = x*z*(1-c)+y*s;
184 result.matrix[9] = y*z*(1-c)-x*s;
185 result.matrix[10] = c+z*z*(1-c);
186 result.flags |= ROTATE;
190 Matrix Matrix::rotation_deg(double a, double x, double y, double z)
192 return rotation(a*M_PI/180, x, y, z);
195 Matrix Matrix::scaling(double s)
198 result.matrix[0] = s;
199 result.matrix[5] = s;
200 result.matrix[10] = s;
201 result.flags |= SCALE;
205 Matrix Matrix::scaling(double x, double y, double z)
208 result.matrix[0] = x;
209 result.matrix[5] = y;
210 result.matrix[10] = z;
211 result.flags |= SCALE|ARBITARY;
215 Matrix Matrix::ortho(double l, double r, double b, double t, double n, double f)
217 if(l==r || b==t || n==f)
218 throw invalid_argument("Matrix::ortho");
221 result.matrix[0] = 2/(r-l);
222 result.matrix[5] = 2/(t-b);
223 result.matrix[10] = -2/(f-n);
224 result.matrix[12] = -(r+l)/(r-l);
225 result.matrix[13] = -(t+b)/(t-b);
226 result.matrix[14] = -(f+n)/(f-n);
227 result.flags = TRANSLATE|SCALE|ARBITARY;
231 Matrix Matrix::ortho_centered(double w, double h)
233 return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
236 Matrix Matrix::ortho_bottomleft(double w, double h)
238 return ortho(0, w, 0, h, -1, 1);
241 Matrix Matrix::ortho_topleft(double w, double h)
243 return ortho(0, w, h, 0, -1, 1);
246 Matrix Matrix::frustum(double l, double r, double b, double t, double n, double f)
248 if(l==r || b==t || n<=0 || f<=n)
249 throw invalid_argument("Matrix::frustum");
252 result.matrix[0] = 2*n/(r-l);
253 result.matrix[5] = 2*n/(t-b);
254 result.matrix[8] = (r+l)/(r-l);
255 result.matrix[9] = (t+b)/(t-b);
256 result.matrix[10] = -(f+n)/(f-n);
257 result.matrix[11] = -1;
258 result.matrix[14] = -2*f*n/(f-n);
259 result.matrix[15] = 0;
260 result.flags = ARBITARY;
264 Matrix Matrix::frustum_centered(double w, double h, double n, double f)
266 return frustum(-w/2, w/2, -h/2, h/2, n, f);
269 Matrix Matrix::perspective(double h, double a, double n, double f)
271 double hh = tan(h/2)*n;
272 return frustum(-hh*a, hh*a, -hh, hh, n, f);
276 GLenum MatrixStack::current_mode = GL_MODELVIEW;
278 MatrixStack::MatrixStack(GLenum m):
281 matrices.push_back(Matrix());
284 MatrixStack::MatrixStack():
287 matrices.push_back(Matrix());
290 const Matrix &MatrixStack::top() const
292 return matrices.back();
295 void MatrixStack::load(const Matrix &m)
301 void MatrixStack::multiply(const Matrix &m)
303 matrices.back() *= m;
307 void MatrixStack::push()
309 matrices.push_back(top());
312 void MatrixStack::pop()
314 if(matrices.size()==1)
315 throw stack_underflow("MatrixStack::pop()");
321 void MatrixStack::update()
326 if(mode!=current_mode)
332 glLoadMatrixd(matrices.back().data());
335 MatrixStack &MatrixStack::operator=(const Matrix &m)
341 MatrixStack &MatrixStack::operator*=(const Matrix &m)
347 MatrixStack &MatrixStack::modelview()
349 static MatrixStack ms(GL_MODELVIEW);
353 MatrixStack &MatrixStack::projection()
355 static MatrixStack ms(GL_PROJECTION);
362 MatrixStack *active_stack = &MatrixStack::modelview();
364 void matrix_mode(MatrixMode m)
367 active_stack = &MatrixStack::modelview();
368 else if(m==PROJECTION)
369 active_stack = &MatrixStack::projection();
371 throw invalid_argument("matrix_mode");
376 *active_stack = Matrix();
379 void load_matrix(const float *matrix)
381 *active_stack = Matrix(matrix);
384 void load_matrix(const double *matrix)
386 *active_stack = Matrix(matrix);
389 void mult_matrix(const float *matrix)
391 *active_stack *= Matrix(matrix);
394 void mult_matrix(const double *matrix)
396 *active_stack *= Matrix(matrix);
401 active_stack->push();
409 void translate(float x, float y, float z)
411 *active_stack *= Matrix::translation(x, y, z);
414 void rotate(float a, float x, float y, float z)
416 *active_stack *= Matrix::rotation_deg(a, x, y, z);
419 void scale(float x, float y, float z)
421 *active_stack *= Matrix::scaling(x, y, z);
424 void scale_uniform(float s)
426 *active_stack *= Matrix::scaling(s);