From 3027f8c0aacc81ee82776d98971b49987ba1ba99 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 14 Aug 2015 16:53:31 +0300 Subject: [PATCH] port line rendering fixes for perspective views --- src/mbgl/renderer/painter_line.cpp | 22 +++++++++++++++++++--- src/mbgl/shader/line.fragment.glsl | 4 +++- src/mbgl/shader/line.vertex.glsl | 21 ++++++++++++++++----- src/mbgl/shader/line_shader.hpp | 2 ++ src/mbgl/shader/linepattern.vertex.glsl | 2 +- src/mbgl/shader/linesdf.vertex.glsl | 2 +- 6 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index f406c0d8f..cf586c05e 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -11,6 +11,7 @@ #include #include #include +#include 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 717c46e10..e0ef64996 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 1f5432991..f818565cc 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 85152d6e0..0572cac88 100644 --- a/src/mbgl/shader/line_shader.hpp +++ b/src/mbgl/shader/line_shader.hpp @@ -18,6 +18,8 @@ public: Uniform> u_linewidth = {"u_linewidth", *this}; Uniform u_ratio = {"u_ratio", *this}; Uniform u_blur = {"u_blur", *this}; + Uniform 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 ada384a3d..47d551e2e 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 7f246ced6..6529fb45c 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);