-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.c
More file actions
149 lines (119 loc) · 3.42 KB
/
transform.c
File metadata and controls
149 lines (119 loc) · 3.42 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
#include "transform.h"
#include <math.h>
#include <assert.h>
# define M_PI 3.14159265358979323846 /* pi */
static struct mat4 inv_transMat(vec3 movement)
{
struct mat4 out = mat4_identity;
out = mat4_SetCell(out, 0, 3, -movement.x);
out = mat4_SetCell(out, 1, 3, -movement.y);
out = mat4_SetCell(out, 2, 3, -movement.z);
return out;
}
static struct mat4 translationMat(vec3 movement)
{
struct mat4 out = mat4_identity;
out = mat4_SetCell(out, 0, 3, movement.x);
out = mat4_SetCell(out, 1, 3, movement.y);
out = mat4_SetCell(out, 2, 3, movement.z);
return out;
}
static struct mat4 rotationMat(vec3 rotation)
{
struct mat4 out = mat4_identity,
rotX = mat4_identity,
rotY = mat4_identity,
rotZ = mat4_identity;
float x = rotation.x * M_PI / 180,
y = rotation.y * M_PI / 180,
z = rotation.z * M_PI / 180;
// Set X
{
rotX = mat4_SetCell(rotX, 1, 1, cos(x));
rotX = mat4_SetCell(rotX, 1, 2, sin(x));
rotX = mat4_SetCell(rotX, 2, 1, -sin(x));
rotX = mat4_SetCell(rotX, 2, 2, cos(x));
}
// Set Y
{
rotY = mat4_SetCell(rotY, 0, 0, cos(y));
rotY = mat4_SetCell(rotY, 0, 2, -sin(y));
rotY = mat4_SetCell(rotY, 2, 0, sin(y));
rotY = mat4_SetCell(rotY, 2, 2, cos(y));
}
// Set Z
{
rotZ = mat4_SetCell(rotZ, 0, 0, cos(z));
rotZ = mat4_SetCell(rotZ, 0, 1, sin(z));
rotZ = mat4_SetCell(rotZ, 1, 0, -sin(z));
rotZ = mat4_SetCell(rotZ, 1, 1, cos(z));
}
return mat4mul(mat4mul(mat4mul(mat4_identity, rotZ), rotX), rotY);
}
static struct mat4 inv_rotationMat(vec3 rotation)
{
return mat4_Transpose(rotationMat(rotation));
}
static struct mat4 inv_scaleMat(vec3 scale)
{
struct mat4 out = mat4_identity;
out = mat4_SetCell(out, 0, 0, 1 / scale.x);
out = mat4_SetCell(out, 1, 1, 1 / scale.y);
out = mat4_SetCell(out, 2, 2, 1 / scale.z);
return out;
}
static struct mat4 scaleMat(vec3 scale)
{
struct mat4 out = mat4_identity;
out = mat4_SetCell(out, 0, 0, scale.x);
out = mat4_SetCell(out, 1, 1, scale.y);
out = mat4_SetCell(out, 2, 2, scale.z);
return out;
}
static vec3 applyMat(struct mat4 mat, vec3 point)
{
vec4 tempPoint = {
.x = point.x,
.y = point.y,
.z = point.z,
.w = 1
};
vec4 transformed = mat4_transform(mat, tempPoint);
vec3 out = {
.x = transformed.x,
.y = transformed.y,
.z = transformed.z
};
assert(transformed.w == 1);
return out;
}
vec3 Transform(struct transform_t transform, vec3 point)
{
return applyMat(Transform_GetMatrix(transform), point);
}
vec3 InverseTransform(struct transform_t transform, vec3 point)
{
return applyMat(Transform_GetInvMatrix(transform), point);
}
vec3 Transform_Forward(struct transform_t transform)
{
return applyMat(rotationMat(transform.rotation), vec3_forward);
}
vec3 Transform_Right(struct transform_t transform)
{
return applyMat(rotationMat(transform.rotation), vec3_right);
}
struct mat4 Transform_GetMatrix(struct transform_t transform)
{
struct mat4 scaled = mat4mul(scaleMat(transform.scale), mat4_identity);
struct mat4 rotated = mat4mul(rotationMat(transform.rotation), scaled);
struct mat4 translated = mat4mul(translationMat(transform.position), rotated);
return translated;
}
struct mat4 Transform_GetInvMatrix(struct transform_t transform)
{
struct mat4 translated = mat4mul(inv_transMat(transform.position), mat4_identity);
struct mat4 rotated = mat4mul(inv_rotationMat(transform.rotation), translated);
struct mat4 scaled = mat4mul(inv_scaleMat(transform.scale), rotated);
return scaled;
}