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