forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackball.cpp
More file actions
475 lines (375 loc) · 16.2 KB
/
Trackball.cpp
File metadata and controls
475 lines (375 loc) · 16.2 KB
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
/* <editor-fold desc="MIT License">
Copyright(c) 2019 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/io/Options.h>
#include <vsg/viewer/Trackball.h>
#include <iostream>
using namespace vsg;
Trackball::Trackball(ref_ptr<Camera> camera, ref_ptr<EllipsoidModel> ellipsoidModel) :
_camera(camera),
_ellipsoidModel(ellipsoidModel)
{
_lookAt = camera->viewMatrix.cast<LookAt>();
if (!_lookAt)
{
// TODO: need to work out how to map the original ViewMatrix to a LookAt and back, for now just fallback to assigning our own LookAt
_lookAt = new LookAt;
}
clampToGlobe();
addKeyViewpoint(KEY_Space, LookAt::create(*_lookAt), 1.0);
}
void Trackball::clampToGlobe()
{
// std::cout<<"Trackball::clampToGlobe()"<<std::endl;
if (!_ellipsoidModel) return;
// get the location of the current lookAt center
auto location_center = _ellipsoidModel->convertECEFToLatLongAltitude(_lookAt->center);
auto location_eye = _ellipsoidModel->convertECEFToLatLongAltitude(_lookAt->eye);
double ratio = location_eye.z / (location_eye.z - location_center.z);
auto location = _ellipsoidModel->convertECEFToLatLongAltitude(_lookAt->center * ratio + _lookAt->eye * (1.0 - ratio));
// clamp to the globe
location.z = 0.0;
// compute clamped position back in ECEF
auto ecef = _ellipsoidModel->convertLatLongAltitudeToECEF(location);
// apply the new clamped position to the LookAt.
_lookAt->center = ecef;
double minimum_altitude = 0.1;
if (location_eye.z < minimum_altitude)
{
location_eye.z = minimum_altitude;
_lookAt->eye = _ellipsoidModel->convertLatLongAltitudeToECEF(location_eye);
_thrown = false;
}
}
bool Trackball::withinRenderArea(int32_t x, int32_t y) const
{
auto renderArea = _camera->getRenderArea();
return (x >= renderArea.offset.x && x < static_cast<int32_t>(renderArea.offset.x + renderArea.extent.width)) &&
(y >= renderArea.offset.y && y < static_cast<int32_t>(renderArea.offset.y + renderArea.extent.height));
}
/// compute non dimensional window coordinate (-1,1) from event coords
dvec2 Trackball::ndc(PointerEvent& event)
{
auto renderArea = _camera->getRenderArea();
double aspectRatio = static_cast<double>(renderArea.extent.width) / static_cast<double>(renderArea.extent.height);
dvec2 v(
(renderArea.extent.width > 0) ? (static_cast<double>(event.x - renderArea.offset.x) / static_cast<double>(renderArea.extent.width) * 2.0 - 1.0) * aspectRatio : 0.0,
(renderArea.extent.height > 0) ? static_cast<double>(event.y - renderArea.offset.y) / static_cast<double>(renderArea.extent.height) * 2.0 - 1.0 : 0.0);
return v;
}
/// compute trackball coordinate from event coords
dvec3 Trackball::tbc(PointerEvent& event)
{
dvec2 v = ndc(event);
double l = length(v);
if (l < 1.0f)
{
double h = 0.5 + cos(l * PI) * 0.5;
return dvec3(v.x, -v.y, h);
}
else
{
return dvec3(v.x, -v.y, 0.0);
}
}
void Trackball::apply(KeyPressEvent& keyPress)
{
if (keyPress.handled || !_lastPointerEventWithinRenderArea) return;
if (auto itr = keyViewpoitMap.find(keyPress.keyBase); itr != keyViewpoitMap.end())
{
_previousTime = keyPress.time;
setViewpoint(itr->second.lookAt, itr->second.duration);
keyPress.handled = true;
}
}
void Trackball::apply(ButtonPressEvent& buttonPress)
{
if (buttonPress.handled) return;
_hasFocus = withinRenderArea(buttonPress.x, buttonPress.y);
_lastPointerEventWithinRenderArea = _hasFocus;
if (buttonPress.mask & BUTTON_MASK_1)
_updateMode = ROTATE;
else if (buttonPress.mask & BUTTON_MASK_2)
_updateMode = PAN;
else if (buttonPress.mask & BUTTON_MASK_3)
_updateMode = ZOOM;
else
_updateMode = INACTIVE;
if (_hasFocus) buttonPress.handled = true;
_zoomPreviousRatio = 0.0;
_pan.set(0.0, 0.0);
_rotateAngle = 0.0;
_previousPointerEvent = &buttonPress;
}
void Trackball::apply(ButtonReleaseEvent& buttonRelease)
{
if (supportsThrow) _thrown = _previousPointerEvent && (buttonRelease.time == _previousPointerEvent->time);
_lastPointerEventWithinRenderArea = withinRenderArea(buttonRelease.x, buttonRelease.y);
_hasFocus = false;
_previousPointerEvent = &buttonRelease;
}
void Trackball::apply(MoveEvent& moveEvent)
{
_lastPointerEventWithinRenderArea = withinRenderArea(moveEvent.x, moveEvent.y);
if (moveEvent.handled || !_hasFocus) return;
dvec2 new_ndc = ndc(moveEvent);
dvec3 new_tbc = tbc(moveEvent);
if (!_previousPointerEvent) _previousPointerEvent = &moveEvent;
dvec2 prev_ndc = ndc(*_previousPointerEvent);
dvec3 prev_tbc = tbc(*_previousPointerEvent);
#if 1
dvec2 control_ndc = new_ndc;
dvec3 control_tbc = new_tbc;
#else
dvec2 control_ndc = (new_ndc + prev_ndc) * 0.5;
dvec3 control_tbc = (new_tbc + prev_tbc) * 0.5;
#endif
double dt = std::chrono::duration<double, std::chrono::seconds::period>(moveEvent.time - _previousPointerEvent->time).count();
_previousDelta = dt;
double scale = 1.0;
//if (_previousTime > _previousPointerEvent->time) scale = std::chrono::duration<double, std::chrono::seconds::period>(moveEvent.time - _previousTime).count() / dt;
// scale *= 2.0;
_previousTime = moveEvent.time;
if (moveEvent.mask & rotateButtonMask)
{
_updateMode = ROTATE;
moveEvent.handled = true;
dvec3 xp = cross(normalize(control_tbc), normalize(prev_tbc));
double xp_len = length(xp);
if (xp_len > 0.0)
{
_rotateAngle = asin(xp_len);
_rotateAxis = xp / xp_len;
rotate(_rotateAngle * scale, _rotateAxis);
}
else
{
_rotateAngle = 0.0;
}
}
else if (moveEvent.mask & panButtonMask)
{
_updateMode = PAN;
moveEvent.handled = true;
dvec2 delta = control_ndc - prev_ndc;
_pan = delta;
pan(delta * scale);
}
else if (moveEvent.mask & zoomButtonMask)
{
_updateMode = ZOOM;
moveEvent.handled = true;
dvec2 delta = control_ndc - prev_ndc;
if (delta.y != 0.0)
{
_zoomPreviousRatio = zoomScale * 2.0 * delta.y;
zoom(_zoomPreviousRatio * scale);
}
}
_thrown = false;
_previousPointerEvent = &moveEvent;
}
void Trackball::apply(ScrollWheelEvent& scrollWheel)
{
if (scrollWheel.handled || !_lastPointerEventWithinRenderArea) return;
scrollWheel.handled = true;
zoom(scrollWheel.delta.y * 0.1);
}
void Trackball::apply(FrameEvent& frame)
{
if (_endLookAt)
{
double timeSinceOfAnimation = std::chrono::duration<double, std::chrono::seconds::period>(frame.time - _startTime).count();
if (timeSinceOfAnimation < _animationDuration)
{
double r = smoothstep(0.0, 1.0, timeSinceOfAnimation / _animationDuration);
if (_ellipsoidModel)
{
auto interpolate = [](const dvec3& start, const dvec3& end, double ratio) -> dvec3 {
if (ratio >= 1.0) return end;
double length_start = length(start);
double length_end = length(end);
double acos_ratio = dot(start, end) / (length_start * length_end);
double angle = acos_ratio >= 1.0 ? 0.0 : (acos_ratio <= -1.0 ? vsg::PI : acos(acos_ratio));
auto cross_start_end = cross(start, end);
auto length_cross = length(cross_start_end);
if (angle != 0.0 && length_cross != 0.0)
{
cross_start_end /= length_cross;
auto rotation = vsg::rotate(angle * ratio, cross_start_end);
dvec3 new_dir = normalize(rotation * start);
return new_dir * mix(length_start, length_end, ratio);
}
else
{
return mix(start, end, ratio);
}
};
auto interpolate_arc = [](const dvec3& start, const dvec3& end, double ratio, double arc_height = 0.0) -> dvec3 {
if (ratio >= 1.0) return end;
double length_start = length(start);
double length_end = length(end);
double acos_ratio = dot(start, end) / (length_start * length_end);
double angle = acos_ratio >= 1.0 ? 0.0 : (acos_ratio <= -1.0 ? vsg::PI : acos(acos_ratio));
auto cross_start_end = cross(start, end);
auto length_cross = length(cross_start_end);
if (angle != 0.0 && length_cross != 0.0)
{
cross_start_end /= length_cross;
auto rotation = vsg::rotate(angle * ratio, cross_start_end);
dvec3 new_dir = normalize(rotation * start);
double target_length = mix(length_start, length_end, ratio) + (ratio - ratio * ratio) * arc_height * 4.0;
return new_dir * target_length;
}
else
{
return mix(start, end, ratio);
}
};
double length_center_start = length(_startLookAt->center);
double length_center_end = length(_endLookAt->center);
double length_center_mid = (length_center_start + length_center_end) * 0.5;
double distance_between = length(_startLookAt->center - _endLookAt->center);
double transition_length = length_center_mid + distance_between;
double length_eye_start = length(_startLookAt->eye);
double length_eye_end = length(_endLookAt->eye);
double length_eye_mid = (length_eye_start + length_eye_end) * 0.5;
double arc_height = (transition_length > length_eye_mid) ? (transition_length - length_eye_mid) : 0.0;
_lookAt->eye = interpolate_arc(_startLookAt->eye, _endLookAt->eye, r, arc_height);
_lookAt->center = interpolate(_startLookAt->center, _endLookAt->center, r);
_lookAt->up = interpolate(_startLookAt->up, _endLookAt->up, r);
}
else
{
_lookAt->eye = mix(_startLookAt->eye, _endLookAt->eye, r);
_lookAt->center = mix(_startLookAt->center, _endLookAt->center, r);
double angle = acos(dot(_startLookAt->up, _endLookAt->up) / (length(_startLookAt->up) * length(_endLookAt->up)));
if (angle != 0.0)
{
auto rotation = vsg::rotate(angle * r, normalize(cross(_startLookAt->up, _endLookAt->up)));
_lookAt->up = rotation * _startLookAt->up;
}
else
{
_lookAt->up = _endLookAt->up;
}
}
}
else
{
_lookAt->eye = _endLookAt->eye;
_lookAt->center = _endLookAt->center;
_lookAt->up = _endLookAt->up;
_endLookAt = nullptr;
_animationDuration = 0.0;
}
}
else if (_thrown)
{
double scale = _previousDelta > 0.0 ? std::chrono::duration<double, std::chrono::seconds::period>(frame.time - _previousTime).count() / _previousDelta : 0.0;
switch (_updateMode)
{
case (ROTATE):
rotate(_rotateAngle * scale, _rotateAxis);
break;
case (PAN):
pan(_pan * scale);
break;
case (ZOOM):
zoom(_zoomPreviousRatio * scale);
break;
default:
break;
}
}
_previousTime = frame.time;
}
void Trackball::rotate(double angle, const dvec3& axis)
{
dmat4 rotation = vsg::rotate(angle, axis);
dmat4 lv = lookAt(_lookAt->eye, _lookAt->center, _lookAt->up);
dvec3 centerEyeSpace = (lv * _lookAt->center);
dmat4 matrix = inverse(lv) * translate(centerEyeSpace) * rotation * translate(-centerEyeSpace) * lv;
_lookAt->up = normalize(matrix * (_lookAt->eye + _lookAt->up) - matrix * _lookAt->eye);
_lookAt->center = matrix * _lookAt->center;
_lookAt->eye = matrix * _lookAt->eye;
clampToGlobe();
}
void Trackball::zoom(double ratio)
{
dvec3 lookVector = _lookAt->center - _lookAt->eye;
_lookAt->eye = _lookAt->eye + lookVector * ratio;
clampToGlobe();
}
void Trackball::pan(const dvec2& delta)
{
dvec3 lookVector = _lookAt->center - _lookAt->eye;
dvec3 lookNormal = normalize(lookVector);
dvec3 upNormal = _lookAt->up;
dvec3 sideNormal = cross(lookNormal, upNormal);
double distance = length(lookVector);
distance *= 0.25; // TODO use Camera project matrix to guide how much to scale
if (_ellipsoidModel)
{
double scale = distance;
double angle = (length(delta) * scale) / _ellipsoidModel->radiusEquator();
if (angle != 0.0)
{
dvec3 globeNormal = normalize(_lookAt->center);
dvec3 m = upNormal * (-delta.y) + sideNormal * (delta.x); // compute the position relative to the center in the eye plane
dvec3 v = m + lookNormal * dot(m, globeNormal); // compensate for any tile relative to the globe normal
dvec3 axis = normalize(cross(globeNormal, v)); // compute the axis of rotation to map the mouse pan
dmat4 matrix = vsg::rotate(-angle, axis);
_lookAt->up = normalize(matrix * (_lookAt->eye + _lookAt->up) - matrix * _lookAt->eye);
_lookAt->center = matrix * _lookAt->center;
_lookAt->eye = matrix * _lookAt->eye;
clampToGlobe();
}
}
else
{
dvec3 translation = sideNormal * (-delta.x * distance) + upNormal * (delta.y * distance);
_lookAt->eye = _lookAt->eye + translation;
_lookAt->center = _lookAt->center + translation;
}
}
void Trackball::addKeyViewpoint(KeySymbol key, ref_ptr<LookAt> lookAt, double duration)
{
keyViewpoitMap[key].lookAt = lookAt;
keyViewpoitMap[key].duration = duration;
}
void Trackball::addKeyViewpoint(KeySymbol key, double latitude, double longitude, double altitude, double duration)
{
if (!_ellipsoidModel) return;
auto lookAt = LookAt::create();
lookAt->eye = _ellipsoidModel->convertLatLongAltitudeToECEF(dvec3(latitude, longitude, altitude));
lookAt->center = _ellipsoidModel->convertLatLongAltitudeToECEF(dvec3(latitude, longitude, 0.0));
lookAt->up = normalize(cross(lookAt->center, dvec3(-lookAt->center.y, lookAt->center.x, 0.0)));
keyViewpoitMap[key].lookAt = lookAt;
keyViewpoitMap[key].duration = duration;
}
void Trackball::setViewpoint(ref_ptr<LookAt> lookAt, double duration)
{
if (!lookAt) return;
_thrown = false;
if (duration == 0.0)
{
_lookAt->eye = lookAt->eye;
_lookAt->center = lookAt->center;
_lookAt->up = lookAt->up;
_startLookAt = nullptr;
_endLookAt = nullptr;
_animationDuration = 0.0;
clampToGlobe();
}
else
{
_startTime = _previousTime;
_startLookAt = vsg::LookAt::create(*_lookAt);
_endLookAt = lookAt;
_animationDuration = duration;
}
}