Make renderLine more similar to JS

This commit is contained in:
John Firebaugh
2014-08-29 15:31:02 -07:00
parent 71c31cee24
commit 586cb9144a
5 changed files with 51 additions and 48 deletions

View File

@@ -14,12 +14,11 @@ public:
void setExtrudeMatrix(const std::array<float, 16>& exmatrix);
void setLineWidth(const std::array<float, 2>& linewidth);
void setRatio(float ratio);
void setDebug(float debug);
void setPatternSize(const std::array<float, 2>& new_pattern_size);
void setPatternTopLeft(const std::array<float, 2>& new_pattern_tl);
void setPatternBottomRight(const std::array<float, 2>& new_pattern_br);
void setOffset(const std::array<float, 2>& offset);
void setGamma(float new_gamma);
void setPoint(float new_point);
void setBlur(float new_blur);
void setFade(float new_fade);
private:
@@ -45,11 +44,11 @@ private:
std::array<float, 2> pattern_br = {{}};
int32_t u_pattern_br = -1;
std::array<float, 2> offset = {{}};
int32_t u_offset = -1;
float point = 0.0f;
int32_t u_point = -1;
float gamma = 0.0f;
int32_t u_gamma = -1;
float blur = 0.0f;
int32_t u_blur = -1;
float fade = 0;
int32_t u_fade = -1;

View File

