From 117078270d4f3d4491746e4ac8c054731ddd2a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 11 Mar 2014 15:46:55 +0100 Subject: [PATCH] add text buffer code --- include/llmr/geometry/text_buffer.hpp | 23 +++++++++++++++++-- include/llmr/util/math.hpp | 10 ++++++++ src/geometry/text_buffer.cpp | 33 +++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/geometry/text_buffer.cpp diff --git a/include/llmr/geometry/text_buffer.hpp b/include/llmr/geometry/text_buffer.hpp index 005d54a3a..e0166666c 100644 --- a/include/llmr/geometry/text_buffer.hpp +++ b/include/llmr/geometry/text_buffer.hpp @@ -5,10 +5,29 @@ namespace llmr { -class TextVertexBuffer : public Buffer< - 4 // 2 coordinates per vertex +class GlyphPosition { +public: + int16_t x = 0, y = 0; + float ox = 0.0f, oy = 0.0f; + uint16_t tx = 0, ty = 0; + float angle = 0.0f; + float minzoom = 0.0f; + float rangefrom = 0.0f, rangeto = 0.0f; + float maxzoom = 25.0f; + float labelminzoom = 0.0f; +}; + +class TextVertexBuffer : public Buffer < + 16, + GL_ARRAY_BUFFER, + 32768 > { public: + typedef int16_t vertex_type; + + static const double angleFactor; + + size_t add(const GlyphPosition& pos); }; diff --git a/include/llmr/util/math.hpp b/include/llmr/util/math.hpp index a7aeb6345..466640134 100644 --- a/include/llmr/util/math.hpp +++ b/include/llmr/util/math.hpp @@ -8,6 +8,16 @@ namespace llmr { namespace util { +template +inline T max(T a, T b) { + return b > a ? b : a; +} + +template +inline T min(T a, T b) { + return b < a ? b : a; +} + inline double angle_between(double ax, double ay, double bx, double by) { return atan2((ax * by - ay * bx), ax * bx + ay * by); } diff --git a/src/geometry/text_buffer.cpp b/src/geometry/text_buffer.cpp new file mode 100644 index 000000000..92580cdc3 --- /dev/null +++ b/src/geometry/text_buffer.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include + +using namespace llmr; + +const double TextVertexBuffer::angleFactor = 128 / M_PI; + + +size_t TextVertexBuffer::add(const GlyphPosition& pos) { + size_t idx = index(); + void *data = addElement(); + + int16_t *shorts = static_cast(data); + shorts[0] = pos.x; + shorts[1] = pos.y; + shorts[2] = round(pos.ox * 64); // use 1/64 pixels for placement + shorts[3] = round(pos.oy * 64); + + uint8_t *ubytes = static_cast(data); + ubytes[8] = pos.tx / 4; + ubytes[9] = pos.ty / 4; + ubytes[10] = pos.labelminzoom * 10; + ubytes[11] = pos.minzoom * 10; // 1/10 zoom levels: z16 == 160. + ubytes[12] = fmin(pos.maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160. + ubytes[13] = (int16_t)round(pos.angle * angleFactor) % 256; + ubytes[14] = util::max((int16_t)round(pos.rangefrom * angleFactor), (int16_t)0) % 256; + ubytes[15] = util::min((int16_t)round(pos.rangeto * angleFactor), (int16_t)255) % 256; + + return idx; +}