]> git.localhorst.tv Git - blank.git/blob - src/camera.cpp
8804d115b36574cdab2618fb441f0d1a50958696
[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 Camera::~Camera() {
22
23 }
24
25
26 void Camera::Viewport(int width, int height) {
27         Viewport(0, 0, width, height);
28 }
29
30 void Camera::Viewport(int x, int y, int width, int height) {
31         glViewport(x, y, width, height);
32         Aspect(width, height);
33 }
34
35 void Camera::FOV(float f) {
36         fov = f;
37         UpdateProjection();
38 }
39
40 void Camera::Aspect(float r) {
41         aspect = r;
42         UpdateProjection();
43 }
44
45 void Camera::Aspect(float w, float h) {
46         Aspect(w / h);
47 }
48
49 void Camera::Clip(float near, float far) {
50         near_clip = near;
51         far_clip = far;
52         UpdateProjection();
53 }
54
55 Ray Camera::Aim() const {
56         const glm::mat4 inv_vp(glm::inverse(vp));
57         glm::vec4 from = inv_vp * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
58         from /= from.w;
59         glm::vec4 to = inv_vp * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
60         to /= to.w;
61         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
62 }
63
64
65 void Camera::Update(int dt) {
66         FPSController::Update(dt);
67         view = glm::inverse(Transform());
68         vp = projection * view;
69 }
70
71 void Camera::UpdateProjection() {
72         projection = glm::perspective(fov, aspect, near_clip, far_clip);
73 }
74
75 }