1 /******************************************************************************
2     Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #pragma once
19 
20 #include "vec3.h"
21 #include "vec4.h"
22 #include "axisang.h"
23 
24 /* 4x4 Matrix */
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 struct matrix3;
31 
32 struct matrix4 {
33 	struct vec4 x, y, z, t;
34 };
35 
matrix4_copy(struct matrix4 * dst,const struct matrix4 * m)36 static inline void matrix4_copy(struct matrix4 *dst, const struct matrix4 *m)
37 {
38 	dst->x.m = m->x.m;
39 	dst->y.m = m->y.m;
40 	dst->z.m = m->z.m;
41 	dst->t.m = m->t.m;
42 }
43 
matrix4_identity(struct matrix4 * dst)44 static inline void matrix4_identity(struct matrix4 *dst)
45 {
46 	vec4_zero(&dst->x);
47 	vec4_zero(&dst->y);
48 	vec4_zero(&dst->z);
49 	vec4_zero(&dst->t);
50 	dst->x.x = 1.0f;
51 	dst->y.y = 1.0f;
52 	dst->z.z = 1.0f;
53 	dst->t.w = 1.0f;
54 }
55 
56 EXPORT void matrix4_from_matrix3(struct matrix4 *dst, const struct matrix3 *m);
57 EXPORT void matrix4_from_quat(struct matrix4 *dst, const struct quat *q);
58 EXPORT void matrix4_from_axisang(struct matrix4 *dst, const struct axisang *aa);
59 
60 EXPORT void matrix4_mul(struct matrix4 *dst, const struct matrix4 *m1,
61 			const struct matrix4 *m2);
62 
63 EXPORT float matrix4_determinant(const struct matrix4 *m);
64 
65 EXPORT void matrix4_translate3v(struct matrix4 *dst, const struct matrix4 *m,
66 				const struct vec3 *v);
67 EXPORT void matrix4_translate4v(struct matrix4 *dst, const struct matrix4 *m,
68 				const struct vec4 *v);
69 EXPORT void matrix4_rotate(struct matrix4 *dst, const struct matrix4 *m,
70 			   const struct quat *q);
71 EXPORT void matrix4_rotate_aa(struct matrix4 *dst, const struct matrix4 *m,
72 			      const struct axisang *aa);
73 EXPORT void matrix4_scale(struct matrix4 *dst, const struct matrix4 *m,
74 			  const struct vec3 *v);
75 EXPORT bool matrix4_inv(struct matrix4 *dst, const struct matrix4 *m);
76 EXPORT void matrix4_transpose(struct matrix4 *dst, const struct matrix4 *m);
77 
78 EXPORT void matrix4_translate3v_i(struct matrix4 *dst, const struct vec3 *v,
79 				  const struct matrix4 *m);
80 EXPORT void matrix4_translate4v_i(struct matrix4 *dst, const struct vec4 *v,
81 				  const struct matrix4 *m);
82 EXPORT void matrix4_rotate_i(struct matrix4 *dst, const struct quat *q,
83 			     const struct matrix4 *m);
84 EXPORT void matrix4_rotate_aa_i(struct matrix4 *dst, const struct axisang *aa,
85 				const struct matrix4 *m);
86 EXPORT void matrix4_scale_i(struct matrix4 *dst, const struct vec3 *v,
87 			    const struct matrix4 *m);
88 
matrix4_translate3f(struct matrix4 * dst,const struct matrix4 * m,float x,float y,float z)89 static inline void matrix4_translate3f(struct matrix4 *dst,
90 				       const struct matrix4 *m, float x,
91 				       float y, float z)
92 {
93 	struct vec3 v;
94 	vec3_set(&v, x, y, z);
95 	matrix4_translate3v(dst, m, &v);
96 }
97 
matrix4_rotate_aa4f(struct matrix4 * dst,const struct matrix4 * m,float x,float y,float z,float rot)98 static inline void matrix4_rotate_aa4f(struct matrix4 *dst,
99 				       const struct matrix4 *m, float x,
100 				       float y, float z, float rot)
101 {
102 	struct axisang aa;
103 	axisang_set(&aa, x, y, z, rot);
104 	matrix4_rotate_aa(dst, m, &aa);
105 }
106 
matrix4_scale3f(struct matrix4 * dst,const struct matrix4 * m,float x,float y,float z)107 static inline void matrix4_scale3f(struct matrix4 *dst, const struct matrix4 *m,
108 				   float x, float y, float z)
109 {
110 	struct vec3 v;
111 	vec3_set(&v, x, y, z);
112 	matrix4_scale(dst, m, &v);
113 }
114 
115 #ifdef __cplusplus
116 }
117 #endif
118