cglm 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/Gemfile.lock +18 -16
- data/README.md +2 -0
- data/cglm.gemspec +3 -3
- data/ext/cglm/cglm-0.6.2/include/cglm/affine-mat.h +168 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/affine.h +490 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/applesimd.h +95 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/bezier.h +154 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/box.h +279 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/affine.h +117 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/bezier.h +31 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/box.h +79 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/cam.h +143 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/curve.h +23 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/ease.h +143 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/euler.h +55 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/frustum.h +41 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/io.h +44 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/mat3.h +86 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/mat4.h +127 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/plane.h +23 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/project.h +33 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/quat.h +159 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/sphere.h +39 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/vec3.h +312 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call/vec4.h +290 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/call.h +36 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/cam.h +585 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/cglm.h +32 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/color.h +26 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/common.h +37 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/curve.h +40 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/ease.h +317 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/euler.h +453 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/frustum.h +255 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/io.h +203 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/mat3.h +422 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/mat4.h +726 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/plane.h +36 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/project.h +118 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/quat.h +828 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/arm.h +83 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/avx/affine.h +66 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/avx/mat4.h +66 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/intrin.h +90 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/neon/mat4.h +57 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/sse2/affine.h +111 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/sse2/mat3.h +59 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/sse2/mat4.h +405 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/sse2/quat.h +46 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/simd/x86.h +192 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/sphere.h +99 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/affine.h +337 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/box.h +256 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/cam.h +451 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/color.h +27 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/curve.h +40 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/euler.h +152 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/frustum.h +155 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/io.h +82 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/mat3.h +285 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/mat4.h +459 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/plane.h +40 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/project.h +104 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/quat.h +532 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/sphere.h +93 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/vec3-ext.h +257 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/vec3.h +970 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/vec4-ext.h +257 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct/vec4.h +814 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/struct.h +36 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/types-struct.h +129 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/types.h +76 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/util.h +328 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/vec3-ext.h +272 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/vec3.h +1078 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/vec4-ext.h +315 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/vec4.h +1078 -0
- data/ext/cglm/cglm-0.6.2/include/cglm/version.h +15 -0
- data/ext/cglm/extconf.rb +2 -3
- data/ext/cglm/rb_cglm.h +5 -3
- data/ext/cglm/rb_cglm_mat3.c +3 -3
- data/ext/cglm/rb_cglm_mat4.c +3 -3
- data/ext/cglm/rb_cglm_quat.c +2 -2
- data/ext/cglm/rb_cglm_vec3.c +63 -61
- data/ext/cglm/rb_cglm_vec4.c +2 -0
- data/ext/cglm/ruby_pre27.h +35 -0
- data/lib/cglm/vec3.rb +2 -2
- data/lib/cglm/vec4.rb +2 -2
- data/lib/cglm/vector_type.rb +15 -0
- data/lib/cglm/version.rb +1 -1
- metadata +89 -13
@@ -0,0 +1,453 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c), Recep Aslantas.
|
3
|
+
*
|
4
|
+
* MIT License (MIT), http://opensource.org/licenses/MIT
|
5
|
+
* Full license can be found in the LICENSE file
|
6
|
+
*/
|
7
|
+
|
8
|
+
/*
|
9
|
+
NOTE:
|
10
|
+
angles must be passed as [X-Angle, Y-Angle, Z-angle] order
|
11
|
+
For instance you don't pass angles as [Z-Angle, X-Angle, Y-angle] to
|
12
|
+
glm_euler_zxy funciton, All RELATED functions accept angles same order
|
13
|
+
which is [X, Y, Z].
|
14
|
+
*/
|
15
|
+
|
16
|
+
/*
|
17
|
+
Types:
|
18
|
+
enum glm_euler_seq
|
19
|
+
|
20
|
+
Functions:
|
21
|
+
CGLM_INLINE glm_euler_seq glm_euler_order(int newOrder[3]);
|
22
|
+
CGLM_INLINE void glm_euler_angles(mat4 m, vec3 dest);
|
23
|
+
CGLM_INLINE void glm_euler(vec3 angles, mat4 dest);
|
24
|
+
CGLM_INLINE void glm_euler_xyz(vec3 angles, mat4 dest);
|
25
|
+
CGLM_INLINE void glm_euler_zyx(vec3 angles, mat4 dest);
|
26
|
+
CGLM_INLINE void glm_euler_zxy(vec3 angles, mat4 dest);
|
27
|
+
CGLM_INLINE void glm_euler_xzy(vec3 angles, mat4 dest);
|
28
|
+
CGLM_INLINE void glm_euler_yzx(vec3 angles, mat4 dest);
|
29
|
+
CGLM_INLINE void glm_euler_yxz(vec3 angles, mat4 dest);
|
30
|
+
CGLM_INLINE void glm_euler_by_order(vec3 angles,
|
31
|
+
glm_euler_seq ord,
|
32
|
+
mat4 dest);
|
33
|
+
*/
|
34
|
+
|
35
|
+
#ifndef cglm_euler_h
|
36
|
+
#define cglm_euler_h
|
37
|
+
|
38
|
+
#include "common.h"
|
39
|
+
|
40
|
+
/*!
|
41
|
+
* if you have axis order like vec3 orderVec = [0, 1, 2] or [0, 2, 1]...
|
42
|
+
* vector then you can convert it to this enum by doing this:
|
43
|
+
* @code
|
44
|
+
* glm_euler_seq order;
|
45
|
+
* order = orderVec[0] | orderVec[1] << 2 | orderVec[2] << 4;
|
46
|
+
* @endcode
|
47
|
+
* you may need to explicit cast if required
|
48
|
+
*/
|
49
|
+
typedef enum glm_euler_seq {
|
50
|
+
GLM_EULER_XYZ = 0 << 0 | 1 << 2 | 2 << 4,
|
51
|
+
GLM_EULER_XZY = 0 << 0 | 2 << 2 | 1 << 4,
|
52
|
+
GLM_EULER_YZX = 1 << 0 | 2 << 2 | 0 << 4,
|
53
|
+
GLM_EULER_YXZ = 1 << 0 | 0 << 2 | 2 << 4,
|
54
|
+
GLM_EULER_ZXY = 2 << 0 | 0 << 2 | 1 << 4,
|
55
|
+
GLM_EULER_ZYX = 2 << 0 | 1 << 2 | 0 << 4
|
56
|
+
} glm_euler_seq;
|
57
|
+
|
58
|
+
typedef glm_euler_seq glm_euler_sq;
|
59
|
+
|
60
|
+
CGLM_INLINE
|
61
|
+
glm_euler_seq
|
62
|
+
glm_euler_order(int ord[3]) {
|
63
|
+
return (glm_euler_seq)(ord[0] << 0 | ord[1] << 2 | ord[2] << 4);
|
64
|
+
}
|
65
|
+
|
66
|
+
/*!
|
67
|
+
* @brief extract euler angles (in radians) using xyz order
|
68
|
+
*
|
69
|
+
* @param[in] m affine transform
|
70
|
+
* @param[out] dest angles vector [x, y, z]
|
71
|
+
*/
|
72
|
+
CGLM_INLINE
|
73
|
+
void
|
74
|
+
glm_euler_angles(mat4 m, vec3 dest) {
|
75
|
+
float m00, m01, m10, m11, m20, m21, m22;
|
76
|
+
float thetaX, thetaY, thetaZ;
|
77
|
+
|
78
|
+
m00 = m[0][0]; m10 = m[1][0]; m20 = m[2][0];
|
79
|
+
m01 = m[0][1]; m11 = m[1][1]; m21 = m[2][1];
|
80
|
+
m22 = m[2][2];
|
81
|
+
|
82
|
+
if (m20 < 1.0f) {
|
83
|
+
if (m20 > -1.0f) {
|
84
|
+
thetaY = asinf(m20);
|
85
|
+
thetaX = atan2f(-m21, m22);
|
86
|
+
thetaZ = atan2f(-m10, m00);
|
87
|
+
} else { /* m20 == -1 */
|
88
|
+
/* Not a unique solution */
|
89
|
+
thetaY = -GLM_PI_2f;
|
90
|
+
thetaX = -atan2f(m01, m11);
|
91
|
+
thetaZ = 0.0f;
|
92
|
+
}
|
93
|
+
} else { /* m20 == +1 */
|
94
|
+
thetaY = GLM_PI_2f;
|
95
|
+
thetaX = atan2f(m01, m11);
|
96
|
+
thetaZ = 0.0f;
|
97
|
+
}
|
98
|
+
|
99
|
+
dest[0] = thetaX;
|
100
|
+
dest[1] = thetaY;
|
101
|
+
dest[2] = thetaZ;
|
102
|
+
}
|
103
|
+
|
104
|
+
/*!
|
105
|
+
* @brief build rotation matrix from euler angles
|
106
|
+
*
|
107
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
108
|
+
* @param[out] dest rotation matrix
|
109
|
+
*/
|
110
|
+
CGLM_INLINE
|
111
|
+
void
|
112
|
+
glm_euler_xyz(vec3 angles, mat4 dest) {
|
113
|
+
float cx, cy, cz,
|
114
|
+
sx, sy, sz, czsx, cxcz, sysz;
|
115
|
+
|
116
|
+
sx = sinf(angles[0]); cx = cosf(angles[0]);
|
117
|
+
sy = sinf(angles[1]); cy = cosf(angles[1]);
|
118
|
+
sz = sinf(angles[2]); cz = cosf(angles[2]);
|
119
|
+
|
120
|
+
czsx = cz * sx;
|
121
|
+
cxcz = cx * cz;
|
122
|
+
sysz = sy * sz;
|
123
|
+
|
124
|
+
dest[0][0] = cy * cz;
|
125
|
+
dest[0][1] = czsx * sy + cx * sz;
|
126
|
+
dest[0][2] = -cxcz * sy + sx * sz;
|
127
|
+
dest[1][0] = -cy * sz;
|
128
|
+
dest[1][1] = cxcz - sx * sysz;
|
129
|
+
dest[1][2] = czsx + cx * sysz;
|
130
|
+
dest[2][0] = sy;
|
131
|
+
dest[2][1] = -cy * sx;
|
132
|
+
dest[2][2] = cx * cy;
|
133
|
+
dest[0][3] = 0.0f;
|
134
|
+
dest[1][3] = 0.0f;
|
135
|
+
dest[2][3] = 0.0f;
|
136
|
+
dest[3][0] = 0.0f;
|
137
|
+
dest[3][1] = 0.0f;
|
138
|
+
dest[3][2] = 0.0f;
|
139
|
+
dest[3][3] = 1.0f;
|
140
|
+
}
|
141
|
+
|
142
|
+
/*!
|
143
|
+
* @brief build rotation matrix from euler angles
|
144
|
+
*
|
145
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
146
|
+
* @param[out] dest rotation matrix
|
147
|
+
*/
|
148
|
+
CGLM_INLINE
|
149
|
+
void
|
150
|
+
glm_euler(vec3 angles, mat4 dest) {
|
151
|
+
glm_euler_xyz(angles, dest);
|
152
|
+
}
|
153
|
+
|
154
|
+
/*!
|
155
|
+
* @brief build rotation matrix from euler angles
|
156
|
+
*
|
157
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
158
|
+
* @param[out] dest rotation matrix
|
159
|
+
*/
|
160
|
+
CGLM_INLINE
|
161
|
+
void
|
162
|
+
glm_euler_xzy(vec3 angles, mat4 dest) {
|
163
|
+
float cx, cy, cz,
|
164
|
+
sx, sy, sz, sxsy, cysx, cxsy, cxcy;
|
165
|
+
|
166
|
+
sx = sinf(angles[0]); cx = cosf(angles[0]);
|
167
|
+
sy = sinf(angles[1]); cy = cosf(angles[1]);
|
168
|
+
sz = sinf(angles[2]); cz = cosf(angles[2]);
|
169
|
+
|
170
|
+
sxsy = sx * sy;
|
171
|
+
cysx = cy * sx;
|
172
|
+
cxsy = cx * sy;
|
173
|
+
cxcy = cx * cy;
|
174
|
+
|
175
|
+
dest[0][0] = cy * cz;
|
176
|
+
dest[0][1] = sxsy + cxcy * sz;
|
177
|
+
dest[0][2] = -cxsy + cysx * sz;
|
178
|
+
dest[1][0] = -sz;
|
179
|
+
dest[1][1] = cx * cz;
|
180
|
+
dest[1][2] = cz * sx;
|
181
|
+
dest[2][0] = cz * sy;
|
182
|
+
dest[2][1] = -cysx + cxsy * sz;
|
183
|
+
dest[2][2] = cxcy + sxsy * sz;
|
184
|
+
dest[0][3] = 0.0f;
|
185
|
+
dest[1][3] = 0.0f;
|
186
|
+
dest[2][3] = 0.0f;
|
187
|
+
dest[3][0] = 0.0f;
|
188
|
+
dest[3][1] = 0.0f;
|
189
|
+
dest[3][2] = 0.0f;
|
190
|
+
dest[3][3] = 1.0f;
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
/*!
|
195
|
+
* @brief build rotation matrix from euler angles
|
196
|
+
*
|
197
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
198
|
+
* @param[out] dest rotation matrix
|
199
|
+
*/
|
200
|
+
CGLM_INLINE
|
201
|
+
void
|
202
|
+
glm_euler_yxz(vec3 angles, mat4 dest) {
|
203
|
+
float cx, cy, cz,
|
204
|
+
sx, sy, sz, cycz, sysz, czsy, cysz;
|
205
|
+
|
206
|
+
sx = sinf(angles[0]); cx = cosf(angles[0]);
|
207
|
+
sy = sinf(angles[1]); cy = cosf(angles[1]);
|
208
|
+
sz = sinf(angles[2]); cz = cosf(angles[2]);
|
209
|
+
|
210
|
+
cycz = cy * cz;
|
211
|
+
sysz = sy * sz;
|
212
|
+
czsy = cz * sy;
|
213
|
+
cysz = cy * sz;
|
214
|
+
|
215
|
+
dest[0][0] = cycz + sx * sysz;
|
216
|
+
dest[0][1] = cx * sz;
|
217
|
+
dest[0][2] = -czsy + cysz * sx;
|
218
|
+
dest[1][0] = -cysz + czsy * sx;
|
219
|
+
dest[1][1] = cx * cz;
|
220
|
+
dest[1][2] = cycz * sx + sysz;
|
221
|
+
dest[2][0] = cx * sy;
|
222
|
+
dest[2][1] = -sx;
|
223
|
+
dest[2][2] = cx * cy;
|
224
|
+
dest[0][3] = 0.0f;
|
225
|
+
dest[1][3] = 0.0f;
|
226
|
+
dest[2][3] = 0.0f;
|
227
|
+
dest[3][0] = 0.0f;
|
228
|
+
dest[3][1] = 0.0f;
|
229
|
+
dest[3][2] = 0.0f;
|
230
|
+
dest[3][3] = 1.0f;
|
231
|
+
}
|
232
|
+
|
233
|
+
/*!
|
234
|
+
* @brief build rotation matrix from euler angles
|
235
|
+
*
|
236
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
237
|
+
* @param[out] dest rotation matrix
|
238
|
+
*/
|
239
|
+
CGLM_INLINE
|
240
|
+
void
|
241
|
+
glm_euler_yzx(vec3 angles, mat4 dest) {
|
242
|
+
float cx, cy, cz,
|
243
|
+
sx, sy, sz, sxsy, cxcy, cysx, cxsy;
|
244
|
+
|
245
|
+
sx = sinf(angles[0]); cx = cosf(angles[0]);
|
246
|
+
sy = sinf(angles[1]); cy = cosf(angles[1]);
|
247
|
+
sz = sinf(angles[2]); cz = cosf(angles[2]);
|
248
|
+
|
249
|
+
sxsy = sx * sy;
|
250
|
+
cxcy = cx * cy;
|
251
|
+
cysx = cy * sx;
|
252
|
+
cxsy = cx * sy;
|
253
|
+
|
254
|
+
dest[0][0] = cy * cz;
|
255
|
+
dest[0][1] = sz;
|
256
|
+
dest[0][2] = -cz * sy;
|
257
|
+
dest[1][0] = sxsy - cxcy * sz;
|
258
|
+
dest[1][1] = cx * cz;
|
259
|
+
dest[1][2] = cysx + cxsy * sz;
|
260
|
+
dest[2][0] = cxsy + cysx * sz;
|
261
|
+
dest[2][1] = -cz * sx;
|
262
|
+
dest[2][2] = cxcy - sxsy * sz;
|
263
|
+
dest[0][3] = 0.0f;
|
264
|
+
dest[1][3] = 0.0f;
|
265
|
+
dest[2][3] = 0.0f;
|
266
|
+
dest[3][0] = 0.0f;
|
267
|
+
dest[3][1] = 0.0f;
|
268
|
+
dest[3][2] = 0.0f;
|
269
|
+
dest[3][3] = 1.0f;
|
270
|
+
}
|
271
|
+
|
272
|
+
/*!
|
273
|
+
* @brief build rotation matrix from euler angles
|
274
|
+
*
|
275
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
276
|
+
* @param[out] dest rotation matrix
|
277
|
+
*/
|
278
|
+
CGLM_INLINE
|
279
|
+
void
|
280
|
+
glm_euler_zxy(vec3 angles, mat4 dest) {
|
281
|
+
float cx, cy, cz,
|
282
|
+
sx, sy, sz, cycz, sxsy, cysz;
|
283
|
+
|
284
|
+
sx = sinf(angles[0]); cx = cosf(angles[0]);
|
285
|
+
sy = sinf(angles[1]); cy = cosf(angles[1]);
|
286
|
+
sz = sinf(angles[2]); cz = cosf(angles[2]);
|
287
|
+
|
288
|
+
cycz = cy * cz;
|
289
|
+
sxsy = sx * sy;
|
290
|
+
cysz = cy * sz;
|
291
|
+
|
292
|
+
dest[0][0] = cycz - sxsy * sz;
|
293
|
+
dest[0][1] = cz * sxsy + cysz;
|
294
|
+
dest[0][2] = -cx * sy;
|
295
|
+
dest[1][0] = -cx * sz;
|
296
|
+
dest[1][1] = cx * cz;
|
297
|
+
dest[1][2] = sx;
|
298
|
+
dest[2][0] = cz * sy + cysz * sx;
|
299
|
+
dest[2][1] = -cycz * sx + sy * sz;
|
300
|
+
dest[2][2] = cx * cy;
|
301
|
+
dest[0][3] = 0.0f;
|
302
|
+
dest[1][3] = 0.0f;
|
303
|
+
dest[2][3] = 0.0f;
|
304
|
+
dest[3][0] = 0.0f;
|
305
|
+
dest[3][1] = 0.0f;
|
306
|
+
dest[3][2] = 0.0f;
|
307
|
+
dest[3][3] = 1.0f;
|
308
|
+
}
|
309
|
+
|
310
|
+
/*!
|
311
|
+
* @brief build rotation matrix from euler angles
|
312
|
+
*
|
313
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
314
|
+
* @param[out] dest rotation matrix
|
315
|
+
*/
|
316
|
+
CGLM_INLINE
|
317
|
+
void
|
318
|
+
glm_euler_zyx(vec3 angles, mat4 dest) {
|
319
|
+
float cx, cy, cz,
|
320
|
+
sx, sy, sz, czsx, cxcz, sysz;
|
321
|
+
|
322
|
+
sx = sinf(angles[0]); cx = cosf(angles[0]);
|
323
|
+
sy = sinf(angles[1]); cy = cosf(angles[1]);
|
324
|
+
sz = sinf(angles[2]); cz = cosf(angles[2]);
|
325
|
+
|
326
|
+
czsx = cz * sx;
|
327
|
+
cxcz = cx * cz;
|
328
|
+
sysz = sy * sz;
|
329
|
+
|
330
|
+
dest[0][0] = cy * cz;
|
331
|
+
dest[0][1] = cy * sz;
|
332
|
+
dest[0][2] = -sy;
|
333
|
+
dest[1][0] = czsx * sy - cx * sz;
|
334
|
+
dest[1][1] = cxcz + sx * sysz;
|
335
|
+
dest[1][2] = cy * sx;
|
336
|
+
dest[2][0] = cxcz * sy + sx * sz;
|
337
|
+
dest[2][1] = -czsx + cx * sysz;
|
338
|
+
dest[2][2] = cx * cy;
|
339
|
+
dest[0][3] = 0.0f;
|
340
|
+
dest[1][3] = 0.0f;
|
341
|
+
dest[2][3] = 0.0f;
|
342
|
+
dest[3][0] = 0.0f;
|
343
|
+
dest[3][1] = 0.0f;
|
344
|
+
dest[3][2] = 0.0f;
|
345
|
+
dest[3][3] = 1.0f;
|
346
|
+
}
|
347
|
+
|
348
|
+
/*!
|
349
|
+
* @brief build rotation matrix from euler angles
|
350
|
+
*
|
351
|
+
* @param[in] angles angles as vector [Xangle, Yangle, Zangle]
|
352
|
+
* @param[in] ord euler order
|
353
|
+
* @param[out] dest rotation matrix
|
354
|
+
*/
|
355
|
+
CGLM_INLINE
|
356
|
+
void
|
357
|
+
glm_euler_by_order(vec3 angles, glm_euler_seq ord, mat4 dest) {
|
358
|
+
float cx, cy, cz,
|
359
|
+
sx, sy, sz;
|
360
|
+
|
361
|
+
float cycz, cysz, cysx, cxcy,
|
362
|
+
czsy, cxcz, czsx, cxsz,
|
363
|
+
sysz;
|
364
|
+
|
365
|
+
sx = sinf(angles[0]); cx = cosf(angles[0]);
|
366
|
+
sy = sinf(angles[1]); cy = cosf(angles[1]);
|
367
|
+
sz = sinf(angles[2]); cz = cosf(angles[2]);
|
368
|
+
|
369
|
+
cycz = cy * cz; cysz = cy * sz;
|
370
|
+
cysx = cy * sx; cxcy = cx * cy;
|
371
|
+
czsy = cz * sy; cxcz = cx * cz;
|
372
|
+
czsx = cz * sx; cxsz = cx * sz;
|
373
|
+
sysz = sy * sz;
|
374
|
+
|
375
|
+
switch (ord) {
|
376
|
+
case GLM_EULER_XZY:
|
377
|
+
dest[0][0] = cycz;
|
378
|
+
dest[0][1] = sx * sy + cx * cysz;
|
379
|
+
dest[0][2] = -cx * sy + cysx * sz;
|
380
|
+
dest[1][0] = -sz;
|
381
|
+
dest[1][1] = cxcz;
|
382
|
+
dest[1][2] = czsx;
|
383
|
+
dest[2][0] = czsy;
|
384
|
+
dest[2][1] = -cysx + cx * sysz;
|
385
|
+
dest[2][2] = cxcy + sx * sysz;
|
386
|
+
break;
|
387
|
+
case GLM_EULER_XYZ:
|
388
|
+
dest[0][0] = cycz;
|
389
|
+
dest[0][1] = czsx * sy + cxsz;
|
390
|
+
dest[0][2] = -cx * czsy + sx * sz;
|
391
|
+
dest[1][0] = -cysz;
|
392
|
+
dest[1][1] = cxcz - sx * sysz;
|
393
|
+
dest[1][2] = czsx + cx * sysz;
|
394
|
+
dest[2][0] = sy;
|
395
|
+
dest[2][1] = -cysx;
|
396
|
+
dest[2][2] = cxcy;
|
397
|
+
break;
|
398
|
+
case GLM_EULER_YXZ:
|
399
|
+
dest[0][0] = cycz + sx * sysz;
|
400
|
+
dest[0][1] = cxsz;
|
401
|
+
dest[0][2] = -czsy + cysx * sz;
|
402
|
+
dest[1][0] = czsx * sy - cysz;
|
403
|
+
dest[1][1] = cxcz;
|
404
|
+
dest[1][2] = cycz * sx + sysz;
|
405
|
+
dest[2][0] = cx * sy;
|
406
|
+
dest[2][1] = -sx;
|
407
|
+
dest[2][2] = cxcy;
|
408
|
+
break;
|
409
|
+
case GLM_EULER_YZX:
|
410
|
+
dest[0][0] = cycz;
|
411
|
+
dest[0][1] = sz;
|
412
|
+
dest[0][2] = -czsy;
|
413
|
+
dest[1][0] = sx * sy - cx * cysz;
|
414
|
+
dest[1][1] = cxcz;
|
415
|
+
dest[1][2] = cysx + cx * sysz;
|
416
|
+
dest[2][0] = cx * sy + cysx * sz;
|
417
|
+
dest[2][1] = -czsx;
|
418
|
+
dest[2][2] = cxcy - sx * sysz;
|
419
|
+
break;
|
420
|
+
case GLM_EULER_ZXY:
|
421
|
+
dest[0][0] = cycz - sx * sysz;
|
422
|
+
dest[0][1] = czsx * sy + cysz;
|
423
|
+
dest[0][2] = -cx * sy;
|
424
|
+
dest[1][0] = -cxsz;
|
425
|
+
dest[1][1] = cxcz;
|
426
|
+
dest[1][2] = sx;
|
427
|
+
dest[2][0] = czsy + cysx * sz;
|
428
|
+
dest[2][1] = -cycz * sx + sysz;
|
429
|
+
dest[2][2] = cxcy;
|
430
|
+
break;
|
431
|
+
case GLM_EULER_ZYX:
|
432
|
+
dest[0][0] = cycz;
|
433
|
+
dest[0][1] = cysz;
|
434
|
+
dest[0][2] = -sy;
|
435
|
+
dest[1][0] = czsx * sy - cxsz;
|
436
|
+
dest[1][1] = cxcz + sx * sysz;
|
437
|
+
dest[1][2] = cysx;
|
438
|
+
dest[2][0] = cx * czsy + sx * sz;
|
439
|
+
dest[2][1] = -czsx + cx * sysz;
|
440
|
+
dest[2][2] = cxcy;
|
441
|
+
break;
|
442
|
+
}
|
443
|
+
|
444
|
+
dest[0][3] = 0.0f;
|
445
|
+
dest[1][3] = 0.0f;
|
446
|
+
dest[2][3] = 0.0f;
|
447
|
+
dest[3][0] = 0.0f;
|
448
|
+
dest[3][1] = 0.0f;
|
449
|
+
dest[3][2] = 0.0f;
|
450
|
+
dest[3][3] = 1.0f;
|
451
|
+
}
|
452
|
+
|
453
|
+
#endif /* cglm_euler_h */
|
@@ -0,0 +1,255 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c), Recep Aslantas.
|
3
|
+
*
|
4
|
+
* MIT License (MIT), http://opensource.org/licenses/MIT
|
5
|
+
* Full license can be found in the LICENSE file
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef cglm_frustum_h
|
9
|
+
#define cglm_frustum_h
|
10
|
+
|
11
|
+
#include "common.h"
|
12
|
+
#include "plane.h"
|
13
|
+
#include "vec3.h"
|
14
|
+
#include "vec4.h"
|
15
|
+
#include "mat4.h"
|
16
|
+
|
17
|
+
#define GLM_LBN 0 /* left bottom near */
|
18
|
+
#define GLM_LTN 1 /* left top near */
|
19
|
+
#define GLM_RTN 2 /* right top near */
|
20
|
+
#define GLM_RBN 3 /* right bottom near */
|
21
|
+
|
22
|
+
#define GLM_LBF 4 /* left bottom far */
|
23
|
+
#define GLM_LTF 5 /* left top far */
|
24
|
+
#define GLM_RTF 6 /* right top far */
|
25
|
+
#define GLM_RBF 7 /* right bottom far */
|
26
|
+
|
27
|
+
#define GLM_LEFT 0
|
28
|
+
#define GLM_RIGHT 1
|
29
|
+
#define GLM_BOTTOM 2
|
30
|
+
#define GLM_TOP 3
|
31
|
+
#define GLM_NEAR 4
|
32
|
+
#define GLM_FAR 5
|
33
|
+
|
34
|
+
/* you can override clip space coords
|
35
|
+
but you have to provide all with same name
|
36
|
+
e.g.: define GLM_CSCOORD_LBN {0.0f, 0.0f, 1.0f, 1.0f} */
|
37
|
+
#ifndef GLM_CUSTOM_CLIPSPACE
|
38
|
+
|
39
|
+
/* near */
|
40
|
+
#define GLM_CSCOORD_LBN {-1.0f, -1.0f, -1.0f, 1.0f}
|
41
|
+
#define GLM_CSCOORD_LTN {-1.0f, 1.0f, -1.0f, 1.0f}
|
42
|
+
#define GLM_CSCOORD_RTN { 1.0f, 1.0f, -1.0f, 1.0f}
|
43
|
+
#define GLM_CSCOORD_RBN { 1.0f, -1.0f, -1.0f, 1.0f}
|
44
|
+
|
45
|
+
/* far */
|
46
|
+
#define GLM_CSCOORD_LBF {-1.0f, -1.0f, 1.0f, 1.0f}
|
47
|
+
#define GLM_CSCOORD_LTF {-1.0f, 1.0f, 1.0f, 1.0f}
|
48
|
+
#define GLM_CSCOORD_RTF { 1.0f, 1.0f, 1.0f, 1.0f}
|
49
|
+
#define GLM_CSCOORD_RBF { 1.0f, -1.0f, 1.0f, 1.0f}
|
50
|
+
|
51
|
+
#endif
|
52
|
+
|
53
|
+
/*!
|
54
|
+
* @brief extracts view frustum planes
|
55
|
+
*
|
56
|
+
* planes' space:
|
57
|
+
* 1- if m = proj: View Space
|
58
|
+
* 2- if m = viewProj: World Space
|
59
|
+
* 3- if m = MVP: Object Space
|
60
|
+
*
|
61
|
+
* You probably want to extract planes in world space so use viewProj as m
|
62
|
+
* Computing viewProj:
|
63
|
+
* glm_mat4_mul(proj, view, viewProj);
|
64
|
+
*
|
65
|
+
* Exracted planes order: [left, right, bottom, top, near, far]
|
66
|
+
*
|
67
|
+
* @param[in] m matrix (see brief)
|
68
|
+
* @param[out] dest extracted view frustum planes (see brief)
|
69
|
+
*/
|
70
|
+
CGLM_INLINE
|
71
|
+
void
|
72
|
+
glm_frustum_planes(mat4 m, vec4 dest[6]) {
|
73
|
+
mat4 t;
|
74
|
+
|
75
|
+
glm_mat4_transpose_to(m, t);
|
76
|
+
|
77
|
+
glm_vec4_add(t[3], t[0], dest[0]); /* left */
|
78
|
+
glm_vec4_sub(t[3], t[0], dest[1]); /* right */
|
79
|
+
glm_vec4_add(t[3], t[1], dest[2]); /* bottom */
|
80
|
+
glm_vec4_sub(t[3], t[1], dest[3]); /* top */
|
81
|
+
glm_vec4_add(t[3], t[2], dest[4]); /* near */
|
82
|
+
glm_vec4_sub(t[3], t[2], dest[5]); /* far */
|
83
|
+
|
84
|
+
glm_plane_normalize(dest[0]);
|
85
|
+
glm_plane_normalize(dest[1]);
|
86
|
+
glm_plane_normalize(dest[2]);
|
87
|
+
glm_plane_normalize(dest[3]);
|
88
|
+
glm_plane_normalize(dest[4]);
|
89
|
+
glm_plane_normalize(dest[5]);
|
90
|
+
}
|
91
|
+
|
92
|
+
/*!
|
93
|
+
* @brief extracts view frustum corners using clip-space coordinates
|
94
|
+
*
|
95
|
+
* corners' space:
|
96
|
+
* 1- if m = invViewProj: World Space
|
97
|
+
* 2- if m = invMVP: Object Space
|
98
|
+
*
|
99
|
+
* You probably want to extract corners in world space so use invViewProj
|
100
|
+
* Computing invViewProj:
|
101
|
+
* glm_mat4_mul(proj, view, viewProj);
|
102
|
+
* ...
|
103
|
+
* glm_mat4_inv(viewProj, invViewProj);
|
104
|
+
*
|
105
|
+
* if you have a near coord at i index, you can get it's far coord by i + 4
|
106
|
+
*
|
107
|
+
* Find center coordinates:
|
108
|
+
* for (j = 0; j < 4; j++) {
|
109
|
+
* glm_vec3_center(corners[i], corners[i + 4], centerCorners[i]);
|
110
|
+
* }
|
111
|
+
*
|
112
|
+
* @param[in] invMat matrix (see brief)
|
113
|
+
* @param[out] dest exracted view frustum corners (see brief)
|
114
|
+
*/
|
115
|
+
CGLM_INLINE
|
116
|
+
void
|
117
|
+
glm_frustum_corners(mat4 invMat, vec4 dest[8]) {
|
118
|
+
vec4 c[8];
|
119
|
+
|
120
|
+
/* indexOf(nearCoord) = indexOf(farCoord) + 4 */
|
121
|
+
vec4 csCoords[8] = {
|
122
|
+
GLM_CSCOORD_LBN,
|
123
|
+
GLM_CSCOORD_LTN,
|
124
|
+
GLM_CSCOORD_RTN,
|
125
|
+
GLM_CSCOORD_RBN,
|
126
|
+
|
127
|
+
GLM_CSCOORD_LBF,
|
128
|
+
GLM_CSCOORD_LTF,
|
129
|
+
GLM_CSCOORD_RTF,
|
130
|
+
GLM_CSCOORD_RBF
|
131
|
+
};
|
132
|
+
|
133
|
+
glm_mat4_mulv(invMat, csCoords[0], c[0]);
|
134
|
+
glm_mat4_mulv(invMat, csCoords[1], c[1]);
|
135
|
+
glm_mat4_mulv(invMat, csCoords[2], c[2]);
|
136
|
+
glm_mat4_mulv(invMat, csCoords[3], c[3]);
|
137
|
+
glm_mat4_mulv(invMat, csCoords[4], c[4]);
|
138
|
+
glm_mat4_mulv(invMat, csCoords[5], c[5]);
|
139
|
+
glm_mat4_mulv(invMat, csCoords[6], c[6]);
|
140
|
+
glm_mat4_mulv(invMat, csCoords[7], c[7]);
|
141
|
+
|
142
|
+
glm_vec4_scale(c[0], 1.0f / c[0][3], dest[0]);
|
143
|
+
glm_vec4_scale(c[1], 1.0f / c[1][3], dest[1]);
|
144
|
+
glm_vec4_scale(c[2], 1.0f / c[2][3], dest[2]);
|
145
|
+
glm_vec4_scale(c[3], 1.0f / c[3][3], dest[3]);
|
146
|
+
glm_vec4_scale(c[4], 1.0f / c[4][3], dest[4]);
|
147
|
+
glm_vec4_scale(c[5], 1.0f / c[5][3], dest[5]);
|
148
|
+
glm_vec4_scale(c[6], 1.0f / c[6][3], dest[6]);
|
149
|
+
glm_vec4_scale(c[7], 1.0f / c[7][3], dest[7]);
|
150
|
+
}
|
151
|
+
|
152
|
+
/*!
|
153
|
+
* @brief finds center of view frustum
|
154
|
+
*
|
155
|
+
* @param[in] corners view frustum corners
|
156
|
+
* @param[out] dest view frustum center
|
157
|
+
*/
|
158
|
+
CGLM_INLINE
|
159
|
+
void
|
160
|
+
glm_frustum_center(vec4 corners[8], vec4 dest) {
|
161
|
+
vec4 center;
|
162
|
+
|
163
|
+
glm_vec4_copy(corners[0], center);
|
164
|
+
|
165
|
+
glm_vec4_add(corners[1], center, center);
|
166
|
+
glm_vec4_add(corners[2], center, center);
|
167
|
+
glm_vec4_add(corners[3], center, center);
|
168
|
+
glm_vec4_add(corners[4], center, center);
|
169
|
+
glm_vec4_add(corners[5], center, center);
|
170
|
+
glm_vec4_add(corners[6], center, center);
|
171
|
+
glm_vec4_add(corners[7], center, center);
|
172
|
+
|
173
|
+
glm_vec4_scale(center, 0.125f, dest);
|
174
|
+
}
|
175
|
+
|
176
|
+
/*!
|
177
|
+
* @brief finds bounding box of frustum relative to given matrix e.g. view mat
|
178
|
+
*
|
179
|
+
* @param[in] corners view frustum corners
|
180
|
+
* @param[in] m matrix to convert existing conners
|
181
|
+
* @param[out] box bounding box as array [min, max]
|
182
|
+
*/
|
183
|
+
CGLM_INLINE
|
184
|
+
void
|
185
|
+
glm_frustum_box(vec4 corners[8], mat4 m, vec3 box[2]) {
|
186
|
+
vec4 v;
|
187
|
+
vec3 min, max;
|
188
|
+
int i;
|
189
|
+
|
190
|
+
glm_vec3_broadcast(FLT_MAX, min);
|
191
|
+
glm_vec3_broadcast(-FLT_MAX, max);
|
192
|
+
|
193
|
+
for (i = 0; i < 8; i++) {
|
194
|
+
glm_mat4_mulv(m, corners[i], v);
|
195
|
+
|
196
|
+
min[0] = glm_min(min[0], v[0]);
|
197
|
+
min[1] = glm_min(min[1], v[1]);
|
198
|
+
min[2] = glm_min(min[2], v[2]);
|
199
|
+
|
200
|
+
max[0] = glm_max(max[0], v[0]);
|
201
|
+
max[1] = glm_max(max[1], v[1]);
|
202
|
+
max[2] = glm_max(max[2], v[2]);
|
203
|
+
}
|
204
|
+
|
205
|
+
glm_vec3_copy(min, box[0]);
|
206
|
+
glm_vec3_copy(max, box[1]);
|
207
|
+
}
|
208
|
+
|
209
|
+
/*!
|
210
|
+
* @brief finds planes corners which is between near and far planes (parallel)
|
211
|
+
*
|
212
|
+
* this will be helpful if you want to split a frustum e.g. CSM/PSSM. This will
|
213
|
+
* find planes' corners but you will need to one more plane.
|
214
|
+
* Actually you have it, it is near, far or created previously with this func ;)
|
215
|
+
*
|
216
|
+
* @param[in] corners view frustum corners
|
217
|
+
* @param[in] splitDist split distance
|
218
|
+
* @param[in] farDist far distance (zFar)
|
219
|
+
* @param[out] planeCorners plane corners [LB, LT, RT, RB]
|
220
|
+
*/
|
221
|
+
CGLM_INLINE
|
222
|
+
void
|
223
|
+
glm_frustum_corners_at(vec4 corners[8],
|
224
|
+
float splitDist,
|
225
|
+
float farDist,
|
226
|
+
vec4 planeCorners[4]) {
|
227
|
+
vec4 corner;
|
228
|
+
float dist, sc;
|
229
|
+
|
230
|
+
/* because distance and scale is same for all */
|
231
|
+
dist = glm_vec3_distance(corners[GLM_RTF], corners[GLM_RTN]);
|
232
|
+
sc = dist * (splitDist / farDist);
|
233
|
+
|
234
|
+
/* left bottom */
|
235
|
+
glm_vec4_sub(corners[GLM_LBF], corners[GLM_LBN], corner);
|
236
|
+
glm_vec4_scale_as(corner, sc, corner);
|
237
|
+
glm_vec4_add(corners[GLM_LBN], corner, planeCorners[0]);
|
238
|
+
|
239
|
+
/* left top */
|
240
|
+
glm_vec4_sub(corners[GLM_LTF], corners[GLM_LTN], corner);
|
241
|
+
glm_vec4_scale_as(corner, sc, corner);
|
242
|
+
glm_vec4_add(corners[GLM_LTN], corner, planeCorners[1]);
|
243
|
+
|
244
|
+
/* right top */
|
245
|
+
glm_vec4_sub(corners[GLM_RTF], corners[GLM_RTN], corner);
|
246
|
+
glm_vec4_scale_as(corner, sc, corner);
|
247
|
+
glm_vec4_add(corners[GLM_RTN], corner, planeCorners[2]);
|
248
|
+
|
249
|
+
/* right bottom */
|
250
|
+
glm_vec4_sub(corners[GLM_RBF], corners[GLM_RBN], corner);
|
251
|
+
glm_vec4_scale_as(corner, sc, corner);
|
252
|
+
glm_vec4_add(corners[GLM_RBN], corner, planeCorners[3]);
|
253
|
+
}
|
254
|
+
|
255
|
+
#endif /* cglm_frustum_h */
|