use sprite atlas

This commit is contained in:
Konstantin Käfer
2014-05-19 10:48:11 +02:00
parent 24bd0bb667
commit 13575330f7
20 changed files with 439 additions and 86 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;
};

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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>

View File

@@ -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);
}
}
}

View File

@@ -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;

View File

@@ -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; }
};
}