-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.odin
92 lines (77 loc) · 2.16 KB
/
camera.odin
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
package zephr
import m "core:math/linalg/glsl"
Camera :: struct {
position: m.vec3,
front: m.vec3,
up: m.vec3,
yaw: f32,
pitch: f32,
speed: f32,
sensitivity: f32,
fov: f32,
view_mat: m.mat4,
proj_mat: m.mat4,
}
DEFAULT_CAMERA :: Camera {
position = m.vec3{0, 1, 3},
front = m.vec3{0, 0, -1},
up = m.vec3{0, 1, 0},
yaw = -90,
pitch = 0,
speed = 5,
sensitivity = 0.05,
fov = 45,
view_mat = 1, // 1 is identity mat
proj_mat = 1,
}
@(private)
editor_camera: Camera = DEFAULT_CAMERA
new_camera :: proc(pitch: f32 = 0, yaw: f32 = -90, fov: f32 = 45) -> Camera {
front := m.vec3 {
m.cos(m.radians(pitch)) * m.cos(m.radians(yaw)),
m.sin(m.radians(pitch)),
m.cos(m.radians(pitch)) * m.sin(m.radians(yaw)),
}
position := m.vec3{0, 0, 0}
up := m.vec3{0, 1, 0}
window_size := get_window_size()
view_mat := m.mat4LookAt(
position,
position + front,
up,
)
projection := m.mat4Perspective(m.radians(fov), window_size.x / window_size.y, 0.01, 200)
return Camera {
position = m.vec3{0, 0, 0},
front = front,
up = m.vec3{0, 1, 0},
yaw = yaw,
pitch = pitch,
speed = 5,
sensitivity = 0.05,
fov = fov,
view_mat = view_mat,
proj_mat = projection,
}
}
move_camera_view :: proc(camera: ^Camera, xoffset: f32, yoffset: f32) {
xoffset := xoffset * camera.sensitivity
yoffset := yoffset * camera.sensitivity
camera.yaw += xoffset
camera.pitch += yoffset
if camera.pitch > 89.0 {
camera.pitch = 89.0
}
if camera.pitch < -89.0 {
camera.pitch = -89.0
}
front := m.vec3 {
m.cos(m.radians(camera.pitch)) * m.cos(m.radians(camera.yaw)),
m.sin(m.radians(camera.pitch)),
m.cos(m.radians(camera.pitch)) * m.sin(m.radians(camera.yaw)),
}
camera.front = m.normalize(front)
}
get_editor_camera :: proc() -> ^Camera {
return &editor_camera
}