rev |
line source |
nuclear@10
|
1 /*
|
nuclear@10
|
2 libvmath - a vector math library
|
nuclear@10
|
3 Copyright (C) 2004-2015 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@10
|
4
|
nuclear@10
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@10
|
6 it under the terms of the GNU Lesser General Public License as published
|
nuclear@10
|
7 by the Free Software Foundation, either version 3 of the License, or
|
nuclear@10
|
8 (at your option) any later version.
|
nuclear@10
|
9
|
nuclear@10
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@10
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@10
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@10
|
13 GNU Lesser General Public License for more details.
|
nuclear@10
|
14
|
nuclear@10
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@10
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@10
|
17 */
|
nuclear@10
|
18
|
nuclear@10
|
19 #ifndef VMATH_MATRIX_H_
|
nuclear@10
|
20 #define VMATH_MATRIX_H_
|
nuclear@10
|
21
|
nuclear@10
|
22 #include <stdio.h>
|
nuclear@10
|
23 #include "vmath_types.h"
|
nuclear@10
|
24 #include "vector.h"
|
nuclear@10
|
25
|
nuclear@10
|
26 #ifdef __cplusplus
|
nuclear@10
|
27 extern "C" {
|
nuclear@10
|
28 #endif /* __cplusplus */
|
nuclear@10
|
29
|
nuclear@10
|
30 /* C matrix 3x3 functions */
|
nuclear@10
|
31 static inline void m3_identity(mat3_t m);
|
nuclear@10
|
32 static inline void m3_cons(mat3_t m,
|
nuclear@10
|
33 scalar_t m11, scalar_t m12, scalar_t m13,
|
nuclear@10
|
34 scalar_t m21, scalar_t m22, scalar_t m23,
|
nuclear@10
|
35 scalar_t m31, scalar_t m32, scalar_t m33);
|
nuclear@10
|
36 static inline void m3_copy(mat3_t dest, mat3_t src);
|
nuclear@10
|
37 void m3_to_m4(mat4_t dest, mat3_t src);
|
nuclear@10
|
38
|
nuclear@10
|
39 void m3_print(FILE *fp, mat3_t m);
|
nuclear@10
|
40
|
nuclear@10
|
41 /* C matrix 4x4 functions */
|
nuclear@10
|
42 static inline void m4_identity(mat4_t m);
|
nuclear@10
|
43 static inline void m4_cons(mat4_t m,
|
nuclear@10
|
44 scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
|
nuclear@10
|
45 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
|
nuclear@10
|
46 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
|
nuclear@10
|
47 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
|
nuclear@10
|
48 static inline void m4_copy(mat4_t dest, mat4_t src);
|
nuclear@10
|
49 void m4_to_m3(mat3_t dest, mat4_t src);
|
nuclear@10
|
50
|
nuclear@10
|
51 static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2);
|
nuclear@10
|
52
|
nuclear@10
|
53 void m4_set_translation(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
|
nuclear@10
|
54 void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
|
nuclear@10
|
55
|
nuclear@10
|
56 void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
|
nuclear@10
|
57
|
nuclear@10
|
58 void m4_set_rotation_x(mat4_t m, scalar_t angle);
|
nuclear@10
|
59 void m4_rotate_x(mat4_t m, scalar_t angle);
|
nuclear@10
|
60 void m4_set_rotation_y(mat4_t m, scalar_t angle);
|
nuclear@10
|
61 void m4_rotate_y(mat4_t m, scalar_t angle);
|
nuclear@10
|
62 void m4_set_rotation_z(mat4_t m, scalar_t angle);
|
nuclear@10
|
63 void m4_rotate_z(mat4_t m, scalar_t angle);
|
nuclear@10
|
64 /* axis-angle rotation */
|
nuclear@10
|
65 void m4_set_rotation_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
|
nuclear@10
|
66 void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
|
nuclear@10
|
67 /* concatentate a rotation quaternion */
|
nuclear@10
|
68 void m4_rotate_quat(mat4_t m, quat_t q);
|
nuclear@10
|
69
|
nuclear@10
|
70 void m4_set_scaling(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
|
nuclear@10
|
71 void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
|
nuclear@10
|
72
|
nuclear@10
|
73 static inline void m4_set_column(mat4_t m, vec4_t v, int idx);
|
nuclear@10
|
74 static inline void m4_set_row(mat4_t m, vec4_t v, int idx);
|
nuclear@10
|
75
|
nuclear@10
|
76 void m4_transpose(mat4_t res, mat4_t m);
|
nuclear@10
|
77 scalar_t m4_determinant(mat4_t m);
|
nuclear@10
|
78 void m4_adjoint(mat4_t res, mat4_t m);
|
nuclear@10
|
79 void m4_inverse(mat4_t res, mat4_t m);
|
nuclear@10
|
80
|
nuclear@10
|
81 void m4_print(FILE *fp, mat4_t m);
|
nuclear@10
|
82
|
nuclear@10
|
83 #ifdef __cplusplus
|
nuclear@10
|
84 }
|
nuclear@10
|
85
|
nuclear@10
|
86 /* when included from C++ source files, also define the matrix classes */
|
nuclear@10
|
87
|
nuclear@10
|
88 /** 3x3 matrix */
|
nuclear@10
|
89 class Matrix3x3 {
|
nuclear@10
|
90 public:
|
nuclear@10
|
91 scalar_t m[3][3];
|
nuclear@10
|
92
|
nuclear@10
|
93 static Matrix3x3 identity;
|
nuclear@10
|
94
|
nuclear@10
|
95 Matrix3x3();
|
nuclear@10
|
96 Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13,
|
nuclear@10
|
97 scalar_t m21, scalar_t m22, scalar_t m23,
|
nuclear@10
|
98 scalar_t m31, scalar_t m32, scalar_t m33);
|
nuclear@10
|
99 Matrix3x3(const Vector3 &ivec, const Vector3 &jvec, const Vector3 &kvec);
|
nuclear@10
|
100 Matrix3x3(const mat3_t cmat);
|
nuclear@10
|
101
|
nuclear@10
|
102 Matrix3x3(const Matrix4x4 &mat4x4);
|
nuclear@10
|
103
|
nuclear@10
|
104 /* binary operations matrix (op) matrix */
|
nuclear@10
|
105 friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
106 friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
107 friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
108
|
nuclear@10
|
109 friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
110 friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
111 friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
112
|
nuclear@10
|
113 /* binary operations matrix (op) scalar and scalar (op) matrix */
|
nuclear@10
|
114 friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
|
nuclear@10
|
115 friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
|
nuclear@10
|
116
|
nuclear@10
|
117 friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
|
nuclear@10
|
118
|
nuclear@10
|
119 inline scalar_t *operator [](int index);
|
nuclear@10
|
120 inline const scalar_t *operator [](int index) const;
|
nuclear@10
|
121
|
nuclear@10
|
122 inline void reset_identity();
|
nuclear@10
|
123
|
nuclear@10
|
124 void translate(const Vector2 &trans);
|
nuclear@10
|
125 void set_translation(const Vector2 &trans);
|
nuclear@10
|
126
|
nuclear@10
|
127 void rotate(scalar_t angle); /* 2d rotation */
|
nuclear@10
|
128 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
|
nuclear@10
|
129 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
|
nuclear@10
|
130 void set_rotation(scalar_t angle);
|
nuclear@10
|
131 void set_rotation(const Vector3 &euler_angles);
|
nuclear@10
|
132 void set_rotation(const Vector3 &axis, scalar_t angle);
|
nuclear@10
|
133 Quaternion get_rotation_quat() const;
|
nuclear@10
|
134
|
nuclear@10
|
135 void scale(const Vector3 &scale_vec);
|
nuclear@10
|
136 void set_scaling(const Vector3 &scale_vec);
|
nuclear@10
|
137
|
nuclear@10
|
138 void set_column_vector(const Vector3 &vec, unsigned int col_index);
|
nuclear@10
|
139 void set_row_vector(const Vector3 &vec, unsigned int row_index);
|
nuclear@10
|
140 Vector3 get_column_vector(unsigned int col_index) const;
|
nuclear@10
|
141 Vector3 get_row_vector(unsigned int row_index) const;
|
nuclear@10
|
142
|
nuclear@10
|
143 void transpose();
|
nuclear@10
|
144 Matrix3x3 transposed() const;
|
nuclear@10
|
145 scalar_t determinant() const;
|
nuclear@10
|
146 Matrix3x3 inverse() const;
|
nuclear@10
|
147 };
|
nuclear@10
|
148
|
nuclear@10
|
149 /* binary operations matrix (op) matrix */
|
nuclear@10
|
150 Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
151 Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
152 Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
153
|
nuclear@10
|
154 void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
155 void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
156 void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
|
nuclear@10
|
157
|
nuclear@10
|
158 /* binary operations matrix (op) scalar and scalar (op) matrix */
|
nuclear@10
|
159 Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
|
nuclear@10
|
160 Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
|
nuclear@10
|
161
|
nuclear@10
|
162 void operator *=(Matrix3x3 &mat, scalar_t scalar);
|
nuclear@10
|
163
|
nuclear@10
|
164
|
nuclear@10
|
165
|
nuclear@10
|
166 /** 4x4 matrix */
|
nuclear@10
|
167 class Matrix4x4 {
|
nuclear@10
|
168 public:
|
nuclear@10
|
169 scalar_t m[4][4];
|
nuclear@10
|
170
|
nuclear@10
|
171 static Matrix4x4 identity;
|
nuclear@10
|
172
|
nuclear@10
|
173 Matrix4x4();
|
nuclear@10
|
174 Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
|
nuclear@10
|
175 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
|
nuclear@10
|
176 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
|
nuclear@10
|
177 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
|
nuclear@10
|
178 Matrix4x4(const mat4_t cmat);
|
nuclear@10
|
179
|
nuclear@10
|
180 Matrix4x4(const Matrix3x3 &mat3x3);
|
nuclear@10
|
181
|
nuclear@10
|
182 /* binary operations matrix (op) matrix */
|
nuclear@10
|
183 friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
184 friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
185 friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
186
|
nuclear@10
|
187 friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
188 friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
189 friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
190
|
nuclear@10
|
191 /* binary operations matrix (op) scalar and scalar (op) matrix */
|
nuclear@10
|
192 friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
|
nuclear@10
|
193 friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
|
nuclear@10
|
194
|
nuclear@10
|
195 friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
|
nuclear@10
|
196
|
nuclear@10
|
197 inline scalar_t *operator [](int index);
|
nuclear@10
|
198 inline const scalar_t *operator [](int index) const;
|
nuclear@10
|
199
|
nuclear@10
|
200 inline void reset_identity();
|
nuclear@10
|
201
|
nuclear@10
|
202 void translate(const Vector3 &trans);
|
nuclear@10
|
203 void set_translation(const Vector3 &trans);
|
nuclear@10
|
204 Vector3 get_translation() const; /* extract translation */
|
nuclear@10
|
205
|
nuclear@10
|
206 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
|
nuclear@10
|
207 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
|
nuclear@10
|
208 void rotate(const Quaternion &quat);
|
nuclear@10
|
209 void set_rotation(const Vector3 &euler_angles);
|
nuclear@10
|
210 void set_rotation(const Vector3 &axis, scalar_t angle);
|
nuclear@10
|
211 void set_rotation(const Quaternion &quat);
|
nuclear@10
|
212 Quaternion get_rotation_quat() const; /* extract rotation */
|
nuclear@10
|
213
|
nuclear@10
|
214 void scale(const Vector4 &scale_vec);
|
nuclear@10
|
215 void set_scaling(const Vector4 &scale_vec);
|
nuclear@10
|
216 Vector3 get_scaling() const; /* extract scaling */
|
nuclear@10
|
217
|
nuclear@10
|
218 void set_frustum(float left, float right, float top, float bottom, float znear, float zfar);
|
nuclear@10
|
219 void set_perspective(float vfov, float aspect, float znear, float zfar);
|
nuclear@10
|
220 void set_orthographic(float left, float right, float bottom, float top, float znear = -1.0, float zfar = 1.0);
|
nuclear@10
|
221
|
nuclear@10
|
222 void set_lookat(const Vector3 &pos, const Vector3 &targ = Vector3(0, 0, 0), const Vector3 &up = Vector3(0, 1, 0));
|
nuclear@10
|
223
|
nuclear@10
|
224 void set_column_vector(const Vector4 &vec, unsigned int col_index);
|
nuclear@10
|
225 void set_row_vector(const Vector4 &vec, unsigned int row_index);
|
nuclear@10
|
226 Vector4 get_column_vector(unsigned int col_index) const;
|
nuclear@10
|
227 Vector4 get_row_vector(unsigned int row_index) const;
|
nuclear@10
|
228
|
nuclear@10
|
229 void transpose();
|
nuclear@10
|
230 Matrix4x4 transposed() const;
|
nuclear@10
|
231 scalar_t determinant() const;
|
nuclear@10
|
232 Matrix4x4 adjoint() const;
|
nuclear@10
|
233 Matrix4x4 inverse() const;
|
nuclear@10
|
234 };
|
nuclear@10
|
235
|
nuclear@10
|
236 /* binary operations matrix (op) matrix */
|
nuclear@10
|
237 Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
238 Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
239 inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
240
|
nuclear@10
|
241 void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
242 void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
243 inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
|
nuclear@10
|
244
|
nuclear@10
|
245 /* binary operations matrix (op) scalar and scalar (op) matrix */
|
nuclear@10
|
246 Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
|
nuclear@10
|
247 Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
|
nuclear@10
|
248
|
nuclear@10
|
249 void operator *=(Matrix4x4 &mat, scalar_t scalar);
|
nuclear@10
|
250
|
nuclear@10
|
251 #endif /* __cplusplus */
|
nuclear@10
|
252
|
nuclear@10
|
253 #include "matrix.inl"
|
nuclear@10
|
254
|
nuclear@10
|
255 #endif /* VMATH_MATRIX_H_ */
|