-
Notifications
You must be signed in to change notification settings - Fork 1
/
FirstPersonCamera.cpp
145 lines (120 loc) · 3.09 KB
/
FirstPersonCamera.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "FirstPersonCamera.hpp"
#include "Config.hpp"
#include "WindowBase.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
FirstPersonCamera::FirstPersonCamera() : _position(0.0f, 0.0f, -1.0f),
_front(0.0f, 0.0f, 1.0f), _right(1.0f, 0.0f, 0.0f),
_view(1.0f), _perspective(1.0f), _ypr(0.0f, 0.0f, 0.0f),
_sensitivity(0.01f), _speed(1.0f), _near(DEFAULT_NEAR), _far(DEFAULT_FAR), _fov(DEFAULT_FOV),
_invertUp(false)
{
}
const glm::mat4 &FirstPersonCamera::getView() const
{
return _view;
}
const glm::mat4 &FirstPersonCamera::getPerspective() const
{
return _perspective;
}
void FirstPersonCamera::update(const WindowBase &window)
{
glm::vec3 arbitraryUp = !_invertUp ? glm::vec3(0.0f, 1.0f, 0.0f) : glm::vec3(0.0f, -1.0f, 0.0f);
_front = glm::vec3(cosf(_ypr.y) * sinf(_ypr.x),
sinf(_ypr.y),
cosf(_ypr.y) * cosf(_ypr.x));
_right = glm::normalize(glm::cross(_front, arbitraryUp));
_view = glm::lookAt(_position, _position + _front, arbitraryUp);
float aspect = (float) window.getWidth() / window.getHeight();
_perspective = glm::perspective(_fov, aspect, _near, _far);
}
void FirstPersonCamera::applyMotion(glm::vec3 dir)
{
_position += dir * _speed;
}
void FirstPersonCamera::applyDelta(float dYaw, float dPitch)
{
float modifier = _invertUp ? -1.0f : 1.0f;
_ypr.x -= modifier * dYaw * _sensitivity;
_ypr.y = glm::min(glm::max(_ypr.y - modifier * dPitch * _sensitivity, -0.5f * PI_F + EPSILON), 0.5f * PI_F - EPSILON);
}
void FirstPersonCamera::setSensitivity(float sensitivity)
{
_sensitivity = sensitivity;
}
const glm::vec3 &FirstPersonCamera::getYPR() const
{
return _ypr;
}
void FirstPersonCamera::setYPR(const glm::vec3 &ypr)
{
_ypr = ypr;
}
float FirstPersonCamera::getSpeed() const
{
return _speed;
}
void FirstPersonCamera::setSpeed(float speed)
{
_speed = speed;
}
const glm::vec3 &FirstPersonCamera::getFront() const
{
return _front;
}
const glm::vec3 &FirstPersonCamera::getRight() const
{
return _right;
}
const glm::vec3 &FirstPersonCamera::getPosition() const
{
return _position;
}
void FirstPersonCamera::setPosition(const glm::vec3 &pos)
{
_position = pos;
}
glm::vec2 FirstPersonCamera::getNearFar() const
{
return glm::vec2(_near, _far);
}
void FirstPersonCamera::lookAt(const glm::vec3 &point)
{
glm::vec3 dir = glm::normalize(point - _position);
_ypr.y = asinf(dir.y);
float yaw1 = asinf(dir.x / cosf(_ypr.y));
float yaw2 = acosf(dir.z / cosf(_ypr.y));
if (fabs(yaw1 - yaw2) > EPSILON)
{
_ypr.x += PI_F;
}
// We don't support rolls.
_ypr.z = 0.0f;
}
void FirstPersonCamera::setNearFar(float near, float far)
{
_near = near;
_far = far;
}
float FirstPersonCamera::getFOV() const
{
return _fov;
}
float FirstPersonCamera::getSensitivity() const
{
return _sensitivity;
}
void FirstPersonCamera::setFOV(float fov)
{
_fov = fov;
}
void FirstPersonCamera::setInvertUp(bool invert)
{
_invertUp = invert;
}
bool FirstPersonCamera::getInvertUp() const
{
return _invertUp;
}