-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
250 lines (211 loc) · 5.82 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "Camera.hpp"
#include <iostream>
using namespace std;
Camera::Camera()
{
loc = glm::vec4(3.0, 3.0, 3.0, 0.0);
focus = glm::vec4(0.0, 0.0, 0.0, 0.0);
up = glm::vec4(-1.0, -1.0, 1.0, 0.0);
dir = focus - loc;
far = 250.0;
near = 0.1;
mode = CAM_ORBIT;
speed = 0.5;
mb = -1;
}
void Camera::rot_mouse(int x, int y)
{
// update FPS mode direction vector to prevent 'jumping'
// at camera mode change
dir = glm::normalize(focus - loc);
// find displacement from point where mouse was pressed
float dx = x - oldMouseX;
float dy = y - oldMouseY;
float xrad = -5e-3 * dx;
float yrad = -5e-3 * dy;
// GLUT_LEFT_BUTTON = 0
// GLUT_RIGHT_BUTTON = 2
// shift + left = 3
if (mb == 0)
{
// vertical rotation
glm::vec3 w = glm::normalize(glm::vec3(loc-focus));
glm::vec3 v = glm::normalize(glm::vec3(up));
glm::vec3 u = glm::cross(v, w);
glm::mat4 R_v = Transform::rotate(u, yrad);
// horizontal rotation
glm::mat4 R_h = Transform::rotate(glm::vec3(0.0, 0.0, 1.0), xrad);
// compose two rotations and rotate location and up vectors
glm::mat4 R = R_h*R_v;
loc = focus + R*(loc-focus);
up = R*up;
}
else if (mb == 2)
{
// amount of scaling is relative to distance from stored point
// zoom in if mouse moved upwards, zoom out if mouse moved downwards
float zoomfactor = 0.005*sqrt(pow(dx, 2) + pow(dy, 2));
if (dy > 0)
{
loc += -(loc-focus)*zoomfactor;
}
if (dy < 0)
{
loc += (loc-focus)*zoomfactor;
}
}
else if (mb == 3)
{
// figure out unit vectors towards right and up
glm::vec3 up3 = glm::normalize(glm::vec3(up));
glm::vec3 right = glm::normalize(glm::cross(glm::vec3(focus-loc), up3));
dx *= -0.01;
dy *= 0.01;
// scale right and up vectors by mouse displacement in proper direction
glm::vec3 T3 = dx*right + dy*up3;
focus += glm::vec4(T3, 0.0);
loc += glm::vec4(T3, 0.0);
}
update();
}
void Camera::rot_fps(int x, int y)
{
int dx = x - oldMouseX;
int dy = y - oldMouseY;
float xrad = -1e-2 * dx;
float yrad = 1e-2 * dy;
// vertical rotation
glm::vec3 w = glm::normalize(glm::vec3(dir.x, dir.y, dir.z));
glm::vec3 v = glm::normalize(glm::vec3(up.x, up.y, up.z));
glm::vec3 u = glm::cross(v, w);
glm::mat4 R_v = Transform::rotate(glm::vec3(u.x, u.y, u.z), yrad);
// horizontal rotation
glm::mat4 R_h = Transform::rotate(glm::vec3(0.0, 0.0, 1.0), xrad);
glm::mat4 R = R_h*R_v;
dir = R*dir;
up = R*up;
update();
}
void Camera::update()
{
// calculate new focus before (for FPS movement only)
if (mode == CAM_FPS)
{
focus = loc + dir;
}
// reload projection and modelview matrices after changes in
// camera positioning
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)screenWidth/(float)screenHeight,
near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// the proper target (centerx, centery, centerz)
// depend on camera mode
if (mode == CAM_ORBIT)
{
gluLookAt(loc.x, loc.y, loc.z,
focus.x, focus.y, focus.z,
up.x, up.y, up.z);
}
else if (mode == CAM_FPS)
{
gluLookAt(loc.x, loc.y, loc.z,
loc.x+dir.x, loc.y+dir.y, loc.z+dir.z,
up.x, up.y, up.z);
}
}
void Camera::storeMouseLoc(int x, int y, int button)
{
this->oldMouseX = x;
this->oldMouseY = y;
this->mb = button;
}
void Camera::storeScreenSize(int w, int h)
{
screenWidth = w;
screenHeight = h;
}
void Camera::toggleMode()
{
// set focus accordingly
focus = loc + dir;
if (mode == CAM_ORBIT)
{
cout << "Camera mode: FPS" << endl;
mode = CAM_FPS;
}
else
{
cout << "Camera mode: Orbit" << endl;
mode = CAM_ORBIT;
focus = loc + dir;
// make sure that up vector points upwards
glm::vec3 z = glm::vec3(0.0, 0.0, 1.0);
glm::vec3 w = glm::normalize(glm::vec3(focus-loc));
glm::vec3 u = glm::normalize(glm::cross(z, w));
glm::vec3 up3 = glm::cross(w, u);
up = glm::vec4(up3.x, up3.y, up3.z, 0.0);
// if loc is along Z axis, the result will be zero vector.
// In this case, set up to -X (-1.0, 0.0, 0.0, 0.0)
if(glm::length(up3) <= 0.01)
{
up = glm::vec4(-1.0, 0.0, 0.0, 0.0);
}
}
update();
}
int Camera::getMode()
{
return mode;
}
void Camera::mov_forward(float amount)
{
loc += glm::normalize(dir)*amount*speed;
update();
}
void Camera::mov_upward(float amount)
{
loc += glm::vec4(0.0, 0.0, 1.0, 0.0)*amount*speed;
update();
}
void Camera::mov_sideways(float amount)
{
// find sideways vector
glm::vec3 z = glm::vec3(0.0, 0.0, 1.0);
glm::vec3 w = glm::vec3(loc-focus);
glm::vec3 right3 = glm::normalize(glm::cross(z, w));
glm::vec4 right4 = glm::vec4(right3.x, right3.y, right3.z, 0.0);
loc += right4*amount*speed;
update();
}
void Camera::changeSpeed(float amount)
{
speed += amount;
if (speed <= 0.1)
{
speed = 0.1;
}
cout << "Camera speed changed to " << speed << endl;
}
int Camera::getMouseButton()
{
return mb;
}
glm::mat4 Camera::getModelview()
{
if (mode == CAM_ORBIT)
{
return glm::lookAt(vec3(loc), vec3(focus), vec3(up));
}
else if (mode == CAM_FPS)
{
return glm::lookAt(vec3(loc), vec3(focus+dir), vec3(up));
}
}
glm::mat4 Camera::getProjection()
{
float rad = 60.0*M_PI/180;
gluPerspective(rad, (float)screenWidth/(float)screenHeight, near, far);
}