]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/geometry.c
Implement transform math to rotate movement direction
[shadowclad.git] / src / engine / geometry.c
1 #include "geometry.h"
2
3 #include <math.h>
4 #include <stddef.h>
5
6 const float TAU = 6.28318530718f;
7
8
9
10 Transform identity() {
11         return (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = 0.0f,
12                              .b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = 0.0f,
13                              .c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = 0.0f,
14                              .d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f };
15 }
16
17 Transform multiply(Transform t1, Transform t2) {
18         GLfloat* a = (GLfloat*) &t1;
19         GLfloat* b = (GLfloat*) &t2;
20
21         for (size_t row = 0; row < 4; ++row) {
22                 for (size_t col = 0; col < 4; ++col) {
23                         b[(row * 4) + col] =
24                                 a[(row * 4) + 0] * b[(0 * 4) + col]
25                                 + a[(row * 4) + 1] * b[(1 * 4) + col]
26                                 + a[(row * 4) + 2] * b[(2 * 4) + col]
27                                 + a[(row * 4) + 3] * b[(3 * 4) + col];
28                 }
29         }
30         return t2;
31 }
32
33 void translate(Transform* transform, Vector3D vec) {
34         *transform = multiply(
35                 (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = vec.x,
36                               .b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = vec.y,
37                               .c1 = 0.0f, .c2 = 0.0f, .c3 = 1.0f, .c4 = vec.z,
38                               .d1 = 0.0f, .d2 = 0.0f, .d3 = 0.0f, .d4 = 1.0f },
39                 *transform);
40 }
41
42 void rotate(Transform* transform, Vector3D axis, float angle) {
43         axis = normalized(axis);
44         float l = axis.x;
45         float m = axis.y;
46         float n = axis.z;
47         float sinA = sinf(angle);
48         float cosA = cosf(angle);
49         float omcA = 1 - cosA;
50         *transform = multiply(
51                 (Transform) { l*l*omcA + cosA, m*l*omcA - n*sinA, n*l*omcA + m*sinA, 0.0f,
52                               l*m*omcA + n*sinA, m*m*omcA + cosA, n*m*omcA - l*sinA, 0.0f,
53                               l*n*omcA - m*sinA, m*n*omcA + l*sinA, n*n*omcA + cosA, 0.0f,
54                               0.0f, 0.0f, 0.0f, 1.0f },
55                 *transform);
56 }
57
58 Vector3D applyTransform(Transform* transform, Vector3D vec) {
59         GLfloat* a = (GLfloat*) &transform;
60         GLfloat b[4] = { vec.x, vec.y, vec.z, 1.0f };
61         GLfloat c[4];
62
63         for (size_t row = 0; row < 4; ++row) {
64                 c[row] =
65                         a[(row * 4) + 0] * b[0]
66                         + a[(row * 4) + 1] * b[1]
67                         + a[(row * 4) + 2] * b[2]
68                         + a[(row * 4) + 3] * b[3];
69         }
70         return (Vector3D) { c[0], c[1], c[2] };
71 }
72
73 Vector3D translationOf(Transform transform) {
74         return (Vector3D) { transform.a4, transform.b4, transform.c4 };
75 }
76
77 Vector3D normalized(Vector3D vec) {
78         float magnitude = sqrtf(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
79         return (Vector3D) {vec.x / magnitude, vec.y / magnitude, vec.z / magnitude};
80 }