-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdump_glsl.comp
More file actions
399 lines (339 loc) · 8.17 KB
/
dump_glsl.comp
File metadata and controls
399 lines (339 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#version 410
out vec4 fragColor;
uniform mat4 uIdentityM;
uniform mat4 uMatA;
uniform mat4 uMatB;
uniform mat4 uScaleM;
uniform mat4 uTranslateM;
uniform mat4 uRotateXM;
uniform mat4 uRotateYM;
uniform mat4 uRotateZM;
uniform mat4 uAxisMat;
uniform mat4 uHardMat;
uniform vec3 uVecA;
uniform vec4 uVecB;
uniform mat2 uMat2A;
uniform mat2 uMat2B;
uniform mat3 uMat3A;
uniform mat3 uMat3B;
uniform vec2 uVec2A;
uniform vec2 uVec2B;
uniform vec3 uVec3C;
const float angleA = radians(37.0);
const float angleB = radians(-23.0);
const float angleC = radians(71.0);
const float axisAngle = radians(48.0);
const float rotAngle2D = radians(45.0);
mat4 identityM() {
return uIdentityM;
}
mat4 matA() {
return uMatA;
}
mat4 matB() {
return uMatB;
}
vec3 vecA() {
return uVecA;
}
vec4 vecB() {
return uVecB;
}
mat2 mat2A() {
return uMat2A;
}
mat2 mat2B() {
return uMat2B;
}
mat3 mat3A() {
return uMat3A;
}
mat3 mat3B() {
return uMat3B;
}
vec2 vec2A() {
return uVec2A;
}
vec2 vec2B() {
return uVec2B;
}
vec3 vec3C() {
return uVec3C;
}
mat4 scaleM() {
return uScaleM;
}
mat4 translateM() {
return uTranslateM;
}
mat4 rotateXM() {
return uRotateXM;
}
mat4 rotateYM() {
return uRotateYM;
}
mat4 rotateZM() {
return uRotateZM;
}
mat4 pureRotationM() {
return rotateZM() * rotateYM() * rotateXM();
}
vec3 axisNormalized() {
return normalize(vec3(1.0, 2.0, -3.0));
}
vec4 quatIdentity() {
return vec4(0.0, 0.0, 0.0, 1.0);
}
vec4 quatAxisAngle(vec3 axis, float angle) {
vec3 a = normalize(axis);
float s = sin(angle * 0.5);
return vec4(a * s, cos(angle * 0.5));
}
vec4 quatX() {
return quatAxisAngle(vec3(1.0, 0.0, 0.0), angleA);
}
vec4 quatY() {
return quatAxisAngle(vec3(0.0, 1.0, 0.0), angleB);
}
vec4 quatZ() {
return quatAxisAngle(vec3(0.0, 0.0, 1.0), angleC);
}
vec4 axisQuat() {
return quatAxisAngle(axisNormalized(), axisAngle);
}
vec4 quatMultiply(vec4 a, vec4 b) {
return vec4(
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z,
a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x,
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
);
}
vec3 quatRotate(vec4 q, vec3 v) {
vec3 uv = cross(q.xyz, v);
vec3 uuv = cross(q.xyz, uv);
return v + ((uv * q.w) + uuv) * 2.0;
}
mat4 quatMat4(vec4 q) {
float xx = q.x * q.x;
float xy = q.x * q.y;
float xz = q.x * q.z;
float xw = q.x * q.w;
float yy = q.y * q.y;
float yz = q.y * q.z;
float yw = q.y * q.w;
float zz = q.z * q.z;
float zw = q.z * q.w;
return mat4(
vec4(1.0 - 2.0 * (yy + zz), 2.0 * (xy + zw), 2.0 * (xz - yw), 0.0),
vec4(2.0 * (xy - zw), 1.0 - 2.0 * (xx + zz), 2.0 * (yz + xw), 0.0),
vec4(2.0 * (xz + yw), 2.0 * (yz - xw), 1.0 - 2.0 * (xx + yy), 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
}
vec4 mat4Quat(mat4 m) {
float m00 = m[0][0];
float m11 = m[1][1];
float m22 = m[2][2];
float fourXSquaredMinus1 = m00 - m11 - m22;
float fourYSquaredMinus1 = m11 - m00 - m22;
float fourZSquaredMinus1 = m22 - m00 - m11;
float fourWSquaredMinus1 = m00 + m11 + m22;
int biggestIndex = 0;
float fourBiggestSquaredMinus1 = fourWSquaredMinus1;
if (fourXSquaredMinus1 > fourBiggestSquaredMinus1) {
fourBiggestSquaredMinus1 = fourXSquaredMinus1;
biggestIndex = 1;
}
if (fourYSquaredMinus1 > fourBiggestSquaredMinus1) {
fourBiggestSquaredMinus1 = fourYSquaredMinus1;
biggestIndex = 2;
}
if (fourZSquaredMinus1 > fourBiggestSquaredMinus1) {
fourBiggestSquaredMinus1 = fourZSquaredMinus1;
biggestIndex = 3;
}
float biggestVal = sqrt(fourBiggestSquaredMinus1 + 1.0) * 0.5;
float mult = 0.25 / biggestVal;
vec4 q = vec4(0.0);
if (biggestIndex == 0) {
q.w = biggestVal;
q.x = (m[1][2] - m[2][1]) * mult;
q.y = (m[2][0] - m[0][2]) * mult;
q.z = (m[0][1] - m[1][0]) * mult;
} else if (biggestIndex == 1) {
q.w = (m[1][2] - m[2][1]) * mult;
q.x = biggestVal;
q.y = (m[0][1] + m[1][0]) * mult;
q.z = (m[2][0] + m[0][2]) * mult;
} else if (biggestIndex == 2) {
q.w = (m[2][0] - m[0][2]) * mult;
q.x = (m[0][1] + m[1][0]) * mult;
q.y = biggestVal;
q.z = (m[1][2] + m[2][1]) * mult;
} else {
q.w = (m[0][1] - m[1][0]) * mult;
q.x = (m[2][0] + m[0][2]) * mult;
q.y = (m[1][2] + m[2][1]) * mult;
q.z = biggestVal;
}
return q;
}
mat4 axisMat() {
return uAxisMat;
}
mat4 transformM() {
return translateM() * rotateZM() * rotateYM() * rotateXM() * scaleM();
}
mat4 rotationOnly(mat4 m) {
m[3] = vec4(0.0, 0.0, 0.0, 1.0);
return m;
}
vec3 hardAxis() {
return normalize(vec3(1.0, -2.0, 3.0));
}
vec4 hardQuat() {
return quatAxisAngle(hardAxis(), radians(170.0));
}
mat4 hardMat() {
return uHardMat;
}
mat4 frustumM(float left, float right, float bottom, float top, float nearV, float farV) {
float rl = right - left;
float tb = top - bottom;
float fn = farV - nearV;
return mat4(
vec4((nearV * 2.0) / rl, 0.0, 0.0, 0.0),
vec4(0.0, (nearV * 2.0) / tb, 0.0, 0.0),
vec4((right + left) / rl, (top + bottom) / tb, -(farV + nearV) / fn, -1.0),
vec4(0.0, 0.0, -(farV * nearV * 2.0) / fn, 0.0)
);
}
mat4 perspectiveM(float fovyDeg, float aspect, float nearV, float farV) {
float top = nearV * tan(fovyDeg * 3.14159265359 / 360.0);
float right = top * aspect;
return frustumM(-right, right, -top, top, nearV, farV);
}
mat4 orthoM(float left, float right, float bottom, float top, float nearV, float farV) {
float rl = right - left;
float tb = top - bottom;
float fn = farV - nearV;
return mat4(
vec4(2.0 / rl, 0.0, 0.0, 0.0),
vec4(0.0, 2.0 / tb, 0.0, 0.0),
vec4(0.0, 0.0, -2.0 / fn, 0.0),
vec4(-(left + right) / rl, -(top + bottom) / tb, -(farV + nearV) / fn, 1.0)
);
}
mat4 lookAtM(vec3 eye, vec3 center, vec3 up) {
if (all(equal(eye, center))) {
return identityM();
}
vec3 z = normalize(eye - center);
vec3 x = cross(up, z);
if (length(x) == 0.0) {
x = vec3(0.0);
} else {
x = normalize(x);
}
vec3 y = cross(z, x);
return mat4(
vec4(x.x, y.x, z.x, 0.0),
vec4(x.y, y.y, z.y, 0.0),
vec4(x.z, y.z, z.z, 0.0),
vec4(-dot(x, eye), -dot(y, eye), -dot(z, eye), 1.0)
);
}
vec4 quatInverse(vec4 q) {
float d = dot(q, q);
return vec4(-q.x / d, -q.y / d, -q.z / d, q.w / d);
}
vec3 orthogonalV(vec3 v) {
vec3 av = abs(v);
vec3 other;
if (av.x < av.y) {
other = av.x < av.z ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 0.0, 1.0);
} else {
other = av.y < av.z ? vec3(0.0, 1.0, 0.0) : vec3(0.0, 0.0, 1.0);
}
return cross(av, other);
}
vec4 fromTwoVectorsQ(vec3 a, vec3 b) {
vec3 u = normalize(b);
vec3 v = normalize(a);
if (all(equal(u, -v))) {
vec3 q = normalize(orthogonalV(u));
return vec4(q.x, q.y, q.z, 0.0);
}
vec3 halfV = normalize(u + v);
vec3 q = cross(v, halfV);
float w = dot(v, halfV);
return vec4(q.x, q.y, q.z, w);
}
vec4 slerpQ(vec4 a, vec4 b, float t) {
vec4 z = b;
float cosTheta = dot(a, b);
if (cosTheta < 0.0) {
z = -b;
cosTheta = -cosTheta;
}
if (cosTheta > 1.0 - 1e-6) {
return vec4(
a.x + (z.x - a.x) * t,
a.y + (z.y - a.y) * t,
a.z + (z.z - a.z) * t,
a.w + (z.w - a.w) * t
);
} else {
float angle = acos(cosTheta);
return (sin((1.0 - t) * angle) * a + sin(t * angle) * z) / sin(angle);
}
}
vec3 quatAxisOnly(vec4 q) {
float cosAngle = q.w;
float sinAngle = sqrt(max(0.0, 1.0 - cosAngle * cosAngle));
if (abs(sinAngle) < 0.0005) {
sinAngle = 1.0;
}
return vec3(q.x / sinAngle, q.y / sinAngle, q.z / sinAngle);
}
float quatAngleOnly(vec4 q) {
return acos(q.w) * 2.0;
}
vec3 quatToAngles(vec4 q) {
float x = q.x;
float y = q.y;
float z = q.z;
float w = q.w;
return vec3(
atan(2.0 * (w * x + y * z), 1.0 - 2.0 * (x * x + y * y)),
asin(2.0 * (w * y - z * x)),
atan(2.0 * (w * z + x * y), 1.0 - 2.0 * (y * y + z * z))
);
}
mat3 scale2D(vec2 s) {
return mat3(
vec3(s.x, 0.0, 0.0),
vec3(0.0, s.y, 0.0),
vec3(0.0, 0.0, 1.0)
);
}
mat3 translate2D(vec2 t) {
return mat3(
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(t.x, t.y, 1.0)
);
}
mat3 rotate2D(float angle) {
float c = cos(angle);
float s = sin(angle);
return mat3(
vec3(c, s, 0.0),
vec3(-s, c, 0.0),
vec3(0.0, 0.0, 1.0)
);
}
void main() {
__MAIN_BODY__
}