diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-11-25 12:42:34 +0100 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-03 11:41:53 -0800 |
commit | 964a1e7b62c3a7e8fd742e16fe842f8a702437e5 (patch) | |
tree | b94ace89c3688577b196893b7988496f8cbc41a3 /src/mbgl/shader | |
parent | c279fb171700c464c1cdac5cd2da39f142274d8d (diff) | |
download | qtlocation-mapboxgl-964a1e7b62c3a7e8fd742e16fe842f8a702437e5.tar.gz |
[core] rudimentary support for offsetting lines
Diffstat (limited to 'src/mbgl/shader')
-rw-r--r-- | src/mbgl/shader/line.vertex.glsl | 12 | ||||
-rw-r--r-- | src/mbgl/shader/line_shader.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/shader/linepattern.vertex.glsl | 14 | ||||
-rw-r--r-- | src/mbgl/shader/linepattern_shader.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/shader/linesdf.vertex.glsl | 14 | ||||
-rw-r--r-- | src/mbgl/shader/linesdf_shader.hpp | 1 |
6 files changed, 38 insertions, 5 deletions
diff --git a/src/mbgl/shader/line.vertex.glsl b/src/mbgl/shader/line.vertex.glsl index 980212384b..1bd7f5ef5f 100644 --- a/src/mbgl/shader/line.vertex.glsl +++ b/src/mbgl/shader/line.vertex.glsl @@ -14,6 +14,7 @@ uniform mat4 u_matrix; // shared uniform float u_ratio; uniform vec2 u_linewidth; +uniform float u_offset; uniform float u_extra; uniform mat2 u_antialiasingmatrix; @@ -23,6 +24,7 @@ varying float v_gamma_scale; void main() { vec2 a_extrude = a_data.xy; + float a_direction = sign(a_data.z) * mod(a_data.z, 2.0); // We store the texture normals in the most insignificant bit // transform y so that 0 => -1 and 1 => 1 @@ -36,11 +38,19 @@ void main() { // of this vertex. vec2 dist = u_linewidth.s * a_extrude * scale; + // Calculate the offset when drawing a line that is to the side of the actual line. + // We do this by creating a vector that points towards the extrude, but rotate + // it when we're drawing round end points (a_direction = -1 or 1) since their + // extrude vector points in another direction. + float u = 0.5 * a_direction; + float t = 1.0 - abs(u); + vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); + // 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 / u_ratio, 0.0, 1.0); + gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + 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/line_shader.hpp b/src/mbgl/shader/line_shader.hpp index 50c209ef24..14e03c2eaf 100644 --- a/src/mbgl/shader/line_shader.hpp +++ b/src/mbgl/shader/line_shader.hpp @@ -19,6 +19,7 @@ public: Uniform<GLfloat> u_ratio = {"u_ratio", *this}; Uniform<GLfloat> u_blur = {"u_blur", *this}; Uniform<GLfloat> u_extra = {"u_extra", *this}; + Uniform<GLfloat> u_offset = {"u_offset", *this}; UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this}; private: diff --git a/src/mbgl/shader/linepattern.vertex.glsl b/src/mbgl/shader/linepattern.vertex.glsl index 541c9fc3a7..5b3c468be0 100644 --- a/src/mbgl/shader/linepattern.vertex.glsl +++ b/src/mbgl/shader/linepattern.vertex.glsl @@ -17,6 +17,7 @@ uniform mat4 u_exmatrix; // shared uniform float u_ratio; uniform vec2 u_linewidth; +uniform float u_offset; uniform vec4 u_color; uniform float u_extra; @@ -28,7 +29,8 @@ varying float v_gamma_scale; void main() { vec2 a_extrude = a_data.xy; - float a_linesofar = a_data.z * 128.0 + a_data.w; + float a_direction = sign(a_data.z) * mod(a_data.z, 2.0); + float a_linesofar = abs(floor(a_data.z / 2.0)) + a_data.w * 64.0; // We store the texture normals in the most insignificant bit // transform y so that 0 => -1 and 1 => 1 @@ -42,11 +44,19 @@ void main() { // of this vertex. vec2 dist = u_linewidth.s * a_extrude * scale; + // Calculate the offset when drawing a line that is to the side of the actual line. + // We do this by creating a vector that points towards the extrude, but rotate + // it when we're drawing round end points (a_direction = -1 or 1) since their + // extrude vector points in another direction. + float u = 0.5 * a_direction; + float t = 1.0 - abs(u); + vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); + // 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); + gl_Position = u_matrix * vec4(floor(a_pos / 2.0) + (offset + dist) / u_ratio, 0.0, 1.0); v_linesofar = a_linesofar; // position of y on the screen diff --git a/src/mbgl/shader/linepattern_shader.hpp b/src/mbgl/shader/linepattern_shader.hpp index aae550665c..6c83cb0cc3 100644 --- a/src/mbgl/shader/linepattern_shader.hpp +++ b/src/mbgl/shader/linepattern_shader.hpp @@ -27,6 +27,7 @@ public: Uniform<GLfloat> u_fade = {"u_fade", *this}; Uniform<GLfloat> u_opacity = {"u_opacity", *this}; Uniform<GLfloat> u_extra = {"u_extra", *this}; + Uniform<GLfloat> u_offset = {"u_offset", *this}; UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this}; private: diff --git a/src/mbgl/shader/linesdf.vertex.glsl b/src/mbgl/shader/linesdf.vertex.glsl index 03851ed533..80516f97c0 100644 --- a/src/mbgl/shader/linesdf.vertex.glsl +++ b/src/mbgl/shader/linesdf.vertex.glsl @@ -17,6 +17,7 @@ uniform mat4 u_exmatrix; // shared uniform float u_ratio; uniform vec2 u_linewidth; +uniform float u_offset; uniform vec2 u_patternscale_a; uniform float u_tex_y_a; uniform vec2 u_patternscale_b; @@ -32,7 +33,8 @@ varying float v_gamma_scale; void main() { vec2 a_extrude = a_data.xy; - float a_linesofar = a_data.z * 128.0 + a_data.w; + float a_direction = sign(a_data.z) * mod(a_data.z, 2.0); + float a_linesofar = abs(floor(a_data.z / 2.0)) + a_data.w * 64.0; // We store the texture normals in the most insignificant bit // transform y so that 0 => -1 and 1 => 1 @@ -46,11 +48,19 @@ void main() { // of this vertex. vec2 dist = u_linewidth.s * a_extrude * scale; + // Calculate the offset when drawing a line that is to the side of the actual line. + // We do this by creating a vector that points towards the extrude, but rotate + // it when we're drawing round end points (a_direction = -1 or 1) since their + // extrude vector points in another direction. + float u = 0.5 * a_direction; + float t = 1.0 - abs(u); + vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); + // 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 / u_ratio, 0.0, 1.0); + gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + 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); diff --git a/src/mbgl/shader/linesdf_shader.hpp b/src/mbgl/shader/linesdf_shader.hpp index 1fdae14069..2a8bdc6629 100644 --- a/src/mbgl/shader/linesdf_shader.hpp +++ b/src/mbgl/shader/linesdf_shader.hpp @@ -26,6 +26,7 @@ public: Uniform<GLfloat> u_sdfgamma = {"u_sdfgamma", *this}; Uniform<GLfloat> u_mix = {"u_mix", *this}; Uniform<GLfloat> u_extra = {"u_extra", *this}; + Uniform<GLfloat> u_offset = {"u_offset", *this}; UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this}; private: |