-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.cpp
91 lines (82 loc) · 1.96 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "camera.h"
Camera::Camera(glm::vec3 cameraPosition, glm::vec3 cameraFront, glm::vec3 cameraUp):
mouseSensivity(0.06f),
fov(45.0f),
yaw(-90.0f), pitch(0.0f),
transformMatrix(glm::mat4(1.0f)),
movementSpeed(10.0f)
{
position = cameraPosition;
front = cameraFront;
up = cameraUp;
transformMatrix = glm::lookAt(position, front, up);
this->updateViewSpace();
}
glm::mat4 Camera::getViewMatrix()
{
transformMatrix = glm::lookAt(position, position + front, up);
return transformMatrix;
}
void Camera::handleKeyInput(Directions direction, const GLdouble& dt)
{
if (direction == FORWARD)
{
position += front * movementSpeed * (GLfloat)dt;
}
if (direction == BACKWARD)
{
position -= front * movementSpeed * (GLfloat)dt;
}
if (direction == LEFT)
{
position -= glm::cross(front, up) * movementSpeed * (GLfloat)dt;
}
if (direction == RIGHT)
{
position += glm::cross(front, up) * movementSpeed * (GLfloat)dt;
}
//position.y = 0; // stay on xz plane
}
void Camera::handleMouseInput(double dx, double dy)
{
yaw += dx * mouseSensivity;
pitch += dy * mouseSensivity;
if (pitch > 89.0f)
{
pitch = 89.0f;
}
if (pitch < -89.0f)
{
pitch = -89.0f;
}
this->updateViewSpace();
}
void Camera::handleMouseScrollInput(double xoffset, double yoffset)
{
if (fov >= 1.0f && fov <= 45.0f)
{
fov -= yoffset/20.0f;
}
if (fov <= 1.0f)
{
fov = 1.0f;
}
if (fov >= 45.0f)
{
fov = 45.0f;
}
}
GLfloat Camera::getZOOM() const
{
return fov;
}
void Camera::updateViewSpace()
{
glm::vec3 target;
target.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
target.y = sin(glm::radians(pitch));
target.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
front = glm::normalize(target);
right = glm::normalize(cross(front, up));
up = glm::normalize(cross(right, front));
}