3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include <msp/core/except.h>
21 for(unsigned i=0; i<16; ++i)
22 matrix[i] = (i%5 ? 0 : 1);
25 Matrix::Matrix(const float *m):
28 copy(m, m+16, matrix);
32 Matrix::Matrix(const double *m):
35 copy(m, m+16, matrix);
39 void Matrix::check_flags()
41 const double *m = matrix;
42 if(m[12]!=0 || m[13]!=0 || m[14]!=0)
47 if(m[5]!=m[0] || m[10]!=m[0])
50 if(m[1]!=0 || m[2]!=0 || m[4]!=0 || m[6]!=0 || m[8]!=0 || m[9]!=0)
53 double x_dot_y = m[0]*m[1]+m[4]*m[5]+m[8]*m[9];
54 double x_dot_z = m[0]*m[2]+m[4]*m[6]+m[8]*m[10];
55 double y_dot_z = m[1]*m[2]+m[5]*m[6]+m[9]*m[10];
56 if(x_dot_y!=0 || x_dot_z!=0 || y_dot_z!=0)
59 if(m[3]!=0 || m[7]!=0 || m[11]!=0 || m[15]!=1)
63 void Matrix::multiply(const Matrix &other)
68 void Matrix::translate(double x, double y, double z)
70 multiply(translation(x, y, z));
73 void Matrix::rotate(double a, double x, double y, double z)
75 multiply(rotation(a, x, y, z));
78 void Matrix::rotate_deg(double a, double x, double y, double z)
80 multiply(rotation_deg(a, x, y, z));
83 void Matrix::scale(double s)
88 void Matrix::scale(double x, double y, double z)
90 multiply(scaling(x, y, z));
93 Matrix Matrix::operator*(const Matrix &other) const
97 else if(other.flags==IDENTITY)
99 else if(flags==TRANSLATE && !(other.flags&ARBITARY))
101 Matrix result = other;
102 result.matrix[12] += matrix[12];
103 result.matrix[13] += matrix[13];
104 result.matrix[14] += matrix[14];
105 result.flags |= flags;
108 else if(!(flags&ARBITARY) && other.flags==TRANSLATE)
110 Matrix result = *this;
111 const double *m = other.matrix;
112 result.matrix[12] += matrix[0]*m[12]+matrix[4]*m[13]+matrix[8]*m[14];
113 result.matrix[13] += matrix[1]*m[12]+matrix[5]*m[13]+matrix[9]*m[14];
114 result.matrix[14] += matrix[2]*m[12]+matrix[6]*m[13]+matrix[10]*m[14];
115 result.flags |= other.flags;
121 fill(result.matrix, result.matrix+16, 0.0);
122 for(unsigned i=0; i<4; ++i)
123 for(unsigned j=0; j<4; ++j)
124 for(unsigned k=0; k<4; ++k)
125 result.matrix[i+j*4] += matrix[i+k*4]*other.matrix[k+j*4];
126 result.flags = flags|other.flags;
131 Matrix &Matrix::operator*=(const Matrix &other)
137 Vector4 Matrix::operator*(const Vector4 &vec) const
141 else if(flags==TRANSLATE)
142 return Vector4(vec.x+vec.w*matrix[12], vec.y+vec.w*matrix[13], vec.z+vec.w*matrix[14], vec.w);
143 else if(flags==SCALE)
144 return Vector4(vec.x*matrix[0], vec.y*matrix[5], vec.z*matrix[10], vec.w);
148 result.x = vec.x*matrix[0]+vec.y*matrix[4]+vec.z*matrix[8]+vec.w*matrix[12];
149 result.y = vec.x*matrix[1]+vec.y*matrix[5]+vec.z*matrix[9]+vec.w*matrix[13];
150 result.z = vec.x*matrix[2]+vec.y*matrix[6]+vec.z*matrix[10]+vec.w*matrix[14];
151 result.w = vec.x*matrix[3]+vec.y*matrix[7]+vec.z*matrix[11]+vec.w*matrix[15];
156 double Matrix::operator[](unsigned i) const
159 throw InvalidParameterValue("Matrix element index out of range");
163 Matrix Matrix::translation(double x, double y, double z)
166 result.matrix[12] = x;
167 result.matrix[13] = y;
168 result.matrix[14] = z;
169 result.flags |= TRANSLATE;
173 Matrix Matrix::rotation(double a, double x, double y, double z)
175 double l = sqrt(x*x+y*y+z*z);
182 // http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_given_an_axis_and_an_angle
184 result.matrix[0] = c+x*x*(1-c);
185 result.matrix[1] = y*x*(1-c)+z*s;
186 result.matrix[2] = z*x*(1-c)-y*s;
187 result.matrix[4] = x*y*(1-c)-z*s;
188 result.matrix[5] = c+y*y*(1-c);
189 result.matrix[6] = z*y*(1-c)+x*s;
190 result.matrix[8] = x*z*(1-c)+y*s;
191 result.matrix[9] = y*z*(1-c)-x*s;
192 result.matrix[10] = c+z*z*(1-c);
193 result.flags |= ROTATE;
197 Matrix Matrix::rotation_deg(double a, double x, double y, double z)
199 return rotation(a*M_PI/180, x, y, z);
202 Matrix Matrix::scaling(double s)
205 result.matrix[0] = s;
206 result.matrix[5] = s;
207 result.matrix[10] = s;
208 result.flags |= SCALE;
212 Matrix Matrix::scaling(double x, double y, double z)
215 result.matrix[0] = x;
216 result.matrix[5] = y;
217 result.matrix[10] = z;
218 result.flags |= SCALE|ARBITARY;
222 Matrix Matrix::ortho(double l, double r, double b, double t, double n, double f)
224 if(l==r || b==t || n==f)
225 throw InvalidParameterValue("Orthogonal projection can't have zero dimension");
228 result.matrix[0] = 2/(r-l);
229 result.matrix[5] = 2/(t-b);
230 result.matrix[10] = -2/(f-n);
231 result.matrix[12] = -(r+l)/(r-l);
232 result.matrix[13] = -(t+b)/(t-b);
233 result.matrix[14] = -(f+n)/(f-n);
234 result.flags = TRANSLATE|SCALE|ARBITARY;
238 Matrix Matrix::ortho_centered(double w, double h)
240 return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
243 Matrix Matrix::ortho_bottomleft(double w, double h)
245 return ortho(0, w, 0, h, -1, 1);
248 Matrix Matrix::ortho_topleft(double w, double h)
250 return ortho(0, w, h, 0, -1, 1);
253 Matrix Matrix::frustum(double l, double r, double b, double t, double n, double f)
255 if(l==r || b==t || n<=0 || f<=n)
256 throw InvalidParameterValue("Invalid frustum parameters");
259 result.matrix[0] = 2*n/(r-l);
260 result.matrix[5] = 2*n/(t-b);
261 result.matrix[8] = (r+l)/(r-l);
262 result.matrix[9] = (t+b)/(t-b);
263 result.matrix[10] = -(f+n)/(f-n);
264 result.matrix[11] = -1;
265 result.matrix[14] = -2*f*n/(f-n);
266 result.matrix[15] = 0;
267 result.flags = ARBITARY;
271 Matrix Matrix::frustum_centered(double w, double h, double n, double f)
273 return frustum(-w/2, w/2, -h/2, h/2, n, f);
276 Matrix Matrix::perspective(double h, double a, double n, double f)
278 double hh = tan(h/2)*n;
279 return frustum(-hh*a, hh*a, -hh, hh, n, f);
283 GLenum MatrixStack::current_mode = GL_MODELVIEW;
285 MatrixStack::MatrixStack(GLenum m):
288 matrices.push_back(Matrix());
291 MatrixStack::MatrixStack():
294 matrices.push_back(Matrix());
297 const Matrix &MatrixStack::top() const
299 return matrices.back();
302 void MatrixStack::load(const Matrix &m)
308 void MatrixStack::multiply(const Matrix &m)
310 matrices.back() *= m;
314 void MatrixStack::push()
316 matrices.push_back(top());
319 void MatrixStack::pop()
321 if(matrices.size()==1)
322 throw InvalidState("Can't pop the last matrix");
328 void MatrixStack::update()
333 if(mode!=current_mode)
339 glLoadMatrixd(matrices.back().data());
342 MatrixStack &MatrixStack::operator=(const Matrix &m)
348 MatrixStack &MatrixStack::operator*=(const Matrix &m)
354 MatrixStack &MatrixStack::modelview()
356 static MatrixStack ms(GL_MODELVIEW);
360 MatrixStack &MatrixStack::projection()
362 static MatrixStack ms(GL_PROJECTION);
369 MatrixStack *active_stack = &MatrixStack::modelview();
371 void matrix_mode(MatrixMode m)
374 active_stack = &MatrixStack::modelview();
375 else if(m==PROJECTION)
376 active_stack = &MatrixStack::projection();
378 throw InvalidParameterValue("Texture matrices are not supported");
383 *active_stack = Matrix();
386 void load_matrix(const float *matrix)
388 *active_stack = Matrix(matrix);
391 void load_matrix(const double *matrix)
393 *active_stack = Matrix(matrix);
396 void mult_matrix(const float *matrix)
398 *active_stack *= Matrix(matrix);
401 void mult_matrix(const double *matrix)
403 *active_stack *= Matrix(matrix);
408 active_stack->push();
416 void translate(float x, float y, float z)
418 *active_stack *= Matrix::translation(x, y, z);
421 void rotate(float a, float x, float y, float z)
423 *active_stack *= Matrix::rotation_deg(a, x, y, z);
426 void scale(float x, float y, float z)
428 *active_stack *= Matrix::scaling(x, y, z);
431 void scale_uniform(float s)
433 *active_stack *= Matrix::scaling(s);