-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
63 lines (45 loc) · 2.07 KB
/
camera.py
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
import glm
from settings import *
class Camera:
def __init__(self, position, yaw, pitch):
self.position = glm.vec3(position)
self.yaw = glm.radians(yaw)
self.pitch = glm.radians(pitch)
self.up = glm.vec3(0, 1, 0)
self.right = glm.vec3(1, 0, 0)
self.forward = glm.vec3(0, 0, -1)
self.m_proj = glm.perspective(V_FOV, ASPECT_RATIO, NEAR, FAR)
self.m_view = glm.mat4()
def update(self):
self.update_vectors()
self.update_view_matrix()
def update_view_matrix(self):
self.m_view = glm.lookAt(self.position, self.position + self.forward, self.up)
def update_vectors(self):
self.forward.x = glm.cos(self.yaw) * glm.cos(self.pitch)
self.forward.y = glm.sin(self.pitch)
self.forward.z = glm.sin(self.yaw) * glm.cos(self.pitch)
self.forward = glm.normalize(self.forward)
self.right = glm.normalize(glm.cross(self.forward, glm.vec3(0, 1, 0)))
self.up = glm.normalize(glm.cross(self.right, self.forward))
def rotate_pitch(self, delta_y):
self.pitch -= delta_y
self.pitch = glm.clamp(self.pitch, -PITCH_MAX, PITCH_MAX)
def rotate_yaw(self, delta_x):
self.yaw += delta_x
def move_left(self, velocity):
self.position -= self.right * velocity
def move_right(self, velocity):
self.position += self.right * velocity
def move_up(self, velocity):
self.position += self.up * glm.cos(self.pitch) * velocity
self.position += self.forward * glm.sin(self.pitch) * velocity
def move_down(self, velocity):
self.position -= self.up * glm.cos(self.pitch) * velocity
self.position -= self.forward * glm.sin(self.pitch) * velocity
def move_forward(self, velocity):
self.position += self.forward * glm.cos(self.pitch) * velocity
self.position -= self.up * glm.sin(self.pitch) * velocity
def move_backward(self, velocity):
self.position -= self.forward * glm.cos(self.pitch) * velocity
self.position += self.up * glm.sin(self.pitch) * velocity