diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-01-22 16:43:05 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-01-22 16:43:05 +0100 |
commit | 0f74a4bc90e1d4d69b50a443aa9f1ed42d071d4d (patch) | |
tree | d9894ef4a93a3a722b7b0e7215c33eb32ac25e52 /src | |
parent | 7983b968d74a395b29dff01246fe0ee9d6d8149e (diff) | |
download | qtlocation-mapboxgl-0f74a4bc90e1d4d69b50a443aa9f1ed42d071d4d.tar.gz |
fill drawing
Diffstat (limited to 'src')
-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 |
4 files changed, 232 insertions, 13 deletions
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. |