-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
124 lines (97 loc) · 2.56 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
116
117
118
119
120
121
122
123
124
#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 = e.motion.xrel;
int ypos = e.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::computeMatricesFromInputs(){
// glfwGetTime is called only once, the first time this function is called
// Compute time difference between current and last frame
}
void Camera::move(CameraDirection dir)
{
switch (dir)
{
case FORWARD: // Move forward
cout << "w pressed-" << endl;
position += direction * timeDifference * speed;
break;
case BACK: // Move backward
cout << "s pressed-" << endl;
position -= direction * timeDifference * speed;
break;
case RIGHT: // Move right
cout << "d pressed-" << endl;
position += right * timeDifference * speed;
break;
case LEFT: // Move left
cout << "a pressed-" << endl;
position -= right * timeDifference * 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::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;
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
// 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;
verticalAngle = 0.0f;
calcMatrices();
}