From ce8d4651fd0243486f886123b32a1fb91abc025f Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Thu, 8 Jun 2017 15:44:09 +0300 Subject: [glfw] Added extrusions (E) and route (R) shortcuts --- CMakeLists.txt | 1 + cmake/glfw.cmake | 4 ++ platform/glfw/glfw_view.cpp | 102 ++++++++++++++++++++++++++++++++++++++----- platform/glfw/glfw_view.hpp | 7 +++ platform/glfw/ny_route.hpp | 104 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 206 insertions(+), 12 deletions(-) create mode 100644 platform/glfw/ny_route.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8144d880ce..fe657718c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ mason_use(pixelmatch VERSION 0.10.0 HEADER_ONLY) mason_use(geojson VERSION 0.4.0 HEADER_ONLY) mason_use(polylabel VERSION 1.0.2 HEADER_ONLY) mason_use(wagyu VERSION 0.4.1 HEADER_ONLY) +mason_use(cheap-ruler VERSION 2.5.1 HEADER_ONLY) add_definitions(-DRAPIDJSON_HAS_STDSTRING=1) diff --git a/cmake/glfw.cmake b/cmake/glfw.cmake index f1ace9e6ef..744477e39a 100644 --- a/cmake/glfw.cmake +++ b/cmake/glfw.cmake @@ -34,7 +34,11 @@ target_link_libraries(mbgl-glfw PRIVATE mbgl-core ) +target_add_mason_package(mbgl-glfw PRIVATE cheap-ruler) +target_add_mason_package(mbgl-glfw PRIVATE geojson) +target_add_mason_package(mbgl-glfw PRIVATE geometry) target_add_mason_package(mbgl-glfw PRIVATE glfw) +target_add_mason_package(mbgl-glfw PRIVATE variant) mbgl_platform_glfw() diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 31b0b92c58..5abbec8785 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -1,8 +1,10 @@ #include "glfw_view.hpp" +#include "ny_route.hpp" #include #include #include +#include #include #include #include @@ -10,6 +12,10 @@ #include #include +#include +#include +#include + #if MBGL_USE_GLES2 #define GLFW_INCLUDE_ES2 #endif // MBGL_USE_GLES2 @@ -99,7 +105,8 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_) printf("- Press `S` to cycle through bundled styles\n"); printf("- Press `X` to reset the transform\n"); printf("- Press `N` to reset north\n"); - printf("- Press `R` to toggle any available `night` style class\n"); + printf("- Press `R` to enable the route demo\n"); + printf("- Press `E` to insert an example building extrusion layer\n"); printf("- Press `Z` to cycle through north orientations\n"); printf("- Prezz `X` to cycle through the viewport modes\n"); printf("- Press `A` to cycle through Mapbox offices in the world + dateline monument\n"); @@ -148,6 +155,9 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, GLFWView *view = reinterpret_cast(glfwGetWindowUserPointer(window)); if (action == GLFW_RELEASE) { + if (key != GLFW_KEY_R || key != GLFW_KEY_S) + view->animateRouteCallback = nullptr; + switch (key) { case GLFW_KEY_ESCAPE: glfwSetWindowShouldClose(window, true); @@ -163,17 +173,6 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, if (view->changeStyleCallback) view->changeStyleCallback(); break; - case GLFW_KEY_R: - if (!mods) { - static const mbgl::style::TransitionOptions transition { { mbgl::Milliseconds(300) } }; - view->map->setTransitionOptions(transition); - if (view->map->hasClass("night")) { - view->map->removeClass("night"); - } else { - view->map->addClass("night"); - } - } - break; #if not MBGL_USE_GLES2 case GLFW_KEY_B: { auto debug = view->map->getDebug(); @@ -231,6 +230,37 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, view->map->flyTo(cameraOptions, animationOptions); nextPlace = nextPlace % places.size(); } break; + case GLFW_KEY_R: { + view->show3DExtrusions = true; + view->toggle3DExtrusions(view->show3DExtrusions); + if (view->animateRouteCallback) break; + view->animateRouteCallback = [](mbgl::Map* routeMap) { + static mapbox::cheap_ruler::CheapRuler ruler { 40.7 }; // New York + static mapbox::geojson::geojson route { mapbox::geojson::parse(mbgl::platform::glfw::route) }; + const auto& geometry = route.get>(); + const auto& lineString = geometry.get>(); + + static double routeDistance = ruler.lineDistance(lineString); + static double routeProgress = 0; + routeProgress += 0.0005; + if (routeProgress > 1.0) routeProgress = 0; + + double distance = routeProgress * routeDistance; + auto point = ruler.along(lineString, distance); + auto latLng = routeMap->getLatLng(); + routeMap->setLatLng({ point.y, point.x }); + double bearing = ruler.bearing({ latLng.longitude(), latLng.latitude() }, point); + double easing = bearing - routeMap->getBearing(); + easing += easing > 180.0 ? -360.0 : easing < -180 ? 360.0 : 0; + routeMap->setBearing(routeMap->getBearing() + (easing / 20)); + routeMap->setPitch(60.0); + routeMap->setZoom(18.0); + }; + view->animateRouteCallback(view->map); + } break; + case GLFW_KEY_E: + view->toggle3DExtrusions(!view->show3DExtrusions); + break; } } @@ -459,6 +489,9 @@ void GLFWView::run() { if (dirty) { const double started = glfwGetTime(); + if (animateRouteCallback) + animateRouteCallback(map); + activate(); mbgl::BackendScope scope { *this, mbgl::BackendScope::ScopeType::Implicit }; @@ -540,6 +573,51 @@ void GLFWView::setWindowTitle(const std::string& title) { glfwSetWindowTitle(window, (std::string { "Mapbox GL: " } + title).c_str()); } +void GLFWView::onDidFinishLoadingStyle() { + if (show3DExtrusions) { + toggle3DExtrusions(show3DExtrusions); + } +} + +void GLFWView::toggle3DExtrusions(bool visible) { + show3DExtrusions = visible; + + // Satellite-only style does not contain building extrusions data. + if (!map->getSource("composite")) { + return; + } + + if (auto layer = map->getLayer("3d-buildings")) { + layer->setVisibility(mbgl::style::VisibilityType(!show3DExtrusions)); + return; + } + + auto extrusionLayer = std::make_unique("3d-buildings", "composite"); + extrusionLayer->setSourceLayer("building"); + extrusionLayer->setMinZoom(15.0f); + extrusionLayer->setFilter(mbgl::style::EqualsFilter { "extrude", { std::string("true") } }); + + auto colorFn = mbgl::style::SourceFunction { "height", + mbgl::style::ExponentialStops { + std::map { + { 0.f, *mbgl::Color::parse("#160e23") }, + { 50.f, *mbgl::Color::parse("#00615f") }, + { 100.f, *mbgl::Color::parse("#55e9ff") } + } + } + }; + extrusionLayer->setFillExtrusionColor({ colorFn }); + extrusionLayer->setFillExtrusionOpacity({ 0.6f }); + + auto heightSourceFn = mbgl::style::SourceFunction { "height", mbgl::style::IdentityStops() }; + extrusionLayer->setFillExtrusionHeight({ heightSourceFn }); + + auto baseSourceFn = mbgl::style::SourceFunction { "min_height", mbgl::style::IdentityStops() }; + extrusionLayer->setFillExtrusionBase({ baseSourceFn }); + + map->addLayer(std::move(extrusionLayer)); +} + namespace mbgl { namespace platform { diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index 77f4f64b01..f69721cf0b 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -41,6 +41,9 @@ public: void invalidate() override; void updateAssumedState() override; + // mbgl::MapObserver implementation + void onDidFinishLoadingStyle() override; + protected: // mbgl::Backend implementation mbgl::gl::ProcAddress initializeExtension(const char*) override; @@ -77,6 +80,8 @@ private: std::vector spriteIDs; private: + void toggle3DExtrusions(bool visible); + mbgl::Map* map = nullptr; bool fullscreen = false; @@ -84,6 +89,7 @@ private: bool tracking = false; bool rotating = false; bool pitching = false; + bool show3DExtrusions = false; // Frame timer int frames = 0; @@ -102,6 +108,7 @@ private: std::function changeStyleCallback; std::function pauseResumeCallback; + std::function animateRouteCallback; mbgl::util::RunLoop runLoop; mbgl::util::Timer frameTick; diff --git a/platform/glfw/ny_route.hpp b/platform/glfw/ny_route.hpp new file mode 100644 index 0000000000..c3d5157106 --- /dev/null +++ b/platform/glfw/ny_route.hpp @@ -0,0 +1,104 @@ +#include + +namespace mbgl { +namespace platform { +namespace glfw { + +constexpr const char* route = R"route( +{ + "coordinates": [ + [ -74.013841, 40.702449 ], + [ -74.013863, 40.702462 ], + [ -74.013977, 40.702548 ], + [ -74.01404, 40.702595 ], + [ -74.014152, 40.702685 ], + [ -74.014213, 40.702749 ], + [ -74.014284, 40.702835 ], + [ -74.014333, 40.702911 ], + [ -74.014368, 40.702978 ], + [ -74.014407, 40.703066 ], + [ -74.014438, 40.703152 ], + [ -74.014449, 40.703209 ], + [ -74.01445, 40.703263 ], + [ -74.01445, 40.703332 ], + [ -74.014442, 40.703401 ], + [ -74.014404, 40.703614 ], + [ -74.014245, 40.704524 ], + [ -74.01422, 40.704633 ], + [ -74.014329, 40.704667 ], + [ -74.01445, 40.704705 ], + [ -74.014548, 40.704733 ], + [ -74.014641, 40.704756 ], + [ -74.014727, 40.704776 ], + [ -74.014841, 40.704799 ], + [ -74.014977, 40.704827 ], + [ -74.015033, 40.704838 ], + [ -74.015365, 40.704905 ], + [ -74.015454, 40.704921 ], + [ -74.015541, 40.704933 ], + [ -74.015638, 40.704945 ], + [ -74.015699, 40.70495 ], + [ -74.015755, 40.704953 ], + [ -74.01583, 40.704952 ], + [ -74.015909, 40.704949 ], + [ -74.016073, 40.704935 ], + [ -74.016157, 40.704927 ], + [ -74.016224, 40.704921 ], + [ -74.016284, 40.70491 ], + [ -74.016416, 40.704882 ], + [ -74.016424, 40.704918 ], + [ -74.016437, 40.704962 ], + [ -74.016453, 40.705007 ], + [ -74.016462, 40.705041 ], + [ -74.016467, 40.705072 ], + [ -74.016463, 40.705112 ], + [ -74.016457, 40.70515 ], + [ -74.016447, 40.705189 ], + [ -74.016151, 40.705949 ], + [ -74.016121, 40.706032 ], + [ -74.01609, 40.706121 ], + [ -74.01606, 40.706214 ], + [ -74.016037, 40.706296 ], + [ -74.016016, 40.706383 ], + [ -74.016003, 40.70645 ], + [ -74.015986, 40.706549 ], + [ -74.015971, 40.706613 ], + [ -74.015953, 40.706677 ], + [ -74.015888, 40.706844 ], + [ -74.015805, 40.707053 ], + [ -74.015735, 40.707222 ], + [ -74.015697, 40.707307 ], + [ -74.015597, 40.70752 ], + [ -74.015512, 40.707701 ], + [ -74.015476, 40.707784 ], + [ -74.015442, 40.707859 ], + [ -74.015363, 40.708065 ], + [ -74.015197, 40.708495 ], + [ -74.014864, 40.709446 ], + [ -74.01476, 40.709725 ], + [ -74.014744, 40.709777 ], + [ -74.014729, 40.709827 ], + [ -74.01472, 40.709873 ], + [ -74.014712, 40.709925 ], + [ -74.014709, 40.709998 ], + [ -74.014699, 40.710139 ], + [ -74.014689, 40.710215 ], + [ -74.014674, 40.710286 ], + [ -74.014655, 40.710373 ], + [ -74.014631, 40.710477 ], + [ -74.014602, 40.710583 ], + [ -74.014523, 40.710825 ], + [ -74.014492, 40.710899 ], + [ -74.014463, 40.710966 ], + [ -74.014434, 40.711033 ], + [ -74.014406, 40.711098 ], + [ -74.01438, 40.711171 ], + [ -74.01436, 40.71125 ], + [ -74.014147, 40.712245 ] + ], + "type": "LineString" +})route"; + +} // namespace glfw +} // namespace platform +} // namespace mbgl -- cgit v1.2.1