-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
112 lines (93 loc) · 2.59 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "Camera.h"
//Constructor
Camera::Camera(glm::vec3 InitialPosition, GLfloat InitialYaw, GLfloat InitialPitch, GLfloat InitialMovementSpeed, GLfloat InitialTurnSpeed)
//Initialzer list
:Position{ InitialPosition }, Front{ glm::vec3(0.f,0.f,-1.f) }, Yaw{ InitialYaw }, Pitch{ InitialPitch }, MovementSpeed{ InitialMovementSpeed }, TurnSpeed{ InitialTurnSpeed }, IsSpotLightOn{true}
{
Update();
}
//Handles key input and movement control
void Camera::KeyControl(bool* KeyArray, double DeltaTime)
{
//GLFW_KEY_ is a preprocessor directive which substitutes the directive with the ASCII value of the key.
//GLFW_KEY_W will be replaced by 87
float Velocity = (float)(MovementSpeed * DeltaTime);
//Basically add velocity in the direction of the front vector to move forward.
//To move backward added velocity in the direction opposite to front vector
if (KeyArray[GLFW_KEY_W])
{
Position += (Front * Velocity);
}
if (KeyArray[GLFW_KEY_S])
{
Position -= (Front * Velocity);
}
//Basically add velocity in the direction of the right vector to move right.
//To move left added velocity in the direction opposite to right vector
if (KeyArray[GLFW_KEY_D])
{
Position += (Right * Velocity);
}
if (KeyArray[GLFW_KEY_A])
{
Position -= (Right * Velocity);
}
//Vertical movement
if (KeyArray[GLFW_KEY_Q])
{
Position = Position + (WorldUp * Velocity);
}
if (KeyArray[GLFW_KEY_E])
{
Position = Position - (WorldUp * Velocity);
}
if (KeyArray[GLFW_KEY_F])
{
IsSpotLightOn = !IsSpotLightOn;
}
}
//Handles mouse input
void Camera::MouseControl(double MouseX, double MouseY, double DeltaTime)
{
//Magic numbers are bad but couldnt help it with this one.
//Delta time *2 with a turn speed of 40.0 , DeltaTime*2 gives the optimal rate of turn
double MovementSpeed = TurnSpeed * DeltaTime;
MouseX *= MovementSpeed;
MouseY *= MovementSpeed;
Yaw += MouseX;
Pitch += MouseY;
if (Pitch > 89.9f)
{
Pitch = 89.9f;
}
else if (Pitch < -89.9f)
{
Pitch = -89.9f;
}
Update();
}
glm::mat4 Camera::CalculateCameraMatrix()
{
return glm::lookAt(Position, Position + Front, Up);
}
glm::vec3 Camera::GetCameraPosition()
{
return Position;
}
glm::vec3 Camera::GetCameraDirection()
{
return Front;
}
bool Camera::GetFlashLightStatus()
{
return IsSpotLightOn;
}
void Camera::Update()
{
Front.x = (float)(cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)));
Front.y = (float)sin(glm::radians(Pitch));
Front.z = (float)(sin(glm::radians(Yaw)) * cos(glm::radians(Pitch)));
Front = glm::normalize(Front);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}