summaryrefslogtreecommitdiff
path: root/inc/opengl/camera.hpp
blob: 62ae837332f13c376028e7529858a1f7be2f455c (plain)
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
#ifndef camera_h
#define camera_h

struct Camera
{
        public:
                Camera(const glm::vec3& pos, float fov, float aspect, float zNear, float zFar){
                this->pos = pos;
                this->forward = glm::vec3(0.0f, 0.0f, -1.0f);
                this->up = glm::vec3(0.0f, 1.0f, 0.0f);
                this->projection = glm::perspective(fov, aspect, zNear, zFar);
                }

                inline glm::mat4 GetViewProjection() const {
                return projection * glm::lookAt(pos, pos + forward, up);
                }

                void MoveForward(){
                pos += forward * amt;
                }

                void MoveBackward(){
                pos += forward * -amt;
                }

                void MoveRight(){
                pos += -glm::cross(up, forward) * amt;
                }

                void MoveLeft(){
                pos += glm::cross(up, forward) * amt;
                }

                void MoveUp(){
                pos += glm::vec3(0,.2,0);
                }

                void MoveDown(){
                pos -= glm::vec3(0,.2,0);
                }

                void Pitch(float angle){
                glm::vec3 right = glm::normalize(glm::cross(up, forward));

                forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0)));
                up = glm::normalize(glm::cross(forward, right));
                }

                private:
                float amt = .2;
                glm::mat4 projection;
                glm::vec3 pos;
                glm::vec3 forward;
                glm::vec3 up;
};

#endif