Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
First pass at port of mapbox/mapbox-gl-js#2668
Browse files Browse the repository at this point in the history
  • Loading branch information
yhahn committed Jun 10, 2016
1 parent a8df0fe commit f4b6411
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 31 deletions.
3 changes: 3 additions & 0 deletions include/mbgl/style/layers/symbol_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class SymbolLayer : public Layer {
PropertyValue<std::array<float, 2>> getIconOffset() const;
void setIconOffset(PropertyValue<std::array<float, 2>>);

PropertyValue<RotationAlignmentType> getTextPitchAlignment() const;
void setTextPitchAlignment(PropertyValue<RotationAlignmentType>);

PropertyValue<RotationAlignmentType> getTextRotationAlignment() const;
void setTextRotationAlignment(PropertyValue<RotationAlignmentType>);

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"csscolorparser": "^1.0.2",
"ejs": "^2.4.1",
"express": "^4.11.1",
"mapbox-gl-shaders": "mapbox/mapbox-gl-shaders#f0b94dcc5f782958e9aade61d592a207d8a46e0f",
"mapbox-gl-style-spec": "^8.5.1",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#3e27bc27ea050952481ef1f9a655538590eccb26",
"mapbox-gl-shaders": "mapbox/mapbox-gl-shaders#adcc18ad79b363ff17443d746d26c2ecee6bcb5e",
"mapbox-gl-style-spec": "mapbox/mapbox-gl-style-spec#586dbf48f7ebd79ed623c5ad809a564013173acf",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#fd5e956e948fb430d1b56659c6bd93d3d27b8962",
"node-gyp": "^3.3.1",
"request": "^2.72.0",
"tape": "^4.5.1"
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ global.propertyType = function (property) {
if (/-translate-anchor$/.test(property.name)) {
return 'TranslateAnchorType';
}
if (/-rotation-alignment$/.test(property.name)) {
if (/-(rotation|pitch)-alignment$/.test(property.name)) {
return 'RotationAlignmentType';
}
switch (property.type) {
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/geometry/icon_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace mbgl {

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) {
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, uint8_t labelangle) {
const size_t idx = index();
void *data = addElement();

Expand All @@ -21,6 +21,7 @@ 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] /* labelangle */ = labelangle;

// a_data2
ubytes[12] /* minzoom */ = minzoom * 10; // 1/10 zoom levels: z16 == 160.
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/geometry/icon_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace mbgl {
16
> {
public:
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);
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, uint8_t labelangle);

};

Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/geometry/text_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace mbgl {

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) {
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, uint8_t labelangle) {
const size_t idx = index();
void *data = addElement();

Expand All @@ -21,6 +21,7 @@ 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] /* labelangle */ = labelangle;

// a_data2
ubytes[12] /* minzoom */ = minzoom * 10; // 1/10 zoom levels: z16 == 160.
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/geometry/text_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TextVertexBuffer : public Buffer <
public:
typedef int16_t vertex_type;

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);
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, uint8_t labelangle);
};


