/
camera.cpp
450 lines (403 loc) · 9.88 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <math.h>
#include "stdafx.h"
#include "Camera.h"
//#include "triple.h"
//using namespace std;
#ifndef clamp
#define clamp(x,a,b) ( max( (a), min((b), (x))) )
#endif
Camera::Camera(void) :
EPS(1e-12)
{
lastX = lastY = 0;
mode = 0;
reverse = false;
projo = CAM_PERSPECTIVE;
Xoffset = Yoffset = Zoffset = 0;
rho = 1.0;
scale = 1.0;
lastCamera[X] = lastCamera[Y] = lastCamera[Z] = 0;
upvector[X] = upvector[Y] = upvector[Z] = 0;
upvector[Y] = 1.0;
aspect = 1.0;
spin = 0;
angle = 65;
znear = 0.1;
zfar = 10.0;
input = true;
eyeSeparation = 0.01;
focalRatio = 0.85;
axisflip = true;
theta_shift = phi_shift = 0;
rho_shift = 1.0;
shifting = 0;
}
// Secondary constructor: sets offset/rotation/radius of camera
Camera::Camera(double x, double y, double z, double _rotX, double _rotY, double _rho) :
EPS(1e-12)
{
lastX = lastY = 0;
mode = 0;
reverse = false;
projo = CAM_PERSPECTIVE;
Xoffset = x;
Yoffset = y;
Zoffset = z;
rotX = _rotX;
rotY = _rotY;
rho = _rho;
scale = 1.0;
lastCamera[X] = lastCamera[Y] = lastCamera[Z] = 0;
upvector[X] = upvector[Y] = upvector[Z] = 0;
upvector[Y] = 1.0;
aspect = 1.0;
spin = 0;
angle = 75;
znear = 0.1;
zfar = 10.0;
eyeSeparation = 0.015;
focalRatio = 0.5;
}
Camera::~Camera(void)
{
}
// Initializes the projection and modelview matrices
bool Camera::Draw(int drawmode /* = 0 */)
{
glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
if (shifting > 0)
{
shifting--;
rotX+=theta_shift;
rotY+=phi_shift;
rho*=rho_shift;
if (rotY < EPS) // avoid singularities using EPS constant.
rotY = EPS;
if (rotY > PI-EPS)
rotY = PI-EPS;
if (rho < EPS)
rho = EPS;
}
triple right(0, 0, 0);
triple up(upvector[X], upvector[Y], upvector[Z]);
triple pos(lastCamera[X], lastCamera[Y], lastCamera[Z]);
triple target(Xoffset, Yoffset, Zoffset);
triple dir = (target - pos);
double L, R, B, T, wd2, ndfl, rad = (angle / 360.0) * PI;
double focal = length(dir) * focalRatio; // 4.0
if (projo == CAM_LEFTEYE || projo == CAM_RIGHTEYE)
{
normalize(up);
normalize(dir);
right = crossProduct(dir, up);
normalize(right);
right = right * eyeSeparation / 2.0;
wd2 = znear * tan(rad);
ndfl = znear / focal;
}
switch (projo)
{
case CAM_PERSPECTIVE:
gluPerspective(angle, aspect, znear, zfar);
break;
case CAM_ORTHO:
glOrtho(aspect*-rho, aspect*rho, -rho, rho, -zfar, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
break;
case CAM_ORTHO2: // deprecated
glOrtho(0, 110, -0.5, 107.5, -zfar, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotated(-90, 1, 0, 0);
break;
case CAM_LEFTEYE:
glLoadIdentity();
L = -aspect*wd2 + 0.5*eyeSeparation*ndfl;
R = aspect*wd2 + 0.5*eyeSeparation*ndfl;
T = wd2;
B = -wd2;
glFrustum(L, R, B, T, znear, zfar);
break;
case CAM_RIGHTEYE:
glLoadIdentity();
L = -aspect*wd2 - 0.5*eyeSeparation*ndfl;
R = aspect*wd2 - 0.5*eyeSeparation*ndfl;
T = wd2;
B = -wd2;
glFrustum(L, R, B, T, znear, zfar);
break;
}
if (projo == CAM_LEFTEYE) right = right * -1;
theta = rotX;
phi = rotY;
double sphereX = (rho*sin(phi) * cos(theta));
double sphereY = (rho*cos(phi));
double sphereZ = (rho*sin(phi) * sin(theta));
// from spherical coordinates (rho, theta, phi)
if (input)
{
lastCamera[X] = sphereX + Xoffset;
lastCamera[Y] = sphereY + Yoffset;
lastCamera[Z] = sphereZ + Zoffset;
}
else
{
sphereX = lastCamera[X] - Xoffset;
sphereY = lastCamera[Y] - Yoffset;
sphereZ = lastCamera[Z] - Zoffset;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (!((sphereX == 0) && (sphereZ == 0)))
{
gluLookAt( lastCamera[X]+right.x, lastCamera[Y]+right.y, lastCamera[Z]+right.z,
Xoffset+right.x, Yoffset+right.y, Zoffset+right.z,
0, 1, 0);
//upvector[X], upvector[Y], upvector[Z]);
}
else
{
// gluLookAt doesn't work when viewer is incident to up-vector
// Just orient manually in this case
if ((lastCamera[Y] - Yoffset) >= 0)
{
glRotated(-90, 0, 1, 0);
glRotated(-90, 0, 0, 1);
}
else
{
glRotated(-90, 0, 1, 0);
glRotated(90, 0, 0, 1);
}
glTranslated(-Xoffset, -lastCamera[Y], -Zoffset);
}
if (upvector[X]==1)
glRotatef(90, 0, 0, 1);
if (upvector[Z]==1)
glRotatef(-90, 1, 0, 0);
return true;
}
// Sets X coordinate (horizontal mouse movement)
void Camera::setX(int _X)
{
if (input)
{
switch (mode)
{
case 0:
rotX -= ((lastX - _X) / 100.0);
break;
case 1:
case 2:
Xoffset += cos(rotX+3.14159265358979/2) * scale * (lastX - _X);
Zoffset += sin(rotX+3.14159265358979/2) * scale * (lastX - _X);
break;
case 3:
break;
}
lastX = _X;
}
}
// Sets Y coordinate (vertical mouse movement)
void Camera::setY(int _Y)
{
if (input)
{
switch (mode)
{
case 0:
rotY += (((lastY - _Y)*(axisflip ? 1.0 : -1.0)) / 100.0);
if (rotY < EPS) // avoid singularities using EPS constant.
rotY = EPS;
if (rotY > PI-EPS)
rotY = PI-EPS;
break;
case 1:
Yoffset += scale * (lastY - _Y);
break;
case 2:
Xoffset -= cos(rotX) * scale * (lastY - _Y);
Zoffset -= sin(rotX) * scale * (lastY - _Y);
break;
case 3:
setRadius(fabs(rho * (1.0 + (lastY - _Y) * -.01)));
break;
}
lastY = _Y;
}
}
// Returns the 6 current camera coordinates
void Camera::getCamera(double * temp)
{
temp[0] = lastCamera[0];
temp[1] = lastCamera[1];
temp[2] = lastCamera[2];
temp[3] = Xoffset;
temp[4] = Yoffset;
temp[5] = Zoffset;
}
// Returns 3 camera coordinates (relative position)
void Camera::getQuadrant(double * temp)
{
if (!reverse)
{
temp[0] = lastCamera[0]-Xoffset;
temp[1] = lastCamera[1]-Yoffset;
temp[2] = lastCamera[2]-Zoffset;
}
else
{
temp[0] = lastCamera[0]+Xoffset;
temp[1] = lastCamera[1]+Yoffset;
temp[2] = lastCamera[2]+Zoffset;
}
}
// cycle through projection modes
void Camera::toggleProjection()
{
mode = -1;
projo = (projo+1)%3;
if (projo==CAM_ORTHO)
{
theta = rotX = 3.14159265358979 / 2;
phi = rotY = 3.14159265358979 / 2;
}
if (projo==CAM_ORTHO2)
{
theta = rotX = 3.14159265358979;
phi = rotY = 3.14159265358979 / 2;
}
lastCamera[X] = (rho*sin(phi) * cos(theta)) + Xoffset;
lastCamera[Z] = (rho*sin(phi) * sin(theta)) + Zoffset;
lastCamera[Y] = (rho*cos(phi)) + Yoffset;
}
// Draw a vertex at camera lookAt point
void Camera::drawCenter()
{
glVertex3d(Xoffset, Yoffset, Zoffset);
}
// Set radius of "orbit"
void Camera::setRadius(double r)
{
rho = r;
scale = r / 100.0;
}
// Force camera to look from x/y/z to tx/ty/tz
void Camera::setManualPosition(double x, double y, double z, double tx, double ty, double tz) // specifies manual camera position and lookAt point
{
lastCamera[X] = x;
lastCamera[Y] = y;
lastCamera[Z] = z;
Xoffset = tx;
Yoffset = ty;
Zoffset = tz;
input = false;
}
void Camera::setViewDirection(int axis, double FPS)
{
// used to insure any chained setviews result in "forward" axes (PI/2 or 0)
rotX = fmod(rotX, 2.0*PI);
if (rotX < 0)
rotX += 2.0*PI;
//rotY = fmod(rotY, PI);
if (rotY > PI-EPS) rotY = PI - EPS;
//setOffset(0, 0, 0);
// take a half of a second at most
shifting = clamp((int)(FPS/2.0), 1, 24);
rho_shift = 1.0;
// calculate the distance to shift the view, depending on current angle.
switch (axis)
{
case X:
if (rotX < PI/2)
theta_shift = (0+EPS - rotX) / (double)shifting;
else if (rotX <= PI)
theta_shift = (PI - EPS - rotX) / (double)shifting;
else if (rotX <= 3.0*PI/2.0)
theta_shift = (PI - EPS - rotX) / (double)shifting;
else
theta_shift = (2*PI + EPS - rotX) / (double)shifting;
phi_shift = (PI/2.0 - rotY) / (double)shifting;
break;
case Y:
if (rotX < PI/2)
theta_shift = (0+EPS - rotX) / (double)shifting;
else if (rotX <= PI)
theta_shift = (PI - EPS - rotX) / (double)shifting;
else if (rotX <= 3.0*PI/2.0)
theta_shift = (PI - EPS - rotX) / (double)shifting;
else
theta_shift = (2*PI + EPS - rotX) / (double)shifting;
phi_shift = (0+EPS - rotY) / (double)shifting;
break;
case Z:
if (rotX <= PI)
theta_shift = (PI/2.0 - EPS - rotX) / (double)shifting;
else
theta_shift = (3.0*PI/2.0 + EPS - rotX) / (double)shifting;
phi_shift = (PI/2.0 - rotY) / (double)shifting;
break;
}
}
void Camera::StartPan(double value)
{
shifting = 1000;
theta_shift = value;
phi_shift = 0;
rho_shift = 1.0;
}
void Camera::StartTilt(double value)
{
shifting = 1000;
phi_shift = value;
theta_shift = 0;
rho_shift = 1.0;
}
void Camera::StartZoom(double value)
{
shifting = 1000;
rho_shift = value;
theta_shift = phi_shift = 0;
}
void Camera::StopMotion()
{
shifting = 0;
theta_shift = phi_shift = 0;
rho_shift = 1.0;
}
bool Camera::getFrustumPlanes(triple points[4], triple normals[4])
{
triple right(0, 0, 0);
triple up(upvector[X], upvector[Y], upvector[Z]);
triple pos(lastCamera[X], lastCamera[Y], lastCamera[Z]);
triple target(Xoffset, Yoffset, Zoffset);
triple dir = (target - pos);
double rad = (angle / 360.0) * PI;
normalize(up);
normalize(dir);
right = crossProduct(dir, up);
normalize(right);
triple Y_axis = crossProduct(right, dir).Normalized();
if (projo == CAM_PERSPECTIVE)
{
double near_height = 2.0 * tan(rad) * znear;
double near_width = near_height * aspect;
double far_height = 2.0 * tan(rad) * zfar;
double far_width = far_height * aspect;
triple near_center = dir * znear + pos;
// clockwise from "upper left"
points[0] = near_center-(Y_axis*near_height/2.0)-(right*near_width/2.0);
points[1] = points[0];
points[2] = near_center+(Y_axis*near_height/2.0)+(right*near_width/2.0);
points[3] = points[2];
normals[0] = crossProduct((points[0] - pos).Normalized(), Y_axis).Normalized(); // left
normals[1] = crossProduct((points[1] - pos).Normalized(), right*-1.0).Normalized(); // top
normals[2] = crossProduct((points[2] - pos).Normalized(), Y_axis*-1.0).Normalized(); // right
normals[3] = crossProduct((points[3] - pos).Normalized(), right).Normalized(); // bottom
points[0] = points[1] = points[2] = points[3] = pos;
return true;
}
else return false;
}