[glfw] Added extrusions (E) and route (R) shortcuts

This commit is contained in:
Bruno de Oliveira Abinader
2017-06-08 15:44:09 +03:00
parent 5e0018cdc3
commit 12279b5dcc
5 changed files with 206 additions and 1 deletions

View File

@@ -57,6 +57,7 @@ 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(shelf-pack VERSION 2.1.0 HEADER_ONLY)
mason_use(cheap-ruler VERSION 2.5.1 HEADER_ONLY)
add_definitions(-DRAPIDJSON_HAS_STDSTRING=1)

View File

@@ -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()

View File

@@ -1,8 +1,10 @@
#include "glfw_view.hpp"
#include "ny_route.hpp"
#include <mbgl/annotation/annotation.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/layers/fill_extrusion_layer.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/string.hpp>
@@ -10,6 +12,10 @@
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/map/camera.hpp>
#include <mapbox/cheap_ruler.hpp>
#include <mapbox/geometry.hpp>
#include <mapbox/geojson.hpp>
#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,
auto *view = reinterpret_cast<GLFWView *>(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);
@@ -220,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<mapbox::geometry::geometry<double>>();
const auto& lineString = geometry.get<mapbox::geometry::line_string<double>>();
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;
}
}
@@ -450,6 +491,9 @@ void GLFWView::run() {
if (dirty) {
const double started = glfwGetTime();
if (animateRouteCallback)
animateRouteCallback(map);
activate();
mbgl::BackendScope scope { *this, mbgl::BackendScope::ScopeType::Implicit };
@@ -531,6 +575,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<mbgl::style::FillExtrusionLayer>("3d-buildings", "composite");
extrusionLayer->setSourceLayer("building");
extrusionLayer->setMinZoom(15.0f);
extrusionLayer->setFilter(mbgl::style::EqualsFilter { "extrude", { std::string("true") } });
auto colorFn = mbgl::style::SourceFunction<mbgl::Color> { "height",
mbgl::style::ExponentialStops<mbgl::Color> {
std::map<float, mbgl::Color> {
{ 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<float> { "height", mbgl::style::IdentityStops<float>() };
extrusionLayer->setFillExtrusionHeight({ heightSourceFn });
auto baseSourceFn = mbgl::style::SourceFunction<float> { "min_height", mbgl::style::IdentityStops<float>() };
extrusionLayer->setFillExtrusionBase({ baseSourceFn });
map->addLayer(std::move(extrusionLayer));
}
namespace mbgl {
namespace platform {

View File

@@ -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<std::string> 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<void()> changeStyleCallback;
std::function<void()> pauseResumeCallback;
std::function<void(mbgl::Map*)> animateRouteCallback;
mbgl::util::RunLoop runLoop;
mbgl::util::Timer frameTick;

104
platform/glfw/ny_route.hpp Normal file
View File

@@ -0,0 +1,104 @@
#include <string>
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