-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
64 lines (47 loc) · 1.51 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "camera.h"
#include "renderer.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
Camera::Camera(float width, float height) : position(4.0f, 8.0f, 17.0f), horizontalAngle(3.14f), verticalAngle(-0.3f), FoV(45.0f) {
computeMatrices(width, height);
}
void Camera::computeMatrices(float width, float height)
{
glm::vec3 direction(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);
// Right vector
glm::vec3 right = glm::vec3(
sin(horizontalAngle - 3.14f/2.0f),
0,
cos(horizontalAngle - 3.14f/2.0f)
);
// Up vector : perpendicular to both direction and right
glm::vec3 up = glm::cross( right, direction );
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
projectionMatrix = glm::perspective(glm::radians(FoV), width/height, 0.1f, 100.0f);
// Camera matrix
viewMatrix = glm::lookAt(
position, // Camera is here
position+direction, // and looks here : at the same position, plus "direction"
up // Head is up (set to 0,-1,0 to look upside-down)
);
}
void Camera::setFoV(float newFoV)
{
FoV = newFoV;
}
const glm::mat4 &Camera::getViewMatrix() const
{
return viewMatrix;
}
const glm::mat4 &Camera::getProjectionMatrix() const
{
return projectionMatrix;
}
void Camera::Bind(Shader *shader)
{
shader->setUniform3fv("camPosition", position);
}