-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.h
More file actions
188 lines (163 loc) · 6.13 KB
/
camera.h
File metadata and controls
188 lines (163 loc) · 6.13 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
#ifndef CAMERA_H
#define CAMERA_H
#include "physica_math.h"
struct camera_zoom_ {
f32 factor;
v2 relative_center;
};
struct camera_ {
v2 center, to_top_right;
f32 orientation;
camera_zoom_ zoom;
};
inline m4x4
get_view_transform_4x4(camera_ camera) {
assert_(camera.zoom.factor != 0.0f);
v2 to_top_right = camera.to_top_right * (1.0f / camera.zoom.factor);
v2 center = camera.center + rotate(v2 {
camera.to_top_right.x * camera.zoom.relative_center.x,
camera.to_top_right.y * camera.zoom.relative_center.y
}, camera.orientation);
f32 camera_scale_x = 1.0f / to_top_right.x;
f32 camera_scale_y = 1.0f / to_top_right.y;
m4x4 view_rotate = get_rotation_matrix_4x4(-camera.orientation);
m4x4 view_translate = identity_4x4();
m4x4 view_scale = identity_4x4();
//scale
view_scale.r1.c1 = camera_scale_x;
view_scale.r2.c2 = camera_scale_y;
//translate
view_translate.r1.c4 = -center.x;
view_translate.r2.c4 = -center.y;
m4x4 view = view_scale * view_rotate * view_translate;
return view;
}
inline m4x4
get_parallaxed_view_transform_4x4(camera_ camera, f32 parallax) {
assert_(camera.zoom.factor != 0.0f);
v2 to_top_right = camera.to_top_right * (1.0f / camera.zoom.factor);
v2 center = camera.center * parallax + rotate(v2 {
camera.to_top_right.x * camera.zoom.relative_center.x,
camera.to_top_right.y * camera.zoom.relative_center.y
}, camera.orientation);
f32 camera_scale_x = 1.0f / to_top_right.x;
f32 camera_scale_y = 1.0f / to_top_right.y;
m4x4 view_rotate = get_rotation_matrix_4x4(-camera.orientation);
m4x4 view_translate = identity_4x4();
m4x4 view_scale = identity_4x4();
//scale
view_scale.r1.c1 = camera_scale_x;
view_scale.r2.c2 = camera_scale_y;
//translate
view_translate.r1.c4 = -center.x;
view_translate.r2.c4 = -center.y;
m4x4 view = view_scale * view_rotate * view_translate;
return view;
}
inline m3x3
get_parallaxed_view_transform_3x3(camera_ camera, f32 parallax) {
assert_(camera.zoom.factor != 0.0f);
v2 to_top_right = camera.to_top_right * (1.0f / camera.zoom.factor);
v2 center = camera.center * parallax + rotate(v2 {
camera.to_top_right.x * camera.zoom.relative_center.x,
camera.to_top_right.y * camera.zoom.relative_center.y
}, camera.orientation);
f32 camera_scale_x = 1.0f / to_top_right.x;
f32 camera_scale_y = 1.0f / to_top_right.y;
m3x3 view_rotate = get_rotation_matrix_3x3(-camera.orientation);
m3x3 view_translate = identity_3x3();
m3x3 view_scale = identity_3x3();
//scale
view_scale.r1.c1 = camera_scale_x;
view_scale.r2.c2 = camera_scale_y;
//translate
view_translate.r1.c3 = -center.x;
view_translate.r2.c3 = -center.y;
m3x3 view = view_scale * view_rotate * view_translate;
return view;
}
inline m3x3
get_inverse_parallaxed_view_transform_3x3(camera_ camera, f32 parallax) {
assert_(camera.zoom.factor != 0.0f);
v2 to_top_right = camera.to_top_right * (1.0f / camera.zoom.factor);
v2 center = (camera.center - rotate(v2 {
camera.to_top_right.x * camera.zoom.relative_center.x,
camera.to_top_right.y * camera.zoom.relative_center.y
}, -camera.orientation)) * (1.0f / parallax);
f32 camera_scale_x = to_top_right.x;
f32 camera_scale_y = to_top_right.y;
m3x3 view_rotate = get_rotation_matrix_3x3(camera.orientation);
m3x3 view_translate = identity_3x3();
m3x3 view_scale = identity_3x3();
//scale
view_scale.r1.c1 = camera_scale_x;
view_scale.r2.c2 = camera_scale_y;
//translate
view_translate.r1.c3 = center.x;
view_translate.r2.c3 = center.y;
m3x3 view = view_translate * view_rotate * view_scale;
return view;
}
inline m3x3
get_view_transform_3x3(camera_ camera) {
assert_(camera.zoom.factor != 0.0f);
v2 to_top_right = camera.to_top_right * (1.0f / camera.zoom.factor);
v2 center = camera.center + rotate(v2 {
camera.to_top_right.x * camera.zoom.relative_center.x,
camera.to_top_right.y * camera.zoom.relative_center.y
}, camera.orientation);
f32 camera_scale_x = 1.0f / to_top_right.x;
f32 camera_scale_y = 1.0f / to_top_right.y;
m3x3 view_rotate = get_rotation_matrix_3x3(-camera.orientation);
m3x3 view_translate = identity_3x3();
m3x3 view_scale = identity_3x3();
//scale
view_scale.r1.c1 = camera_scale_x;
view_scale.r2.c2 = camera_scale_y;
//translate
view_translate.r1.c3 = -center.x;
view_translate.r2.c3 = -center.y;
m3x3 view = view_scale * view_rotate * view_translate;
return view;
}
inline m3x3
get_inverse_view_transform_3x3(camera_ camera) {
assert_(camera.zoom.factor != 0.0f);
v2 to_top_right = camera.to_top_right * (1.0f / camera.zoom.factor);
v2 center = camera.center + rotate(v2 {
camera.to_top_right.x * camera.zoom.relative_center.x,
camera.to_top_right.y * camera.zoom.relative_center.y
}, -camera.orientation);
f32 camera_scale_x = to_top_right.x;
f32 camera_scale_y = to_top_right.y;
m3x3 view_rotate = get_rotation_matrix_3x3(camera.orientation);
m3x3 view_translate = identity_3x3();
m3x3 view_scale = identity_3x3();
//scale
view_scale.r1.c1 = camera_scale_x;
view_scale.r2.c2 = camera_scale_y;
//translate
view_translate.r1.c3 = center.x;
view_translate.r2.c3 = center.y;
m3x3 view = view_translate * view_rotate * view_scale;
return view;
}
inline m2x2
get_inverse_view_transform_2x2(camera_ camera) {
assert_(camera.zoom.factor != 0.0f);
v2 to_top_right = camera.to_top_right * (1.0f / camera.zoom.factor);
v2 center = camera.center + rotate(v2 {
camera.to_top_right.x * camera.zoom.relative_center.x,
camera.to_top_right.y * camera.zoom.relative_center.y
}, -camera.orientation);
f32 camera_scale_x = to_top_right.x;
f32 camera_scale_y = to_top_right.y;
m2x2 view_rotate = get_rotation_matrix(camera.orientation);
m2x2 view_scale = identity_2x2();
//scale
view_scale.r1.c1 = camera_scale_x;
view_scale.r2.c2 = camera_scale_y;
m2x2 view = view_rotate * view_scale;
return view;
}
#endif