6 const float TAU = 6.28318530718f;
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 };
17 Transform multiply(Transform t1, Transform t2) {
18 GLfloat* a = (GLfloat*) &t1;
19 GLfloat* b = (GLfloat*) &t2;
21 for (size_t row = 0; row < 4; ++row) {
22 for (size_t col = 0; col < 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];
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 },
42 void rotate(Transform* transform, Vector3D axis, float angle) {
43 axis = normalized(axis);
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 },
58 Vector3D applyTransform(Transform* transform, Vector3D vec) {
59 GLfloat* a = (GLfloat*) &transform;
60 GLfloat b[4] = { vec.x, vec.y, vec.z, 1.0f };
63 for (size_t row = 0; row < 4; ++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];
70 return (Vector3D) { c[0], c[1], c[2] };
73 Vector3D translationOf(Transform transform) {
74 return (Vector3D) { transform.a4, transform.b4, transform.c4 };
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};