-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
196 lines (183 loc) · 6.12 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "camera.h"
#include "Player.h"
Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch)
: Front(glm::vec3(0.0f, 0.0f, -1.0f)), speed(SPEED), sensitivity(SENSITIVITY)
{
Position = position;
WorldUp = up;
this->yaw = yaw;
this->pitch = pitch;
updateCameraVectors();
}
Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch)
: Front(glm::vec3(0.0f, 0.0f, -1.0f)), speed(SPEED), sensitivity(SENSITIVITY) {
Position = glm::vec3(posX, posY, posZ);
WorldUp = glm::vec3(upX, upY, upZ);
this->yaw = yaw;
this->pitch = pitch;
updateCameraVectors();
}
void Camera::updateCameraVectors() {
glm::vec3 front;
if (!bindToPlayer) {
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);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
else {
front.x = cos(glm::radians(player->yaw)) * cos(glm::radians(player->pitch));
front.y = sin(glm::radians(player->pitch));
front.z = sin(glm::radians(player->yaw)) * cos(glm::radians(player->pitch));
player->Front = glm::normalize(glm::vec3(front.x, 0.0f, front.z));
Front = glm::normalize(front);
player->Right = Right = glm::normalize(glm::cross(player->Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
}
const float cameraA = 20.0f;
void Camera::processKey(cameraMovement key, float deltaTime, float nowTime) {
float moveLen;
if (!bindToPlayer) moveLen = deltaTime * speed;
else moveLen = deltaTime * player->speed;
//cout << "Camera:" << deltaTime << " " << nowTime << endl;
switch (key) {
case FORWARD:
if (!bindToPlayer) Position += Front * moveLen;
else {
//player->position_new += player->Front * moveLen;
player->V += cameraA * deltaTime * player->Front;
}
break;
case BACKWARD:
if (!bindToPlayer) Position -= Front * moveLen;
else {
//player->position_new -= player->Front * moveLen;
player->V -= cameraA * deltaTime * player->Front;
}
break;
case LEFT:
if (!bindToPlayer) Position -= Right * moveLen;
else {
//player->position_new -= player->Right * moveLen;
player->V -= cameraA * deltaTime * Right;
}
break;
case RIGHT:
if (!bindToPlayer) Position += Right * moveLen;
else {
//player->position_new += player->Right * moveLen;
player->V += cameraA * deltaTime * Right;
}
break;
case UP:
if (!bindToPlayer) Position += Up * moveLen;
else player->startJump(nowTime);
break;
case DOWN:
//if (!bindToPlayer) Position -= Up * moveLen;
//else player->dive(nowTime);
break;
case CAMERA_PLAYER:
shooting = false;
if (jitter < 0.3f) break;
bindToPlayer = !bindToPlayer;
player = Player::playerQueue[playerid];
if (bindToPlayer) player->inControl = true;
else player->inControl = false;
updateCameraVectors();
jitter = 0.0f;
break;
case LAST_PLAYER:
shooting = false;
if (jitter < 0.3f) break;
if (bindToPlayer) {
player->inControl = false;
if (--playerid < 0) playerid = (int)Player::playerQueue.size() - 1;
player = Player::playerQueue[playerid];
player->inControl = true;
updateCameraVectors();
}
jitter = 0.0f;
break;
case NEXT_PLAYER:
shooting = false;
if (jitter < 0.3f) break;
if (bindToPlayer) {
player->inControl = false;
if (++playerid >= Player::playerQueue.size()) playerid = 0;
player = Player::playerQueue[playerid];
player->inControl = true;
updateCameraVectors();
}
jitter = 0.0f;
break;
case SHOOT:
if (shootJitter < 0.08f) break;
if (bindToPlayer) {
shooting = true;
if (player->shoot(Front, nowTime)) {
// playShootSound();
}
}
shootJitter = 0.0f;
break;
}
}
void Camera::processMouse(float xOffset, float yOffset, float time) {
xOffset *= sensitivity;
yOffset *= sensitivity;
if (!bindToPlayer) {
yaw += xOffset; pitch += yOffset;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
}
else {
player->yaw_new += xOffset, player->pitch_new += yOffset;
if (player->pitch_new < -50.0f) player->pitch_new = -50.0f;
if (player->pitch_new > 30.0f) player->pitch_new = 30.0f;
}
updateCameraVectors();
}
void Camera::processScroll(float yOffset) {
if (!bindToPlayer) {
zoom -= yOffset;
if (zoom < 20.0f) zoom = 20.0f;
if (zoom > ZOOM) zoom = ZOOM;
}
else {
player->zoom -= yOffset;
if (player->zoom < 20.0f) player->zoom = 20.0f;
if (player->zoom > ZOOM) player->zoom = ZOOM;
}
}
float Camera::getZoom() {
if (bindToPlayer) return player->zoom;
return zoom;
}
glm::vec3 Camera::getPosition() const {
return Position;
}
glm::mat4 Camera::getViewMat() {
if (!bindToPlayer) {
return glm::lookAt(Position, Position + Front, Up);
}
else {
glm::vec3 pos = player->position - Front * 12.0f + glm::vec3(0.0f, 8.0f, 0.0f);
if (pos.y < 0.1f) pos.y = 0.1f;
playerPos = pos;
return glm::lookAt(pos, player->position + Front * 15.0f, Up);
}
}
glm::vec3 Camera::getPlayerPos() {
return playerPos;
}
bool Camera::bind() {
return bindToPlayer;
}
void Camera::invertPitch() {
pitch = -pitch;
updateCameraVectors();
}