From 7b4a126cadbfacccb2de507d6d7cfaf18abf4b30 Mon Sep 17 00:00:00 2001 From: Mikko Pulkki Date: Mon, 27 Apr 2020 13:30:55 +0300 Subject: Add camera demo to the glfw app --- platform/glfw/glfw_view.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++- platform/glfw/glfw_view.hpp | 3 +++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 0ff208586e..2eb98b113d 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -206,6 +207,7 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_, const mbgl::ResourceOption printf("- Press `U` to toggle pitch bounds\n"); printf("- Press `H` to take a snapshot of a current map.\n"); printf("- Press `J` to take a snapshot of a current map with an extrusions overlay.\n"); + printf("- Press `Y` to start a camera fly-by demo\n"); printf("\n"); printf("- Press `1` through `6` to add increasing numbers of point annotations for testing\n"); 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, case GLFW_KEY_G: { view->toggleLocationIndicatorLayer(); } break; + case GLFW_KEY_Y: { + view->freeCameraDemoPhase = 0; + view->freeCameraDemoStartTime = mbgl::Clock::now(); + view->invalidate(); + } break; } } @@ -508,6 +515,52 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, } } +namespace mbgl { +namespace util { + +template <> +struct Interpolator { + mbgl::LatLng operator()(const mbgl::LatLng &a, const mbgl::LatLng &b, const double t) { + return { + interpolate(a.latitude(), b.latitude(), t), + interpolate(a.longitude(), b.longitude(), t), + }; + } +}; + +} // namespace util +} // namespace mbgl + +void GLFWView::updateFreeCameraDemo() { + const mbgl::LatLng trainStartPos = {60.171367, 24.941359}; + const mbgl::LatLng trainEndPos = {60.185147, 24.936668}; + const mbgl::LatLng cameraStartPos = {60.167443, 24.927176}; + const mbgl::LatLng cameraEndPos = {60.185107, 24.933366}; + const double cameraStartAlt = 1000.0; + const double cameraEndAlt = 150.0; + const double duration = 8.0; + + // Interpolate between starting and ending points + std::chrono::duration deltaTime = mbgl::Clock::now() - freeCameraDemoStartTime; + freeCameraDemoPhase = deltaTime.count() / duration; + + auto trainPos = mbgl::util::interpolate(trainStartPos, trainEndPos, freeCameraDemoPhase); + auto cameraPos = mbgl::util::interpolate(cameraStartPos, cameraEndPos, freeCameraDemoPhase); + auto cameraAlt = mbgl::util::interpolate(cameraStartAlt, cameraEndAlt, freeCameraDemoPhase); + + mbgl::FreeCameraOptions camera; + + // Update camera position and focus point on the map with interpolated values + camera.setLocation({cameraPos, cameraAlt}); + camera.lookAtPoint(trainPos); + + map->setFreeCameraOptions(camera); + + if (freeCameraDemoPhase > 1.0) { + freeCameraDemoPhase = -1.0; + } +} + mbgl::Color GLFWView::makeRandomColor() const { const float r = 1.0f * float(std::rand()) / float(RAND_MAX); const float g = 1.0f * float(std::rand()) / float(RAND_MAX); @@ -860,11 +913,14 @@ void GLFWView::run() { rendererFrontend->render(); + if (freeCameraDemoPhase >= 0.0) { + updateFreeCameraDemo(); + } + report(1000 * (glfwGetTime() - started)); if (benchmark) { invalidate(); } - } }; diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index 73c274e4fc..918f5fb513 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -89,6 +89,7 @@ private: void addRandomShapeAnnotations(int count); void addRandomCustomPointAnnotations(int count); void addAnimatedAnnotation(); + void updateFreeCameraDemo(); void updateAnimatedAnnotations(); void toggleCustomSource(); void toggleLocationIndicatorLayer(); @@ -114,6 +115,8 @@ private: std::string testDirectory = "."; + double freeCameraDemoPhase = -1; + mbgl::TimePoint freeCameraDemoStartTime; bool fullscreen = false; const bool benchmark = false; bool tracking = false; -- cgit v1.2.1