-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.cpp
75 lines (61 loc) · 1.92 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
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "camera.h"
#include <algorithm>
Camera::Camera()
{
}
Camera::~Camera()
{
}
bool Camera::Initialize(int w, int h)
{
//--Init the view and projection matrices
// if you will be having a moving camera the view matrix will need to more dynamic
// ...Like you should update it before you render more dynamic
// for this project having them static will be fine
//view = glm::lookAt( glm::vec3(x, y, z), //Eye Position
// glm::vec3(0.0, 0.0, 0.0), //Focus point
// glm::vec3(0.0, 1.0, 0.0)); //Positive Y is up
position = glm::vec3(25, 0, 0);
target = glm::vec3(0);
up = glm::vec3(0, 1, 0);
zoom = 40;
aspect = w / float(h);
return true;
}
void Camera::UpdateVectors(const glm::vec3& offset) {
this->offset = offset;
}
void Camera::Follow(const SceneNode* node)
{
targetNode = const_cast<SceneNode*>(node);
}
void Camera::Zoom(float amount) {
this->zoom = std::max<float>(std::min<float>(this->zoom - amount, 11.f), 8.f);
}
glm::mat4 Camera::GetProjection()
{
return glm::perspective(glm::radians(90.f), //the FoV typically 90 degrees is good which is what this is set to
aspect, //Aspect Ratio, so Circles stay Circular
0.01f, //Distance to the near plane, normally a small value like this
300000.0f); //Distance to the far plane
}
glm::mat4 Camera::GetView()
{
return matrix;
}
void Camera::Update(double dt)
{
if (targetNode != nullptr) {
target = targetNode->getPosition();
up = targetNode->getUpVector();
position = targetNode->getPosition() + offset;
glm::vec3 forward = glm::normalize(target - position);
position += forward * (zoom / 12.f);
}
else {
target = glm::mix(target, t_target, dt * 20);
position = glm::mix(position, t_position, dt * 30);
up = glm::mix(up, t_up, dt * 30);
}
matrix = glm::lookAt(position, target, up);
}