]> git.localhorst.tv Git - blank.git/blob - src/camera.cpp
remove unused (explicit) destructors
[blank.git] / src / camera.cpp
1 #include "camera.hpp"
2
3 #include <GL/glew.h>
4 #include <glm/gtc/matrix_transform.hpp>
5
6
7 namespace blank {
8
9 Camera::Camera()
10 : FPSController()
11 , fov(45.0f)
12 , aspect(1.0f)
13 , near_clip(0.1f)
14 , far_clip(100.0f)
15 , projection(glm::perspective(fov, aspect, near_clip, far_clip))
16 , view(glm::inverse(Transform()))
17 , vp(projection) {
18
19 }
20
21
22 void Camera::Viewport(int width, int height) {
23         Viewport(0, 0, width, height);
24 }
25
26 void Camera::Viewport(int x, int y, int width, int height) {
27         glViewport(x, y, width, height);
28         Aspect(width, height);
29 }
30
31 void Camera::FOV(float f) {
32         fov = f;
33         UpdateProjection();
34 }
35
36 void Camera::Aspect(float r) {
37         aspect = r;
38         UpdateProjection();
39 }
40
41 void Camera::Aspect(float w, float h) {
42         Aspect(w / h);
43 }
44
45 void Camera::Clip(float near, float far) {
46         near_clip = near;
47         far_clip = far;
48         UpdateProjection();
49 }
50
51 Ray Camera::Aim() const {
52         const glm::mat4 inv_vp(glm::inverse(vp));
53         glm::vec4 from = inv_vp * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
54         from /= from.w;
55         glm::vec4 to = inv_vp * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
56         to /= to.w;
57         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
58 }
59
60
61 void Camera::Update(int dt) {
62         FPSController::Update(dt);
63         view = glm::inverse(Transform());
64         vp = projection * view;
65 }
66
67 void Camera::UpdateProjection() {
68         projection = glm::perspective(fov, aspect, near_clip, far_clip);
69 }
70
71 }