fix rendering errors

This commit is contained in:
Konstantin Käfer
2014-12-11 11:58:46 +01:00
parent f8ba23377d
commit febfb7ac09
6 changed files with 33 additions and 31 deletions

View File

@@ -77,15 +77,12 @@ void copy_bitmap(const uint32_t *src, const int src_stride, const int src_x, con
}
}
Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(size_t pixel_width, size_t pixel_height) {
uint16_t pack_width = pixel_width + buffer * 2;
uint16_t pack_height = pixel_height + buffer * 2;
Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(const size_t pixel_width, const size_t pixel_height) {
// Increase to next number divisible by 4, but at least 1.
// This is so we can scale down the texture coordinates and pack them
// into 2 bytes rather than 4 bytes.
pack_width += (4 - pack_width % 4);
pack_height += (4 - pack_height % 4);
const uint16_t pack_width = pixel_width + (4 - pixel_width % 4);
const uint16_t pack_height = pixel_height + (4 - pixel_width % 4);
// We have to allocate a new area in the bin, and store an empty image in it.
// Add a 1px border around every image.
@@ -96,8 +93,6 @@ Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(size_t pixel_width, size
rect.x += buffer;
rect.y += buffer;
rect.w = pixel_width;
rect.h = pixel_height;
return rect;
}
@@ -132,15 +127,24 @@ Rect<SpriteAtlas::dimension> SpriteAtlas::getImage(const std::string& name) {
SpriteAtlasPosition SpriteAtlas::getPosition(const std::string& name, bool repeating) {
std::lock_guard<std::recursive_mutex> lock(mtx);
// `repeating` indicates that the image will be used in a repeating pattern
// repeating pattern images are assumed to have a 1px padding that mirrors the opposite edge
// positions for repeating images are adjusted to exclude the edge
Rect<dimension> rect = getImage(name);
const int r = repeating ? 1 : 0;
if (repeating) {
// When the image is repeating, get the correct position of the image, rather than the
// one rounded up to 4 pixels.
const SpritePosition &pos = sprite->getSpritePosition(name);
if (!pos.width || !pos.height) {
return SpriteAtlasPosition {};
}
rect.w = pos.width;
rect.h = pos.height;
}
return SpriteAtlasPosition {
{{ float(rect.w) / pixelRatio, float(rect.h) / pixelRatio }},
{{ float(rect.x + r) / width, float(rect.y + r) / height }},
{{ float(rect.x + rect.w - 2*r) / width, float(rect.y + rect.h - 2*r) / height }}
{{ float(rect.w) / pixelRatio, float(rect.h) / pixelRatio }},
{{ float(rect.x) / width, float(rect.y) / height }},
{{ float(rect.x + rect.w) / width, float(rect.y + rect.h) / height }}
};
}

View File

@@ -61,7 +61,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr<StyleLayer> layer_desc, c
}
if (properties.image.size()) {
SpriteAtlasPosition imagePos = spriteAtlas.getPosition(properties.image);
SpriteAtlasPosition imagePos = spriteAtlas.getPosition(properties.image, true);
float factor = 8.0 / std::pow(2, state.getIntegerZoom() - id.z);
float fade = std::fmod(state.getZoom(), 1.0);
@@ -79,6 +79,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr<StyleLayer> layer_desc, c
linepatternShader->u_pattern_br = imagePos.br;
linepatternShader->u_fade = fade;
glActiveTexture(GL_TEXTURE0);
spriteAtlas.bind(true);
MBGL_CHECK_ERROR(glDepthRange(strata + strata_epsilon, 1.0f)); // may or may not matter

View File

@@ -3,11 +3,11 @@
// 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
#define scale 0.015873016
attribute vec2 a_pos;
attribute vec2 a_extrude;
attribute float a_linesofar;
attribute vec4 a_data;
// matrix is for the vertex position, exmatrix is for rotating and projecting
// the extrusion vector.
@@ -24,6 +24,9 @@ varying vec2 v_normal;
varying float v_linesofar;
void main() {
vec2 a_extrude = a_data.xy;
float a_linesofar = a_data.z * 128.0 + a_data.w;
// 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
@@ -34,7 +37,7 @@ 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;
vec2 extrude = a_extrude * scale;
vec2 dist = u_linewidth.s * extrude * (1.0 - u_point);
// If the x coordinate is the maximum integer, we move the z coordinates out

View File

@@ -1,7 +1,6 @@
#include <mbgl/shader/linepattern_shader.hpp>
#include <mbgl/shader/shaders.hpp>
#include <mbgl/platform/gl.hpp>
#include <iostream>
#include <cstdio>
@@ -19,17 +18,13 @@ LinepatternShader::LinepatternShader()
}
a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_pos"));
a_extrude = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_extrude"));
a_linesofar = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_linesofar"));
a_data = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data"));
}
void LinepatternShader::bind(char *offset) {
MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos));
MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0));
MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_extrude));
MBGL_CHECK_ERROR(glVertexAttribPointer(a_extrude, 2, GL_BYTE, false, 8, offset + 4));
MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_linesofar));
MBGL_CHECK_ERROR(glVertexAttribPointer(a_linesofar, 1, GL_SHORT, false, 8, offset + 6));
MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data));
MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_BYTE, false, 8, offset + 4));
}

View File

@@ -25,8 +25,7 @@ public:
private:
int32_t a_pos = -1;
int32_t a_extrude = -1;
int32_t a_linesofar = -1;
int32_t a_data = -1;
};
}

View File

@@ -220,7 +220,7 @@ INSTANTIATE_TEST_CASE_P(Headless, HeadlessTest, ::testing::ValuesIn([] {
for (dirent *dp = nullptr; (dp = readdir(dir)) != nullptr;) {
const std::string name = dp->d_name;
if (name != "." && name != ".." && name != "index.html") {
if (name != "index.html" && !(name.size() >= 1 && name[0] == '.')) {
names.push_back(name);
}
}