diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/llmr/geometry/sprite_atlas.hpp | 68 | ||||
-rw-r--r-- | include/llmr/map/map.hpp | 2 | ||||
-rw-r--r-- | include/llmr/map/tile_parser.hpp | 6 | ||||
-rw-r--r-- | include/llmr/platform/platform.hpp | 5 | ||||
-rw-r--r-- | include/llmr/renderer/icon_bucket.hpp | 4 | ||||
-rw-r--r-- | include/llmr/style/style.hpp | 1 | ||||
-rw-r--r-- | include/llmr/util/math.hpp | 16 | ||||
-rw-r--r-- | include/llmr/util/raster.hpp | 2 | ||||
-rw-r--r-- | include/llmr/util/rect.hpp | 5 |
9 files changed, 104 insertions, 5 deletions
diff --git a/include/llmr/geometry/sprite_atlas.hpp b/include/llmr/geometry/sprite_atlas.hpp new file mode 100644 index 0000000000..e6557846af --- /dev/null +++ b/include/llmr/geometry/sprite_atlas.hpp @@ -0,0 +1,68 @@ +#ifndef LLMR_GEOMETRY_SPRITE_ATLAS +#define LLMR_GEOMETRY_SPRITE_ATLAS + +#include <llmr/geometry/binpack.hpp> + +#include <string> +#include <map> +#include <mutex> +#include <atomic> +#include <set> + +namespace llmr { + +class Sprite; + +class SpriteAtlas { +public: + typedef uint16_t dimension; + +public: + // Add way to construct this from another SpriteAtlas (e.g. with another pixelRatio) + SpriteAtlas(dimension width, dimension height); + ~SpriteAtlas(); + + // Changes the pixel ratio. + bool resize(float newRatio); + + // Update uninitialized sprites in this atlas from the given sprite. + void update(const Sprite &sprite); + + // Returns the coordinates of the icon. The getter also *creates* new icons in the atlas + // if they don't exist, but they'll be default-initialized with a a black circle. + Rect<dimension> getIcon(int size, const std::string &name); + + // Updates or creates an icon with a given size and name. The data must be a + // a (pixelRatio * size)^2 * 4 byte long RGBA image buffer. + Rect<dimension> setIcon(int size, const std::string &name, const std::string &icon); + + // Binds the image buffer of this sprite atlas to the GPU, and uploads data if it is out + // of date. + void bind(bool linear = false); + + inline float getTextureWidth() const { return width * pixelRatio; } + inline float getTextureHeight() const { return height * pixelRatio; } + +private: + void allocate(); + +public: + const dimension width = 0; + const dimension height = 0; + +private: + std::mutex mtx; + float pixelRatio = 1.0f; + BinPack<dimension> bin; + std::map<int, std::map<std::string, Rect<dimension>>> index; + std::set<std::pair<int, std::string>> uninitialized; + char *data = nullptr; + std::atomic<bool> dirty; + uint32_t texture = 0; + uint32_t filter = 0; + static const int buffer = 1; +}; + +}; + +#endif diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 4600e1a967..87d64b0733 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -92,6 +92,7 @@ public: inline const TransformState &getState() const { return state; } inline const Style &getStyle() const { return style; } inline GlyphAtlas &getGlyphAtlas() { return glyphAtlas; } + inline SpriteAtlas &getSpriteAtlas() { return spriteAtlas; } inline uv_loop_t *getLoop() { return loop; } inline time getAnimationTime() const { return animationTime; } inline Texturepool &getTexturepool() { return texturepool; } @@ -143,6 +144,7 @@ private: Texturepool texturepool; Style style; GlyphAtlas glyphAtlas; + SpriteAtlas spriteAtlas; Painter painter; std::map<std::string, const std::unique_ptr<Source>> sources; diff --git a/include/llmr/map/tile_parser.hpp b/include/llmr/map/tile_parser.hpp index 12f3da8ff2..86545e4f76 100644 --- a/include/llmr/map/tile_parser.hpp +++ b/include/llmr/map/tile_parser.hpp @@ -9,13 +9,14 @@ namespace llmr { class Style; class GlyphAtlas; +class SpriteAtlas; class LayerDescription; class Bucket; class TileParser { public: - TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas); + TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas, SpriteAtlas &spriteAtlas); private: bool obsolete() const; @@ -27,13 +28,14 @@ private: std::unique_ptr<Bucket> createIconBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc); std::unique_ptr<Bucket> createTextBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc); template <class Bucket> void addBucketFeatures(Bucket& bucket, const VectorTileLayer& layer, const BucketDescription& bucket_desc); - template <class Bucket, typename... Args> void addBucketFeatures(Bucket& bucket, const VectorTileLayer& layer, const BucketDescription& bucket_desc, Args... args); + template <class Bucket, typename ...Args> void addBucketFeatures(Bucket& bucket, const VectorTileLayer& layer, const BucketDescription& bucket_desc, Args&& ...args); private: const VectorTile vector_data; VectorTileData& tile; const Style& style; GlyphAtlas& glyphAtlas; + SpriteAtlas &spriteAtlas; Faces faces; Placement placement; }; diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index 4f7b4ed693..dc8f7b5faf 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -32,8 +32,11 @@ std::shared_ptr<Request> request_http(const std::string &url, // Cancels an HTTP request. void cancel_request_http(const std::shared_ptr<Request> &req); -// Shows an RGBA image with the specified dimensions in a named window. +// Shows an alpha image with the specified dimensions in a named window. void show_debug_image(std::string name, const char *data, size_t width, size_t height); + +// Shows an alpha image with the specified dimensions in a named window. +void show_color_debug_image(std::string name, const char *data, size_t width, size_t height); } } diff --git a/include/llmr/renderer/icon_bucket.hpp b/include/llmr/renderer/icon_bucket.hpp index ce90edce30..0878622ec3 100644 --- a/include/llmr/renderer/icon_bucket.hpp +++ b/include/llmr/renderer/icon_bucket.hpp @@ -20,7 +20,7 @@ class IconVertexBuffer; class BucketDescription; class IconShader; class DotShader; -class Sprite; +class SpriteAtlas; class VectorTileFeature; class IconBucket : public Bucket { @@ -31,7 +31,7 @@ public: virtual void render(Painter& painter, const std::string& layer_name, const Tile::ID& id); virtual bool hasData() const; - void addFeature(const VectorTileFeature &feature, const std::shared_ptr<Sprite> &sprite); + void addFeature(const VectorTileFeature &feature, SpriteAtlas &sprite_atlas); void drawIcons(IconShader& shader); void drawIcons(DotShader& shader); diff --git a/include/llmr/style/style.hpp b/include/llmr/style/style.hpp index 8fcb99732e..d9c8c22bda 100644 --- a/include/llmr/style/style.hpp +++ b/include/llmr/style/style.hpp @@ -7,6 +7,7 @@ #include <llmr/style/bucket_description.hpp> #include <llmr/style/layer_description.hpp> #include <llmr/style/class_description.hpp> +#include <llmr/geometry/sprite_atlas.hpp> #include <map> #include <vector> diff --git a/include/llmr/util/math.hpp b/include/llmr/util/math.hpp index 531a13a1a5..ab8e392c48 100644 --- a/include/llmr/util/math.hpp +++ b/include/llmr/util/math.hpp @@ -87,12 +87,28 @@ inline T dist(const S1& a, const S2& b) { return c; } +template <typename T> +inline T length(T a, T b) { + return std::sqrt(a * a + b * b); +} + // Take the magnitude of vector a. template <typename T = double, typename S> inline T mag(const S& a) { return std::sqrt(a.x * a.x + a.y * a.y); } +template <typename T> +T clamp(T value, T min, T max) { + return value < min ? min : (value > max ? max : value); +} + +template <typename T> +T smoothstep(T edge0, T edge1, T x) { + T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1)); + return t * t * (T(3) - T(2) * t); +} + } } diff --git a/include/llmr/util/raster.hpp b/include/llmr/util/raster.hpp index ccdffeef37..0ad29dc7a2 100644 --- a/include/llmr/util/raster.hpp +++ b/include/llmr/util/raster.hpp @@ -32,6 +32,8 @@ public: bool needsTransition() const; void updateTransitions(time now); + inline const char *getData() const { return img; } + public: // loaded image dimensions uint32_t width = 0, height = 0; diff --git a/include/llmr/util/rect.hpp b/include/llmr/util/rect.hpp index 56bab2c9c9..2e142c3018 100644 --- a/include/llmr/util/rect.hpp +++ b/include/llmr/util/rect.hpp @@ -9,6 +9,11 @@ struct Rect { T x = 0, y = 0; T w = 0, h = 0; + template <typename Number> + Rect operator *(Number value) const { + return Rect(x * value, y * value, w * value, h * value); + } + operator bool() const { return w == 0 || h == 0; } }; } |