Expand Down
1 change: 1 addition & 0 deletions src/mbgl/renderer/painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class Painter : private util::noncopyable {

// Layout
style::RotationAlignmentType rotationAlignment,
style::RotationAlignmentType pitchAlignment,
float layoutSize,

// Paint
Expand Down
18 changes: 14 additions & 4 deletions src/mbgl/renderer/painter_symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void Painter::renderSDF(SymbolBucket &bucket,

// Layout
RotationAlignmentType rotationAlignment,
RotationAlignmentType pitchAlignment,
float layoutSize,

// Paint
Expand All @@ -45,10 +46,11 @@ void Painter::renderSDF(SymbolBucket &bucket,

float scale = fontScale;
std::array<float, 2> exScale = extrudeScale;
bool alignedWithMap = rotationAlignment == RotationAlignmentType::Map;
bool rotateWithMap = rotationAlignment == RotationAlignmentType::Map;
bool pitchWithMap = pitchAlignment == RotationAlignmentType::Map;
float gammaScale = 1.0f;

if (alignedWithMap) {
if (pitchWithMap) {
scale *= tileID.pixelsToTileUnits(1, state.getZoom());
exScale.fill(scale);
gammaScale /= std::cos(state.getPitch());
Expand All @@ -60,8 +62,12 @@ void Painter::renderSDF(SymbolBucket &bucket,
sdfShader.u_matrix = vtxMatrix;
sdfShader.u_extrude_scale = exScale;
sdfShader.u_texsize = texsize;
sdfShader.u_skewed = alignedWithMap;
sdfShader.u_rotate_with_map = rotateWithMap;
sdfShader.u_pitch_with_map = pitchWithMap;
sdfShader.u_texture = 0;
sdfShader.u_pitch = state.getPitch() * util::DEG2RAD;
sdfShader.u_bearing = -1.0f * state.getAngle();
sdfShader.u_aspect_ratio = (state.getWidth() * 1.0f) / (state.getHeight() * 1.0f);

// adjust min/max zooms for variable font sies
float zoomAdjust = std::log(fontSize / layoutSize) / std::log(2);
Expand Down Expand Up @@ -167,6 +173,9 @@ void Painter::renderSymbol(SymbolBucket& bucket,
*sdfIconShader,
&SymbolBucket::drawIcons,
layout.iconRotationAlignment,
// icon-pitch-alignment is not yet implemented
// and we simply inherit the rotation alignment
layout.iconRotationAlignment,
layout.iconSize,
paint.iconOpacity,
paint.iconColor,
Expand Down Expand Up @@ -194,7 +203,7 @@ void Painter::renderSymbol(SymbolBucket& bucket,
iconShader->u_matrix = vtxMatrix;
iconShader->u_extrude_scale = exScale;
iconShader->u_texsize = {{ float(activeSpriteAtlas->getWidth()) / 4.0f, float(activeSpriteAtlas->getHeight()) / 4.0f }};
iconShader->u_skewed = alignedWithMap;
iconShader->u_rotate_with_map = alignedWithMap;
iconShader->u_texture = 0;

// adjust min/max zooms for variable font sies
Expand Down Expand Up @@ -230,6 +239,7 @@ void Painter::renderSymbol(SymbolBucket& bucket,
*sdfGlyphShader,
&SymbolBucket::drawGlyphs,
layout.textRotationAlignment,
layout.textPitchAlignment,
layout.textSize,
paint.textOpacity,
paint.textColor,
Expand Down
13 changes: 8 additions & 5 deletions src/mbgl/renderer/symbol_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void SymbolBucket::addSymbols(Buffer &buffer, const SymbolQuads &symbols, float
const auto &anchorPoint = symbol.anchorPoint;

// drop upside down versions of glyphs
const float a = std::fmod(symbol.angle + placementAngle + M_PI, M_PI * 2);
const float a = std::fmod(symbol.anchorAngle + placementAngle + M_PI, M_PI * 2);
if (keepUpright && alongLine && (a <= M_PI / 2 || a > M_PI * 3 / 2)) continue;


Expand All @@ -521,15 +521,18 @@ void SymbolBucket::addSymbols(Buffer &buffer, const SymbolQuads &symbols, float
auto &triangleGroup = *buffer.groups.back();
GLsizei triangleIndex = triangleGroup.vertex_length;

// Encode angle of glyph
uint8_t glyphAngle = std::round((symbol.glyphAngle / (M_PI * 2)) * 256);

// coordinates (2 triangles)
buffer.vertices.add(anchorPoint.x, anchorPoint.y, tl.x, tl.y, tex.x, tex.y, minZoom,
maxZoom, placementZoom);
maxZoom, placementZoom, glyphAngle);
buffer.vertices.add(anchorPoint.x, anchorPoint.y, tr.x, tr.y, tex.x + tex.w, tex.y,
minZoom, maxZoom, placementZoom);
minZoom, maxZoom, placementZoom, glyphAngle);
buffer.vertices.add(anchorPoint.x, anchorPoint.y, bl.x, bl.y, tex.x, tex.y + tex.h,
minZoom, maxZoom, placementZoom);
minZoom, maxZoom, placementZoom, glyphAngle);
buffer.vertices.add(anchorPoint.x, anchorPoint.y, br.x, br.y, tex.x + tex.w, tex.y + tex.h,
minZoom, maxZoom, placementZoom);
minZoom, maxZoom, placementZoom, glyphAngle);

// add the two triangles, referencing the four coordinates we just inserted.
buffer.triangles.add(triangleIndex + 0, triangleIndex + 1, triangleIndex + 2);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/shader/icon_shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class IconShader : public Shader {
Uniform<GLfloat> u_zoom = {"u_zoom", *this};
Uniform<GLfloat> u_opacity = {"u_opacity", *this};
Uniform<std::array<GLfloat, 2>> u_texsize = {"u_texsize", *this};
Uniform<GLint> u_skewed = {"u_skewed", *this};
Uniform<GLint> u_rotate_with_map = {"u_rotate_with_map", *this};
Uniform<GLint> u_texture = {"u_texture", *this};
Uniform<GLint> u_fadetexture = {"u_fadetexture", *this};

Expand Down
6 changes: 5 additions & 1 deletion src/mbgl/shader/sdf_shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class SDFShader : public Shader {
Uniform<GLfloat> u_buffer = {"u_buffer", *this};
Uniform<GLfloat> u_gamma = {"u_gamma", *this};
Uniform<GLfloat> u_zoom = {"u_zoom", *this};
Uniform<GLint> u_skewed = {"u_skewed", *this};
Uniform<GLfloat> u_pitch = {"u_pitch", *this};
Uniform<GLfloat> u_bearing = {"u_bearing", *this};
Uniform<GLfloat> u_aspect_ratio = {"u_aspect_ratio", *this};
Uniform<GLint> u_rotate_with_map = {"u_rotate_with_map",*this};
Uniform<GLint> u_pitch_with_map = {"u_pitch_with_map",*this};
Uniform<GLint> u_texture = {"u_texture", *this};
Uniform<GLint> u_fadetexture = {"u_fadetexture", *this};

Expand Down
7 changes: 7 additions & 0 deletions src/mbgl/style/layers/symbol_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ PropertyValue<std::array<float, 2>> SymbolLayer::getIconOffset() const {
void SymbolLayer::setIconOffset(PropertyValue<std::array<float, 2>> value) {
impl->layout.iconOffset.set(value);
}
PropertyValue<RotationAlignmentType> SymbolLayer::getTextPitchAlignment() const {
return impl->layout.textPitchAlignment.get();
}

void SymbolLayer::setTextPitchAlignment(PropertyValue<RotationAlignmentType> value) {
impl->layout.textPitchAlignment.set(value);
}
PropertyValue<RotationAlignmentType> SymbolLayer::getTextRotationAlignment() const {
return impl->layout.textRotationAlignment.get();
}
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/style/layers/symbol_layer_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void SymbolLayoutProperties::parse(const JSValue& value) {
iconPadding.parse("icon-padding", value);
iconKeepUpright.parse("icon-keep-upright", value);
iconOffset.parse("icon-offset", value);
textPitchAlignment.parse("text-pitch-alignment", value);
textRotationAlignment.parse("text-rotation-alignment", value);
textField.parse("text-field", value);
textFont.parse("text-font", value);
Expand Down Expand Up @@ -53,6 +54,7 @@ void SymbolLayoutProperties::recalculate(const CalculationParameters& parameters
iconPadding.calculate(parameters);
iconKeepUpright.calculate(parameters);
iconOffset.calculate(parameters);
textPitchAlignment.calculate(parameters);
textRotationAlignment.calculate(parameters);
textField.calculate(parameters);
textFont.calculate(parameters);
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/style/layers/symbol_layer_properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SymbolLayoutProperties {
LayoutProperty<float> iconPadding { 2 };
LayoutProperty<bool> iconKeepUpright { false };
LayoutProperty<std::array<float, 2>> iconOffset { {{ 0, 0 }} };
LayoutProperty<RotationAlignmentType> textPitchAlignment { RotationAlignmentType::Map };
LayoutProperty<RotationAlignmentType> textRotationAlignment { RotationAlignmentType::Viewport };
LayoutProperty<std::string> textField { "" };
LayoutProperty<std::vector<std::string>> textFont { { "Open Sans Regular", "Arial Unicode MS Regular" } };
Expand Down
16 changes: 7 additions & 9 deletions src/mbgl/text/quads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ SymbolQuads getIconQuads(Anchor& anchor, const PositionedIcon& shapedIcon,
}

SymbolQuads quads;
quads.emplace_back(tl, tr, bl, br, image.pos, 0, anchor.point, globalMinScale, std::numeric_limits<float>::infinity());
quads.emplace_back(tl, tr, bl, br, image.pos, 0, 0, anchor.point, globalMinScale, std::numeric_limits<float>::infinity());
return quads;
}

Expand Down Expand Up @@ -102,8 +102,6 @@ void getSegmentGlyphs(std::back_insert_iterator<GlyphInstances> glyphs, Anchor &
float angle = std::atan2(end.y - newAnchorPoint.y, end.x - newAnchorPoint.x);
if (!forward)
angle += M_PI;
if (upsideDown)
angle += M_PI;

glyphs = GlyphInstance{
/* anchor */ newAnchorPoint,
Expand Down Expand Up @@ -188,12 +186,11 @@ SymbolQuads getGlyphQuads(Anchor& anchor, const Shaping& shapedText,
Point<float> tr = otr;
Point<float> bl = obl;
Point<float> br = obr;
const float angle = instance.angle + textRotate;

if (angle) {
if (textRotate) {
// Compute the transformation matrix.
float angle_sin = std::sin(angle);
float angle_cos = std::cos(angle);
float angle_sin = std::sin(textRotate);
float angle_cos = std::cos(textRotate);
std::array<float, 4> matrix = {{angle_cos, -angle_sin, angle_sin, angle_cos}};

tl = util::matrixMultiply(matrix, tl);
Expand All @@ -205,8 +202,9 @@ SymbolQuads getGlyphQuads(Anchor& anchor, const Shaping& shapedText,
// Prevent label from extending past the end of the line
const float glyphMinScale = std::max(instance.minScale, anchor.scale);

const float glyphAngle = std::fmod((anchor.angle + textRotate + instance.offset + 2 * M_PI), (2 * M_PI));
quads.emplace_back(tl, tr, bl, br, rect, glyphAngle, instance.anchorPoint, glyphMinScale, instance.maxScale);
const float anchorAngle = std::fmod((anchor.angle + textRotate + instance.offset + 2 * M_PI), (2 * M_PI));
const float glyphAngle = std::fmod((instance.angle + textRotate + instance.offset + 2 * M_PI), (2 * M_PI));
quads.emplace_back(tl, tr, bl, br, rect, anchorAngle, glyphAngle, instance.anchorPoint, glyphMinScale, instance.maxScale);

}

Expand Down
7 changes: 4 additions & 3 deletions src/mbgl/text/quads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ class SymbolLayoutProperties;
struct SymbolQuad {
explicit SymbolQuad(const Point<float> &tl_, const Point<float> &tr_,
const Point<float> &bl_, const Point<float> &br_,
const Rect<uint16_t> &tex_, float angle_, const Point<float> &anchorPoint_,
const Rect<uint16_t> &tex_, float anchorAngle_, float glyphAngle_, const Point<float> &anchorPoint_,
float minScale_, float maxScale_)
: tl(tl_),
tr(tr_),
bl(bl_),
br(br_),
tex(tex_),
angle(angle_),
anchorAngle(anchorAngle_),
glyphAngle(glyphAngle_),
anchorPoint(anchorPoint_),
minScale(minScale_),
maxScale(maxScale_) {}

Point<float> tl, tr, bl, br;
Rect<uint16_t> tex;
float angle;
float anchorAngle, glyphAngle;
Point<float> anchorPoint;
float minScale, maxScale;
};
Expand Down

0 comments on commit f4b6411

Please sign in to comment.