summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnsis Brammanis <brammanis@gmail.com>2015-08-27 17:33:56 -0400
committerAnsis Brammanis <brammanis@gmail.com>2015-08-27 17:42:27 -0400
commitd50ade7aa47a4460b0d6bc4e89f83288070b8d9f (patch)
tree4b93cb3e1091e7f45c5016d372672726e5d6576e /src
parent294b307cb3f470b1cf5fb94633c9082dc0bfd719 (diff)
downloadqtlocation-mapboxgl-d50ade7aa47a4460b0d6bc4e89f83288070b8d9f.tar.gz
improve dashed and patterned line antialiasing
in perspective view. This is exactly the same approach used for regular lines. I just forgot to implement it for these types of lines.
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/renderer/painter_line.cpp4
-rw-r--r--src/mbgl/shader/line.vertex.glsl4
-rw-r--r--src/mbgl/shader/linepattern.fragment.glsl9
-rw-r--r--src/mbgl/shader/linepattern.vertex.glsl28
-rw-r--r--src/mbgl/shader/linepattern_shader.hpp2
-rw-r--r--src/mbgl/shader/linesdf.fragment.glsl4
-rw-r--r--src/mbgl/shader/linesdf.vertex.glsl19
-rw-r--r--src/mbgl/shader/linesdf_shader.hpp2
8 files changed, 49 insertions, 23 deletions
diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp
index 0b7e8a96f7..258bab6aef 100644
--- a/src/mbgl/renderer/painter_line.cpp
+++ b/src/mbgl/renderer/painter_line.cpp
@@ -97,6 +97,8 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linesdfShader->u_image = 0;
linesdfShader->u_sdfgamma = lineAtlas->width / (properties.dash_line_width * std::min(posA.width, posB.width) * 256.0 * data.pixelRatio) / 2;
linesdfShader->u_mix = properties.dash_array.t;
+ linesdfShader->u_extra = extra;
+ linesdfShader->u_antialiasingmatrix = antialiasingMatrix;
bucket.drawLineSDF(*linesdfShader);
@@ -122,6 +124,8 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
linepatternShader->u_pattern_br_b = imagePosB.br;
linepatternShader->u_fade = properties.image.t;
linepatternShader->u_opacity = properties.opacity;
+ linepatternShader->u_extra = extra;
+ linepatternShader->u_antialiasingmatrix = antialiasingMatrix;
MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0));
spriteAtlas->bind(true);
diff --git a/src/mbgl/shader/line.vertex.glsl b/src/mbgl/shader/line.vertex.glsl
index 3b3caaab15..980212384b 100644
--- a/src/mbgl/shader/line.vertex.glsl
+++ b/src/mbgl/shader/line.vertex.glsl
@@ -34,13 +34,13 @@ void main() {
// Scale the extrusion vector down to a normal and then up by the line width
// of this vertex.
- vec4 dist = vec4(u_linewidth.s * a_extrude * scale, 0.0, 0.0);
+ vec2 dist = u_linewidth.s * a_extrude * scale;
// Remove the texture normal bit of the position before scaling it with the
// 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) + dist.xy / u_ratio, 0.0, 1.0);
+ gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + dist / u_ratio, 0.0, 1.0);
// position of y on the screen
float y = gl_Position.y / gl_Position.w;
diff --git a/src/mbgl/shader/linepattern.fragment.glsl b/src/mbgl/shader/linepattern.fragment.glsl
index 08feff4396..4110a99ead 100644
--- a/src/mbgl/shader/linepattern.fragment.glsl
+++ b/src/mbgl/shader/linepattern.fragment.glsl
@@ -1,5 +1,4 @@
uniform vec2 u_linewidth;
-uniform float u_point;
uniform float u_blur;
uniform vec2 u_pattern_size_a;
@@ -15,17 +14,17 @@ uniform sampler2D u_image;
varying vec2 v_normal;
varying float v_linesofar;
+varying float v_gamma_scale;
void main() {
// Calculate the distance of the pixel from the line in pixels.
- float dist = length(v_normal) * (1.0 - u_point) + u_point * length(gl_PointCoord * 2.0 - 1.0);
-
- dist *= u_linewidth.s;
+ float dist = length(v_normal) * u_linewidth.s;
// 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);
float x_a = mod(v_linesofar / u_pattern_size_a.x, 1.0);
float y_a = 0.5 + (v_normal.y * u_linewidth.s / u_pattern_size_a.y);
diff --git a/src/mbgl/shader/linepattern.vertex.glsl b/src/mbgl/shader/linepattern.vertex.glsl
index 47d551e2eb..541c9fc3a7 100644
--- a/src/mbgl/shader/linepattern.vertex.glsl
+++ b/src/mbgl/shader/linepattern.vertex.glsl
@@ -18,10 +18,13 @@ uniform mat4 u_exmatrix;
uniform float u_ratio;
uniform vec2 u_linewidth;
uniform vec4 u_color;
-uniform float u_point;
+
+uniform float u_extra;
+uniform mat2 u_antialiasingmatrix;
varying vec2 v_normal;
varying float v_linesofar;
+varying float v_gamma_scale;
void main() {
vec2 a_extrude = a_data.xy;
@@ -37,24 +40,23 @@ void main() {
// Scale the extrusion vector down to a normal and then up by the line width
// of this vertex.
- vec2 extrude = a_extrude * scale;
- vec2 dist = u_linewidth.s * extrude * (1.0 - u_point);
-
- // If the x coordinate is the maximum integer, we move the z coordinates out
- // of the view plane so that the triangle gets clipped. This makes it easier
- // for us to create degenerate triangle strips.
- float z = step(32767.0, a_pos.x);
-
- // When drawing points, skip every other vertex
- z += u_point * step(1.0, v_normal.y);
+ vec2 dist = u_linewidth.s * a_extrude * scale;
// Remove the texture normal bit of the position before scaling it with the
// 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) + dist.xy / u_ratio, 0.0, 1.0);
- v_linesofar = a_linesofar;// * u_ratio;
+ v_linesofar = a_linesofar;
+
+ // 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);
- gl_PointSize = 2.0 * u_linewidth.s - 1.0;
+ v_gamma_scale = perspective_scale * squish_scale;
}
diff --git a/src/mbgl/shader/linepattern_shader.hpp b/src/mbgl/shader/linepattern_shader.hpp
index 16d9ff8be7..dce065a0d5 100644
--- a/src/mbgl/shader/linepattern_shader.hpp
+++ b/src/mbgl/shader/linepattern_shader.hpp
@@ -26,6 +26,8 @@ public:
Uniform<float> u_blur = {"u_blur", *this};
Uniform<float> u_fade = {"u_fade", *this};
Uniform<float> u_opacity = {"u_opacity", *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/linesdf.fragment.glsl b/src/mbgl/shader/linesdf.fragment.glsl
index 5d53dbe883..f8962fa570 100644
--- a/src/mbgl/shader/linesdf.fragment.glsl
+++ b/src/mbgl/shader/linesdf.fragment.glsl
@@ -8,6 +8,7 @@ uniform float u_mix;
varying vec2 v_normal;
varying vec2 v_tex_a;
varying vec2 v_tex_b;
+varying float v_gamma_scale;
void main() {
// Calculate the distance of the pixel from the line in pixels.
@@ -16,7 +17,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);
float sdfdist_a = texture2D(u_image, v_tex_a).a;
float sdfdist_b = texture2D(u_image, v_tex_b).a;
diff --git a/src/mbgl/shader/linesdf.vertex.glsl b/src/mbgl/shader/linesdf.vertex.glsl
index 6529fb45c5..03851ed533 100644
--- a/src/mbgl/shader/linesdf.vertex.glsl
+++ b/src/mbgl/shader/linesdf.vertex.glsl
@@ -22,9 +22,13 @@ uniform float u_tex_y_a;
uniform vec2 u_patternscale_b;
uniform float u_tex_y_b;
+uniform float u_extra;
+uniform mat2 u_antialiasingmatrix;
+
varying vec2 v_normal;
varying vec2 v_tex_a;
varying vec2 v_tex_b;
+varying float v_gamma_scale;
void main() {
vec2 a_extrude = a_data.xy;
@@ -40,14 +44,25 @@ void main() {
// Scale the extrusion vector down to a normal and then up by the line width
// of this vertex.
- vec4 dist = vec4(u_linewidth.s * a_extrude * scale, 0.0, 0.0);
+ vec2 dist = u_linewidth.s * a_extrude * scale;
// Remove the texture normal bit of the position before scaling it with the
// 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) + dist.xy / u_ratio, 0.0, 1.0);
+ gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + dist / 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);
+
+ // 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 - min(y * u_extra, 0.9));
+
+ v_gamma_scale = perspective_scale * squish_scale;
}
diff --git a/src/mbgl/shader/linesdf_shader.hpp b/src/mbgl/shader/linesdf_shader.hpp
index 16b7f3cb1b..9928d5b304 100644
--- a/src/mbgl/shader/linesdf_shader.hpp
+++ b/src/mbgl/shader/linesdf_shader.hpp
@@ -25,6 +25,8 @@ public:
Uniform<int32_t> u_image = {"u_image", *this};
Uniform<float> u_sdfgamma = {"u_sdfgamma", *this};
Uniform<float> u_mix = {"u_mix", *this};
+ Uniform<float> u_extra = {"u_extra", *this};
+ UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this};
private:
int32_t a_pos = -1;