/
panoptic.cpp
558 lines (420 loc) · 14.3 KB
/
panoptic.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (C) 2005-2011 Robert Kooima
//
// THUMB is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
#include <SDL_mouse.h>
#include <cmath>
#include <ogl-opengl.hpp>
#include <etc-log.hpp>
#include <etc-socket.hpp>
#include <etc-vector.hpp>
#include <app-data.hpp>
#include <app-host.hpp>
#include <app-conf.hpp>
#include <app-prog.hpp>
#include <app-view.hpp>
#include <app-event.hpp>
#include <app-frustum.hpp>
#include <app-default.hpp>
#include <util3d/math3d.h>
#include <scm-log.hpp>
#include "panoptic.hpp"
//------------------------------------------------------------------------------
panoptic::panoptic(const std::string& exe,
const std::string& tag) : view_app(exe, tag),
demo_turn(0),
demo_dist_delay(0),
demo_turn_delay(0),
report_sock(INVALID_SOCKET)
{
// Initialize all interaction state.
speed_min = ::conf->get_f("panoptic_speed_min", 0.0);
speed_max = ::conf->get_f("panoptic_speed_max", 0.2);
minimum_agl = ::conf->get_f("panoptic_minimum_agl", 50.0);
auto_pitch = ::conf->get_i("panoptic_auto_pitch" , 0);
demo_delay = ::conf->get_i("panoptic_demo_delay" , 0);
// Initialize the reportage socket.
int port = ::conf->get_i("panoptic_report_port", 8111);
std::string host = ::conf->get_s("panoptic_report_host");
if (port && !host.empty())
{
if (init_sockaddr(report_addr, host.c_str(), port))
report_sock = socket(AF_INET, SOCK_DGRAM, 0);
}
}
panoptic::~panoptic()
{
if (report_sock != INVALID_SOCKET)
close(report_sock);
}
// This is a potentially troublesome function that solves a tough problem in
// an unsatisfactory fashion. It determines whether the current scene is or is
// not a panorama.
//
// Currently, a large sphere implies a planet and a small sphere implies a
// panorama. The dividing line is 100 meters.
//
// The mapping of user input onto view state changes depending on the outcome.
bool panoptic::pan_mode() const
{
return (get_minimum_ground() < 100.0);
}
//------------------------------------------------------------------------------
// The report mechanism transmits the current view location to a remote host,
// as configured in options.xml. This allows the creation of an external map
// display showing the user in the context of the globe.
void panoptic::report()
{
// If a report destination has been configured...
if (report_addr.sin_addr.s_addr != INADDR_NONE &&
report_sock != INVALID_SOCKET)
{
// Compute the current longitude, latitude, and altitude.
double p[3], alt = here.get_distance();
here.get_position(p);
double lon = atan2(p[0], p[2]) * 180.0 / M_PI;
double lat = asin(p[1]) * 180.0 / M_PI;
// Encode these to an ASCII string.
char buf[128];
sprintf(buf, "%+12.8f %+13.8f %17.8f\n", lat, lon, alt);
// And send the string to the configured host.
sendto(report_sock, buf, strlen(buf) + 1, 0,
(const sockaddr *) &report_addr, sizeof (sockaddr_in));
}
}
//------------------------------------------------------------------------------
ogl::aabb panoptic::prep(int frusc, const app::frustum *const *frusv)
{
ogl::aabb aabb = view_app::prep(frusc, frusv);
report();
if (pan_mode())
return aabb;
else
{
// Compute a horizon line based on altitude and minimum terrain height.
const double k = get_scale();
const double r = k * get_current_ground();
const double m = k * get_minimum_ground();
const double d = k * here.get_distance();
double n = 0.5 * (d - r );
double f = 1.5 * sqrt(d * d - m * m);
// Exploit an AABB special case to transmit near and far directly.
return ogl::aabb(vec3(0, 0, n), vec3(0, 0, f));
}
}
void panoptic::draw(int frusi, const app::frustum *frusp, int chani)
{
mat4 M = ::view->get_transform();
// Set the label clipping plane.
const double m = get_minimum_ground();
const double d = here.get_distance();
double C[4] = { 0.0, 0.0, 1.0, 0.0 };
here.get_position(C);
C[3] = -m * m / d;
glLoadMatrixd(transpose(M));
glClipPlane(GL_CLIP_PLANE0, C);
// Set the light position.
double l[3];
GLfloat L[4];
here.get_light(l);
L[0] = GLfloat(l[0]);
L[1] = GLfloat(l[1]);
L[2] = GLfloat(l[2]);
L[3] = 0.0f;
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, L);
view_app::draw(frusi, frusp, chani);
view_app::over(frusi, frusp, chani);
}
//------------------------------------------------------------------------------
// Return an altitude scalar.
double panoptic::get_speed() const
{
const double d = here.get_distance();
const double h = get_current_ground();
const double k = (d - h) / h;
if (k > speed_max) return speed_max;
if (k < speed_min) return speed_min;
return k;
}
// Return a scaling value that gives an appropriately stereoscopic view
// of the planet from the current altitude.
double panoptic::get_scale() const
{
if (pan_mode())
return 1.0;
else
{
const double s = ::host->get_distance();
const double d = here.get_distance();
const double h = get_current_ground();
const double k = std::min(1.0, s / (d - h));
return k;
}
}
// Set the pitch of the viewer appropriately for the current altitude.
void panoptic::set_pitch(scm_state& s) const
{
const double d = s.get_distance();
const double h = s.get_minimum_ground();
const double a = (d - h) / h;
s.set_pitch(-M_PI_2 * mix(std::min(1.0, pow(a, 0.4)), 1.0, a));
}
//------------------------------------------------------------------------------
quat panoptic::get_local() const
{
const vec3 p(view_app::get_position());
const mat3 R(view_app::get_orientation());
vec3 y = normal(p);
vec3 x;
vec3 z;
if (fabs(xvector(R) * y) < fabs(zvector(R) * y))
{
x = normal(xvector(R));
z = normal(cross(x, y));
x = normal(cross(y, z));
}
else
{
z = normal(zvector(R));
x = normal(cross(y, z));
z = normal(cross(x, y));
}
return quat(mat3(x, y, z));
}
quat panoptic::get_orientation() const
{
if (pan_mode())
return view_app::get_orientation();
else
return inverse(get_local()) * view_app::get_orientation();
}
void panoptic::set_orientation(const quat &q)
{
if (pan_mode())
here.set_orientation(q);
else
here.set_orientation(get_local() * q);
}
void panoptic::offset_position(const vec3 &d)
{
if (pan_mode())
view_app::offset_position(d);
else
{
// Apply the motion vector as rotation of the step.
const double k = 500000.0 * get_speed();
const double r = here.get_distance();
const mat3 B(get_local());
mat4 zM = mat4(mat3(quat(zvector(B), atan2(-d[0] * k, r))));
mat4 xM = mat4(mat3(quat(xvector(B), atan2( d[2] * k, r))));
if (0) // HACK: Light transformation temporarily disabled.
{
here.transform_light (transpose(xM));
here.transform_light (transpose(zM));
}
else
{
here.transform_orientation(transpose(xM));
here.transform_position (transpose(xM));
here.transform_light (transpose(xM));
here.transform_orientation(transpose(zM));
here.transform_position (transpose(zM));
here.transform_light (transpose(zM));
// Clamp the altitude.
here.set_distance(std::max(d[1] * k + r,
minimum_agl + here.get_current_ground()));
}
// Set the automatic view pitch if requested.
if (auto_pitch) set_pitch(here);
}
}
//------------------------------------------------------------------------------
// Compute the length of the Archimedean spiral with polar equation r = a theta.
double arclen(double a, double theta)
{
double d = sqrt(1 + theta * theta);
return a * (theta * d + log(theta + d)) / 2.0;
}
// Calculate the length of the arc of length theta along an Archimedean spiral
// that begins at radius r0 and ends at radius r1.
double spiral(double r0, double r1, double theta)
{
double dr = fabs(r1 - r0);
if (theta > 0.0)
{
if (dr > 0.0)
{
double a = dr / theta;
return fabs(arclen(a, r1 / a) - arclen(a, r0 / a));
}
return theta * r0;
}
return dr;
}
void panoptic::jump_to(int i)
{
view_app::jump_to(i);
}
void panoptic::fade_to(int i)
{
view_app::fade_to(i);
#if 0
bool before = pan_mode();
view_app::fade_to(i);
bool after = pan_mode();
if (before != after)
view_app::jump_to(i);
#endif
}
void panoptic::move_to(int i)
{
// Construct a path from here to there.
if (!play && 0 <= i && i < max_location && !location[i].empty())
{
// Set the location and destination. Cycle the selected location queue.
scm_state src = here;
scm_state dst = location[i].front();
location[i].pop_front();
location[i].push_back(dst);
// Determine the beginning and ending positions and altitudes.
double g0 = src.get_current_ground();
double g1 = dst.get_current_ground();
double d0 = src.get_distance();
double d1 = dst.get_distance();
double p0[3];
double p1[3];
src.get_position(p0);
dst.get_position(p1);
// Compute the ground trace length and orbit length.
double a = acos(vdot(p0, p1));
double lg = spiral(g0, g1, a);
double lo = spiral(d0, d1, a);
// Calculate a "hump" for a low orbit path.
double aa = std::min(d0 - g0, d1 - g1);
double dd = lg ? log10(lg / aa) * lg / 10 : 0;
// Enqueue the path.
sequence.clear();
if (lo > 0)
{
for (double t = 0.0; t < 1.0; )
{
double dt = 0.01;
double q = 4 * t - 4 * t * t;
// Estimate the current velocity.
scm_state t0(src, dst, t);
scm_state t1(src, dst, t + dt);
t0.set_distance(t0.get_distance() + dd * q);
t1.set_distance(t1.get_distance() + dd * q);
// Queue this step.
if (auto_pitch) set_pitch(t0);
sequence.push_back(t0);
// Move forward at a velocity appropriate for the altitude.
double g = t0.get_minimum_ground();
t += 2 * (t0.get_distance() - g) * dt * dt / (t1 - t0);
}
}
sequence.push_back(dst);
// Program the scene fade.
const int n = int(sequence.size());
for (int i = 0; i <= n - 2; i++)
sequence[i].set_fade(hermite(double(i) / double(n - 2)));
// Trigger playback.
play_path(false);
}
}
//------------------------------------------------------------------------------
bool panoptic::process_tick(app::event *E)
{
double t = ::host->get_time_since_event() - demo_delay;
double dt = E->data.tick.dt;
view_app::process_tick(E);
// If the demo delay timer has expired, demo.
if (demo_delay > 0 && t > 0)
{
// If the demo distance timer has expired, choose a new distance.
if (demo_dist_delay <= 0)
{
const double h = here.get_distance();
const double g = get_minimum_ground();
demo_dist_delay = mix(40, 60, drand48());
demo_dist_T = mix(10, 20, drand48());
demo_dist_t = 0;
demo_dist_0 = h;
demo_dist_1 = mix(g * 1.05, g * 2.5, pow(drand48(), 2.0));
}
// If the demo turn timer has expired, choose a new turning radius.
if (demo_turn_delay <= 0)
{
demo_turn_delay = mix(10, 30, drand48());
demo_turn_value = mix(radians(-5), radians(+5), drand48());
}
// Set the current move and turn values, filtered.
vec3 d = vec3(0, 0, -1.0);
double a = demo_turn_value;
demo_move = mix(d, demo_move, 0.99);
demo_turn = mix(a, demo_turn, 0.99);
// Apply the move and turn.
set_orientation(quat(vec3(0, 1, 0), demo_turn * dt) * get_orientation());
offset_position(demo_move * dt);
// Set the interpolated distance.
if (demo_dist_t < demo_dist_T)
{
double t = std::min(1.0, demo_dist_t / demo_dist_T);
here.set_distance(mix(demo_dist_0, demo_dist_1,
3 * t * t - 2 * t * t * t));
}
// Handle the timers and delays.
demo_dist_t += dt;
demo_dist_delay -= dt;
demo_turn_delay -= dt;
}
else
{
demo_turn = 0;
demo_move = vec3();
demo_dist_delay = 0;
demo_turn_delay = 0;
}
return false;
}
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
try
{
std::string t(DEFAULT_TAG);
std::string d;
app::prog *P;
for (int i = 1; i < argc; i++)
{
if (std::string(argv[i]) == "-t" && i < argc - 1)
{
t = std::string(argv[i + 1]);
i++;
}
if (std::string(argv[i]) == "-d" && i < argc - 1)
{
d = std::string(argv[i + 1]);
i++;
}
}
P = new panoptic(argv[0], t);
if (d.size()) P->dump(d);
P->run();
delete P;
}
catch (std::exception& e)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Uncaught exception", e.what(), 0);
}
return 0;
}