-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdump_glmatrix.js
More file actions
436 lines (383 loc) · 15.4 KB
/
Copy pathdump_glmatrix.js
File metadata and controls
436 lines (383 loc) · 15.4 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import * as mat2 from "../../gl-matrix/src/mat2.js";
import * as mat3 from "../../gl-matrix/src/mat3.js";
import * as mat4 from "../../gl-matrix/src/mat4.js";
import * as vec2 from "../../gl-matrix/src/vec2.js";
import * as vec3 from "../../gl-matrix/src/vec3.js";
import * as vec4 from "../../gl-matrix/src/vec4.js";
import * as quat from "../../gl-matrix/src/quat.js";
import { writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const OutputPath = join(__dirname, "dump_glmatrix.txt");
function cleanFloat(value) {
if (Math.abs(value) < 0.0000005) return 0;
return value;
}
function fmt(value) {
const cleaned = cleanFloat(value);
const sign = cleaned < 0 ? "-" : "+";
let abs = Math.abs(cleaned);
let whole = Math.floor(abs);
let frac = Math.round((abs - whole) * 1000);
if (frac === 1000) {
whole++;
frac = 0;
}
return sign + String(whole).padStart(3, "0") + "." + String(frac).padStart(3, "0");
}
const lines = [];
function appendLine(line = "") {
lines.push(line);
}
function heading(title) {
if (lines.length > 0) appendLine();
appendLine("== " + title + " ==");
}
function dumpScalar(label, value) {
appendLine(label + ": " + fmt(value));
}
function dumpVec2(label, v) {
appendLine(label + ": <" + fmt(v[0]) + ", " + fmt(v[1]) + ">");
}
function dumpVec3(label, v) {
appendLine(label + ": <" + fmt(v[0]) + ", " + fmt(v[1]) + ", " + fmt(v[2]) + ">");
}
function dumpVec4(label, v) {
appendLine(label + ": <" + fmt(v[0]) + ", " + fmt(v[1]) + ", " + fmt(v[2]) + ", " + fmt(v[3]) + ">");
}
function dumpQuat(label, q) {
appendLine(label + ": <" + fmt(q[0]) + ", " + fmt(q[1]) + ", " + fmt(q[2]) + ", " + fmt(q[3]) + ">");
}
function dumpMat2(label, m) {
appendLine(label + ":");
appendLine("[");
appendLine(" " + fmt(m[0]) + " " + fmt(m[2]));
appendLine(" " + fmt(m[1]) + " " + fmt(m[3]));
appendLine("]");
}
function dumpMat3(label, m) {
appendLine(label + ":");
appendLine("[");
appendLine(" " + fmt(m[0]) + " " + fmt(m[3]) + " " + fmt(m[6]));
appendLine(" " + fmt(m[1]) + " " + fmt(m[4]) + " " + fmt(m[7]));
appendLine(" " + fmt(m[2]) + " " + fmt(m[5]) + " " + fmt(m[8]));
appendLine("]");
}
function dumpMat4(label, m) {
appendLine(label + ":");
appendLine("[");
appendLine(" " + fmt(m[0]) + " " + fmt(m[4]) + " " + fmt(m[8]) + " " + fmt(m[12]));
appendLine(" " + fmt(m[1]) + " " + fmt(m[5]) + " " + fmt(m[9]) + " " + fmt(m[13]));
appendLine(" " + fmt(m[2]) + " " + fmt(m[6]) + " " + fmt(m[10]) + " " + fmt(m[14]));
appendLine(" " + fmt(m[3]) + " " + fmt(m[7]) + " " + fmt(m[11]) + " " + fmt(m[15]));
appendLine("]");
}
function dumpMat4Default(label, m) {
appendLine(label + ": " + mat4.str(m));
}
function scaleMat(sx, sy, sz) {
return mat4.scale(mat4.create(), mat4.create(), vec3.fromValues(sx, sy, sz));
}
function translateMat(tx, ty, tz) {
return mat4.translate(mat4.create(), mat4.create(), vec3.fromValues(tx, ty, tz));
}
function rotateXMat(angle) {
return mat4.rotateX(mat4.create(), mat4.create(), angle);
}
function rotateYMat(angle) {
return mat4.rotateY(mat4.create(), mat4.create(), angle);
}
function rotateZMat(angle) {
return mat4.rotateZ(mat4.create(), mat4.create(), angle);
}
function mul(a, b) {
return mat4.multiply(mat4.create(), a, b);
}
function transformVec3(m, v) {
return vec3.transformMat4(vec3.create(), v, m);
}
function transformVec4(m, v) {
return vec4.transformMat4(vec4.create(), v, m);
}
function quatFromAxisAngle(axis, angle) {
return quat.setAxisAngle(quat.create(), axis, angle);
}
function quatMul(a, b) {
return quat.multiply(quat.create(), a, b);
}
function quatRotateVec3(q, v) {
return vec3.transformQuat(vec3.create(), v, q);
}
function mat4FromQuat(q) {
return mat4.fromQuat(mat4.create(), q);
}
function rotationOnlyCopy(m) {
const out = mat4.clone(m);
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
function quatFromMat(m) {
return quat.fromMat3(quat.create(), mat3.fromMat4(mat3.create(), m));
}
function quatToEulerXYZ(q) {
const x = q[0];
const y = q[1];
const z = q[2];
const w = q[3];
const ex = Math.atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y));
const ey = Math.asin(Math.max(-1, Math.min(1, 2 * (w * y - z * x))));
const ez = Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z));
return vec3.fromValues(ex, ey, ez);
}
function basisRight(m) {
return vec3.fromValues(m[0], m[1], m[2]);
}
function basisUp(m) {
return vec3.fromValues(m[4], m[5], m[6]);
}
function basisForward(m) {
return vec3.fromValues(m[8], m[9], m[10]);
}
function quatToAxisAngle(q) {
const normalized = quat.normalize(quat.create(), q);
const w = Math.max(-1, Math.min(1, normalized[3]));
const angle = 2 * Math.acos(w);
const s = Math.sqrt(Math.max(0, 1 - w * w));
if (s < 0.00001) {
return {
axis: vec3.fromValues(1, 0, 0),
angle: 0,
};
}
return {
axis: vec3.fromValues(normalized[0] / s, normalized[1] / s, normalized[2] / s),
angle,
};
}
const DEG2RAD = Math.PI / 180;
const angleA = 37 * DEG2RAD;
const angleB = -23 * DEG2RAD;
const angleC = 71 * DEG2RAD;
const matA = new Float32Array([
1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0
]);
const matB = new Float32Array([
-10.0, -20.0, -30.0, -40.0,
50.0, 60.0, 70.0, 80.0,
90.0, 100.0, 110.0, 120.0,
130.0, 140.0, 150.0, 160.0
]);
const vecA = vec3.fromValues(1.25, -2.5, 3.75);
const vecB = vec4.fromValues(1.25, -2.5, 3.75, 1.0);
const mat2A = new Float32Array([1.0, 2.0, 3.0, 4.0]);
const mat2B = new Float32Array([5.0, -6.0, 7.0, -8.0]);
const vec2A = vec2.fromValues(1.25, -2.5);
const mat3A = new Float32Array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 10.0]);
const mat3B = new Float32Array([-1.0, 3.0, 5.0, 7.0, -2.0, 4.0, 6.0, 8.0, -3.0]);
const vec2B = vec2.fromValues(3.0, -1.5);
const vec3C = vec3.fromValues(1.0, -2.0, 3.0);
const rotAngle2D = 45 * DEG2RAD;
const scaleM = scaleMat(2.0, 3.0, 4.0);
const translateM = translateMat(10.0, 20.0, 30.0);
const rotateXM = rotateXMat(angleA);
const rotateYM = rotateYMat(angleB);
const rotateZM = rotateZMat(angleC);
const pureRotationM = mul(rotateZM, mul(rotateYM, rotateXM));
const axis = vec3.normalize(vec3.create(), vec3.fromValues(1.0, 2.0, -3.0));
const axisAngle = 48 * DEG2RAD;
const axisQuat = quatFromAxisAngle(axis, axisAngle);
const axisMat = mat4FromQuat(axisQuat);
const transformM = mul(translateM, mul(rotateZM, mul(rotateYM, mul(rotateXM, scaleM))));
const quatX = quatFromAxisAngle(vec3.fromValues(1, 0, 0), angleA);
const quatY = quatFromAxisAngle(vec3.fromValues(0, 1, 0), angleB);
const quatZ = quatFromAxisAngle(vec3.fromValues(0, 0, 1), angleC);
const quatXY = quatMul(quatX, quatY);
const quatXYZ = quatMul(quatXY, quatZ);
const hardAxis = vec3.normalize(vec3.create(), vec3.fromValues(1.0, -2.0, 3.0));
const hardAngle = 170 * DEG2RAD;
const hardQuat = quatFromAxisAngle(hardAxis, hardAngle);
const hardMat = mat4FromQuat(hardQuat);
const rotationOnlyM = rotationOnlyCopy(transformM);
const pureRotationQuat = quatFromMat(pureRotationM);
heading("dump");
appendLine("notes: matrices are printed in common column-major order");
heading("matrix basics");
dumpMat4("identity", mat4.create());
dumpMat4("matrix_a", matA);
dumpMat4("matrix_b", matB);
heading("matrix multiply");
dumpMat4("matrix_a * matrix_b", mul(matA, matB));
dumpMat4("matrix_b * matrix_a", mul(matB, matA));
heading("element access [row, col]");
appendLine("N/A");
heading("mat2 basics");
dumpMat2("identity", mat2.create());
dumpMat2("mat2_a", mat2A);
dumpMat2("mat2_b", mat2B);
heading("mat2 multiply");
dumpMat2("mat2_a * mat2_b", mat2.multiply(mat2.create(), mat2A, mat2B));
dumpMat2("mat2_b * mat2_a", mat2.multiply(mat2.create(), mat2B, mat2A));
heading("mat2 element access [row, col]");
appendLine("N/A");
heading("mat2 vector multiply");
dumpVec2("vec2_input", vec2A);
dumpVec2("mat2_a * vec2", vec2.transformMat2(vec2.create(), vec2A, mat2A));
heading("mat2 transpose");
dumpMat2("mat2_a.transpose", mat2.transpose(mat2.create(), mat2A));
heading("mat2 inverse");
dumpMat2("mat2_a.inverse", mat2.invert(mat2.create(), mat2A));
heading("mat3 basics");
dumpMat3("identity", mat3.create());
dumpMat3("mat3_a", mat3A);
dumpMat3("mat3_b", mat3B);
heading("mat3 multiply");
dumpMat3("mat3_a * mat3_b", mat3.multiply(mat3.create(), mat3A, mat3B));
dumpMat3("mat3_b * mat3_a", mat3.multiply(mat3.create(), mat3B, mat3A));
heading("mat3 element access [row, col]");
appendLine("N/A");
heading("mat3 vector multiply");
dumpVec2("vec2_input", vec2B);
dumpVec3("vec3_input", vec3C);
dumpVec2("mat3_a * vec2", vec2.transformMat3(vec2.create(), vec2B, mat3A));
dumpVec3("mat3_a * vec3", vec3.transformMat3(vec3.create(), vec3C, mat3A));
heading("mat3 transpose");
dumpMat3("mat3_a.transpose", mat3.transpose(mat3.create(), mat3A));
heading("mat3 inverse");
dumpMat3("mat3_a.inverse", mat3.invert(mat3.create(), mat3A));
heading("mat3 constructors");
{
const scale2D = mat3.fromScaling(mat3.create(), vec2.fromValues(2.0, 3.0));
dumpMat3("scale2d", scale2D);
const translate2D = mat3.fromTranslation(mat3.create(), vec2.fromValues(5.0, 10.0));
dumpMat3("translate2d", translate2D);
dumpScalar("rotate_angle_radians", rotAngle2D);
const rotate2D = mat3.fromRotation(mat3.create(), rotAngle2D);
dumpMat3("rotate2d", rotate2D);
dumpMat3("translate * rotate * scale", mat3.multiply(mat3.create(), translate2D, mat3.multiply(mat3.create(), rotate2D, scale2D)));
}
heading("matrix constructors and composition");
dumpMat4("scale", scaleM);
dumpMat4("translate", translateM);
dumpMat4("rotate_x", rotateXM);
dumpMat4("rotate_y", rotateYM);
dumpMat4("rotate_z", rotateZM);
dumpMat4("pure_rotation = rotate_z * rotate_y * rotate_x", pureRotationM);
dumpMat4("transform = translate * rotate_z * rotate_y * rotate_x * scale", transformM);
heading("matrix vector multiply");
dumpVec3("vec3_input", vecA);
dumpVec4("vec4_input", vecB);
dumpVec3("transform * vec3", transformVec3(transformM, vecA));
dumpVec4("transform * vec4", transformVec4(transformM, vecB));
dumpVec3("rotate_z * vec3", transformVec3(rotateZM, vecA));
dumpVec3("translate * vec3", transformVec3(translateM, vecA));
heading("quaternion constructors");
dumpQuat("quat_identity", quat.create());
dumpQuat("quat_rotate_x", quatX);
dumpQuat("quat_rotate_y", quatY);
dumpQuat("quat_rotate_z", quatZ);
dumpVec3("axis_normalized", axis);
dumpScalar("axis_angle_radians", axisAngle);
dumpQuat("from_axis_angle", axisQuat);
dumpMat4("from_axis_angle.mat4", axisMat);
heading("quaternion multiply");
dumpQuat("quat_x", quatX);
dumpQuat("quat_y", quatY);
dumpQuat("quat_z", quatZ);
dumpQuat("quat_multiply(quat_x, quat_y)", quatXY);
dumpQuat("quat_multiply(quat_multiply(quat_x, quat_y), quat_z)", quatXYZ);
dumpMat4("quat_xy.mat4", mat4FromQuat(quatXY));
dumpMat4("quat_xyz.mat4", mat4FromQuat(quatXYZ));
heading("quaternion vector rotate");
dumpVec3("input", vecA);
dumpVec3("quat_rotate(quat_x, input)", quatRotateVec3(quatX, vecA));
dumpVec3("quat_rotate(quat_y, input)", quatRotateVec3(quatY, vecA));
dumpVec3("quat_rotate(quat_z, input)", quatRotateVec3(quatZ, vecA));
dumpVec3("quat_rotate(from_axis_angle, input)", quatRotateVec3(axisQuat, vecA));
dumpVec3("quat_z * input", quatRotateVec3(quatZ, vecA));
heading("matrix quaternion roundtrip");
dumpQuat("pure_rotation.quat", pureRotationQuat);
dumpMat4("pure_rotation", pureRotationM);
dumpMat4("pure_rotation.quat.mat4", mat4FromQuat(pureRotationQuat));
dumpMat4("transform.rotation_only", rotationOnlyM);
dumpQuat("transform.rotation_only.quat", quatFromMat(rotationOnlyM));
dumpQuat("axis_mat.quat", quatFromMat(axisMat));
dumpMat4("axis_mat.quat.mat4", mat4FromQuat(quatFromMat(axisMat)));
appendLine("hard_decomp.note: 170 degrees around (1,-2,3) normalized - w near zero");
dumpQuat("hard_decomp.quat_original", hardQuat);
dumpQuat("hard_decomp.quat_from_mat", quatFromMat(hardMat));
dumpMat4("hard_decomp.mat4", hardMat);
dumpMat4("hard_decomp.quat_from_mat.mat4", mat4FromQuat(quatFromMat(hardMat)));
heading("lookAt matrix");
appendLine("notes: eye=(5,5,5), center=(0,0,0), up=(0,1,0)");
dumpMat4("lookAt", mat4.lookAt(
mat4.create(),
vec3.fromValues(5, 5, 5),
vec3.fromValues(0, 0, 0),
vec3.fromValues(0, 1, 0)
));
heading("euler angle decomposition");
appendLine("notes: euler angles as vec3(pitch/x, yaw/y, roll/z) in radians");
dumpVec3("pure_rotation.quat.euler", quatToEulerXYZ(pureRotationQuat));
dumpVec3("from_axis_angle.euler", quatToEulerXYZ(axisQuat));
heading("matrix inverse");
dumpMat4("transform.inverse", mat4.invert(mat4.create(), transformM));
dumpMat4("pure_rotation.inverse", mat4.invert(mat4.create(), pureRotationM));
heading("cross product");
appendLine("notes: cross(a, b) where a and b are vec3");
const crossA = vec3.fromValues(1, 0, 0);
const crossB = vec3.fromValues(0, 1, 0);
const crossC = vec3.normalize(vec3.create(), vec3.fromValues(1, 2, 3));
const crossD = vec3.normalize(vec3.create(), vec3.fromValues(-1, 0.5, 2));
dumpVec3("cross(x_axis, y_axis)", vec3.cross(vec3.create(), crossA, crossB));
dumpVec3("cross(y_axis, x_axis)", vec3.cross(vec3.create(), crossB, crossA));
dumpVec3("cross(c, d)", vec3.cross(vec3.create(), crossC, crossD));
heading("slerp");
appendLine("notes: slerp(a, b, t) between quat_x and quat_z");
dumpQuat("slerp(quat_x, quat_z, 0.25)", quat.slerp(quat.create(), quatX, quatZ, 0.25));
dumpQuat("slerp(quat_x, quat_z, 0.5)", quat.slerp(quat.create(), quatX, quatZ, 0.5));
dumpQuat("slerp(quat_x, quat_z, 0.75)", quat.slerp(quat.create(), quatX, quatZ, 0.75));
heading("fromTwoVectors");
appendLine("notes: quaternion that rotates vector a to vector b");
const fromA = vec3.fromValues(1, 0, 0);
const fromB = vec3.fromValues(0, 1, 0);
const fromC = vec3.normalize(vec3.create(), vec3.fromValues(1, 2, -1));
const fromD = vec3.normalize(vec3.create(), vec3.fromValues(-1, 0.5, 2));
const fromXToY = quat.rotationTo(quat.create(), fromA, fromB);
const fromCToD = quat.rotationTo(quat.create(), fromC, fromD);
dumpQuat("from_x_to_y", fromXToY);
dumpQuat("from_c_to_d", fromCToD);
dumpVec3("verify_x_to_y", vec3.transformQuat(vec3.create(), fromA, fromXToY));
dumpVec3("verify_c_to_d", vec3.transformQuat(vec3.create(), fromC, fromCToD));
heading("quaternion inverse");
dumpQuat("from_axis_angle.inverse", quat.invert(quat.create(), axisQuat));
dumpQuat("verify_q_mul_qinv", quat.multiply(quat.create(), axisQuat, quat.invert(quat.create(), axisQuat)));
heading("quaternion to axis-angle");
const axisAngle1 = quatToAxisAngle(axisQuat);
const axisAngle2 = quatToAxisAngle(quatXYZ);
dumpVec3("from_axis_angle.axis", axisAngle1.axis);
dumpScalar("from_axis_angle.angle", axisAngle1.angle);
dumpVec3("quat_xyz.axis", axisAngle2.axis);
dumpScalar("quat_xyz.angle", axisAngle2.angle);
const DEG2RAD_60 = 60 * DEG2RAD;
heading("perspective matrix");
appendLine("notes: fovy=60 degrees, aspect=1.5, near=0.1, far=100.0");
dumpMat4("perspective", mat4.perspective(mat4.create(), DEG2RAD_60, 1.5, 0.1, 100.0));
heading("ortho matrix");
appendLine("notes: left=-10, right=10, bottom=-7.5, top=7.5, near=0.1, far=100.0");
dumpMat4("ortho", mat4.ortho(mat4.create(), -10, 10, -7.5, 7.5, 0.1, 100.0));
heading("basis directions");
dumpVec3("forward", basisForward(matA));
dumpVec3("right", basisRight(matA));
dumpVec3("up", basisUp(matA));
heading("default matrix printer");
dumpMat4Default("matrix_a.default", matA);
dumpMat4Default("transform.default", transformM);
writeFileSync(OutputPath, lines.join("\n") + "\n");
console.log("Wrote", OutputPath);