linepattern in progress
This commit is contained in:
committed by
John Firebaugh
parent
c1280f0b38
commit
71c31cee24
34
src/shader/linepattern.fragment.glsl
Normal file
34
src/shader/linepattern.fragment.glsl
Normal file
@@ -0,0 +1,34 @@
|
||||
uniform vec2 u_linewidth;
|
||||
uniform float u_gamma;
|
||||
uniform float u_fade;
|
||||
|
||||
uniform vec2 u_pattern_size;
|
||||
uniform vec2 u_pattern_tl;
|
||||
uniform vec2 u_pattern_br;
|
||||
|
||||
uniform sampler2D u_image;
|
||||
|
||||
varying vec2 v_normal;
|
||||
varying float v_linesofar;
|
||||
|
||||
void main() {
|
||||
// Calculate the distance of the pixel from the line in pixels.
|
||||
float dist = length(v_normal) * length(gl_PointCoord * 2.0 - 1.0);
|
||||
|
||||
// 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 - 1.0), u_linewidth.s - dist) * u_gamma, 0.0, 1.0);
|
||||
|
||||
float x = mod(v_linesofar / u_pattern_size.x, 1.0);
|
||||
float y = 0.5 + (v_normal.y * u_linewidth.s / u_pattern_size.y);
|
||||
vec2 pos = mix(u_pattern_tl, u_pattern_br, vec2(x, y));
|
||||
float x2 = mod(x * 2.0, 1.0);
|
||||
vec2 pos2 = mix(u_pattern_tl, u_pattern_br, vec2(x2, y));
|
||||
|
||||
vec4 color = texture2D(u_image, pos) * (1.0 - u_fade) + u_fade * texture2D(u_image, pos2);
|
||||
|
||||
color.rgb *= color.a;
|
||||
|
||||
gl_FragColor = color * alpha;
|
||||
}
|
||||
53
src/shader/linepattern.vertex.glsl
Normal file
53
src/shader/linepattern.vertex.glsl
Normal file
@@ -0,0 +1,53 @@
|
||||
// floor(127 / 2) == 63.0
|
||||
// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
|
||||
// stored in a byte (-128..127). we scale regular normals up to length 63, but
|
||||
// there are also "special" normals that have a bigger length (of up to 126 in
|
||||
// this case).
|
||||
#define scale 63.0
|
||||
|
||||
attribute vec2 a_pos;
|
||||
attribute vec2 a_extrude;
|
||||
attribute float a_linesofar;
|
||||
|
||||
// posmatrix 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;
|
||||
|
||||
varying vec2 v_normal;
|
||||
varying float v_linesofar;
|
||||
|
||||
void main() {
|
||||
// We store the texture normals in the most insignificant bit
|
||||
// transform y so that 0 => -1 and 1 => 1
|
||||
// In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap
|
||||
// y is 1 if the normal points up, and -1 if it points down
|
||||
vec2 normal = mod(a_pos, 2.0);
|
||||
normal.y = sign(normal.y - 0.5);
|
||||
v_normal = normal;
|
||||
|
||||
// Scale the extrusion vector down to a normal and then up by the line width
|
||||
// of this vertex.
|
||||
vec2 extrude = a_extrude / scale;
|
||||
vec4 dist = vec4(u_linewidth.s * extrude, 0.0, 0.0);
|
||||
|
||||
// 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);
|
||||
|
||||
z += step(1.0, v_normal.y);
|
||||
|
||||
// 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), 0.0, 1.0) + u_exmatrix * dist;
|
||||
v_linesofar = a_linesofar; // * u_ratio;
|
||||
}
|
||||
121
src/shader/linepattern_shader.cpp
Normal file
121
src/shader/linepattern_shader.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#include <mbgl/shader/linepattern_shader.hpp>
|
||||
#include <mbgl/shader/shaders.hpp>
|
||||
#include <mbgl/platform/gl.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
LinepatternShader::LinepatternShader()
|
||||
: Shader(
|
||||
"linepattern",
|
||||
shaders[LINEPATTERN_SHADER].vertex,
|
||||
shaders[LINEPATTERN_SHADER].fragment
|
||||
) {
|
||||
if (!valid) {
|
||||
fprintf(stderr, "invalid line pattern shader\n");
|
||||
return;
|
||||
}
|
||||
|
||||
a_pos = glGetAttribLocation(program, "a_pos");
|
||||
a_extrude = glGetAttribLocation(program, "a_extrude");
|
||||
a_linesofar = glGetAttribLocation(program, "a_linesofar");
|
||||
|
||||
u_matrix = glGetUniformLocation(program, "u_matrix");
|
||||
u_exmatrix = glGetUniformLocation(program, "u_exmatrix");
|
||||
u_linewidth = glGetUniformLocation(program, "u_linewidth");
|
||||
u_ratio = glGetUniformLocation(program, "u_ratio");
|
||||
u_pattern_size = glGetUniformLocation(program, "u_pattern_size");
|
||||
|
||||
u_pattern_tl = glGetUniformLocation(program, "u_pattern_tl");
|
||||
u_pattern_br = glGetUniformLocation(program, "u_pattern_br");
|
||||
u_offset = glGetUniformLocation(program, "u_offset");
|
||||
u_gamma = glGetUniformLocation(program, "u_gamma");
|
||||
u_fade = glGetUniformLocation(program, "u_fade");
|
||||
|
||||
//fprintf(stderr, "LinepatternShader:\n");
|
||||
//fprintf(stderr, " - u_matrix: %d\n", u_matrix);
|
||||
//fprintf(stderr, " - u_exmatrix: %d\n", u_exmatrix);
|
||||
//fprintf(stderr, " - u_linewidth: %d\n", u_linewidth);
|
||||
//fprintf(stderr, " - u_ratio: %d\n", u_ratio);
|
||||
//fprintf(stderr, " - u_pattern_size: %d\n", u_pattern_size);
|
||||
//fprintf(stderr, " - u_pattern_tl: %d\n", u_pattern_tl);
|
||||
//fprintf(stderr, " - u_pattern_br: %d\n", u_pattern_br);
|
||||
//fprintf(stderr, " - u_offset: %d\n", u_offset);
|
||||
//fprintf(stderr, " - u_gamma: %d\n", u_gamma);
|
||||
}
|
||||
|
||||
void LinepatternShader::bind(char *offset) {
|
||||
glEnableVertexAttribArray(a_pos);
|
||||
glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0);
|
||||
|
||||
glEnableVertexAttribArray(a_extrude);
|
||||
glVertexAttribPointer(a_extrude, 2, GL_BYTE, false, 8, offset + 4);
|
||||
|
||||
glEnableVertexAttribArray(a_linesofar);
|
||||
glVertexAttribPointer(a_linesofar, 1, GL_SHORT, false, 8, offset + 6);
|
||||
}
|
||||
|
||||
void LinepatternShader::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 LinepatternShader::setPatternSize(const std::array<float, 2>& new_pattern_size) {
|
||||
if (pattern_size != new_pattern_size) {
|
||||
glUniform2fv(u_pattern_size, 1, new_pattern_size.data());
|
||||
pattern_size = new_pattern_size;
|
||||
}
|
||||
}
|
||||
|
||||
void LinepatternShader::setLineWidth(const std::array<float, 2>& new_linewidth) {
|
||||
if (linewidth != new_linewidth) {
|
||||
glUniform2fv(u_linewidth, 1, new_linewidth.data());
|
||||
linewidth = new_linewidth;
|
||||
}
|
||||
}
|
||||
|
||||
void LinepatternShader::setRatio(float new_ratio) {
|
||||
if (ratio != new_ratio) {
|
||||
glUniform1f(u_ratio, new_ratio);
|
||||
ratio = new_ratio;
|
||||
}
|
||||
}
|
||||
|
||||
void LinepatternShader::setPatternTopLeft(const std::array<float, 2>& new_pattern_tl) {
|
||||
if (pattern_tl != new_pattern_tl) {
|
||||
glUniform2fv(u_pattern_tl, 1, new_pattern_tl.data());
|
||||
pattern_tl = new_pattern_tl;
|
||||
}
|
||||
}
|
||||
|
||||
void LinepatternShader::setPatternBottomRight(const std::array<float, 2>& new_pattern_br) {
|
||||
if (pattern_br != new_pattern_br) {
|
||||
glUniform2fv(u_pattern_br, 1, new_pattern_br.data());
|
||||
pattern_br = new_pattern_br;
|
||||
}
|
||||
}
|
||||
|
||||
void LinepatternShader::setOffset(const std::array<float, 2>& new_offset) {
|
||||
if (offset != new_offset) {
|
||||
glUniform2fv(u_offset, 1, new_offset.data());
|
||||
offset = new_offset;
|
||||
}
|
||||
}
|
||||
|
||||
void LinepatternShader::setGamma(float new_gamma) {
|
||||
if (gamma != new_gamma) {
|
||||
glUniform1f(u_gamma, new_gamma);
|
||||
gamma = new_gamma;
|
||||
}
|
||||
}
|
||||
|
||||
void LinepatternShader::setFade(float new_fade) {
|
||||
if (fade != new_fade) {
|
||||
glUniform1f(u_fade, new_fade);
|
||||
fade = new_fade;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user