Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 7b4a126

Browse files
Add camera demo to the glfw app
1 parent 83c06c1 commit 7b4a126

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

platform/glfw/glfw_view.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <mbgl/style/transition_options.hpp>
2121
#include <mbgl/util/chrono.hpp>
2222
#include <mbgl/util/geo.hpp>
23+
#include <mbgl/util/interpolate.hpp>
2324
#include <mbgl/util/io.hpp>
2425
#include <mbgl/util/logging.hpp>
2526
#include <mbgl/util/platform.hpp>
@@ -206,6 +207,7 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_, const mbgl::ResourceOption
206207
printf("- Press `U` to toggle pitch bounds\n");
207208
printf("- Press `H` to take a snapshot of a current map.\n");
208209
printf("- Press `J` to take a snapshot of a current map with an extrusions overlay.\n");
210+
printf("- Press `Y` to start a camera fly-by demo\n");
209211
printf("\n");
210212
printf("- Press `1` through `6` to add increasing numbers of point annotations for testing\n");
211213
printf("- Press `7` through `0` to add increasing numbers of shape annotations for testing\n");
@@ -487,6 +489,11 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
487489
case GLFW_KEY_G: {
488490
view->toggleLocationIndicatorLayer();
489491
} break;
492+
case GLFW_KEY_Y: {
493+
view->freeCameraDemoPhase = 0;
494+
view->freeCameraDemoStartTime = mbgl::Clock::now();
495+
view->invalidate();
496+
} break;
490497
}
491498
}
492499

@@ -508,6 +515,52 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
508515
}
509516
}
510517

518+
namespace mbgl {
519+
namespace util {
520+
521+
template <>
522+
struct Interpolator<mbgl::LatLng> {
523+
mbgl::LatLng operator()(const mbgl::LatLng &a, const mbgl::LatLng &b, const double t) {
524+
return {
525+
interpolate<double>(a.latitude(), b.latitude(), t),
526+
interpolate<double>(a.longitude(), b.longitude(), t),
527+
};
528+
}
529+
};
530+
531+
} // namespace util
532+
} // namespace mbgl
533+
534+
void GLFWView::updateFreeCameraDemo() {
535+
const mbgl::LatLng trainStartPos = {60.171367, 24.941359};
536+
const mbgl::LatLng trainEndPos = {60.185147, 24.936668};
537+
const mbgl::LatLng cameraStartPos = {60.167443, 24.927176};
538+
const mbgl::LatLng cameraEndPos = {60.185107, 24.933366};
539+
const double cameraStartAlt = 1000.0;
540+
const double cameraEndAlt = 150.0;
541+
const double duration = 8.0;
542+
543+
// Interpolate between starting and ending points
544+
std::chrono::duration<double> deltaTime = mbgl::Clock::now() - freeCameraDemoStartTime;
545+
freeCameraDemoPhase = deltaTime.count() / duration;
546+
547+
auto trainPos = mbgl::util::interpolate(trainStartPos, trainEndPos, freeCameraDemoPhase);
548+
auto cameraPos = mbgl::util::interpolate(cameraStartPos, cameraEndPos, freeCameraDemoPhase);
549+
auto cameraAlt = mbgl::util::interpolate(cameraStartAlt, cameraEndAlt, freeCameraDemoPhase);
550+
551+
mbgl::FreeCameraOptions camera;
552+
553+
// Update camera position and focus point on the map with interpolated values
554+
camera.setLocation({cameraPos, cameraAlt});
555+
camera.lookAtPoint(trainPos);
556+
557+
map->setFreeCameraOptions(camera);
558+
559+
if (freeCameraDemoPhase > 1.0) {
560+
freeCameraDemoPhase = -1.0;
561+
}
562+
}
563+
511564
mbgl::Color GLFWView::makeRandomColor() const {
512565
const float r = 1.0f * float(std::rand()) / float(RAND_MAX);
513566
const float g = 1.0f * float(std::rand()) / float(RAND_MAX);
@@ -860,11 +913,14 @@ void GLFWView::run() {
860913

861914
rendererFrontend->render();
862915

916+
if (freeCameraDemoPhase >= 0.0) {
917+
updateFreeCameraDemo();
918+
}
919+
863920
report(1000 * (glfwGetTime() - started));
864921
if (benchmark) {
865922
invalidate();
866923
}
867-
868924
}
869925
};
870926

platform/glfw/glfw_view.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class GLFWView : public mbgl::MapObserver {
8989
void addRandomShapeAnnotations(int count);
9090
void addRandomCustomPointAnnotations(int count);
9191
void addAnimatedAnnotation();
92+
void updateFreeCameraDemo();
9293
void updateAnimatedAnnotations();
9394
void toggleCustomSource();
9495
void toggleLocationIndicatorLayer();
@@ -114,6 +115,8 @@ class GLFWView : public mbgl::MapObserver {
114115

115116
std::string testDirectory = ".";
116117

118+
double freeCameraDemoPhase = -1;
119+
mbgl::TimePoint freeCameraDemoStartTime;
117120
bool fullscreen = false;
118121
const bool benchmark = false;
119122
bool tracking = false;

0 commit comments

Comments
 (0)