-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcamera.cpp
115 lines (91 loc) · 2.25 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
113
114
115
#include "camera.h"
glm::mat4 Camera::getViewMatrix(){
return ViewMatrix;
}
glm::mat4 Camera::getProjectionMatrix(){
return ProjectionMatrix;
}
void Camera::rotate()
{
static double lastTime = SDL_GetTicks();
double currentTime = SDL_GetTicks();
float deltaTime = float(currentTime - lastTime);
timeDifference = deltaTime;
// Get mouse position
int xpos = event.motion.xrel;
int ypos = event.motion.yrel;
// Compute new orientation
horizontalAngle += mouseSpeed * float(/*(1024 / 2) -*/ xpos);
verticalAngle += mouseSpeed * float(/*(768 / 2) -*/ -ypos);
// For the next frame, the "last time" will be "now"
lastTime = currentTime;
}
void Camera::move(CameraDirection dir)
{
switch (dir)
{
case FORWARD: // Move forward
position += direction * speed;
break;
case BACK: // Move backward
position -= direction * speed;
break;
case RIGHT: // Move right
position += right * speed;
break;
case LEFT: // Move left
position -= right * speed;
break;
case UP: // Look up
verticalAngle += 0.1;
break;
case DOWN:
verticalAngle -= 0.1;
break;
case ROT_LEFT:
horizontalAngle -= 0.1;
break;
case ROT_RIGHT:
horizontalAngle += 0.1;
break;
}
}
void Camera::setPosition(glm::vec3 pos)
{
position = InitialPos = pos;
}
void Camera::setProjectionMatrix(glm::mat4 proj)
{
ProjectionMatrix = proj;
}
void Camera::calcMatrices()
{
// Direction : Spherical coordinates to Cartesian coordinates conversion
direction = glm::vec3(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);
// Right vector
right = glm::vec3(
sin(horizontalAngle - 3.14f / 2.0f),
0,
cos(horizontalAngle - 3.14f / 2.0f)
);
// Up vector
up = glm::cross(right, direction);
float FoV = initialFoV;
// Camera matrix
ViewMatrix = glm::lookAt(
position, // Camera is here
position + direction, // and looks here : at the same position, plus "direction"
up // Head is up (set to 0,-1,0 to look upside-down)
);
}
void Camera::reset()
{
position = InitialPos;
horizontalAngle = 3.14f;//0.0f;//
verticalAngle = 3.14f;//0.0f;
calcMatrices();
}