diff options
author | Ansis Brammanis <brammanis@gmail.com> | 2016-02-11 15:52:18 -0800 |
---|---|---|
committer | Ansis Brammanis <brammanis@gmail.com> | 2016-02-15 12:55:41 -0800 |
commit | 47dc38bf4a4956d63c76c1ab351a0fd08a5f560c (patch) | |
tree | 983ed9715fb6d2f8442b280bc8e7f90f1967dedf /src | |
parent | cdab06306fb5d0f2e74d3264495b166bd440d4f8 (diff) | |
download | qtlocation-mapboxgl-47dc38bf4a4956d63c76c1ab351a0fd08a5f560c.tar.gz |
[core] less slanted dashed lines near sharp corners
port https://github.com/mapbox/mapbox-gl-js/pull/2043 from -js
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/layer/line_layer.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/renderer/line_bucket.cpp | 50 | ||||
-rw-r--r-- | src/mbgl/renderer/line_bucket.hpp | 4 |
3 files changed, 49 insertions, 7 deletions
diff --git a/src/mbgl/layer/line_layer.cpp b/src/mbgl/layer/line_layer.cpp index 8454eb2664..edf128fd35 100644 --- a/src/mbgl/layer/line_layer.cpp +++ b/src/mbgl/layer/line_layer.cpp @@ -69,7 +69,7 @@ bool LineLayer::recalculate(const StyleCalculationParameters& parameters) { } std::unique_ptr<Bucket> LineLayer::createBucket(StyleBucketParameters& parameters) const { - auto bucket = std::make_unique<LineBucket>(); + auto bucket = std::make_unique<LineBucket>(parameters.tileID.overscaling); bucket->layout = layout; diff --git a/src/mbgl/renderer/line_bucket.cpp b/src/mbgl/renderer/line_bucket.cpp index 2f648bb67b..51b735daa0 100644 --- a/src/mbgl/renderer/line_bucket.cpp +++ b/src/mbgl/renderer/line_bucket.cpp @@ -6,13 +6,14 @@ #include <mbgl/shader/linesdf_shader.hpp> #include <mbgl/shader/linepattern_shader.hpp> #include <mbgl/util/math.hpp> +#include <mbgl/util/constants.hpp> #include <mbgl/gl/gl.hpp> #include <cassert> using namespace mbgl; -LineBucket::LineBucket() { +LineBucket::LineBucket(float overscaling_) : overscaling(overscaling_) { } LineBucket::~LineBucket() { @@ -25,6 +26,21 @@ void LineBucket::addGeometry(const GeometryCollection& geometryCollection) { } } + +/* + * Sharp corners cause dashed lines to tilt because the distance along the line + * is the same at both the inner and outer corners. To improve the appearance of + * dashed lines we add extra points near sharp corners so that a smaller part + * of the line is tilted. + * + * COS_HALF_SHARP_CORNER controls how sharp a corner has to be for us to add an + * extra vertex. The default is 75 degrees. + * + * The newly created vertices are placed SHARP_CORNER_OFFSET pixels from the corner. + */ +const float COS_HALF_SHARP_CORNER = std::cos(75.0 / 2.0 * (M_PI / 180.0)); +const float SHARP_CORNER_OFFSET = 15.0f; + void LineBucket::addGeometry(const std::vector<Coordinate>& vertices) { const GLsizei len = [&vertices] { GLsizei l = static_cast<GLsizei>(vertices.size()); @@ -42,6 +58,8 @@ void LineBucket::addGeometry(const std::vector<Coordinate>& vertices) { const float miterLimit = layout.join == JoinType::Bevel ? 1.05f : float(layout.miterLimit); + const double sharpCornerOffset = SHARP_CORNER_OFFSET * (util::EXTENT / (512.0 * overscaling)); + const Coordinate firstVertex = vertices.front(); const Coordinate lastVertex = vertices[len - 1]; const bool closed = firstVertex == lastVertex; @@ -98,10 +116,6 @@ void LineBucket::addGeometry(const std::vector<Coordinate>& vertices) { currentVertex = vertices[i]; - // Calculate how far along the line the currentVertex is - if (prevVertex) - distance += util::dist<double>(currentVertex, prevVertex); - // Calculate the normal towards the next vertex in this line. In case // there is no next vertex, pretend that the line is continuing straight, // meaning that we are just using the previous normal. @@ -134,6 +148,18 @@ void LineBucket::addGeometry(const std::vector<Coordinate>& vertices) { const float cosHalfAngle = joinNormal.x * nextNormal.x + joinNormal.y * nextNormal.y; const float miterLength = cosHalfAngle != 0 ? 1 / cosHalfAngle: 1; + const bool isSharpCorner = cosHalfAngle < COS_HALF_SHARP_CORNER && prevVertex && nextVertex; + + if (isSharpCorner && i > 0) { + const double prevSegmentLength = util::dist<double>(currentVertex, prevVertex); + if (prevSegmentLength > 2.0 * sharpCornerOffset) { + Coordinate newPrevVertex = currentVertex - (util::round(vec2<double>(currentVertex - prevVertex) * (sharpCornerOffset / prevSegmentLength))); + distance += util::dist<double>(newPrevVertex, prevVertex); + addCurrentVertex(newPrevVertex, flip, distance, prevNormal, 0, 0, false, startVertex, triangleStore); + prevVertex = newPrevVertex; + } + } + // The join if a middle vertex, otherwise the cap const bool middleVertex = prevVertex && nextVertex; JoinType currentJoin = layout.join; @@ -167,6 +193,10 @@ void LineBucket::addGeometry(const std::vector<Coordinate>& vertices) { } } + // Calculate how far along the line the currentVertex is + if (prevVertex) + distance += util::dist<double>(currentVertex, prevVertex); + if (middleVertex && currentJoin == JoinType::Miter) { joinNormal = joinNormal * miterLength; addCurrentVertex(currentVertex, flip, distance, joinNormal, 0, 0, false, startVertex, @@ -295,6 +325,16 @@ void LineBucket::addGeometry(const std::vector<Coordinate>& vertices) { } } + if (isSharpCorner && i < len - 1) { + const double nextSegmentLength = util::dist<double>(currentVertex, nextVertex); + if (nextSegmentLength > 2 * sharpCornerOffset) { + Coordinate newCurrentVertex = currentVertex + util::round(vec2<double>(nextVertex - currentVertex) * (sharpCornerOffset / nextSegmentLength)); + distance += util::dist<double>(newCurrentVertex, currentVertex); + addCurrentVertex(newCurrentVertex, flip, distance, nextNormal, 0, 0, false, startVertex, triangleStore); + currentVertex = newCurrentVertex; + } + } + startOfLine = false; } diff --git a/src/mbgl/renderer/line_bucket.hpp b/src/mbgl/renderer/line_bucket.hpp index 0c4c1f5d91..883f446b4a 100644 --- a/src/mbgl/renderer/line_bucket.hpp +++ b/src/mbgl/renderer/line_bucket.hpp @@ -24,7 +24,7 @@ class LineBucket : public Bucket { using TriangleGroup = ElementGroup<3>; public: - LineBucket(); + LineBucket(float overscaling); ~LineBucket() override; void upload() override; @@ -62,6 +62,8 @@ private: GLint e3; std::vector<std::unique_ptr<TriangleGroup>> triangleGroups; + + const float overscaling; }; } // namespace mbgl |