]> git.tdb.fi Git - libs/gl.git/blob - source/matrix.cpp
Replace GL matrices with an object-oriented implementation
[libs/gl.git] / source / matrix.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <algorithm>
9 #include <cmath>
10 #include <msp/core/except.h>
11 #include "matrix.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 Matrix::Matrix():
19         flags(IDENTITY)
20 {
21         for(unsigned i=0; i<16; ++i)
22                 matrix[i] = (i%5 ? 0 : 1);
23 }
24
25 Matrix::Matrix(const float *m):
26         flags(IDENTITY)
27 {
28         copy(m, m+16, matrix);
29         check_flags();
30 }
31
32 Matrix::Matrix(const double *m):
33         flags(IDENTITY)
34 {
35         copy(m, m+16, matrix);
36         check_flags();
37 }
38
39 void Matrix::check_flags()
40 {
41         const double *m = matrix;
42         if(m[12]!=0 || m[13]!=0 || m[14]!=0)
43                 flags |= TRANSLATE;
44         if(m[0]!=1)
45         {
46                 flags |= SCALE;
47                 if(m[5]!=m[0] || m[10]!=m[0])
48                         flags |= ARBITARY;
49         }
50         if(m[1]!=0 || m[2]!=0 || m[4]!=0 || m[6]!=0 || m[8]!=0 || m[9]!=0)
51         {
52                 flags |= ROTATE;
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)
57                         flags |= ARBITARY;
58         }
59         if(m[3]!=0 || m[7]!=0 || m[11]!=0 || m[15]!=1)
60                 flags |= ARBITARY;
61 }
62
63 void Matrix::multiply(const Matrix &other)
64 {
65         *this = *this*other;
66 }
67
68 void Matrix::translate(double x, double y, double z)
69 {
70         multiply(translation(x, y, z));
71 }
72
73 void Matrix::rotate(double a, double x, double y, double z)
74 {
75         multiply(rotation(a, x, y, z));
76 }
77
78 void Matrix::rotate_deg(double a, double x, double y, double z)
79 {
80         multiply(rotation_deg(a, x, y, z));
81 }
82
83 void Matrix::scale(double s)
84 {
85         multiply(scaling(s));
86 }
87
88 void Matrix::scale(double x, double y, double z)
89 {
90         multiply(scaling(x, y, z));
91 }
92
93 Matrix Matrix::operator*(const Matrix &other) const
94 {
95         if(flags==IDENTITY)
96                 return other;
97         else if(other.flags==IDENTITY)
98                 return *this;
99         else if(flags==TRANSLATE && !(other.flags&ARBITARY))
100         {
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;
106                 return result;
107         }
108         else if(!(flags&ARBITARY) && other.flags==TRANSLATE)
109         {
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;
116                 return result;
117         }
118         else
119         {
120                 Matrix result;
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;
127                 return result;
128         }
129 }
130
131 Matrix &Matrix::operator*=(const Matrix &other)
132 {
133         multiply(other);
134         return *this;
135 }
136
137 double Matrix::operator[](unsigned i) const
138 {
139         if(i>=16)
140                 throw InvalidParameterValue("Matrix element index out of range");
141         return matrix[i];
142 }
143
144 Matrix Matrix::translation(double x, double y, double z)
145 {
146         Matrix result;
147         result.matrix[12] = x;
148         result.matrix[13] = y;
149         result.matrix[14] = z;
150         result.flags |= TRANSLATE;
151         return result;
152 }
153
154 Matrix Matrix::rotation(double a, double x, double y, double z)
155 {
156         double l = sqrt(x*x+y*y+z*z);
157         x /= l;
158         y /= l;
159         z /= l;
160         double c = cos(a);
161         double s = sin(a);
162
163         // http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_given_an_axis_and_an_angle
164         Matrix result;
165         result.matrix[0] = c+x*x*(1-c);
166         result.matrix[1] = y*x*(1-c)+z*s;
167         result.matrix[2] = z*x*(1-c)-y*s;
168         result.matrix[4] = x*y*(1-c)-z*s;
169         result.matrix[5] = c+y*y*(1-c);
170         result.matrix[6] = z*y*(1-c)+x*s;
171         result.matrix[8] = x*z*(1-c)+y*s;
172         result.matrix[9] = y*z*(1-c)-x*s;
173         result.matrix[10] = c+z*z*(1-c);
174         result.flags |= ROTATE;
175         return result;
176 }
177
178 Matrix Matrix::rotation_deg(double a, double x, double y, double z)
179 {
180         return rotation(a*M_PI/180, x, y, z);
181 }
182
183 Matrix Matrix::scaling(double s)
184 {
185         Matrix result;
186         result.matrix[0] = s;
187         result.matrix[5] = s;
188         result.matrix[10] = s;
189         result.flags |= SCALE;
190         return result;
191 }
192
193 Matrix Matrix::scaling(double x, double y, double z)
194 {
195         Matrix result;
196         result.matrix[0] = x;
197         result.matrix[5] = y;
198         result.matrix[10] = z;
199         result.flags |= SCALE|ARBITARY;
200         return result;
201 }
202
203 Matrix Matrix::ortho(double l, double r, double b, double t, double n, double f)
204 {
205         if(l==r || b==t || n==f)
206                 throw InvalidParameterValue("Orthogonal projection can't have zero dimension");
207
208         Matrix result;
209         result.matrix[0] = 2/(r-l);
210         result.matrix[5] = 2/(t-b);
211         result.matrix[10] = -2/(f-n);
212         result.matrix[12] = -(r+l)/(r-l);
213         result.matrix[13] = -(t+b)/(t-b);
214         result.matrix[14] = -(f+n)/(f-n);
215         result.flags = TRANSLATE|SCALE|ARBITARY;
216         return result;
217 }
218
219 Matrix Matrix::ortho_centered(double w, double h)
220 {
221         return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
222 }
223
224 Matrix Matrix::ortho_bottomleft(double w, double h)
225 {
226         return ortho(0, w, 0, h, -1, 1);
227 }
228
229 Matrix Matrix::ortho_topleft(double w, double h)
230 {
231         return ortho(0, w, h, 0, -1, 1);
232 }
233
234 Matrix Matrix::frustum(double l, double r, double b, double t, double n, double f)
235 {
236         if(l==r || b==t || n<=0 || f<=n)
237                 throw InvalidParameterValue("Invalid frustum parameters");
238
239         Matrix result;
240         result.matrix[0] = 2*n/(r-l);
241         result.matrix[5] = 2*n/(t-b);
242         result.matrix[8] = (r+l)/(r-l);
243         result.matrix[9] = (t+b)/(t-b);
244         result.matrix[10] = -(f+n)/(f-n);
245         result.matrix[11] = -1;
246         result.matrix[14] = -2*f*n/(f-n);
247         result.matrix[15] = 0;
248         result.flags = ARBITARY;
249         return result;
250 }
251
252 Matrix Matrix::frustum_centered(double w, double h, double n, double f)
253 {
254         return frustum(-w/2, w/2, -h/2, h/2, n, f);
255 }
256
257 Matrix Matrix::perspective(double h, double a, double n, double f)
258 {
259         double hh = tan(h)*n;
260         return frustum(-hh*a, hh*a, -hh, hh, n, f);
261 }
262
263
264 GLenum MatrixStack::current_mode = GL_MODELVIEW;
265
266 MatrixStack::MatrixStack(GLenum m):
267         mode(m)
268 {
269         matrices.push_back(Matrix());
270 }
271
272 MatrixStack::MatrixStack():
273         mode(0)
274 {
275         matrices.push_back(Matrix());
276 }
277
278 const Matrix &MatrixStack::top()
279 {
280         return matrices.back();
281 }
282
283 void MatrixStack::load(const Matrix &m)
284 {
285         matrices.back() = m;
286         update();
287 }
288
289 void MatrixStack::multiply(const Matrix &m)
290 {
291         matrices.back() *= m;
292         update();
293 }
294
295 void MatrixStack::push()
296 {
297         matrices.push_back(top());
298 }
299
300 void MatrixStack::pop()
301 {
302         if(matrices.size()==1)
303                 throw InvalidState("Can't pop the last matrix");
304
305         matrices.pop_back();
306         update();
307 }
308
309 void MatrixStack::update()
310 {
311         if(!mode)
312                 return;
313
314         if(mode!=current_mode)
315         {
316                 glMatrixMode(mode);
317                 current_mode = mode;
318         }
319
320         glLoadMatrixd(matrices.back().data());
321 }
322
323 MatrixStack &MatrixStack::operator=(const Matrix &m)
324 {
325         load(m);
326         return *this;
327 }
328
329 MatrixStack &MatrixStack::operator*=(const Matrix &m)
330 {
331         multiply(m);
332         return *this;
333 }
334
335 MatrixStack &MatrixStack::modelview()
336 {
337         static MatrixStack ms(GL_MODELVIEW);
338         return ms;
339 }
340
341 MatrixStack &MatrixStack::projection()
342 {
343         static MatrixStack ms(GL_PROJECTION);
344         return ms;
345 }
346
347
348 // Deprecated stuff
349
350 MatrixStack *active_stack = &MatrixStack::modelview();
351
352 void matrix_mode(MatrixMode m)
353 {
354         if(m==MODELVIEW)
355                 active_stack = &MatrixStack::modelview();
356         else if(m==PROJECTION)
357                 active_stack = &MatrixStack::projection();
358         else
359                 throw InvalidParameterValue("Texture matrices are not supported");
360 }
361
362 void load_identity()
363 {
364         *active_stack = Matrix();
365 }
366
367 void load_matrix(const float *matrix)
368 {
369         *active_stack = Matrix(matrix);
370 }
371
372 void load_matrix(const double *matrix)
373 {
374         *active_stack = Matrix(matrix);
375 }
376
377 void mult_matrix(const float *matrix)
378 {
379         *active_stack *= Matrix(matrix);
380 }
381
382 void mult_matrix(const double *matrix)
383 {
384         *active_stack *= Matrix(matrix);
385 }
386
387 void push_matrix()
388 {
389         active_stack->push();
390 }
391
392 void pop_matrix()
393 {
394         active_stack->pop();
395 }
396
397 void translate(float x, float y, float z)
398 {
399         *active_stack *= Matrix::translation(x, y, z);
400 }
401
402 void rotate(float a, float x, float y, float z)
403 {
404         *active_stack *= Matrix::rotation_deg(a, x, y, z);
405 }
406
407 void scale(float x, float y, float z)
408 {
409         *active_stack *= Matrix::scaling(x, y, z);
410 }
411
412 void scale_uniform(float s)
413 {
414         *active_stack *= Matrix::scaling(s);
415 }
416
417 } // namespace GL
418 } // namespace Msp