summaryrefslogtreecommitdiff
path: root/src/shader
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-08-04 12:59:22 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-08-04 12:59:22 +0200
commit9dd50a29e1e8b975e09172b19b303063a96ba20f (patch)
tree0f35c1d304aafcf465762484848d33e58342c391 /src/shader
parent19e51e8bd89f0e627045b623a2a57b6702bca896 (diff)
downloadqtlocation-mapboxgl-9dd50a29e1e8b975e09172b19b303063a96ba20f.tar.gz
update shaders and backport some collision changes
Diffstat (limited to 'src/shader')
-rw-r--r--src/shader/icon.fragment.glsl11
-rw-r--r--src/shader/icon.vertex.glsl71
-rw-r--r--src/shader/icon_shader.cpp130
3 files changed, 169 insertions, 43 deletions
diff --git a/src/shader/icon.fragment.glsl b/src/shader/icon.fragment.glsl
index 52c9e65aa5..beaee7654e 100644
--- a/src/shader/icon.fragment.glsl
+++ b/src/shader/icon.fragment.glsl
@@ -1,11 +1,10 @@
-uniform sampler2D u_image;
-uniform vec2 u_dimension;
-uniform vec4 u_color;
-uniform float u_size;
+uniform sampler2D u_texture;
varying vec2 v_tex;
+varying float v_alpha;
void main() {
- vec2 pos = (v_tex + (gl_PointCoord - 0.5) * u_size) / u_dimension;
- gl_FragColor = u_color * texture2D(u_image, pos);
+ gl_FragColor = texture2D(u_texture, v_tex);
+ gl_FragColor.a *= v_alpha;
+ gl_FragColor.rgb *= gl_FragColor.a;
}
diff --git a/src/shader/icon.vertex.glsl b/src/shader/icon.vertex.glsl
index d0ffe4b33e..d273b733d7 100644
--- a/src/shader/icon.vertex.glsl
+++ b/src/shader/icon.vertex.glsl
@@ -1,14 +1,73 @@
attribute vec2 a_pos;
+attribute vec2 a_offset;
attribute vec2 a_tex;
+attribute float a_angle;
+attribute float a_minzoom;
+attribute float a_maxzoom;
+attribute float a_rangeend;
+attribute float a_rangestart;
+attribute float a_labelminzoom;
-uniform mat4 u_matrix;
-uniform float u_size;
-uniform float u_ratio;
+
+// posmatrix is for the vertex position, exmatrix is for rotating and projecting
+// the extrusion vector.
+uniform mat4 u_posmatrix;
+uniform mat4 u_exmatrix;
+uniform float u_angle;
+uniform float u_zoom;
+uniform float u_flip;
+uniform float u_fadedist;
+uniform float u_minfadezoom;
+uniform float u_maxfadezoom;
+uniform float u_fadezoom;
+uniform float u_opacity;
+
+uniform vec2 u_texsize;
varying vec2 v_tex;
+varying float v_alpha;
void main() {
- gl_Position = u_matrix * vec4(a_pos, 0, 1);
- gl_PointSize = u_size;
- v_tex = a_tex * u_ratio;
+
+ float a_fadedist = 10.0;
+ float rev = 0.0;
+
+ // u_angle is angle of the map, -128..128 representing 0..2PI
+ // a_angle is angle of the label, 0..256 representing 0..2PI, where 0 is horizontal text
+ float rotated = mod(a_angle + u_angle, 256.0);
+ // if the label rotates with the map, and if the rotated label is upside down, hide it
+ if (u_flip > 0.0 && rotated >= 64.0 && rotated < 192.0) rev = 1.0;
+
+ // If the label should be invisible, we move the vertex outside
+ // of the view plane so that the triangle gets clipped. This makes it easier
+ // for us to create degenerate triangle strips.
+ // u_zoom is the current zoom level adjusted for the change in font size
+ float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom)) + rev;
+
+ // fade out labels
+ float alpha = clamp((u_fadezoom - a_labelminzoom) / u_fadedist, 0.0, 1.0);
+
+ if (u_fadedist >= 0.0) {
+ v_alpha = alpha;
+ } else {
+ v_alpha = 1.0 - alpha;
+ }
+ if (u_maxfadezoom < a_labelminzoom) {
+ v_alpha = 0.0;
+ }
+ if (u_minfadezoom >= a_labelminzoom) {
+ v_alpha = 1.0;
+ }
+
+ // if label has been faded out, clip it
+ z += step(v_alpha, 0.0);
+
+ // all the angles are 0..256 representing 0..2PI
+ // hide if (angle >= a_rangeend && angle < rangestart)
+ z += step(a_rangeend, u_angle) * (1.0 - step(a_rangestart, u_angle));
+
+ gl_Position = u_posmatrix * vec4(a_pos, 0, 1) + u_exmatrix * vec4(a_offset / 64.0, z, 0);
+ v_tex = a_tex / u_texsize;
+
+ v_alpha *= u_opacity;
}
diff --git a/src/shader/icon_shader.cpp b/src/shader/icon_shader.cpp
index b3ac9272ba..435b813fa4 100644
--- a/src/shader/icon_shader.cpp
+++ b/src/shader/icon_shader.cpp
@@ -17,62 +17,130 @@ IconShader::IconShader()
}
a_pos = glGetAttribLocation(program, "a_pos");
+ a_offset = glGetAttribLocation(program, "a_offset");
a_tex = glGetAttribLocation(program, "a_tex");
+ a_angle = glGetAttribLocation(program, "a_angle");
+ a_minzoom = glGetAttribLocation(program, "a_minzoom");
+ a_maxzoom = glGetAttribLocation(program, "a_maxzoom");
+ a_rangeend = glGetAttribLocation(program, "a_rangeend");
+ a_rangestart = glGetAttribLocation(program, "a_rangestart");
+ a_labelminzoom = glGetAttribLocation(program, "a_labelminzoom");
u_matrix = glGetUniformLocation(program, "u_matrix");
- u_color = glGetUniformLocation(program, "u_color");
- u_size = glGetUniformLocation(program, "u_size");
- u_ratio = glGetUniformLocation(program, "u_ratio");
- u_dimension = glGetUniformLocation(program, "u_dimension");
+ u_exmatrix = glGetUniformLocation(program, "u_exmatrix");
+ u_angle = glGetUniformLocation(program, "u_angle");
+ u_zoom = glGetUniformLocation(program, "u_zoom");
+ u_flip = glGetUniformLocation(program, "u_flip");
+ u_fadedist = glGetUniformLocation(program, "u_fadedist");
+ u_minfadezoom = glGetUniformLocation(program, "u_minfadezoom");
+ u_maxfadezoom = glGetUniformLocation(program, "u_maxfadezoom");
+ u_fadezoom = glGetUniformLocation(program, "u_fadezoom");
+ u_opacity = glGetUniformLocation(program, "u_opacity");
// fprintf(stderr, "IconShader:\n");
// fprintf(stderr, " - u_matrix: %d\n", u_matrix);
- // fprintf(stderr, " - u_color: %d\n", u_color);
- // fprintf(stderr, " - u_size: %d\n", u_size);
- // fprintf(stderr, " - u_ratio: %d\n", u_ratio);
- // fprintf(stderr, " - u_dimension: %d\n", u_dimension);
- // fprintf(stderr, " - u_image: %d\n", u_image);
+ // fprintf(stderr, " - u_exmatrix: %d\n", u_exmatrix);
+ // fprintf(stderr, " - u_angle: %d\n", u_angle);
+ // fprintf(stderr, " - u_zoom: %d\n", u_zoom);
+ // fprintf(stderr, " - u_flip: %d\n", u_flip);
+ // fprintf(stderr, " - u_fadedist: %d\n", u_fadedist);
+ // fprintf(stderr, " - u_minfadezoom: %d\n", u_minfadezoom);
+ // fprintf(stderr, " - u_maxfadezoom: %d\n", u_maxfadezoom);
+ // fprintf(stderr, " - u_fadezoom: %d\n", u_fadezoom);
+ // fprintf(stderr, " - u_opacity: %d\n", u_opacity);
}
void IconShader::bind(char *offset) {
+ const int stride = 20;
+
glEnableVertexAttribArray(a_pos);
- glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset);
+ glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset + 0);
+
+ glEnableVertexAttribArray(a_offset);
+ glVertexAttribPointer(a_offset, 2, GL_SHORT, false, stride, offset + 4);
+
+ glEnableVertexAttribArray(a_labelminzoom);
+ glVertexAttribPointer(a_labelminzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 8);
+
+ glEnableVertexAttribArray(a_minzoom);
+ glVertexAttribPointer(a_minzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 9);
+
+ glEnableVertexAttribArray(a_maxzoom);
+ glVertexAttribPointer(a_maxzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 10);
+
+ glEnableVertexAttribArray(a_angle);
+ glVertexAttribPointer(a_angle, 1, GL_UNSIGNED_BYTE, false, stride, offset + 11);
+
+ glEnableVertexAttribArray(a_rangeend);
+ glVertexAttribPointer(a_rangeend, 1, GL_UNSIGNED_BYTE, false, stride, offset + 12);
+
+ glEnableVertexAttribArray(a_rangestart);
+ glVertexAttribPointer(a_rangestart, 1, GL_UNSIGNED_BYTE, false, stride, offset + 13);
glEnableVertexAttribArray(a_tex);
- glVertexAttribPointer(a_tex, 2, GL_UNSIGNED_SHORT, false, 8, offset + 4);
+ glVertexAttribPointer(a_tex, 2, GL_SHORT, false, stride, offset + 16);
+}
+
+void IconShader::setExtrudeMatrix(const std::array<float, 16>& new_exmatrix) {
+ if (exmatrix != new_exmatrix) {
+ glUniformMatrix4fv(u_exmatrix, 1, GL_FALSE, new_exmatrix.data());
+ exmatrix = new_exmatrix;
+ }
}
-void IconShader::setImage(int32_t new_image) {
- if (image != new_image) {
- glUniform1i(u_image, new_image);
- image = new_image;
+void IconShader::setAngle(float new_angle) {
+ if (angle != new_angle) {
+ glUniform1f(u_angle, new_angle);
+ angle = new_angle;
}
}
-void IconShader::setColor(const std::array<float, 4>& new_color) {
- if (color != new_color) {
- glUniform4fv(u_color, 1, new_color.data());
- color = new_color;
+void IconShader::setZoom(float new_zoom) {
+ if (zoom != new_zoom) {
+ glUniform1f(u_zoom, new_zoom);
+ zoom = new_zoom;
}
}
-void IconShader::setSize(float new_size) {
- if (size != new_size) {
- glUniform1f(u_size, new_size);
- size = new_size;
+void IconShader::setFlip(float new_flip) {
+ if (flip != new_flip) {
+ glUniform1f(u_flip, new_flip);
+ flip = new_flip;
}
}
-void IconShader::setRatio(float new_ratio) {
- if (ratio != new_ratio) {
- glUniform1f(u_ratio, new_ratio);
- ratio = new_ratio;
+void IconShader::setFadeDist(float new_fadedist) {
+ if (fadedist != new_fadedist) {
+ glUniform1f(u_fadedist, new_fadedist);
+ fadedist = new_fadedist;
}
}
-void IconShader::setDimension(const std::array<float, 2>& new_dimension) {
- if (dimension != new_dimension) {
- glUniform2fv(u_dimension, 1, new_dimension.data());
- dimension = new_dimension;
+void IconShader::setMinFadeZoom(float new_minfadezoom) {
+ if (minfadezoom != new_minfadezoom) {
+ glUniform1f(u_minfadezoom, new_minfadezoom);
+ minfadezoom = new_minfadezoom;
}
}
+
+void IconShader::setMaxFadeZoom(float new_maxfadezoom) {
+ if (maxfadezoom != new_maxfadezoom) {
+ glUniform1f(u_maxfadezoom, new_maxfadezoom);
+ maxfadezoom = new_maxfadezoom;
+ }
+}
+
+void IconShader::setFadeZoom(float new_fadezoom) {
+ if (fadezoom != new_fadezoom) {
+ glUniform1f(u_fadezoom, new_fadezoom);
+ fadezoom = new_fadezoom;
+ }
+}
+
+void IconShader::setOpacity(float new_opacity) {
+ if (opacity != new_opacity) {
+ glUniform1f(u_opacity, new_opacity);
+ opacity = new_opacity;
+ }
+}
+