diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index 5dbc97143..2aa0353ca 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -88,7 +89,8 @@ public: bool needsAnimation() const; private: void setupShaders(); - void translateLayer(std::array translation, const Tile::ID &id, bool reverse = false); + void translateLayer(const std::array &translation, const Tile::ID &id, bool reverse = false); + mat4 translatedMatrix(const std::array &translation, const Tile::ID &id, TranslateAnchor anchor = TranslateAnchor::Map); void prepareTile(const Tile& tile); void useProgram(uint32_t program); diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 6d85af4be..2d9606b6f 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -171,7 +171,7 @@ void Painter::renderTileLayer(const Tile& tile, const LayerDescription &layer_de } } -void Painter::translateLayer(const std::array translation, const Tile::ID &id, bool reverse) { +void Painter::translateLayer(const std::array &translation, const Tile::ID &id, bool reverse) { if (translation[0] || translation[1]) { const int direction = reverse ? -1 : 1; // TODO: Get rid of the 8 (scaling from 4096 to tile size) @@ -181,6 +181,32 @@ void Painter::translateLayer(const std::array translation, const Tile: } } +mat4 Painter::translatedMatrix(const std::array &translation, const Tile::ID &id, TranslateAnchor anchor) { + if (translation[0] == 0 && translation[1] == 0) { + return matrix; + } else { + // TODO: Get rid of the 8 (scaling from 4096 to tile size) + const double factor = ((double)(1 << id.z)) / map.getState().getScale() / + map.getState().getPixelRatio() * (4096.0 / util::tileSize); + + mat4 result; + if (anchor == TranslateAnchor::Viewport) { + const double sin_a = sin(-map.getState().getAngle()); + const double cos_a = cos(-map.getState().getAngle()); + matrix::translate(result, matrix, + factor * (translation[0] * cos_a - translation[1] * sin_a), + factor * (translation[0] * sin_a + translation[1] * cos_a), + 0); + } else { + matrix::translate(result, matrix, + factor * translation[0], + factor * translation[1], + 0); + } + return result; + } +} + void Painter::renderMatte() { gl::group group("matte"); glDisable(GL_DEPTH_TEST); diff --git a/src/renderer/painter_text.cpp b/src/renderer/painter_text.cpp index 7d2a2d2ae..a17f162e9 100644 --- a/src/renderer/painter_text.cpp +++ b/src/renderer/painter_text.cpp @@ -16,8 +16,6 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons const TextProperties& properties = text_properties_it->second; if (!properties.enabled) return; - translateLayer(properties.translate, id); - mat4 exMatrix; matrix::copy(exMatrix, projMatrix); if (bucket.geom_desc.path == TextPathType::Curve) { @@ -33,8 +31,10 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons float fontSize = fmin(properties.size, bucket.geom_desc.font_size); matrix::scale(exMatrix, exMatrix, fontSize / 24.0f, fontSize / 24.0f, 1.0f); + const mat4 vtxMatrix = translatedMatrix(properties.translate, id, properties.translateAnchor); + useProgram(textShader->program); - textShader->setMatrix(matrix); + textShader->setMatrix(vtxMatrix); textShader->setExtrudeMatrix(exMatrix); map.getGlyphAtlas().bind(); @@ -109,6 +109,4 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons textShader->setBuffer((256.0f - 64.0f) / 256.0f); glDepthRange(strata + strata_epsilon, 1.0f); bucket.drawGlyphs(*textShader); - - translateLayer(properties.translate, id, true); }