port line rendering fixes for perspective views

This commit is contained in:
Ansis Brammanis
2015-08-14 16:53:31 +03:00
parent f0885d36ff
commit 3027f8c0aa
6 changed files with 42 additions and 11 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;

View File

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