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.reserve(mode==GL_MODELVIEW ? 32 : 4);
282 matrices.push_back(Matrix());
285 MatrixStack::MatrixStack():
288 matrices.reserve(32);
289 matrices.push_back(Matrix());
292 const Matrix &MatrixStack::top() const
294 return matrices.back();
297 void MatrixStack::load(const Matrix &m)
303 void MatrixStack::multiply(const Matrix &m)
305 matrices.back() *= m;
309 void MatrixStack::push()
311 matrices.push_back(top());
314 void MatrixStack::pop()
316 if(matrices.size()==1)
317 throw stack_underflow("MatrixStack::pop()");
323 void MatrixStack::update()
328 if(mode!=current_mode)
334 glLoadMatrixd(matrices.back().data());
337 MatrixStack &MatrixStack::operator=(const Matrix &m)
343 MatrixStack &MatrixStack::operator*=(const Matrix &m)
349 MatrixStack &MatrixStack::modelview()
351 static MatrixStack ms(GL_MODELVIEW);
355 MatrixStack &MatrixStack::projection()
357 static MatrixStack ms(GL_PROJECTION);
364 MatrixStack *active_stack = &MatrixStack::modelview();
366 void matrix_mode(MatrixMode m)
369 active_stack = &MatrixStack::modelview();
370 else if(m==PROJECTION)
371 active_stack = &MatrixStack::projection();
373 throw invalid_argument("matrix_mode");
378 *active_stack = Matrix();
381 void load_matrix(const float *matrix)
383 *active_stack = Matrix(matrix);
386 void load_matrix(const double *matrix)
388 *active_stack = Matrix(matrix);
391 void mult_matrix(const float *matrix)
393 *active_stack *= Matrix(matrix);
396 void mult_matrix(const double *matrix)
398 *active_stack *= Matrix(matrix);
403 active_stack->push();
411 void translate(float x, float y, float z)
413 *active_stack *= Matrix::translation(x, y, z);
416 void rotate(float a, float x, float y, float z)
418 *active_stack *= Matrix::rotation_deg(a, x, y, z);
421 void scale(float x, float y, float z)
423 *active_stack *= Matrix::scaling(x, y, z);
426 void scale_uniform(float s)
428 *active_stack *= Matrix::scaling(s);