@@ -15,9 +15,9 @@ void Painter::renderLine(LineBucket& bucket, std::shared_ptr<StyleLayer> layer_d
const LineProperties &properties = layer_desc->getProperties<LineProperties>();
float antialiasing = 1 / map.getState().getPixelRatio();
float width = properties.width;
float offset = properties.offset / 2;
float antialiasing = 1 / map.getState().getPixelRatio();
float blur = properties.blur + antialiasing;
// These are the radii of the line. We are limiting it to 16, which will result
@@ -34,6 +34,7 @@ void Painter::renderLine(LineBucket& bucket, std::shared_ptr<StyleLayer> layer_d
float dash_length = properties.dash_array[0];
float dash_gap = properties.dash_array[1];
float ratio = map.getState().getPixelRatio();
mat4 vtxMatrix = translatedMatrix(matrix, properties.translate, id, properties.translateAnchor);
depthRange(strata, 1.0f);
@@ -67,29 +68,23 @@ void Painter::renderLine(LineBucket& bucket, std::shared_ptr<StyleLayer> layer_d
if (properties.image.size() && sprite) {
SpriteAtlas &spriteAtlas = *map.getSpriteAtlas();
Rect<uint16_t> imagePos = spriteAtlas.getImage(properties.image, *sprite);
float factor = 8.0 / std::pow(2, map.getState().getIntegerZoom() - id.z);
float fade = std::fmod(map.getState().getZoom(), 1.0);
std::array<float, 2> imageSize = {{
imagePos.w * factor,
imagePos.h * factor
}
};
}};
std::array<float, 2> offset = {{
(float)std::fmod(id.x * 4096, imageSize[0]),
(float)std::fmod(id.y * 4096, imageSize[1])
}
};
useProgram(linepatternShader->program);
linepatternShader->setMatrix(vtxMatrix);
linepatternShader->setExtrudeMatrix(extrudeMatrix);
lineShader->setDashArray({{ dash_length, dash_gap }});
linepatternShader->setLineWidth({{ outset, inset }});
linepatternShader->setRatio(map.getState().getPixelRatio());
linepatternShader->setRatio(ratio);
linepatternShader->setBlur(blur);
linepatternShader->setPatternSize(imageSize);
linepatternShader->setPatternTopLeft({{
float(imagePos.x) / spriteAtlas.getWidth(),
@@ -99,8 +94,6 @@ void Painter::renderLine(LineBucket& bucket, std::shared_ptr<StyleLayer> layer_d
float(imagePos.x + imagePos.w) / spriteAtlas.getWidth(),
float(imagePos.y + imagePos.h) / spriteAtlas.getHeight(),
}});
linepatternShader->setOffset(offset);
linepatternShader->setGamma(map.getState().getPixelRatio());
linepatternShader->setFade(fade);
spriteAtlas.bind(true);
@@ -110,13 +103,16 @@ void Painter::renderLine(LineBucket& bucket, std::shared_ptr<StyleLayer> layer_d
} else {
useProgram(lineShader->program);
lineShader->setMatrix(vtxMatrix);
lineShader->setExtrudeMatrix(extrudeMatrix);
lineShader->setDashArray({{ dash_length, dash_gap }});
lineShader->setLineWidth({{ outset, inset }});
lineShader->setRatio(map.getState().getPixelRatio());
lineShader->setRatio(ratio);
lineShader->setBlur(blur);
lineShader->setColor(color);
lineShader->setDashArray({{ dash_length, dash_gap }});
bucket.drawLines(*lineShader);
}
}

View File

@@ -1,10 +1,11 @@
uniform vec2 u_linewidth;
uniform float u_gamma;
uniform float u_fade;
uniform float u_point;
uniform float u_blur;
uniform vec2 u_pattern_size;
uniform vec2 u_pattern_tl;
uniform vec2 u_pattern_br;
uniform float u_fade;
uniform sampler2D u_image;
@@ -13,12 +14,14 @@ 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);
float dist = length(v_normal) * (1.0 - u_point) + u_point * length(gl_PointCoord * 2.0 - 1.0);
dist *= u_linewidth.s;
// 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 alpha = clamp(min(dist - (u_linewidth.t - u_blur), u_linewidth.s - dist) / u_blur, 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);
@@ -28,7 +31,7 @@ void main() {
vec4 color = texture2D(u_image, pos) * (1.0 - u_fade) + u_fade * texture2D(u_image, pos2);
color.rgb *= color.a;
color.rgb *= color.a; // premultiply
gl_FragColor = color * alpha;
}

View File

@@ -3,7 +3,7 @@
// 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
#define scale 63.0
attribute vec2 a_pos;
attribute vec2 a_extrude;
@@ -11,13 +11,14 @@ 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_posmatrix;
uniform mat4 u_exmatrix;
// shared
uniform float u_ratio;
uniform vec2 u_linewidth;
uniform vec4 u_color;
uniform float u_point;
varying vec2 v_normal;
varying float v_linesofar;
@@ -34,20 +35,23 @@ void main() {
// 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);
vec2 dist = u_linewidth.s * extrude * (1.0 - u_point);
// 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);
// When drawing points, skip every other vertex
z += u_point * 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_posmatrix * vec4(floor(a_pos / 2.0), 0.0, 1.0) + u_exmatrix * vec4(dist, z, 0.0);
v_linesofar = a_linesofar;// * u_ratio;
gl_Position = u_matrix * vec4(floor(a_pos / 2.0), 0.0, 1.0) + u_exmatrix * dist;
v_linesofar = a_linesofar; // * u_ratio;
gl_PointSize = 2.0 * u_linewidth.s - 1.0;
}

View File

@@ -22,7 +22,7 @@ LinepatternShader::LinepatternShader()
a_extrude = glGetAttribLocation(program, "a_extrude");
a_linesofar = glGetAttribLocation(program, "a_linesofar");
u_matrix = glGetUniformLocation(program, "u_matrix");
u_matrix = glGetUniformLocation(program, "u_posmatrix");
u_exmatrix = glGetUniformLocation(program, "u_exmatrix");
u_linewidth = glGetUniformLocation(program, "u_linewidth");
u_ratio = glGetUniformLocation(program, "u_ratio");
@@ -30,8 +30,8 @@ LinepatternShader::LinepatternShader()
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_point = glGetUniformLocation(program, "u_point");
u_blur = glGetUniformLocation(program, "u_blur");
u_fade = glGetUniformLocation(program, "u_fade");
//fprintf(stderr, "LinepatternShader:\n");
@@ -42,8 +42,9 @@ LinepatternShader::LinepatternShader()
//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);
//fprintf(stderr, " - u_point: %d\n", u_point);
//fprintf(stderr, " - u_blur: %d\n", u_blur);
//fprintf(stderr, " - u_fade: %d\n", u_fade);
}
void LinepatternShader::bind(char *offset) {
@@ -99,17 +100,17 @@ void LinepatternShader::setPatternBottomRight(const std::array<float, 2>& new_pa
}
}
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::setPoint(float new_point) {
if (point != new_point) {
glUniform1f(u_point, new_point);
point = new_point;
}
}
void LinepatternShader::setGamma(float new_gamma) {
if (gamma != new_gamma) {
glUniform1f(u_gamma, new_gamma);
gamma = new_gamma;
void LinepatternShader::setBlur(float new_blur) {
if (blur != new_blur) {
glUniform1f(u_blur, new_blur);
blur = new_blur;
}
}