summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnsis Brammanis <brammanis@gmail.com>2015-08-14 16:53:31 +0300
committerAnsis Brammanis <brammanis@gmail.com>2015-08-24 18:41:50 -0400
commit3027f8c0aacc81ee82776d98971b49987ba1ba99 (patch)
tree0b8fb61c7bc6c8d442ae6bfabfc0ad167cb20843 /src
parentf0885d36ffac6c5260ab5694cf435946641c47a1 (diff)
downloadqtlocation-mapboxgl-3027f8c0aacc81ee82776d98971b49987ba1ba99.tar.gz
port line rendering fixes for perspective views
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/renderer/painter_line.cpp22
-rw-r--r--src/mbgl/shader/line.fragment.glsl4
-rw-r--r--src/mbgl/shader/line.vertex.glsl21
-rw-r--r--src/mbgl/shader/line_shader.hpp2
-rw-r--r--src/mbgl/shader/linepattern.vertex.glsl2
-rw-r--r--src/mbgl/shader/linesdf.vertex.glsl2
6 files changed, 42 insertions, 11 deletions
diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp
index f406c0d8f1..cf586c05e7 100644
--- a/src/mbgl/renderer/painter_line.cpp
+++ b/src/mbgl/renderer/painter_line.cpp
@@ -11,6 +11,7 @@
#include <mbgl/shader/linepattern_shader.hpp>
#include <mbgl/geometry/sprite_atlas.hpp>
#include <mbgl/geometry/line_atlas.hpp>
+#include <mbgl/util/mat2.hpp>
using namespace mbgl;
@@ -51,6 +52,19 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
color[2] *= properties.opacity;
color[3] *= properties.opacity;
+ float ratio = state.getScale() / std::pow(2, id.z) / 8.0 * id.overscaling;
+
+ mat2 antialiasingMatrix;
+ matrix::identity(antialiasingMatrix);
+ matrix::scale(antialiasingMatrix, antialiasingMatrix, 1.0, std::cos(state.getPitch()));
+ matrix::rotate(antialiasingMatrix, antialiasingMatrix, state.getAngle());
+
+ // calculate how much longer the real world distance is at the top of the screen
+ // than at the middle of the screen.
+ float topedgelength = std::sqrt(std::pow(state.getHeight(), 2) / 4 * (1 + std::pow(state.getAltitude(), 2)));
+ float x = state.getHeight() / 2.0f * std::tan(state.getPitch());
+ float extra = (topedgelength + x) / topedgelength - 1;
+
mat4 vtxMatrix = translatedMatrix(matrix, properties.translate, id, properties.translateAnchor);
config.depthRange = { strata, 1.0f };
@@ -62,7 +76,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linesdfShader->u_matrix = vtxMatrix;
linesdfShader->u_exmatrix = extrudeMatrix;
linesdfShader->u_linewidth = {{ outset, inset }};
- linesdfShader->u_ratio = data.pixelRatio;
+ linesdfShader->u_ratio = ratio;
linesdfShader->u_blur = blur;
linesdfShader->u_color = color;
@@ -97,7 +111,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linepatternShader->u_matrix = vtxMatrix;
linepatternShader->u_exmatrix = extrudeMatrix;
linepatternShader->u_linewidth = {{ outset, inset }};
- linepatternShader->u_ratio = data.pixelRatio;
+ linepatternShader->u_ratio = ratio;
linepatternShader->u_blur = blur;
linepatternShader->u_pattern_size_a = {{imagePosA.size[0] * factor * properties.image.fromScale, imagePosA.size[1]}};
@@ -121,8 +135,10 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
lineShader->u_matrix = vtxMatrix;
lineShader->u_exmatrix = extrudeMatrix;
lineShader->u_linewidth = {{ outset, inset }};
- lineShader->u_ratio = data.pixelRatio;
+ lineShader->u_ratio = ratio;
lineShader->u_blur = blur;
+ lineShader->u_extra = extra;
+ lineShader->u_antialiasingmatrix = antialiasingMatrix;
lineShader->u_color = color;
diff --git a/src/mbgl/shader/line.fragment.glsl b/src/mbgl/shader/line.fragment.glsl
index 717c46e10d..e0ef649965 100644
--- a/src/mbgl/shader/line.fragment.glsl
+++ b/src/mbgl/shader/line.fragment.glsl
@@ -3,6 +3,7 @@ uniform vec4 u_color;
uniform float u_blur;
varying vec2 v_normal;
+varying float v_gamma_scale;
void main() {
// Calculate the distance of the pixel from the line in pixels.
@@ -11,7 +12,8 @@ void main() {
// Calculate the antialiasing fade factor. This is either when fading in
// the line in case of an offset line (v_linewidth.t) or when fading out
// (v_linewidth.s)
- float alpha = clamp(min(dist - (u_linewidth.t - u_blur), u_linewidth.s - dist) / u_blur, 0.0, 1.0);
+ float blur = u_blur * v_gamma_scale;
+ float alpha = clamp(min(dist - (u_linewidth.t - blur), u_linewidth.s - dist) / blur, 0.0, 1.0);
gl_FragColor = u_color * alpha;
}
diff --git a/src/mbgl/shader/line.vertex.glsl b/src/mbgl/shader/line.vertex.glsl
index 1f5432991c..f818565ccd 100644
--- a/src/mbgl/shader/line.vertex.glsl
+++ b/src/mbgl/shader/line.vertex.glsl
@@ -9,17 +9,17 @@
attribute vec2 a_pos;
attribute vec4 a_data;
-// matrix is for the vertex position, exmatrix is for rotating and projecting
-// the extrusion vector.
uniform mat4 u_matrix;
-uniform mat4 u_exmatrix;
// shared
uniform float u_ratio;
uniform vec2 u_linewidth;
-uniform vec4 u_color;
+
+uniform float u_extra;
+uniform mat2 u_antialiasingmatrix;
varying vec2 v_normal;
+varying float v_gamma_scale;
void main() {
vec2 a_extrude = a_data.xy;
@@ -40,5 +40,16 @@ void main() {
// model/view matrix. Add the extrusion vector *after* the model/view matrix
// because we're extruding the line in pixel space, regardless of the current
// tile's zoom level.
- gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0.0, 1.0) + u_exmatrix * dist;
+ gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + dist.xy / u_ratio, 0.0, 1.0);
+
+ // position of y on the screen
+ float y = gl_Position.y / gl_Position.w;
+
+ // how much features are squished in the y direction by the tilt
+ float squish_scale = length(a_extrude) / length(u_antialiasingmatrix * a_extrude);
+
+ // how much features are squished in all directions by the perspectiveness
+ float perspective_scale = 1.0 / (1.0 - y * u_extra);
+
+ v_gamma_scale = perspective_scale * squish_scale;
}
diff --git a/src/mbgl/shader/line_shader.hpp b/src/mbgl/shader/line_shader.hpp
index 85152d6e0c..0572cac88d 100644
--- a/src/mbgl/shader/line_shader.hpp
+++ b/src/mbgl/shader/line_shader.hpp
@@ -18,6 +18,8 @@ public:
Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this};
Uniform<float> u_ratio = {"u_ratio", *this};
Uniform<float> u_blur = {"u_blur", *this};
+ Uniform<float> u_extra = {"u_extra", *this};
+ UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this};
private:
int32_t a_pos = -1;
diff --git a/src/mbgl/shader/linepattern.vertex.glsl b/src/mbgl/shader/linepattern.vertex.glsl
index ada384a3d1..47d551e2eb 100644
--- a/src/mbgl/shader/linepattern.vertex.glsl
+++ b/src/mbgl/shader/linepattern.vertex.glsl
@@ -52,7 +52,7 @@ void main() {
// model/view matrix. Add the extrusion vector *after* the model/view matrix
// because we're extruding the line in pixel space, regardless of the current
// tile's zoom level.
- gl_Position = u_matrix * vec4(floor(a_pos / 2.0), 0.0, 1.0) + u_exmatrix * vec4(dist, z, 0.0);
+ gl_Position = u_matrix * vec4(floor(a_pos / 2.0) + dist.xy / u_ratio, 0.0, 1.0);
v_linesofar = a_linesofar;// * u_ratio;
diff --git a/src/mbgl/shader/linesdf.vertex.glsl b/src/mbgl/shader/linesdf.vertex.glsl
index 7f246ced67..6529fb45c5 100644
--- a/src/mbgl/shader/linesdf.vertex.glsl
+++ b/src/mbgl/shader/linesdf.vertex.glsl
@@ -46,7 +46,7 @@ void main() {
// model/view matrix. Add the extrusion vector *after* the model/view matrix
// because we're extruding the line in pixel space, regardless of the current
// tile's zoom level.
- gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0.0, 1.0) + u_exmatrix * dist;
+ gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + dist.xy / u_ratio, 0.0, 1.0);
v_tex_a = vec2(a_linesofar * u_patternscale_a.x, normal.y * u_patternscale_a.y + u_tex_y_a);
v_tex_b = vec2(a_linesofar * u_patternscale_b.x, normal.y * u_patternscale_b.y + u_tex_y_b);