-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.h
More file actions
211 lines (181 loc) · 5.02 KB
/
Camera.h
File metadata and controls
211 lines (181 loc) · 5.02 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#pragma once
#include<iostream>
using namespace std;
#include<glew.h>
#include<glfw3.h>
#include<glm.hpp>
#include<vec3.hpp>
#include<mat4x4.hpp>
#include<gtc\matrix_transform.hpp>
enum direction {FORWARD = 0, BACKWARD, LEFT, RIGHT, PASSIVE, UP, DOWN};
enum movement {TERMINAL_VELOCITY = 30};
class Camera
{
private:
glm::mat4 ViewMatrix;
GLfloat lateralMomentum;
GLfloat longitudinalMomentum;
GLfloat verticalMomentum;
GLfloat flatPassiveSlow;
GLfloat percentPassiveSlow;
GLfloat lateralAcceleration;
GLfloat longitudinalAcceleration;
GLfloat verticalAcceleration;
GLfloat sensitivity;
glm::vec3 worldUp;
glm::vec3 position;
glm::vec3 front;
glm::vec3 right;
glm::vec3 up;
GLfloat pitch;
GLfloat yaw;
GLfloat roll;
void updateCameraVectors()
{
this->front.x = cos(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
this->front.y = sin(glm::radians(this->pitch));
this->front.z = sin(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
this->front = glm::normalize(this->front);
this->right = glm::normalize(glm::cross(this->front, this->worldUp));
this->up = glm::normalize(glm::cross(this->right, this->front));
}
public:
Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 worldUp)
{
this->ViewMatrix = glm::mat4(1.f);
this->lateralMomentum = 0.f;
this->longitudinalMomentum = 0.f;
this->verticalMomentum = 0.f;
this->flatPassiveSlow = 0.08f;
this->percentPassiveSlow = 0.01f;
this->lateralAcceleration = 0.5f;
this->longitudinalAcceleration = 0.5f;
this->verticalAcceleration = 0.5f;
this->sensitivity = 1.0f;
this->worldUp = worldUp;
this->position = position;
this->right = glm::vec3(0.f);
this->up = worldUp;
this->pitch = 0.f;
this->yaw = -90.f;
this->roll = 0.f;
this->updateCameraVectors();
}
~Camera() {}
//Accessors
const glm::mat4 getViewMatrix()
{
this->updateCameraVectors();
this->ViewMatrix = glm::lookAt(this->position, this->position + this->front, this->up);
return this->ViewMatrix;
}
const glm::vec3 getPosition()
{
return this->position;
}
//Functions
void move(const float& dt, const int direction)
{
/*cout << "[camera] position: " << glm::to_string(position) << "\n\n";
cout << "front: " << glm::to_string(front) << "\n\n";
cout << "right: " << glm::to_string(right) << "\n\n";
cout << "up: " << glm::to_string(up) << "\n\n\n\n";*/
glm::vec3 position;
glm::vec3 front;
glm::vec3 right;
glm::vec3 up;
//Update position vector
switch (direction)
{
case PASSIVE:
this->position += this->right * this->lateralMomentum * dt;
this->position += this->front * this->longitudinalMomentum * dt;
this->position += this->up * this->verticalMomentum * dt;
if (lateralMomentum > flatPassiveSlow)
{
lateralMomentum *= (1 - percentPassiveSlow);
lateralMomentum -= flatPassiveSlow;
}
else if (lateralMomentum < -flatPassiveSlow)
{
lateralMomentum *= (1 - percentPassiveSlow);
lateralMomentum += flatPassiveSlow;
}
else
{
lateralMomentum = 0;
}
if (longitudinalMomentum > flatPassiveSlow)
{
longitudinalMomentum *= (1 - percentPassiveSlow);
longitudinalMomentum -= flatPassiveSlow;
}
else if (longitudinalMomentum < -flatPassiveSlow)
{
longitudinalMomentum *= (1 - percentPassiveSlow);
longitudinalMomentum += flatPassiveSlow;
}
else
{
longitudinalMomentum = 0;
}
if (verticalMomentum > flatPassiveSlow)
{
verticalMomentum *= (1 - percentPassiveSlow);
verticalMomentum -= flatPassiveSlow;
}
else if (verticalMomentum < -flatPassiveSlow)
{
verticalMomentum *= (1 - percentPassiveSlow);
verticalMomentum += flatPassiveSlow;
}
else
{
verticalMomentum = 0;
}
break;
case UP:
if (verticalMomentum < TERMINAL_VELOCITY)
verticalMomentum += verticalAcceleration;
break;
case DOWN:
if (verticalMomentum > -TERMINAL_VELOCITY)
verticalMomentum -= verticalAcceleration * 1.5;
break;
case FORWARD:
if(longitudinalMomentum < TERMINAL_VELOCITY)
longitudinalMomentum += longitudinalAcceleration * 1.5;
break;
case BACKWARD:
if (longitudinalMomentum > -TERMINAL_VELOCITY)
longitudinalMomentum -= longitudinalAcceleration;
break;
case RIGHT:
if (lateralMomentum < TERMINAL_VELOCITY)
lateralMomentum += lateralAcceleration;
break;
case LEFT:
if (lateralMomentum > -TERMINAL_VELOCITY)
lateralMomentum -= lateralAcceleration;
break;
default:
break;
}
}
void updateMouseInput(const float& dt, const double& offsetX, const double& offsetY)
{
//Update pitch yaw and roll
this->pitch += static_cast<GLfloat>(offsetY) * this->sensitivity * dt;
this->yaw += static_cast<GLfloat>(offsetX) * this->sensitivity * dt;
if (this->pitch > 80.f)
this->pitch = 80.f;
else if (this->pitch < -80.f)
this->pitch = -80.f;
if (this->yaw > 360.f || this->yaw < -360.f)
this->yaw = 0.f;
}
void updateInput(const float& dt, const int direction, const double& offsetX, const double& offsetY)
{
this->updateMouseInput(dt, offsetX, offsetY);
}
};