diff options
-rw-r--r-- | include/llmr/geometry/fill_buffer.hpp | 35 | ||||
-rw-r--r-- | include/llmr/geometry/geometry.hpp | 20 | ||||
-rw-r--r-- | include/llmr/geometry/linevertexbuffer.hpp | 2 | ||||
-rw-r--r-- | include/llmr/map/tile.hpp | 72 | ||||
-rw-r--r-- | include/llmr/util/pbf.hpp | 26 | ||||
-rw-r--r-- | macosx/main.mm | 3 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/geometry/fill_buffer.cpp | 71 | ||||
-rw-r--r-- | src/map/tile.cpp | 140 | ||||
-rw-r--r-- | src/renderer/painter.cpp | 32 |
10 files changed, 370 insertions, 33 deletions
diff --git a/include/llmr/geometry/fill_buffer.hpp b/include/llmr/geometry/fill_buffer.hpp new file mode 100644 index 0000000000..d440e1bf13 --- /dev/null +++ b/include/llmr/geometry/fill_buffer.hpp @@ -0,0 +1,35 @@ +#ifndef LLMR_GEOMETRY_FILL_BUFFER +#define LLMR_GEOMETRY_FILL_BUFFER + +#include <vector> + +namespace llmr { + +class fill_buffer { +public: + typedef int16_t vertex_type; + const uint8_t components = 2; + + fill_buffer(); + ~fill_buffer(); + + uint32_t vertex_length() const; + uint32_t elements_length() const; + + void addDegenerate(); + void addCoordinate(vertex_type x, vertex_type y); + void addElements(uint16_t a, uint16_t b, uint16_t c); + + void bind(); + +private: + std::vector<vertex_type> vertices; + std::vector<uint16_t> elements; + uint32_t vertex_buffer; + uint32_t element_buffer; + bool dirty; +}; + +} + +#endif diff --git a/include/llmr/geometry/geometry.hpp b/include/llmr/geometry/geometry.hpp index 16df26f873..6808324295 100644 --- a/include/llmr/geometry/geometry.hpp +++ b/include/llmr/geometry/geometry.hpp @@ -7,9 +7,9 @@ namespace llmr { -class geometry { +class Geometry { public: - inline geometry(const uint8_t *data, uint32_t bytes); + inline Geometry(pbf& data); enum command { end = 0, @@ -21,24 +21,24 @@ public: inline command next(int32_t &rx, int32_t &ry); private: - pbf pbf; + pbf& data; uint32_t cmd; uint32_t length; int32_t x, y; int32_t ox, oy; }; -geometry::geometry(const uint8_t *data, uint32_t bytes) - : pbf(data, bytes), +Geometry::Geometry(pbf& data) + : data(data), cmd(1), length(0), x(0), y(0), ox(0), oy(0) {} -geometry::command geometry::next(int32_t &rx, int32_t &ry) { - if (pbf.data < pbf.end) { +Geometry::command Geometry::next(int32_t &rx, int32_t &ry) { + if (data.data < data.end) { if (!length) { - uint32_t cmd_length = (uint32_t)pbf.varint(); + uint32_t cmd_length = (uint32_t)data.varint(); cmd = cmd_length & 0x7; length = cmd_length >> 3; } @@ -46,8 +46,8 @@ geometry::command geometry::next(int32_t &rx, int32_t &ry) { length--; if (cmd == move_to || cmd == line_to) { - rx = (x += pbf.svarint()); - ry = (y += pbf.svarint()); + rx = (x += data.svarint()); + ry = (y += data.svarint()); if (cmd == move_to) { ox = x; diff --git a/include/llmr/geometry/linevertexbuffer.hpp b/include/llmr/geometry/linevertexbuffer.hpp index 1cf71eaf12..2396c6b4a2 100644 --- a/include/llmr/geometry/linevertexbuffer.hpp +++ b/include/llmr/geometry/linevertexbuffer.hpp @@ -17,7 +17,7 @@ public: void bind(); private: - std::vector<uint16_t> array; + std::vector<int16_t> array; uint32_t buffer; }; diff --git a/include/llmr/map/tile.hpp b/include/llmr/map/tile.hpp index f9d0d217d7..f4cf07148f 100644 --- a/include/llmr/map/tile.hpp +++ b/include/llmr/map/tile.hpp @@ -3,17 +3,24 @@ #include "../geometry/debug_font_buffer.hpp" #include "../geometry/linevertexbuffer.hpp" +#include "../geometry/fill_buffer.hpp" #include <stdint.h> #include <forward_list> #include <mutex> #include <llmr/util/vec.hpp> +#include <string> namespace llmr { +struct pbf; class Tile { public: + struct exception : std::exception {}; + struct geometry_too_long_exception : exception {}; + +public: typedef std::shared_ptr<Tile> Ptr; typedef vec3<int32_t> ID; @@ -25,29 +32,76 @@ public: obsolete }; + struct fill_index { + struct group { + uint32_t vertex_length; + uint32_t elements_length; + + group() : vertex_length(0), elements_length(0) {} + group(uint32_t vertex_length, uint32_t elements_length) + : vertex_length(vertex_length), + elements_length(elements_length) { + } + }; + + uint32_t vertex_start; + uint32_t elements_start; + uint32_t length; + std::vector<group> groups; + + fill_index(uint32_t vertex_start, uint32_t elements_start) + : vertex_start(vertex_start), + elements_start(elements_start), + length(0), + groups(1) { + } + + // debug + std::string name; + }; + + + // struct fill_index { + // uint32_t vertex_start; + // uint32_t vertex_length; + // uint32_t elements_start; + // uint32_t elements_length; + // std::string name; + + // fill_index(uint32_t vertex_start, uint32_t elements_start) + // : vertex_start(vertex_start), + // vertex_length(0), + // elements_start(elements_start), + // elements_length(0) { + // } + // }; + public: Tile(ID id); ~Tile(); // Make noncopyable - Tile(const Tile&) = delete; - Tile(const Tile&&) = delete; - Tile &operator=(const Tile&) = delete; - Tile &operator=(const Tile&&) = delete; + Tile(const Tile &) = delete; + Tile(const Tile &&) = delete; + Tile &operator=(const Tile &) = delete; + Tile &operator=(const Tile && ) = delete; // Other functions void setData(uint8_t *data, uint32_t bytes); bool parse(); void parseLayer(const uint8_t *data, uint32_t bytes); void parseFeature(const uint8_t *data, uint32_t bytes); - void loadGeometry(const uint8_t *data, uint32_t bytes); + + void addLineGeometry(pbf &geom); + void addFillGeometry(pbf &geom); + void cancel(); const std::string toString() const; - static ID parent(const ID& id, int32_t z); - static std::forward_list<ID> children(const ID& id, int32_t z); + static ID parent(const ID &id, int32_t z); + static std::forward_list<ID> children(const ID &id, int32_t z); public: const ID id; @@ -55,11 +109,15 @@ public: linevertexbuffer lineVertex; debug_font_buffer debugFontVertex; + fill_buffer fillBuffer; + std::vector<fill_index> fillIndices; + private: // Source data uint8_t *data; uint32_t bytes; + std::mutex mtx; }; } diff --git a/include/llmr/util/pbf.hpp b/include/llmr/util/pbf.hpp index f499408398..3fe851a7d4 100644 --- a/include/llmr/util/pbf.hpp +++ b/include/llmr/util/pbf.hpp @@ -24,6 +24,9 @@ struct pbf { struct end_of_buffer_exception : exception {}; inline pbf(const unsigned char *data, uint32_t length); + inline pbf(); + + inline operator bool() const; inline bool next(); template <typename T = uint32_t> inline T varint(); @@ -33,6 +36,8 @@ struct pbf { inline double float64(); inline bool boolean(); + inline pbf message(); + inline void skip(); inline void skipValue(uint32_t val); inline void skipBytes(uint32_t bytes); @@ -48,6 +53,16 @@ pbf::pbf(const unsigned char *data, uint32_t length) end(data + length) { } +pbf::pbf() + : data(NULL), + end(NULL) { +} + + +pbf::operator bool() const { + return data < end; +} + bool pbf::next() { if (data < end) { value = (uint32_t)varint(); @@ -110,6 +125,13 @@ bool pbf::boolean() { return *(bool *)(data - 1); } +pbf pbf::message() { + uint32_t bytes = (uint32_t)varint(); + const uint8_t *pos = data; + skipBytes(bytes); + return pbf(pos, bytes); +} + void pbf::skip() { skipValue(value); } @@ -134,10 +156,10 @@ void pbf::skipValue(uint32_t val) { } void pbf::skipBytes(uint32_t bytes) { - data += bytes; - if (data > end) { + if (data + bytes > end) { throw end_of_buffer_exception(); } + data += bytes; } } // end namespace llmr diff --git a/macosx/main.mm b/macosx/main.mm index cbf61eaf9d..cbbbd96ef0 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -249,8 +249,11 @@ void request(void *, Tile::Ptr tile) { }); return; } + // fall through to report tileFailed } + // fall through to report tileFailed } + // fall through to report tileFailed dispatch_async(dispatch_get_main_queue(), ^ { view->map.tileFailed(tile); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f701c9c1f0..5e3aed9b2e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ SET(llmr_SOURCES geometry/debug_font_buffer.cpp geometry/linevertexbuffer.cpp + geometry/fill_buffer.cpp map/map.cpp map/tile.cpp map/transform.cpp @@ -17,6 +18,7 @@ SET(llmr_SOURCES SET(llmr_HEADERS ../include/llmr/geometry/debug_font_buffer.hpp ../include/llmr/geometry/linevertexbuffer.hpp + ../include/llmr/geometry/fill_buffer.hpp ../include/llmr/llmr.hpp ../include/llmr/map/map.hpp ../include/llmr/map/tile.hpp diff --git a/src/geometry/fill_buffer.cpp b/src/geometry/fill_buffer.cpp new file mode 100644 index 0000000000..2f71bedc1d --- /dev/null +++ b/src/geometry/fill_buffer.cpp @@ -0,0 +1,71 @@ +#include <llmr/geometry/fill_buffer.hpp> +#include <llmr/platform/gl.hpp> +// #include <cmath> + +using namespace llmr; + +fill_buffer::fill_buffer() + : vertex_buffer(0), + element_buffer(0), + dirty(true) { + +} + +fill_buffer::~fill_buffer() { + if (vertex_buffer != 0) { + glDeleteBuffers(1, &vertex_buffer); + vertex_buffer = 0; + } + if (element_buffer != 0) { + glDeleteBuffers(1, &element_buffer); + element_buffer = 0; + } +} + +uint32_t fill_buffer::vertex_length() const { + // We store 2 coordinates per vertex + return vertices.size() / 2; +} + +uint32_t fill_buffer::elements_length() const { + // A triangle has 3 indices + return elements.size() / 3; +} + +void fill_buffer::addDegenerate() { + vertices.push_back(32767); + vertices.push_back(0); +} + +void fill_buffer::addCoordinate(int16_t x, int16_t y) { + vertices.push_back(x); + vertices.push_back(y); +} + +void fill_buffer::addElements(uint16_t a, uint16_t b, uint16_t c) { + elements.push_back(a); + elements.push_back(b); + elements.push_back(c); +} + +void fill_buffer::bind() { + if (vertex_buffer == 0) { + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + } else { + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + } + + if (element_buffer == 0) { + glGenBuffers(1, &element_buffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer); + } else { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer); + } + + if (dirty) { + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(uint16_t), vertices.data(), GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(uint16_t), elements.data(), GL_STATIC_DRAW); + dirty = false; + } +} diff --git a/src/map/tile.cpp b/src/map/tile.cpp index fb21f9d408..1d4a660ac5 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -78,6 +78,8 @@ void Tile::cancel() { } bool Tile::parse() { + std::lock_guard<std::mutex> lock(mtx); + if (state == obsolete) { return false; } @@ -95,10 +97,18 @@ bool Tile::parse() { tile.skip(); } } - } catch(const pbf::exception& ex) { + } catch (const pbf::exception& ex) { + fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); + cancel(); + return false; + } catch (const Tile::exception& ex) { fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); cancel(); return false; + } catch (const std::exception& ex) { + fprintf(stderr, "[%p] general exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); + cancel(); + return false; } if (state == obsolete) { @@ -107,15 +117,28 @@ bool Tile::parse() { state = ready; } + // int i = 0; + // for (const fill_index& index : fillIndices) { + // fprintf(stderr, "[%p] %d: vertex % 8d / % 8d\n", this, i, index.vertex_start, index.vertex_length); + // fprintf(stderr, "[%p] %d: elements % 8d / % 8d\n", this, i, index.elements_start, index.elements_length); + // i++; + // } + return true; } void Tile::parseLayer(const uint8_t *data, uint32_t bytes) { pbf layer(data, bytes); std::string name; + + uint32_t vertex_start = fillBuffer.vertex_length(); + uint32_t elements_start = fillBuffer.elements_length(); + fillIndices.emplace_back(vertex_start, elements_start); + while (layer.next()) { if (layer.tag == 1) { name = layer.string(); + fillIndices.back().name = name; } else if (layer.tag == 2) { uint32_t bytes = (uint32_t)layer.varint(); parseFeature(layer.data, bytes); @@ -126,8 +149,21 @@ void Tile::parseLayer(const uint8_t *data, uint32_t bytes) { } } + +enum geom_type { + Unknown = 0, + Point = 1, + LineString = 2, + Polygon = 3 +}; + + void Tile::parseFeature(const uint8_t *data, uint32_t bytes) { pbf feature(data, bytes); + + geom_type type = Unknown; + pbf geom; + while (feature.next()) { if (feature.tag == 1) { /*uint32_t id =*/ feature.varint(); @@ -138,27 +174,109 @@ void Tile::parseFeature(const uint8_t *data, uint32_t bytes) { /*uint32_t value =*/ feature.varint(); } } else if (feature.tag == 3) { - /*uint32_t type =*/ feature.varint(); + type = (geom_type)feature.varint(); } else if (feature.tag == 4) { - uint32_t bytes = (uint32_t)feature.varint(); - loadGeometry(feature.data, bytes); - feature.skipBytes(bytes); + geom = feature.message(); } else { feature.skip(); } } + + if (geom) { + if (type == LineString) { + addLineGeometry(geom); + } else if (type == Polygon) { + addFillGeometry(geom); + } + } } -void Tile::loadGeometry(const uint8_t *data, uint32_t bytes) { - geometry geometry(data, bytes); +void Tile::addFillGeometry(pbf& geom) { + + std::vector<std::vector<std::pair<int16_t, int16_t>>> lines; + + { + std::vector<std::pair<int16_t, int16_t>> line; + Geometry::command cmd; + int32_t x, y; + Geometry geometry(geom); + while ((cmd = geometry.next(x, y)) != Geometry::end) { + if (cmd == Geometry::move_to) { + if (line.size()) { + lines.push_back(line); + line.clear(); + } + } + line.emplace_back(x, y); + } + if (line.size()) { + lines.push_back(line); + } + } + + for (const std::vector<std::pair<int16_t, int16_t>>& line : lines) { + uint32_t vertex_start = fillBuffer.vertex_length(); - geometry::command cmd; + fillBuffer.addDegenerate(); + for (const std::pair<int16_t, int16_t>& coord : line) { + fillBuffer.addCoordinate(coord.first, coord.second); + } + + uint32_t vertex_end = fillBuffer.vertex_length(); + + if (vertex_end - vertex_start > 65535) { + throw geometry_too_long_exception(); + } + + if (!fillIndices.size()) { + // Create a new index because there is none yet. + throw std::runtime_error("no index yet"); + } + + fill_index& index = fillIndices.back(); + if (!index.groups.size()) { + throw std::runtime_error("no group yet"); + } + + uint32_t vertex_count = vertex_end - vertex_start; + index.length += vertex_count; + + if (index.groups.back().vertex_length + vertex_count > 65535) { + // Move to a new group because the old one can't hold the geometry. + index.groups.emplace_back(); + } + + fill_index::group& group = index.groups.back(); + + // We're generating triangle fans, so we always start with the first + // coordinate in this polygon. + // The first valid index that is not a degenerate. + uint16_t firstIndex = group.vertex_length + 1; + + assert(firstIndex + vertex_count - 1 < 65536); + + uint32_t elements_start = fillBuffer.elements_length(); + + for (uint32_t i = 2; i < vertex_count; i++) { + fillBuffer.addElements(firstIndex, firstIndex + i - 1, firstIndex + i); + } + + uint32_t elements_end = fillBuffer.elements_length(); + uint32_t elements_count = elements_end - elements_start; + group.vertex_length += vertex_count; + group.elements_length += elements_count; + } +} + +void Tile::addLineGeometry(pbf& geom) { + Geometry geometry(geom); + + Geometry::command cmd; int32_t x, y; - while ((cmd = geometry.next(x, y)) != geometry::end) { - if (cmd == geometry::move_to) { + while ((cmd = geometry.next(x, y)) != Geometry::end) { + if (cmd == Geometry::move_to) { lineVertex.addDegenerate(); } - lineVertex.addCoordinate(x, y); } } diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 3928a8e5f3..02269a76b7 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -8,6 +8,8 @@ #include <llmr/platform/gl.hpp> #include <llmr/map/settings.hpp> +#include <numeric> + using namespace llmr; #define BUFFER_OFFSET(i) ((char *)NULL + (i)) @@ -151,6 +153,7 @@ void Painter::render(const Tile::Ptr& tile) { glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // glDisable(GL_STENCIL_TEST); + // draw lines: tile->lineVertex.bind(); glVertexAttribPointer(outlineShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, BUFFER_OFFSET(0)); glUniform4f(outlineShader->u_color, 0.0f, 0.0f, 0.0f, 1.0f); @@ -158,6 +161,31 @@ void Painter::render(const Tile::Ptr& tile) { glLineWidth(2.0f); glDrawArrays(GL_LINE_STRIP, 0, tile->lineVertex.length()); + // draw fills: + tile->fillBuffer.bind(); + float i = 0.0f; + for (const Tile::fill_index& index : tile->fillIndices) { + if (!index.length) continue; + + glUniform4f(outlineShader->u_color, i, 0.0f, 1.0f, 0.5f); + glUniform2f(outlineShader->u_world, transform.fb_width, transform.fb_height); + glLineWidth(2.0f); + + char *vertex_index = BUFFER_OFFSET(index.vertex_start * 2 * sizeof(uint16_t)); + char *elements_index = BUFFER_OFFSET(index.elements_start * 3 * sizeof(uint16_t)); + for (const Tile::fill_index::group& group : index.groups) { + glVertexAttribPointer(outlineShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, vertex_index); + glDrawElements(GL_TRIANGLES, group.elements_length * 3, GL_UNSIGNED_SHORT, elements_index); + vertex_index += group.vertex_length * 2 * sizeof(uint16_t); + elements_index += group.elements_length * 3 * sizeof(uint16_t); + } + + vertex_index = BUFFER_OFFSET(index.vertex_start * 2 * sizeof(uint16_t)); + glUniform4f(outlineShader->u_color, i, 1.0f, 1.0f, 1.0f); + glVertexAttribPointer(outlineShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, vertex_index); + glDrawArrays(GL_LINE_STRIP, 0, index.length); + i += 0.1f; + } if (settings.debug) { renderDebug(tile); @@ -198,8 +226,8 @@ bool Painter::switchShader(Shader *shader) { // Disable all attributes from the existing shader that aren't used in // the new shader. Note: attribute indices are *not* program specific! if (currentShader) { - const std::forward_list<uint32_t> &hitherto = currentShader->attributes; - const std::forward_list<uint32_t> &henceforth = shader->attributes; + const std::forward_list<uint32_t>& hitherto = currentShader->attributes; + const std::forward_list<uint32_t>& henceforth = shader->attributes; // Find all attribute indices that are used in the old shader, // but unused in the new one. |