-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
99 lines (77 loc) · 2.25 KB
/
Copy pathCamera.cpp
File metadata and controls
99 lines (77 loc) · 2.25 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
#include "Camera.h"
Camera::Camera()
{
}
Camera::Camera(glm::vec3 startPosition, glm::vec3 startUp, GLfloat startYaw, GLfloat startPitch, GLfloat startMoveSpeed, GLfloat startTurnSpeed)
{
position = startPosition;
worldUp = startUp;
yaw = startYaw;
pitch = startPitch;
front = glm::vec3(0.0f, 0.0f, -1.0f);
moveSpeed = startMoveSpeed;
turnSpeed = startTurnSpeed;
update();
}
void Camera::keyControl(bool* keys, GLfloat deltaTime)
{
GLfloat velocity = moveSpeed * deltaTime; //Use GLfloat velocity = moveSpeed * deltaTime for same movement accross all platforms (causes lag)
if (keys[GLFW_KEY_W]) //if W is pressed, you can also use GLFW_KEY_UP
{
position += front * velocity;
}
if (keys[GLFW_KEY_S]) //if W is pressed, you can also use GLFW_KEY_UP
{
position -= front * velocity;
}
if (keys[GLFW_KEY_A]) //if W is pressed, you can also use GLFW_KEY_UP
{
position -= right * velocity;
}
if (keys[GLFW_KEY_D]) //if W is pressed, you can also use GLFW_KEY_UP
{
position += right * velocity;
}
}
void Camera::mouseControl(GLfloat xChange, GLfloat yChange)
{
xChange *= turnSpeed;
yChange *= turnSpeed;
yaw += xChange;
pitch += yChange;
if (pitch > 89.0f)
{
pitch = 89.0f;
}
if (pitch < -89.0f)
{
pitch = -89.0f;
}
update();
}
glm::mat4 Camera::calculateViewMatrix()
{
return glm::lookAt(position, position + front, up); //It will calculate a matrix which looks at something, basically all the rotations and translations from a given position
//position - refers to the position
//position + front = look at everything in front
}
glm::vec3 Camera::getCameraPosition()
{
return position;
}
glm::vec3 Camera::getCameraDirection()
{
return glm::normalize(front); //returns which direction the front of the camera is facing
}
void Camera::update()
{
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(front); //set the magnitude of the vector to unit in this direction, specifying how far we want to go in a particular direction
right = glm::normalize(glm::cross(front, worldUp)); //cross product of front and worldUp
up = glm::normalize(glm::cross(right, front));
}
Camera::~Camera()
{
}