Debian Codegraph @

third_party/cgltf/test/test_math.cpp

raw
1 #define CGLTF_IMPLEMENTATION
2 #include "../cgltf.h"
3
4 // Performs matrix-vector multiplication, as in (4x4) * (4x1) = (4x1)
5 static void transform(const cgltf_float matrix[16], const cgltf_float source[4], cgltf_float target[4]) {
6 target[0] = matrix[0] * source[0] + matrix[4] * source[1] + matrix[ 8] * source[2] + matrix[12] * source[3];
7 target[1] = matrix[1] * source[0] + matrix[5] * source[1] + matrix[ 9] * source[2] + matrix[13] * source[3];
8 target[2] = matrix[2] * source[0] + matrix[6] * source[1] + matrix[10] * source[2] + matrix[14] * source[3];
9 target[3] = matrix[3] * source[0] + matrix[7] * source[1] + matrix[11] * source[2] + matrix[15] * source[3];
10 }
11
12 static void set(cgltf_float target[3], float x, float y, float z) {
13 target[0] = x;
14 target[1] = y;
15 target[2] = z;
16 }
17
18 static void check(cgltf_float target[3], float x, float y, float z) {
19 if (target[0] != x || target[1] != y || target[2] != z) {
20 fprintf(stderr, "Mismatch detected.\n");
21 exit(1);
22 }
23 }
24
25 int main(int, char**)
26 {
27 cgltf_node node = {};
28
29 cgltf_float matrix[16];
30 cgltf_float source[4] = {1, 2, 3, 1};
31 cgltf_float target[4];
32
33 set(node.scale, 1, 1, 1);
34 set(node.translation, 1, 0, 0);
35 cgltf_node_transform_local(&node, matrix);
36 transform(matrix, source, target);
37 check(target, 2, 2, 3);
38
39 set(node.scale, 3, 1, 1);
40 set(node.translation, 0, 0, 0);
41 cgltf_node_transform_local(&node, matrix);
42 transform(matrix, source, target);
43 check(target, 3, 2, 3);
44
45 set(node.scale, 1, 3, 1);
46 set(node.translation, 1, 0, 0);
47 cgltf_node_transform_local(&node, matrix);
48 transform(matrix, source, target);
49 check(target, 2, 6, 3);
50
51 return 0;
52 }