From eb499e77c1f6cc77452cadd2445b7d99d1b6b7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 10 Apr 2014 11:49:43 -0400 Subject: fix linejoin drawing on retina --- include/llmr/map/map.hpp | 4 ++-- include/llmr/map/transform.hpp | 20 +++++++++++++++----- include/llmr/style/sprite.hpp | 5 +++-- ios/MBXViewController.mm | 10 +++++----- src/map/map.cpp | 26 +++++++++++--------------- src/map/transform.cpp | 8 ++++++++ src/renderer/painter.cpp | 34 ++++++++++++++++++---------------- src/style/sprite.cpp | 6 ++---- 8 files changed, 64 insertions(+), 49 deletions(-) diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 90cd95c01e..99f7a1a192 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -24,10 +24,10 @@ public: ~Map(); /* setup */ - void setup(float pixelRatio = 1); + void setup(); void loadStyle(const uint8_t *const data, uint32_t bytes); void loadSettings(); - void resize(uint32_t width, uint32_t height, uint32_t fb_width, uint32_t fb_height); + void resize(uint16_t width, uint16_t height, uint16_t fb_width, uint16_t fb_height); void toggleRaster(); /* callback */ diff --git a/include/llmr/map/transform.hpp b/include/llmr/map/transform.hpp index f43802ffaf..dbef17c1a1 100644 --- a/include/llmr/map/transform.hpp +++ b/include/llmr/map/transform.hpp @@ -22,6 +22,8 @@ public: void updateAnimations(); void cancelAnimations(); + void resize(uint16_t width, uint16_t height, uint16_t fb_width, uint16_t fb_height); + // Relative changes void moveBy(double dx, double dy, double duration = 0); void scaleBy(double ds, double cx = -1, double cy = -1, double duration = 0); @@ -55,22 +57,30 @@ public: // Temporary box mapCornersToBox(uint32_t z) const; + // More getters + inline uint16_t getWidth() const { return width; } + inline uint16_t getHeight() const { return height; } + inline uint16_t getFramebufferWidth() const { return fb_width; } + inline uint16_t getFramebufferHeight() const { return fb_height; } + inline float getPixelRatio() const { return pixelRatio; } + private: void setScaleXY(double new_scale, double xn, double yn, double duration = 0); double pixel_x() const; double pixel_y() const; -public: +private: // logical dimensions - uint32_t width = 0; - uint32_t height = 0; + uint16_t width = 0; + uint16_t height = 0; // physical (framebuffer) dimensions - float fb_width = 0; - float fb_height = 0; + uint16_t fb_width = 0; + uint16_t fb_height = 0; float pixelRatio = 1; +public: bool rotating = false; bool scaling = false; bool panning = false; diff --git a/include/llmr/style/sprite.hpp b/include/llmr/style/sprite.hpp index f7d281e209..3ac81b39d4 100644 --- a/include/llmr/style/sprite.hpp +++ b/include/llmr/style/sprite.hpp @@ -33,15 +33,16 @@ public: class Sprite : public std::enable_shared_from_this { public: - Sprite(); + Sprite(float pixelRatio = 1); - void load(const std::string& base_url, float pixelRatio = 1); + void load(const std::string& base_url); ImagePosition getPosition(const std::string& name, bool repeating = false); operator bool() const; public: + const float pixelRatio; std::shared_ptr raster; private: diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index 9f50ab4793..3055eca981 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -50,10 +50,7 @@ class MBXMapView { settings.load(); - map.setup([[UIScreen mainScreen] scale]); - - CGRect frame = [[UIScreen mainScreen] bounds]; - map.resize(frame.size.width, frame.size.height, frame.size.width, frame.size.height); + map.setup(); map.loadSettings(); } @@ -128,9 +125,12 @@ class MBXMapView [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateRender:) name:MBXNeedsRenderNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateNetworkActivity:) name:MBXUpdateActivityNotification object:nil]; - mapView = new MBXMapView(); + GLKView *view = (GLKView *)self.view; + CGRect rect = [view frame]; + mapView = new MBXMapView(); mapView->init(); + mapView->map.resize(rect.size.width, rect.size.height, [view drawableWidth], [view drawableHeight]); displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)]; [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; diff --git a/src/map/map.cpp b/src/map/map.cpp index 75bfdb2c9e..884e21146a 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -25,15 +25,9 @@ Map::~Map() { settings.sync(); } -void Map::setup(float pixelRatio) { - +void Map::setup() { painter.setup(); - pixel_ratio = pixelRatio; - - style.sprite = std::make_shared(); - style.sprite->load(kSpriteURL, pixel_ratio); - style.loadJSON(resources::style, resources::style_size); } @@ -50,13 +44,15 @@ void Map::loadSettings() { update(); } -void Map::resize(uint32_t width, uint32_t height, uint32_t fb_width, uint32_t fb_height) { - transform.width = width; - transform.height = height; - transform.fb_width = fb_width; - transform.fb_height = fb_height; - transform.pixelRatio = pixel_ratio; +void Map::resize(uint16_t width, uint16_t height, uint16_t fb_width, uint16_t fb_height) { + transform.resize(width, height, fb_width, fb_height); painter.resize(fb_width, fb_height); + + if (!style.sprite || style.sprite->pixelRatio) { + style.sprite = std::make_shared(transform.getPixelRatio()); + style.sprite->load(kSpriteURL); + } + update(); } @@ -177,8 +173,8 @@ void Map::setAngle(double angle, double x, double y, double duration) { double dx = 0, dy = 0; if (x >= 0 && y >= 0) { - dx = (transform.width / 2) - x; - dy = (transform.height / 2) - y; + dx = (transform.getWidth() / 2) - x; + dy = (transform.getWidth() / 2) - y; transform.moveBy(dx, dy, duration); } diff --git a/src/map/transform.cpp b/src/map/transform.cpp index 5662e36b65..9fb79ed7a9 100644 --- a/src/map/transform.cpp +++ b/src/map/transform.cpp @@ -34,6 +34,14 @@ void Transform::cancelAnimations() { animations.clear(); } +void Transform::resize(uint16_t w, uint16_t h, uint16_t fb_w, uint16_t fb_h) { + width = w; + height = h; + fb_width = fb_w; + fb_height = fb_h; + pixelRatio = (float)fb_w / (float)w; +} + void Transform::moveBy(double dx, double dy, double duration) { double xn = x + cos(angle) * dx + sin(angle) * dy; double yn = y + cos(angle) * dy + sin(-angle) * dx; diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 6ac4e164f6..b576a9dbcd 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -101,7 +101,7 @@ void Painter::depthMask(bool value) { void Painter::changeMatrix() { // Initialize projection matrix - matrix::ortho(projMatrix, 0, transform.width, transform.height, 0, 0, 1); + matrix::ortho(projMatrix, 0, transform.getWidth(), transform.getHeight(), 0, 0, 1); // The extrusion matrix. matrix::identity(extrudeMatrix); @@ -270,7 +270,10 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons outlineShader->setColor(stroke_color); // Draw the entire line - outlineShader->setWorld({{ transform.fb_width, transform.fb_height }}); + outlineShader->setWorld({{ + static_cast(transform.getFramebufferWidth()), + static_cast(transform.getFramebufferHeight()) + }}); glDepthRange(strata, 1.0f); bucket.drawVertices(*outlineShader); } else if (fringeline) { @@ -350,10 +353,9 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // Draw the entire line outlineShader->setWorld({{ - transform.fb_width, - transform.fb_height - } - }); + static_cast(transform.getFramebufferWidth()), + static_cast(transform.getFramebufferHeight()) + }}); glDepthRange(strata + strata_epsilon, 1.0f); bucket.drawVertices(*outlineShader); @@ -391,17 +393,17 @@ void Painter::renderLine(LineBucket& bucket, const std::string& layer_name, cons linejoinShader->setMatrix(matrix); linejoinShader->setColor(color); linejoinShader->setWorld({{ - transform.fb_width * 0.5f, - transform.fb_height * 0.5f + transform.getFramebufferWidth() * 0.5f, + transform.getFramebufferHeight() * 0.5f } }); linejoinShader->setLineWidth({{ - ((outset - 0.25f) * transform.pixelRatio), - ((inset - 0.25f) * transform.pixelRatio) + ((outset - 0.25f) * transform.getPixelRatio()), + ((inset - 0.25f) * transform.getPixelRatio()) } }); - float pointSize = ceil(transform.pixelRatio * outset * 2.0); + float pointSize = ceil(transform.getPixelRatio() * outset * 2.0); #if defined(GL_ES_VERSION_2_0) linejoinShader->setSize(pointSize); #else @@ -467,7 +469,7 @@ void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, co pointShader->setMatrix(matrix); pointShader->setImage(0); pointShader->setColor(color); - const float pointSize = properties.size * 1.4142135623730951 * transform.pixelRatio; + const float pointSize = properties.size * 1.4142135623730951 * transform.getPixelRatio(); #if defined(GL_ES_VERSION_2_0) pointShader->setSize(pointSize); #else @@ -513,7 +515,7 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons glyphAtlas.bind(); textShader->setTextureSize({{ static_cast(glyphAtlas.width), static_cast(glyphAtlas.height) }}); - textShader->setGamma(2.5f / fontSize / transform.pixelRatio); + textShader->setGamma(2.5f / fontSize / transform.getPixelRatio()); // Convert the -pi..pi to an int8 range. float angle = round((transform.getAngle() + rotate) / M_PI * 128); @@ -595,16 +597,16 @@ void Painter::renderDebug(const TileData::Ptr& tile_data) { // draw tile outline tileBorderArray.bind(*plainShader, tileBorderBuffer, BUFFER_OFFSET(0)); plainShader->setColor(1.0f, 0.0f, 0.0f, 1.0f); - lineWidth(4.0f * transform.pixelRatio); + lineWidth(4.0f * transform.getPixelRatio()); glDrawArrays(GL_LINE_STRIP, 0, (GLsizei)tileBorderBuffer.index()); // draw debug info tile_data->debugFontArray.bind(*plainShader, tile_data->debugFontBuffer, BUFFER_OFFSET(0)); plainShader->setColor(1.0f, 1.0f, 1.0f, 1.0f); - lineWidth(4.0f * transform.pixelRatio); + lineWidth(4.0f * transform.getPixelRatio()); glDrawArrays(GL_LINES, 0, (GLsizei)tile_data->debugFontBuffer.index()); plainShader->setColor(0.0f, 0.0f, 0.0f, 1.0f); - lineWidth(2.0f * transform.pixelRatio); + lineWidth(2.0f * transform.getPixelRatio()); glDrawArrays(GL_LINES, 0, (GLsizei)tile_data->debugFontBuffer.index()); glEnable(GL_DEPTH_TEST); diff --git a/src/style/sprite.cpp b/src/style/sprite.cpp index 3998afc628..9b0c091679 100644 --- a/src/style/sprite.cpp +++ b/src/style/sprite.cpp @@ -20,11 +20,9 @@ ImagePosition::ImagePosition(const vec2& size, vec2 tl, vec2(); -} +Sprite::Sprite(float pixelRatio) : pixelRatio(pixelRatio), raster(std::make_shared()) {} -void Sprite::load(const std::string& base_url, float pixelRatio) { +void Sprite::load(const std::string& base_url) { std::shared_ptr sprite = shared_from_this(); auto complete = [sprite]() { -- cgit v1.2.1