-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
98 lines (80 loc) · 1.93 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
#include "Camera.h"
Camera::Camera(unsigned int Width, unsigned int Height, GLFWwindow* Window)
{
CPosition = glm::vec3 (0.0f, 0.0f, -3.0f);
CDirection = glm::vec3 (0.0f, 0.0f, 0.0f);
FoV = 65.f;
Projection = glm::perspective (FoV, (float)Width / (float)Height, 0.1f, 1000.f);
View = glm::lookAt (
CPosition,
CDirection,
glm::vec3 (0.0f, 1.0f, 0.0f));
CameraSpeed = 500.f;
MaxAngle = 0.89f;
MouseSpeed = 0.003f;
glfwGetCursorPos (Window, &MouseX, &MouseY);
}
void Camera::Tick (float dt, GLFWwindow* Window)
{
LastX = MouseX; LastY = MouseY;
glfwGetCursorPos (Window, &MouseX, &MouseY);
MouseDelta.x += MouseSpeed * (LastX - MouseX);
MouseDelta.y += MouseSpeed * (LastY - MouseY);
CDirection = glm::vec3 (
cos (MouseDelta.y) * sin (MouseDelta.x),
sin (MouseDelta.y),
cos (MouseDelta.y) * cos (MouseDelta.x)
);
CRight = glm::vec3 (
sin (MouseDelta.x - 3.14f/2.0f),
0,
cos (MouseDelta.x - 3.14/2.0f)
);
CUp = glm::cross (CRight, CDirection);
if (glfwGetKey (Window, GLFW_KEY_W))
CPosition += CDirection * dt * CameraSpeed;
if (glfwGetKey (Window, GLFW_KEY_S))
CPosition -= CDirection * dt * CameraSpeed;
if (glfwGetKey (Window, GLFW_KEY_D))
CPosition += CRight * dt * CameraSpeed;
if (glfwGetKey (Window, GLFW_KEY_A))
CPosition -= CRight * dt * CameraSpeed;
View = glm::lookAt (
CPosition,
CPosition + CDirection,
CUp
);
View = glm::rotate (View, 270.f, glm::vec3 (1, 0, 0));
}
glm::mat4 Camera::GetMVP (glm::mat4 model)
{
return Projection * View * model;
}
glm::mat4 Camera::GetProjection ()
{
return Projection;
}
glm::mat4 Camera::GetView()
{
return View;
}
void Camera::SetFoV(float fov)
{
FoV = fov;
}
float Camera::GetFoV()
{
return FoV;
}
void Camera::SetCameraSpeed (float speed)
{
CameraSpeed = speed;
}
float Camera::GetCameraSpeed ()
{
return CameraSpeed;
}
glm::vec3 Camera::GetCameraPosition()
{
return CPosition;
}