-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.mbt
More file actions
268 lines (234 loc) · 6.52 KB
/
camera.mbt
File metadata and controls
268 lines (234 loc) · 6.52 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Camera2D: 24 bytes (offset, target as Vector2; rotation, zoom as float)
///|
/// Camera2D, defines position/orientation in 2D space.
pub struct Camera2D {
offset : Vector2
target : Vector2
rotation : Float
zoom : Float
} derive(Eq, Show)
///|
/// Create a new Camera2D.
pub fn Camera2D::new(
offset : Vector2,
target : Vector2,
rotation : Float,
zoom : Float,
) -> Camera2D {
{ offset, target, rotation, zoom }
}
///|
/// Serialize Camera2D to bytes.
pub fn Camera2D::to_bytes(c : Camera2D) -> Bytes {
let buf = @buffer.new(size_hint=24)
buf.write_float_le(c.offset.x)
buf.write_float_le(c.offset.y)
buf.write_float_le(c.target.x)
buf.write_float_le(c.target.y)
buf.write_float_le(c.rotation)
buf.write_float_le(c.zoom)
buf.to_bytes()
}
///|
/// Deserialize Camera2D from bytes.
pub fn Camera2D::from_bytes(b : Bytes) -> Camera2D {
{
offset: { x: read_float(b, 0), y: read_float(b, 4) },
target: { x: read_float(b, 8), y: read_float(b, 12) },
rotation: read_float(b, 16),
zoom: read_float(b, 20),
}
}
// Camera3D: 44 bytes (position, target, up as Vector3; fovy as float; projection as int)
///|
/// Camera3D, defines position/orientation in 3D space.
pub struct Camera3D {
position : Vector3
target : Vector3
up : Vector3
fovy : Float
projection : Int
} derive(Eq, Show)
///|
/// Create a new Camera3D.
pub fn Camera3D::new(
position : Vector3,
target : Vector3,
up : Vector3,
fovy : Float,
projection : Int,
) -> Camera3D {
{ position, target, up, fovy, projection }
}
///|
/// Serialize Camera3D to bytes.
pub fn Camera3D::to_bytes(c : Camera3D) -> Bytes {
let buf = @buffer.new(size_hint=44)
buf.write_float_le(c.position.x)
buf.write_float_le(c.position.y)
buf.write_float_le(c.position.z)
buf.write_float_le(c.target.x)
buf.write_float_le(c.target.y)
buf.write_float_le(c.target.z)
buf.write_float_le(c.up.x)
buf.write_float_le(c.up.y)
buf.write_float_le(c.up.z)
buf.write_float_le(c.fovy)
buf.write_int_le(c.projection)
buf.to_bytes()
}
///|
/// Deserialize Camera3D from bytes.
pub fn Camera3D::from_bytes(b : Bytes) -> Camera3D {
{
position: { x: read_float(b, 0), y: read_float(b, 4), z: read_float(b, 8) },
target: { x: read_float(b, 12), y: read_float(b, 16), z: read_float(b, 20) },
up: { x: read_float(b, 24), y: read_float(b, 28), z: read_float(b, 32) },
fovy: read_float(b, 36),
projection: read_int(b, 40),
}
}
// Camera mode constants
///|
/// Custom camera mode, controlled by user (UpdateCamera() does nothing).
pub const CameraCustom : Int = 0
///|
/// Camera free mode.
pub const CameraFree : Int = 1
///|
/// Camera orbital mode, around target, zoom supported.
pub const CameraOrbital : Int = 2
///|
/// Camera first person mode.
pub const CameraFirstPerson : Int = 3
///|
/// Camera third person mode.
pub const CameraThirdPerson : Int = 4
// Camera projection constants
///|
/// Perspective projection.
pub const CameraPerspective : Int = 0
///|
/// Orthographic projection.
pub const CameraOrthographic : Int = 1
// Re-export passthrough camera functions
///|
pub using @ffi {end_mode_2d, end_mode_3d}
// Camera wrappers
///|
/// Begin 2D mode with custom camera (2D).
pub fn begin_mode_2d(camera : Camera2D) -> Unit {
@ffi.begin_mode_2d(camera.to_bytes())
}
///|
/// Begin 3D mode with custom camera (3D).
pub fn begin_mode_3d(camera : Camera3D) -> Unit {
@ffi.begin_mode_3d(camera.to_bytes())
}
///|
/// Update camera position for selected mode.
pub fn update_camera(camera : Camera3D, mode : Int) -> Camera3D {
Camera3D::from_bytes(@ffi.update_camera(camera.to_bytes(), mode))
}
///|
/// Update camera movement/rotation.
pub fn update_camera_pro(
camera : Camera3D,
movement : Vector3,
rotation : Vector3,
zoom : Float,
) -> Camera3D {
Camera3D::from_bytes(
@ffi.update_camera_pro(
camera.to_bytes(),
movement.to_bytes(),
rotation.to_bytes(),
zoom,
),
)
}
///|
/// Get the screen space position for a 3D world space position.
pub fn get_world_to_screen(position : Vector3, camera : Camera3D) -> Vector2 {
Vector2::from_bytes(
@ffi.get_world_to_screen(position.to_bytes(), camera.to_bytes()),
)
}
///|
/// Get the world space position for a 2D camera screen space position.
pub fn get_screen_to_world_2d(position : Vector2, camera : Camera2D) -> Vector2 {
let inv = Matrix::invert(get_camera_matrix_2d(camera))
let v = Vector3::transform({ x: position.x, y: position.y, z: 0.0 }, inv)
{ x: v.x, y: v.y }
}
///|
/// Get the screen space position for a 2D camera world space position.
pub fn get_world_to_screen_2d(position : Vector2, camera : Camera2D) -> Vector2 {
let mat = get_camera_matrix_2d(camera)
let v = Vector3::transform({ x: position.x, y: position.y, z: 0.0 }, mat)
{ x: v.x, y: v.y }
}
// ============================================================================
// Camera matrix
// ============================================================================
///|
/// Get camera transform matrix (view matrix).
pub fn get_camera_matrix(camera : Camera3D) -> Matrix {
Matrix::look_at(camera.position, camera.target, camera.up)
}
///|
/// Get camera 2D transform matrix.
pub fn get_camera_matrix_2d(camera : Camera2D) -> Matrix {
let mat_origin = Matrix::translate(-camera.target.x, -camera.target.y, 0.0)
let mat_rotation = Matrix::rotate(
{ x: 0.0, y: 0.0, z: 1.0 },
camera.rotation * 0.017453292519943295,
)
let mat_scale = Matrix::scale(camera.zoom, camera.zoom, 1.0)
let mat_translation = Matrix::translate(camera.offset.x, camera.offset.y, 0.0)
Matrix::multiply(
Matrix::multiply(mat_origin, Matrix::multiply(mat_scale, mat_rotation)),
mat_translation,
)
}
///|
/// Get a ray trace from screen position (i.e mouse).
pub fn get_screen_to_world_ray(position : Vector2, camera : Camera3D) -> Ray {
Ray::from_bytes(
@ffi.get_screen_to_world_ray(position.to_bytes(), camera.to_bytes()),
)
}
///|
/// Get a ray trace from screen position (i.e mouse) in a viewport.
pub fn get_screen_to_world_ray_ex(
position : Vector2,
camera : Camera3D,
width : Int,
height : Int,
) -> Ray {
Ray::from_bytes(
@ffi.get_screen_to_world_ray_ex(
position.to_bytes(),
camera.to_bytes(),
width,
height,
),
)
}
///|
/// Get size position for a 3D world space position.
pub fn get_world_to_screen_ex(
position : Vector3,
camera : Camera3D,
width : Int,
height : Int,
) -> Vector2 {
Vector2::from_bytes(
@ffi.get_world_to_screen_ex(
position.to_bytes(),
camera.to_bytes(),
width,
height,
),
)
}