Oolite
Loading...
Searching...
No Matches
OOMatrix.h
Go to the documentation of this file.
1/*
2
3OOMatrix.h
4
5Mathematical framework for Oolite.
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
27#include "OOQuaternion.h"
28#include "OOVector.h"
29
30typedef struct OOMatrix {
31 OOScalar m[4][4];
33
34extern const OOMatrix kIdentityMatrix; /* {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} */
35extern const OOMatrix kZeroMatrix; /* {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} */
36
37/* Matrix construction and standard primitive matrices */
39 OOScalar ba, OOScalar bb, OOScalar bc, OOScalar bd,
40 OOScalar ca, OOScalar cb, OOScalar cc, OOScalar cd,
42
44
47
50
56
59
61
62/* Matrix transformations */
66
69
76
79
80/* Matrix multiplication */
84
85/* Extraction */
87
88/* Orthogonalizion - avoidance of distortions due to numerical inaccuracy. */
90
91/* OpenGL conveniences. Need to be macros to work with OOMacroOpenGL. */
92#define OOMatrixValuesForOpenGL(M) (&(M).m[0][0])
93#define GLMultOOMatrix(M) \
94 do { \
95 OOMatrix m_ = M; \
96 OOGL(glMultMatrixf(OOMatrixValuesForOpenGL(m_))); \
97 } while (0)
98#define GLLoadOOMatrix(M) \
99 do { \
100 OOMatrix m_ = M; \
101 OOGL(glLoadMatrixf(OOMatrixValuesForOpenGL(m_))); \
102 } while (0)
103#define GLMultTransposeOOMatrix(M) \
104 do { \
105 OOMatrix m_ = M; \
106 OOGL(glMultTransposeMatrixf(OOMatrixValuesForOpenGL(m_))); \
107 } while (0)
108#define GLLoadTransposeOOMatrix(M) \
109 do { \
110 OOMatrix m_ = M; \
111 OOGL(glLoadTransposeMatrixf(OOMatrixValuesForOpenGL(m_))); \
112 } while (0)
113#define GLUniformMatrix(location, M) \
114 do { \
115 OOGL(glUniformMatrix4fvARB(location, 1, NO, OOMatrixValuesForOpenGL(M))); \
116 } while (0)
117void GLUniformMatrix3(GLint location, OOMatrix M);
118
120
121#if __OBJC__
122NSString* OOMatrixDescription(OOMatrix matrix); // @"{{#, #, #, #}, {#, #, #, #}, {#, #, #, #}, {#, #, #, #}}"
123#endif
124
125// Row operations
126
127// swap row1 and row2 of M
128void OOMatrixRowSwap(OOMatrix* M, int row1, int row2);
129// scale row of M by factor
130void OOMatrixRowScale(OOMatrix* m, int row, OOScalar factor);
131// replace row 1 of M with factor1 * row1 + factor2 * row2
132void OOMatrixRowOperation(OOMatrix* M, int row1, OOScalar factor1, int row2, OOScalar factor2);
133
134// Column operations
135
136// swap column1 and column2 of M
137void OOMatrixColumnSwap(OOMatrix* M, int column1, int column2);
138// scale column of M by factor
139void OOMatrixColumnScale(OOMatrix* M, int column, OOScalar factor);
140// replace column1 of M with factor1 * column1 + factor2 + row2
141void OOMatrixColumnOperation(OOMatrix* M, int column1, OOScalar factor1, int column2, OOScalar factor2);
142
143// Transforms between square matrices
144
145// return matrix X such that XA = B, or zero matrix if X doesn't exist.
147// return matrix X such that AX = B, or zero matrix if X doesn't exist.
149
150// Matrix inversion
151
153// Inverts matrix returning determinant of inverse in
155
156/*** Only inline definitions beyond this point ***/
157
159 OOScalar ba, OOScalar bb, OOScalar bc, OOScalar bd,
160 OOScalar ca, OOScalar cb, OOScalar cc, OOScalar cd,
161 OOScalar da, OOScalar db, OOScalar dc, OOScalar dd)
162{
163 OOMatrix r = { { { aa, ab, ac, ad },
164 { ba, bb, bc, bd },
165 { ca, cb, cc, cd },
166 { da, db, dc, dd } } };
167 return r;
168}
169
171{
173 return OOMatrixTranslate(m, position);
174}
175
177{
178 return OOMatrixConstruct(
179 i.x, i.y, i.z, 0.0f,
180 j.x, j.y, j.z, 0.0f,
181 k.x, k.y, k.z, 0.0f,
182 p.x, p.y, p.z, 1.0f);
183}
184
189
190/* Standard primitive transformation matrices: */
192{
193 OOScalar s, c;
194
195 s = sin(angle);
196 c = cos(angle);
197
198 return OOMatrixConstruct(
199 1, 0, 0, 0,
200 0, c, s, 0,
201 0, -s, c, 0,
202 0, 0, 0, 1);
203}
204
206{
207 OOScalar s, c;
208
209 s = sin(angle);
210 c = cos(angle);
211
212 return OOMatrixConstruct(
213 c, 0, -s, 0,
214 0, 1, 0, 0,
215 s, 0, c, 0,
216 0, 0, 0, 1);
217}
218
220{
221 OOScalar s, c;
222
223 s = sin(angle);
224 c = cos(angle);
225
226 return OOMatrixConstruct(
227 c, s, 0, 0,
228 -s, c, 0, 0,
229 0, 0, 1, 0,
230 0, 0, 0, 1);
231}
233{
234 return OOMatrixConstruct(
235 1, 0, 0, 0,
236 0, 1, 0, 0,
237 0, 0, 1, 0,
238 dx, dy, dz, 1);
239}
240
245
247{
248 m.m[3][0] += dx;
249 m.m[3][1] += dy;
250 m.m[3][2] += dz;
251 return m;
252}
253
258
263
265{
266 return OOMatrixConstruct(
267 sx, 0, 0, 0,
268 0, sy, 0, 0,
269 0, 0, sz, 0,
270 0, 0, 0, 1);
271}
272
277
282
284{
285 return OOMatrixScale(m, s, s, s);
286}
287
292
297
302
304{
305 return OOMatrixMultiply(m, OOMatrixForRotation(axis, angle));
306}
307
312
317
318OOINLINE void OOMatrixGetBasisVectors(OOMatrix m, Vector* outRight, Vector* outUp, Vector* outForward)
319{
320 assert(outRight != NULL && outUp != NULL && outForward != NULL);
321
322 *outRight = make_vector(m.m[0][0], m.m[1][0], m.m[2][0]);
323 *outUp = make_vector(m.m[0][1], m.m[1][1], m.m[2][1]);
324 *outForward = make_vector(m.m[0][2], m.m[1][2], m.m[2][2]);
325}
326
328{
329 return OOMatrixConstruct(
330 m.m[0][0], m.m[1][0], m.m[2][0], m.m[3][0],
331 m.m[0][1], m.m[1][1], m.m[2][1], m.m[3][1],
332 m.m[0][2], m.m[1][2], m.m[2][2], m.m[3][2],
333 m.m[0][3], m.m[1][3], m.m[2][3], m.m[3][3]);
334}
335
337{
338 OOMatrix m;
339 glGetFloatv(matrixID, OOMatrixValuesForOpenGL(m));
340 return m;
341}
#define ALWAYS_INLINE_FUNC
#define NONNULL_FUNC
#define INLINE_CONST_FUNC
#define OOINLINE
#define CONST_FUNC
GLfloat OOScalar
Definition OOMaths.h:49
OOINLINE OOMatrix OOMatrixForScale(OOScalar sx, OOScalar sy, OOScalar sz) INLINE_CONST_FUNC
Definition OOMatrix.h:264
void OOMatrixColumnSwap(OOMatrix *M, int column1, int column2)
Definition OOMatrix.m:246
OOINLINE OOMatrix OOMatrixTranslateComponents(OOMatrix m, OOScalar dx, OOScalar dy, OOScalar dz) INLINE_CONST_FUNC
Definition OOMatrix.h:246
OOINLINE OOMatrix OOMatrixConstruct(OOScalar aa, OOScalar ab, OOScalar ac, OOScalar ad, OOScalar ba, OOScalar bb, OOScalar bc, OOScalar bd, OOScalar ca, OOScalar cb, OOScalar cc, OOScalar cd, OOScalar da, OOScalar db, OOScalar dc, OOScalar dd) INLINE_CONST_FUNC
Definition OOMatrix.h:158
void GLUniformMatrix3(GLint location, OOMatrix M)
OOINLINE OOMatrix OOMatrixLoadGLMatrix(GLenum matrixID) ALWAYS_INLINE_FUNC
Definition OOMatrix.h:336
OOINLINE void OOMatrixGetBasisVectors(OOMatrix m, Vector *outRight, Vector *outUp, Vector *outForward) NONNULL_FUNC ALWAYS_INLINE_FUNC
Definition OOMatrix.h:318
OOMatrix OOMatrixLeftTransform(OOMatrix A, OOMatrix B)
Definition OOMatrix.m:285
void OOMatrixColumnOperation(OOMatrix *M, int column1, OOScalar factor1, int column2, OOScalar factor2)
Definition OOMatrix.m:273
OOINLINE OOMatrix OOMatrixForRotationZ(OOScalar angle) INLINE_CONST_FUNC
Definition OOMatrix.h:219
void OOMatrixColumnScale(OOMatrix *M, int column, OOScalar factor)
Definition OOMatrix.m:261
OOINLINE OOMatrix OOMatrixScaleUniform(OOMatrix m, OOScalar s) INLINE_CONST_FUNC
Definition OOMatrix.h:283
OOINLINE bool OOMatrixIsIdentity(OOMatrix m) INLINE_CONST_FUNC
Definition OOMatrix.h:313
OOMatrix OOMatrixRightTransform(OOMatrix A, OOMatrix B)
Definition OOMatrix.m:320
#define OOMatrixValuesForOpenGL(M)
Definition OOMatrix.h:92
OOMatrix OOMatrixOrthogonalize(OOMatrix m) CONST_FUNC
Definition OOMatrix.m:150
OOINLINE OOMatrix OOMatrixScale(OOMatrix m, OOScalar sx, OOScalar sy, OOScalar sz) INLINE_CONST_FUNC
Definition OOMatrix.h:278
void OOMatrixRowSwap(OOMatrix *M, int row1, int row2)
Definition OOMatrix.m:207
const OOMatrix kIdentityMatrix
Definition OOMatrix.m:28
OOMatrix OOMatrixInverseWithDeterminant(OOMatrix M, OOScalar *determinant)
Definition OOMatrix.m:364
OOINLINE OOMatrix OOMatrixRotateQuaternion(OOMatrix m, Quaternion quat) INLINE_CONST_FUNC
Definition OOMatrix.h:308
void OOMatrixRowScale(OOMatrix *m, int row, OOScalar factor)
Definition OOMatrix.m:222
OOINLINE OOMatrix OOMatrixFromBasisVectorsAndPosition(Vector i, Vector j, Vector k, Vector position) INLINE_CONST_FUNC
Definition OOMatrix.h:176
OOINLINE OOMatrix OOMatrixRotateZ(OOMatrix m, OOScalar angle) INLINE_CONST_FUNC
Definition OOMatrix.h:298
OOINLINE OOMatrix OOMatrixForRotationX(OOScalar angle) INLINE_CONST_FUNC
Definition OOMatrix.h:191
OOINLINE OOMatrix OOMatrixRotate(OOMatrix m, Vector axis, OOScalar angle) INLINE_CONST_FUNC
Definition OOMatrix.h:303
OOMatrix OOMatrixForBillboard(HPVector bbPos, HPVector eyePos) CONST_FUNC
Definition OOMatrix.m:186
OOINLINE OOMatrix OOMatrixHPTranslate(OOMatrix m, HPVector offset) INLINE_CONST_FUNC
Definition OOMatrix.h:259
OOINLINE OOMatrix OOMatrixRotateY(OOMatrix m, OOScalar angle) INLINE_CONST_FUNC
Definition OOMatrix.h:293
OOINLINE OOMatrix OOMatrixForRotationY(OOScalar angle) INLINE_CONST_FUNC
Definition OOMatrix.h:205
HPVector OOHPVectorMultiplyMatrix(HPVector v, OOMatrix m) CONST_FUNC
Definition OOMatrix.m:137
OOINLINE OOMatrix OOMatrixFromBasisVectors(Vector i, Vector j, Vector k) INLINE_CONST_FUNC
Definition OOMatrix.h:185
OOMatrix OOMatrixForRotation(Vector axis, OOScalar angle) CONST_FUNC
Definition OOMatrix.m:40
const OOMatrix kZeroMatrix
Definition OOMatrix.m:33
OOINLINE OOMatrix OOMatrixFromOrientationAndPosition(Quaternion orientation, Vector position) INLINE_CONST_FUNC
Definition OOMatrix.h:170
Vector OOVectorMultiplyMatrix(Vector v, OOMatrix m) CONST_FUNC
Definition OOMatrix.m:121
bool OOMatrixEqual(OOMatrix a, OOMatrix b) CONST_FUNC
Definition OOMatrix.m:91
OOINLINE OOMatrix OOMatrixForTranslation(Vector v) INLINE_CONST_FUNC
Definition OOMatrix.h:241
OOINLINE OOMatrix OOMatrixForScaleUniform(OOScalar s) INLINE_CONST_FUNC
Definition OOMatrix.h:273
OOMatrix OOMatrixInverse(OOMatrix M)
Definition OOMatrix.m:359
void OOMatrixRowOperation(OOMatrix *M, int row1, OOScalar factor1, int row2, OOScalar factor2)
Definition OOMatrix.m:234
OOINLINE OOMatrix OOMatrixForTranslationComponents(OOScalar dx, OOScalar dy, OOScalar dz) INLINE_CONST_FUNC
Definition OOMatrix.h:232
OOINLINE OOMatrix OOMatrixTranslate(OOMatrix m, Vector offset) INLINE_CONST_FUNC
Definition OOMatrix.h:254
OOMatrix OOMatrixForQuaternionRotation(Quaternion orientation)
Definition OOMatrix.m:56
OOINLINE OOMatrix OOMatrixTranspose(OOMatrix m) INLINE_CONST_FUNC
Definition OOMatrix.h:327
OOMatrix OOMatrixMultiply(OOMatrix a, OOMatrix b) CONST_FUNC
Definition OOMatrix.m:105
OOINLINE OOMatrix OOMatrixRotateX(OOMatrix m, OOScalar angle) INLINE_CONST_FUNC
Definition OOMatrix.h:288
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
const Vector kZeroVector
Definition OOVector.m:27
voidpf uLong offset
Definition ioapi.h:132
OOScalar m[4][4]
Definition OOMatrix.h:31
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34