-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
47 lines (38 loc) · 1.37 KB
/
Camera.cpp
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
#include "Camera.h"
#include "IGLSLShader.h"
#include <glm/gtc/matrix_transform.hpp>
#include <cstdio>
void Camera::Init(int width, int height, float fov, float near = 0.01f, float far = 1000.0f)
{
// Initializing camera
this->_perspective = glm::perspective(glm::radians(fov), (float)width/(float)height, near, far);
this->_position = glm::vec3(0.0f, 0.0f, -1.0f);
this->_rotation = glm::vec3(0.0f);
};
void Camera::Update(IGLSLShader& program)
{
// Initializing forward vector
glm::vec3 forward = glm::vec3(
cos(this->_rotation.y) * sin(-this->_rotation.x),
sin(this->_rotation.y),
cos(this->_rotation.y) * cos(-this->_rotation.x)
);
// Initializing right vector
glm::vec3 right = glm::vec3(
sin(-this->_rotation.x - glm::radians(90.0f)),
0,
cos(-this->_rotation.x - glm::radians(90.0f))
);
// Crossing forward and right vectors to initialize up vector
glm::vec3 up = glm::cross(right, forward);
// Initializing view using matrix multiplication
glm::mat4 view =
this->_perspective *
glm::lookAt(
glm::vec3(-this->_position.x, this->_position.y, this->_position.z),
glm::vec3(-this->_position.x, this->_position.y, this->_position.z) + forward,
up
);
// Sending view matrix to GPU
program.UniformMatrix4fv("view", view);
}