remove in-shader label flipping

This commit is contained in:
Ansis Brammanis
2015-03-30 17:48:52 -07:00
parent fd5b2dbed0
commit 19ddaae6cc
11 changed files with 17 additions and 90 deletions

View File

@@ -6,9 +6,7 @@
namespace mbgl {
const double IconVertexBuffer::angleFactor = 128.0 / M_PI;
size_t IconVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, int16_t tx, int16_t ty, float angle, float minzoom, std::array<float, 2> range, float maxzoom, float labelminzoom) {
size_t IconVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, int16_t tx, int16_t ty, float minzoom, float maxzoom, float labelminzoom) {
const size_t idx = index();
void *data = addElement();
@@ -23,13 +21,10 @@ size_t IconVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, int16_t t
ubytes[8] /* tex */ = tx / 4;
ubytes[9] /* tex */ = ty / 4;
ubytes[10] /* labelminzoom */ = labelminzoom * 10;
ubytes[11] /* angle */ = (int16_t)std::round(angle * angleFactor) % 256;
// a_data2
ubytes[12] /* minzoom */ = minzoom * 10; // 1/10 zoom levels: z16 == 160.
ubytes[13] /* maxzoom */ = std::fmin(maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160.
ubytes[14] /* rangeend */ = util::max((int16_t)std::round(range[0] * angleFactor), (int16_t)0) % 256;
ubytes[15] /* rangestart */ = util::min((int16_t)std::round(range[1] * angleFactor), (int16_t)255) % 256;
return idx;
}

View File

@@ -11,9 +11,7 @@ namespace mbgl {
16
> {
public:
static const double angleFactor;
size_t add(int16_t x, int16_t y, float ox, float oy, int16_t tx, int16_t ty, float angle, float minzoom, std::array<float, 2> range, float maxzoom, float labelminzoom);
size_t add(int16_t x, int16_t y, float ox, float oy, int16_t tx, int16_t ty, float minzoom, float maxzoom, float labelminzoom);
};

View File

@@ -6,9 +6,7 @@
namespace mbgl {
const double TextVertexBuffer::angleFactor = 128.0 / M_PI;
size_t TextVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float angle, float minzoom, std::array<float, 2> range, float maxzoom, float labelminzoom) {
size_t TextVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float minzoom, float maxzoom, float labelminzoom) {
const size_t idx = index();
void *data = addElement();
@@ -23,13 +21,10 @@ size_t TextVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, uint16_t
ubytes[8] /* tex */ = tx / 4;
ubytes[9] /* tex */ = ty / 4;
ubytes[10] /* labelminzoom */ = labelminzoom * 10;
ubytes[11] /* angle */ = (int16_t)std::round(angle * angleFactor) % 256;
// a_data2
ubytes[12] /* minzoom */ = minzoom * 10; // 1/10 zoom levels: z16 == 160.
ubytes[13] /* maxzoom */ = std::fmin(maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160.
ubytes[14] /* rangeend */ = util::max((int16_t)std::round(range[0] * angleFactor), (int16_t)0) % 256;
ubytes[15] /* rangestart */ = util::min((int16_t)std::round(range[1] * angleFactor), (int16_t)255) % 256;
return idx;
}

View File

@@ -14,9 +14,7 @@ class TextVertexBuffer : public Buffer <
public:
typedef int16_t vertex_type;
static const double angleFactor;
size_t add(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float angle, float minzoom, std::array<float, 2> range, float maxzoom, float labelminzoom);
size_t add(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float minzoom, float maxzoom, float labelminzoom);
};

View File

@@ -44,14 +44,9 @@ void Painter::renderSDF(SymbolBucket &bucket,
sdfShader.u_exmatrix = exMatrix;
sdfShader.u_texsize = texsize;
// Convert the -pi..pi to an int8 range.
float angle = std::round(state.getAngle() / M_PI * 128);
// adjust min/max zooms for variable font sies
float zoomAdjust = std::log(fontSize / bucketProperties.max_size) / std::log(2);
sdfShader.u_flip = (aligned_with_map && bucketProperties.keep_upright) ? 1 : 0;
sdfShader.u_angle = (int32_t)(angle + 256) % 256;
sdfShader.u_zoom = (state.getNormalizedZoom() - zoomAdjust) * 10; // current zoom level
FadeProperties f = frameHistory.getFadeProperties(std::chrono::milliseconds(300));
@@ -165,19 +160,10 @@ void Painter::renderSymbol(SymbolBucket &bucket, const StyleLayer &layer_desc, c
iconShader->u_exmatrix = exMatrix;
iconShader->u_texsize = {{ float(spriteAtlas.getWidth()) / 4.0f, float(spriteAtlas.getHeight()) / 4.0f }};
// Convert the -pi..pi to an int8 range.
const float angle = std::round(state.getAngle() / M_PI * 128);
// adjust min/max zooms for variable font sies
float zoomAdjust = std::log(fontSize / layout.icon.max_size) / std::log(2);
iconShader->u_angle = (int32_t)(angle + 256) % 256;
bool flip = (layout.icon.rotation_alignment == RotationAlignmentType::Map)
&& layout.icon.keep_upright;
iconShader->u_flip = flip ? 1 : 0;
iconShader->u_zoom = (state.getNormalizedZoom() - zoomAdjust) * 10; // current zoom level
iconShader->u_fadedist = 0 * 10;
iconShader->u_minfadezoom = state.getNormalizedZoom() * 10;
iconShader->u_maxfadezoom = state.getNormalizedZoom() * 10;

View File

@@ -344,21 +344,20 @@ void SymbolBucket::addFeature(const std::vector<Coordinate> &line, const Shaping
collision.insert(glyphPlacement.boxes, anchor, glyphScale, glyphRange,
horizontalText);
}
if (inside) addSymbols<TextBuffer, TextElementGroup>(text, glyphPlacement.shapes, glyphScale, glyphRange);
if (inside) addSymbols<TextBuffer, TextElementGroup>(text, glyphPlacement.shapes, glyphScale);
}
if (iconScale && std::isfinite(iconScale)) {
if (!layout.icon.ignore_placement) {
collision.insert(iconPlacement.boxes, anchor, iconScale, iconRange, horizontalIcon);
}
if (inside) addSymbols<IconBuffer, IconElementGroup>(icon, iconPlacement.shapes, iconScale, iconRange);
if (inside) addSymbols<IconBuffer, IconElementGroup>(icon, iconPlacement.shapes, iconScale);
}
}
}
template <typename Buffer, typename GroupType>
void SymbolBucket::addSymbols(Buffer &buffer, const PlacedGlyphs &symbols, float scale,
PlacementRange placementRange) {
void SymbolBucket::addSymbols(Buffer &buffer, const PlacedGlyphs &symbols, float scale) {
const float zoom = collision.zoom;
const float placementZoom = std::log(scale) / std::log(2) + zoom;
@@ -369,7 +368,6 @@ void SymbolBucket::addSymbols(Buffer &buffer, const PlacedGlyphs &symbols, float
const auto &bl = symbol.bl;
const auto &br = symbol.br;
const auto &tex = symbol.tex;
const auto &angle = symbol.angle;
float minZoom =
util::max(static_cast<float>(zoom + log(symbol.minScale) / log(2)), placementZoom);
@@ -400,14 +398,14 @@ void SymbolBucket::addSymbols(Buffer &buffer, const PlacedGlyphs &symbols, float
uint32_t triangleIndex = triangleGroup.vertex_length;
// coordinates (2 triangles)
buffer.vertices.add(glyphAnchor.x, glyphAnchor.y, tl.x, tl.y, tex.x, tex.y, angle, minZoom,
placementRange, maxZoom, placementZoom);
buffer.vertices.add(glyphAnchor.x, glyphAnchor.y, tr.x, tr.y, tex.x + tex.w, tex.y, angle,
minZoom, placementRange, maxZoom, placementZoom);
buffer.vertices.add(glyphAnchor.x, glyphAnchor.y, bl.x, bl.y, tex.x, tex.y + tex.h, angle,
minZoom, placementRange, maxZoom, placementZoom);
buffer.vertices.add(glyphAnchor.x, glyphAnchor.y, tl.x, tl.y, tex.x, tex.y, minZoom,
maxZoom, placementZoom);
buffer.vertices.add(glyphAnchor.x, glyphAnchor.y, tr.x, tr.y, tex.x + tex.w, tex.y,
minZoom, maxZoom, placementZoom);
buffer.vertices.add(glyphAnchor.x, glyphAnchor.y, bl.x, bl.y, tex.x, tex.y + tex.h,
minZoom, maxZoom, placementZoom);
buffer.vertices.add(glyphAnchor.x, glyphAnchor.y, br.x, br.y, tex.x + tex.w, tex.y + tex.h,
angle, minZoom, placementRange, maxZoom, placementZoom);
minZoom, maxZoom, placementZoom);
// add the two triangles, referencing the four coordinates we just inserted.
buffer.triangles.add(triangleIndex + 0, triangleIndex + 1, triangleIndex + 2);

View File

@@ -87,7 +87,7 @@ private:
// Adds placed items to the buffer.
template <typename Buffer, typename GroupType>
void addSymbols(Buffer &buffer, const PlacedGlyphs &symbols, float scale, PlacementRange placementRange);
void addSymbols(Buffer &buffer, const PlacedGlyphs &symbols, float scale);
public:
const std::unique_ptr<const StyleLayoutSymbol> styleLayout;

View File

@@ -8,9 +8,7 @@ attribute vec4 a_data2;
// the extrusion vector.
uniform mat4 u_matrix;
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;
@@ -29,24 +27,11 @@ void main() {
vec2 a_zoom = a_data2.st;
float a_minzoom = a_zoom[0];
float a_maxzoom = a_zoom[1];
vec2 a_range = a_data2.pq;
float a_rangeend = a_range[0];
float a_rangestart = a_range[1];
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;
float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom));
// fade out labels
float alpha = clamp((u_fadezoom - a_labelminzoom) / u_fadedist, 0.0, 1.0);
@@ -66,10 +51,6 @@ void main() {
// 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_matrix * vec4(a_pos, 0, 1) + u_exmatrix * vec4(a_offset / 64.0, z, 0);
v_tex = a_tex / u_texsize;

View File

@@ -14,9 +14,7 @@ public:
UniformMatrix<4> u_matrix = {"u_matrix", *this};
UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this};
Uniform<float> u_angle = {"u_angle", *this};
Uniform<float> u_zoom = {"u_zoom", *this};
Uniform<float> u_flip = {"u_flip", *this};
Uniform<float> u_fadedist = {"u_fadedist", *this};
Uniform<float> u_minfadezoom = {"u_minfadezoom", *this};
Uniform<float> u_maxfadezoom = {"u_maxfadezoom", *this};

View File

@@ -8,9 +8,7 @@ attribute vec4 a_data2;
// the extrusion vector.
uniform mat4 u_matrix;
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;
@@ -28,23 +26,9 @@ void main() {
vec2 a_zoom = a_data2.st;
float a_minzoom = a_zoom[0];
float a_maxzoom = a_zoom[1];
vec2 a_range = a_data2.pq;
float a_rangeend = a_range[0];
float a_rangestart = a_range[1];
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;
float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom));
// fade out labels
float alpha = clamp((u_fadezoom - a_labelminzoom) / u_fadedist, 0.0, 1.0);
@@ -64,10 +48,6 @@ void main() {
// 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_matrix * vec4(a_pos, 0, 1) + u_exmatrix * vec4(a_offset / 64.0, z, 0);
v_tex = a_tex / u_texsize;
}

View File

@@ -18,9 +18,7 @@ public:
Uniform<std::array<float, 2>> u_texsize = {"u_texsize", *this};
Uniform<float> u_buffer = {"u_buffer", *this};
Uniform<float> u_gamma = {"u_gamma", *this};
Uniform<float> u_angle = {"u_angle", *this};
Uniform<float> u_zoom = {"u_zoom", *this};
Uniform<float> u_flip = {"u_flip", *this};
Uniform<float> u_fadedist = {"u_fadedist", *this};
Uniform<float> u_minfadezoom = {"u_minfadezoom", *this};
Uniform<float> u_maxfadezoom = {"u_maxfadezoom", *this};