Merge branch 'master' into outdoors-style

Conflicts:
	bin/style.js
This commit is contained in:
Konstantin Käfer
2014-05-19 15:52:42 +02:00
58 changed files with 1096 additions and 1125 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "ios/MVKMapKit"]
path = ios/MVKMapKit
url = git@github.com:mapbox/MVKMapKit.git

View File

@@ -29,3 +29,6 @@ script:
notifications:
- hipchat: WB52YkcbCGMbNcStsGeaoRO7cyBSK4wX2ZHo2Y4b@GL
git:
submodules: false

View File

@@ -6,10 +6,10 @@ implemented in C++11, targeting iOS & OS X.
# Depends
- Modern C++ compiler that supports `-std=c++11`
- libpng
- libuv
- libcurl (depends on OpenSSL)
- glfw3
- `libpng`
- `libuv`
- `libcurl` (depends on OpenSSL)
- `glfw3`
- Boost (for Rtree support)
- Python (for build only)
- Node.js (for build only)
@@ -28,18 +28,26 @@ used on both OS X and iOS.
To create projects, you can run:
- `make xproj`: Creates an Xcode project with OS X-specific handlers for HTTP downloads and
settings storage. It uses GLFW for window handling
settings storage. It uses GLFW for window handling.
- `make lproj`: Creates an Xcode project with platform-independent handlers for downloads
and settings storage. This is what is also being built on Linux
- `make linux`: Builds the Linux GLFW application with make
and settings storage. This is what is also being built on Linux.
- `make linux`: Builds the Linux GLFW application with `make`.
## iOS
Because `libpng` isn't included in the iOS SDK, you will need to build a cross-architecture version
iOS makes use of a Cocoa-specific API currently housed in [MVKMapKit](https://github.com/mapbox/MVKMapKit),
which is included as a submodule and provides a `UIView` interface to the map view.
First, pull down the submodule(s):
git submodule init
git submodule update
Then, because `libpng` isn't included in the iOS SDK, you will need to build a cross-architecture version
yourself. Run `./setup-libraries.sh`, which is derived from Mapnik's cross-architecture build
scripts. This will also run `./configure`.
Then, `make iproj` to create and open an Xcode project with an iOS-specific view controller housing and basic gesture support.
Lastly, `make iproj` to create and open an Xcode project with an iOS-specific view controller housing.
Target devices: iPhone 4 and above (4S, 5, 5c, 5s) and iPad 2 and above (3, 4, mini and/or retina).
@@ -66,7 +74,7 @@ Build static dependencies:
This will automatically run configure for you and set the correct paths.
You can then proceed to build the library, tests or a GLFW app itself.
You can then proceed to build the library, tests, or a GLFW app itself.
# Style

View File

@@ -706,76 +706,13 @@ module.exports = {
"feature_type": "line",
"type": "text"
},
"poi_airport": {
"poi": {
"source": "outdoors",
"layer": "poi_label",
"field": "maki",
"value": "airport",
"size": {
"x": 12,
"y": 12
},
"icon": "maki",
"size": 12,
"type": "point"
},
"poi_rail": {
"source": "outdoors",
"layer": "poi_label",
"field": "maki",
"value": "rail",
"size": {
"x": 12,
"y": 12
},
"type": "point"
},
"poi_park": {
"source": "outdoors",
"layer": "poi_label",
"field": "maki",
"value": "park",
"size": {
"x": 12,
"y": 12
},
"type": "point",
"alwaysVisible": true
},
"poi_golf": {
"source": "outdoors",
"layer": "poi_label",
"field": "maki",
"value": "golf",
"size": {
"x": 12,
"y": 12
},
"type": "point",
"alwaysVisible": true
},
"poi_hospital": {
"source": "outdoors",
"layer": "poi_label",
"field": "maki",
"value": "hospital",
"size": {
"x": 12,
"y": 12
},
"type": "point",
"alwaysVisible": true
},
"poi_college": {
"source": "outdoors",
"layer": "poi_label",
"field": "maki",
"value": "college",
"size": {
"x": 12,
"y": 12
},
"type": "point",
"alwaysVisible": true
},
"poi_label_1": {
"source": "outdoors",
"layer": "poi_label",
@@ -1266,28 +1203,8 @@ module.exports = {
"bucket": "waterway_label"
},
{
"name": "poi_airport",
"bucket": "poi_airport"
},
{
"name": "poi_rail",
"bucket": "poi_rail"
},
{
"name": "poi_golf",
"bucket": "poi_golf"
},
{
"name": "poi_park",
"bucket": "poi_park"
},
{
"name": "poi_hospital",
"bucket": "poi_hospital"
},
{
"name": "poi_college",
"bucket": "poi_college"
"name": "poi",
"bucket": "poi"
}
],
"classes": [
@@ -3458,167 +3375,48 @@ module.exports = {
"strokeWidth": 0.6,
"strokeBlur": 2,
"size": [
"stops",
{
"z": 0,
"val": 12
},
{
"z": 14,
"val": 12
},
{
"z": 15,
"val": 13
},
{
"z": 22,
"val": 13
}
]
},
"water_label": {
"color": "water_text",
"stroke": "water"
},
"waterway_label": {
"color": "water_text",
"stroke": "rgba(255,255,255,0.75)"
},
"poi_airport": {
"color": "#666",
"image": "airport-24"
},
"poi_label_1": {
"color": "#444",
"stroke": "land",
"translate": [
0,
14
],
"translate-anchor": "viewport"
},
"poi_rail": {
"color": "#666",
"image": "rail-12",
"opacity": [
"stops",
{
"z": 0,
"val": 0
},
{
"z": 16.75,
"val": 0
},
{
"z": 17,
"val": 1
},
{
"z": 22,
"val": 1
}
]
},
"poi_park": {
"color": "#666",
"image": "park-12",
"opacity": [
"stops",
{
"z": 0,
"val": 0
},
{
"z": 15.75,
"val": 0
},
{
"z": 16,
"val": 1
},
{
"z": 22,
"val": 1
}
]
},
"poi_golf": {
"color": "#666",
"image": "golf-12",
"opacity": [
"stops",
{
"z": 0,
"val": 0
},
{
"z": 15.75,
"val": 0
},
{
"z": 16,
"val": 1
},
{
"z": 22,
"val": 1
}
]
},
"poi_hospital": {
"color": "#666",
"image": "hospital-12",
"opacity": [
"stops",
{
"z": 0,
"val": 0
},
{
"z": 16.75,
"val": 0
},
{
"z": 17,
"val": 1
},
{
"z": 22,
"val": 1
}
]
},
"poi_college": {
"color": "#666",
"image": "college-12",
"opacity": [
"stops",
{
"z": 0,
"val": 0
},
{
"z": 16.75,
"val": 0
},
{
"z": 17,
"val": 1
},
{
"z": 22,
"val": 1
}
]
},
"contour_label": {
"color": "text",
"stroke": "#fff",
"size": 10
}
}
}
]
"stops", {
"z": 0,
"val": 12
}, {
"z": 14,
"val": 12
}, {
"z": 15,
"val": 13
}
],
},
"water_label": {
"color": "marine_text",
"stroke": [
1,
1,
1,
0.75
],
},
"poi": {
"color": "maki",
},
"satellite": {
"type": "raster",
"opacity": 0.25
},
}
}, {
"name": "satellite",
"layers": {
"building": {
"enabled": false,
},
"building_wall": {
"enabled": false,
},
"building_shadow": {
"enabled": false,
},
}
}],
"sprite": "/img/maki-sprite"
};

View File

@@ -204,6 +204,8 @@ namespace platform {
double elapsed() { return glfwGetTime(); }
void show_debug_image(std::string name, const char *data, size_t width, size_t height) {
glfwInit();
static GLFWwindow *debug_window = nullptr;
if (!debug_window) {
debug_window = glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr);
@@ -231,5 +233,37 @@ void show_debug_image(std::string name, const char *data, size_t width, size_t h
glfwMakeContextCurrent(current_window);
}
void show_color_debug_image(std::string name, const char *data, size_t logical_width, size_t logical_height, size_t width, size_t height) {
glfwInit();
static GLFWwindow *debug_window = nullptr;
if (!debug_window) {
debug_window = glfwCreateWindow(logical_width, logical_height, name.c_str(), nullptr, nullptr);
if (!debug_window) {
glfwTerminate();
fprintf(stderr, "Failed to initialize window\n");
exit(1);
}
}
GLFWwindow *current_window = glfwGetCurrentContext();
glfwSetWindowSize(debug_window, logical_width, logical_height);
glfwMakeContextCurrent(debug_window);
int fb_width, fb_height;
glfwGetFramebufferSize(debug_window, &fb_width, &fb_height);
float x_scale = (float)fb_width / (float)width;
float y_scale = (float)fb_height / (float)height;
glPixelZoom(x_scale, -y_scale);
glRasterPos2f(-1.0f, 1.0f);
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
glfwSwapBuffers(debug_window);
glfwMakeContextCurrent(current_window);
}
}
}

View File

@@ -1,25 +1,25 @@
#ifndef LLMR_GEOMETRY_BUFFER
#define LLMR_GEOMETRY_BUFFER
#include <cstdlib>
#include <cassert>
#include <llmr/platform/gl.hpp>
#include <llmr/util/noncopyable.hpp>
#include <cstdlib>
#include <cassert>
#include <stdexcept>
namespace llmr {
template <
size_t item_size,
int bufferType = GL_ARRAY_BUFFER,
size_t defaultLength = 8192
size_t defaultLength = 8192,
bool retainAfterUpload = false
>
class Buffer : private util::noncopyable {
public:
~Buffer() {
if (array) {
free(array);
array = nullptr;
}
cleanup();
if (buffer != 0) {
glDeleteBuffers(1, &buffer);
buffer = 0;
@@ -33,18 +33,27 @@ public:
}
// Transfers this buffer to the GPU and binds the buffer to the GL context.
void bind() {
void bind(bool force = false) {
if (buffer == 0) {
glGenBuffers(1, &buffer);
glBindBuffer(bufferType, buffer);
force = true;
}
glBindBuffer(bufferType, buffer);
if (force) {
assert("Buffer was already deleted" && array != nullptr);
glBufferData(bufferType, pos, array, GL_STATIC_DRAW);
free(array);
array = nullptr;
} else {
glBindBuffer(bufferType, buffer);
if (!retainAfterUpload) {
cleanup();
}
}
}
void cleanup() {
if (array) {
free(array);
array = nullptr;
}
}
protected:
// increase the buffer size by at least /required/ bytes.
@@ -59,6 +68,17 @@ protected:
return static_cast<char *>(array) + (pos - itemSize);
}
// Get a pointer to the item at a given index.
inline void *getElement(size_t index) {
assert("Buffer was deleted" && array != nullptr);
if (index * itemSize >= pos) {
throw new std::runtime_error("Can't get element after array bounds");
} else {
return static_cast<char *>(array) + (index * itemSize);
}
}
public:
static const size_t itemSize = item_size;

View File

@@ -0,0 +1,20 @@
#ifndef LLMR_GEOMETRY_ICON_BUFFER
#define LLMR_GEOMETRY_ICON_BUFFER
#include "buffer.hpp"
namespace llmr {
class IconVertexBuffer : public Buffer<
4 + // int16 x/y coordinates per vertex (== 4 bytes)
4 // uint16 x/y coordinates of icon in sprite (== 4 bytes)
> {
public:
typedef int16_t vertex_type;
void add(vertex_type x, vertex_type y, uint16_t tx, uint16_t ty);
};
}
#endif

View File

@@ -1,19 +0,0 @@
#ifndef LLMR_GEOMETRY_POINT_BUFFER
#define LLMR_GEOMETRY_POINT_BUFFER
#include "buffer.hpp"
namespace llmr {
class PointVertexBuffer : public Buffer<
4 // 2 coordinates per vertex (== 4 bytes)
> {
public:
typedef int16_t vertex_type;
void add(vertex_type x, vertex_type y);
};
}
#endif

View File

@@ -0,0 +1,74 @@
#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 SpritePosition;
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 a square icon. The getter also *creates* new square 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);
// Returns the coordinates of an image that is sourced from the sprite image.
// This getter does not create images, as the dimension of the texture us unknown if the
// sprite is not yet loaded. Instead, it returns a 0/0/0/0 rect.
Rect<dimension> getImage(const std::string &name, const Sprite &sprite);
// 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 getWidth() const { return width; }
inline float getHeight() const { return height; }
inline float getTextureWidth() const { return width * pixelRatio; }
inline float getTextureHeight() const { return height * pixelRatio; }
private:
void allocate();
Rect<SpriteAtlas::dimension> allocateImage(size_t width, size_t height);
void copy(const Rect<dimension> &dst, const SpritePosition &src, const Sprite &sprite);
public:
const dimension width = 0;
const dimension height = 0;
private:
std::mutex mtx;
float pixelRatio = 1.0f;
BinPack<dimension> bin;
std::map<std::string, Rect<dimension>> images;
std::set<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;
@@ -24,15 +25,17 @@ private:
std::unique_ptr<Bucket> createBucket(const BucketDescription& bucket_desc);
std::unique_ptr<Bucket> createFillBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc);
std::unique_ptr<Bucket> createLineBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc);
std::unique_ptr<Bucket> createPointBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc);
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);
private:
const VectorTile vector_data;
VectorTileData& tile;
const Style& style;
GlyphAtlas& glyphAtlas;
SpriteAtlas &spriteAtlas;
Faces faces;
Placement placement;
};

View File

@@ -10,7 +10,7 @@
#include <llmr/geometry/elements_buffer.hpp>
#include <llmr/geometry/fill_buffer.hpp>
#include <llmr/geometry/line_buffer.hpp>
#include <llmr/geometry/point_buffer.hpp>
#include <llmr/geometry/icon_buffer.hpp>
#include <llmr/geometry/text_buffer.hpp>
#include <map>
@@ -33,7 +33,7 @@ protected:
// Holds the actual geometries in this tile.
FillVertexBuffer fillVertexBuffer;
LineVertexBuffer lineVertexBuffer;
PointVertexBuffer pointVertexBuffer;
IconVertexBuffer iconVertexBuffer;
TextVertexBuffer textVertexBuffer;
TriangleElementsBuffer triangleElementsBuffer;

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 logical_width, size_t logical_height, size_t width, size_t height);
}
}

View File

@@ -1,10 +1,10 @@
#ifndef LLMR_RENDERER_POINTBUCKET
#define LLMR_RENDERER_POINTBUCKET
#ifndef LLMR_RENDERER_ICONBUCKET
#define LLMR_RENDERER_ICONBUCKET
#include <llmr/renderer/bucket.hpp>
#include <llmr/style/bucket_description.hpp>
#include <llmr/geometry/elements_buffer.hpp>
#include <llmr/geometry/point_buffer.hpp>
#include <llmr/geometry/icon_buffer.hpp>
#include <vector>
#include <memory>
@@ -16,31 +16,32 @@
namespace llmr {
class Style;
class PointVertexBuffer;
class IconVertexBuffer;
class BucketDescription;
class PointShader;
class IconShader;
class DotShader;
struct pbf;
class SpriteAtlas;
class VectorTileFeature;
class PointBucket : public Bucket {
class IconBucket : public Bucket {
public:
PointBucket(PointVertexBuffer& vertexBuffer,
IconBucket(IconVertexBuffer& vertexBuffer,
const BucketDescription& bucket_desc);
virtual void render(Painter& painter, const std::string& layer_name, const Tile::ID& id);
virtual bool hasData() const;
void addGeometry(pbf& data);
void addFeature(const VectorTileFeature &feature, SpriteAtlas &sprite_atlas);
void drawPoints(PointShader& shader);
void drawPoints(DotShader& shader);
void drawIcons(IconShader& shader);
void drawIcons(DotShader& shader);
public:
const BucketGeometryDescription geometry;
private:
PointVertexBuffer& vertexBuffer;
IconVertexBuffer& vertexBuffer;
VertexArrayObject array;
const size_t vertex_start;

View File

@@ -14,7 +14,7 @@
#include <llmr/shader/pattern_shader.hpp>
#include <llmr/shader/line_shader.hpp>
#include <llmr/shader/linejoin_shader.hpp>
#include <llmr/shader/point_shader.hpp>
#include <llmr/shader/icon_shader.hpp>
#include <llmr/shader/raster_shader.hpp>
#include <llmr/shader/text_shader.hpp>
#include <llmr/shader/dot_shader.hpp>
@@ -30,7 +30,7 @@ class GlyphAtlas;
class FillBucket;
class LineBucket;
class PointBucket;
class IconBucket;
class TextBucket;
class RasterBucket;
@@ -65,7 +65,7 @@ public:
void renderDebugText(const std::vector<std::string> &strings);
void renderFill(FillBucket& bucket, const std::string& layer_name, const Tile::ID& id);
void renderLine(LineBucket& bucket, const std::string& layer_name, const Tile::ID& id);
void renderPoint(PointBucket& bucket, const std::string& layer_name, const Tile::ID& id);
void renderIcon(IconBucket& bucket, const std::string& layer_name, const Tile::ID& id);
void renderText(TextBucket& bucket, const std::string& layer_name, const Tile::ID& id);
void renderRaster(RasterBucket& bucket, const std::string& layer_name, const Tile::ID& id);
@@ -123,7 +123,7 @@ private:
std::unique_ptr<LineShader> lineShader;
std::unique_ptr<LinejoinShader> linejoinShader;
std::unique_ptr<PatternShader> patternShader;
std::unique_ptr<PointShader> pointShader;
std::unique_ptr<IconShader> iconShader;
std::unique_ptr<RasterShader> rasterShader;
std::unique_ptr<TextShader> textShader;
std::unique_ptr<DotShader> dotShader;

View File

@@ -1,24 +1,25 @@
#ifndef LLMR_SHADER_SHADER_POINT
#define LLMR_SHADER_SHADER_POINT
#ifndef LLMR_SHADER_SHADER_ICON
#define LLMR_SHADER_SHADER_ICON
#include <llmr/shader/shader.hpp>
namespace llmr {
class PointShader : public Shader {
class IconShader : public Shader {
public:
PointShader();
IconShader();
void bind(char *offset);
void setImage(int32_t image);
void setColor(const std::array<float, 4>& color);
void setPointTopLeft(const std::array<float, 2>& point_tl);
void setPointBottomRight(const std::array<float, 2>& point_br);
void setDimension(const std::array<float, 2>& dimension);
void setSize(float size);
void setRatio(float ratio);
private:
int32_t a_pos = -1;
int32_t a_tex = -1;
int32_t image = -1;
int32_t u_image = -1;
@@ -26,14 +27,14 @@ private:
std::array<float, 4> color = {{}};
int32_t u_color = -1;
std::array<float, 2> point_tl = {{}};
int32_t u_point_tl = -1;
std::array<float, 2> point_br = {{}};
int32_t u_point_br = -1;
std::array<float, 2> dimension = {{}};
int32_t u_dimension = -1;
float size = 0;
int32_t u_size = -1;
float ratio = 0;
int32_t u_ratio = -1;
};
}

View File

@@ -13,7 +13,7 @@ enum class BucketType {
None = 0,
Fill = 1,
Line = 2,
Point = 3,
Icon = 3,
Text = 4,
Raster = 5
};
@@ -41,7 +41,7 @@ enum class TextPathType {
inline BucketType bucketType(const std::string& type) {
if (type == "fill") return BucketType::Fill;
else if (type == "line") return BucketType::Line;
else if (type == "point") return BucketType::Point;
else if (type == "point") return BucketType::Icon;
else if (type == "text") return BucketType::Text;
else if (type == "raster") return BucketType::Raster;
else return BucketType::None;
@@ -73,8 +73,9 @@ public:
CapType cap = CapType::None;
JoinType join = JoinType::None;
std::string font;
std::string text_field;
float font_size = 0.0f;
std::string field;
std::string icon;
float size = 0.0f;
float miter_limit = 2.0f;
float round_limit = 1.0f;
TextPathType path = TextPathType::Horizontal;

View File

@@ -13,7 +13,7 @@ public:
BackgroundClass background;
std::map<std::string, FillClass> fill;
std::map<std::string, LineClass> line;
std::map<std::string, PointClass> point;
std::map<std::string, IconClass> icon;
std::map<std::string, TextClass> text;
std::map<std::string, RasterClass> raster;
};

View File

@@ -60,7 +60,7 @@ struct FunctionProperty {
template <typename T> inline T evaluate(float z) const { return function(z, values); }
};
struct PointClass {
struct IconClass {
FunctionProperty enabled = true;
std::array<FunctionProperty, 2> translate = {{ 0, 0 }};
TranslateAnchor translateAnchor = TranslateAnchor::Map;
@@ -72,7 +72,7 @@ struct PointClass {
FunctionProperty blur = 0;
};
struct PointProperties {
struct IconProperties {
bool enabled = true;
std::array<float, 2> translate = {{ 0, 0 }};
TranslateAnchor translateAnchor = TranslateAnchor::Map;

View File

@@ -16,37 +16,27 @@ class Map;
class SpritePosition {
public:
SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t pixelRatio = 1);
explicit SpritePosition() {}
explicit SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t pixelRatio = 1);
uint16_t x = 0, y = 0;
uint16_t width = 0, height = 0;
uint8_t pixelRatio = 1;
};
class ImagePosition {
public:
ImagePosition() {}
ImagePosition(const vec2<uint16_t>& size, vec2<float> tl, vec2<float> br);
vec2<uint16_t> size = { 0, 0 };
vec2<float> tl = { 0, 0 };
vec2<float> br = { 0, 0 };
};
class Sprite : public std::enable_shared_from_this<Sprite> {
public:
Sprite(Map &map, float pixelRatio = 1);
void load(const std::string& base_url);
ImagePosition getPosition(const std::string& name, bool repeating = false);
const SpritePosition &getSpritePosition(const std::string& name) const;
bool isLoaded() const;
public:
const float pixelRatio;
Raster raster;
std::unique_ptr<util::Image> raster;
private:
void asyncParseJSON();
@@ -62,6 +52,7 @@ private:
std::string image;
std::atomic<bool> loaded;
std::map<std::string, SpritePosition> pos;
const SpritePosition empty;
};
}

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>
@@ -46,7 +47,7 @@ public:
BackgroundProperties background;
std::map<std::string, FillProperties> fills;
std::map<std::string, LineProperties> lines;
std::map<std::string, PointProperties> points;
std::map<std::string, IconProperties> icons;
std::map<std::string, TextProperties> texts;
std::map<std::string, RasterProperties> rasters;
} computed;

View File

@@ -22,7 +22,7 @@ private:
void parseClass(const std::string& name, JSVal value, ClassDescription& class_desc, std::map<std::string, BucketDescription>& buckets, std::vector<LayerDescription>& layers);
FillClass parseFillClass(JSVal value);
LineClass parseLineClass(JSVal value);
PointClass parsePointClass(JSVal value);
IconClass parseIconClass(JSVal value);
TextClass parseTextClass(JSVal value);
BackgroundClass parseBackgroundClass(JSVal value);
RasterClass parseRasterClass(JSVal value);

View File

@@ -8,6 +8,26 @@ namespace util {
std::string compress_png(int width, int height, void *rgba, bool flip = false);
class Image {
public:
Image(const std::string &img);
~Image();
inline const char *getData() const { return img; }
inline uint32_t getWidth() const { return width; }
inline uint32_t getHeight() const { return height; }
private:
// loaded image dimensions
uint32_t width = 0, height = 0;
// the raw image data
char *img = nullptr;
};
}
}

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

@@ -3,6 +3,7 @@
#include <llmr/util/transition.hpp>
#include <llmr/util/texturepool.hpp>
#include <llmr/util/image.hpp>
#include <string>
#include <mutex>
@@ -45,10 +46,6 @@ public:
// texture opacity
double opacity = 0;
private:
// load raw pixels
void loadImage(const std::string& data);
private:
mutable std::mutex mtx;
@@ -62,7 +59,7 @@ private:
uint32_t filter = 0;
// the raw pixels
char *img = nullptr;
std::unique_ptr<util::Image> img;
// fade in transition
std::shared_ptr<util::transition> fade_transition = nullptr;

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

View File

@@ -1,24 +1,14 @@
#import "MBXViewController.h"
#import "../common/settings_nsuserdefaults.hpp"
#import "../common/foundation_request.h"
#import "MVKMapView.h"
#import "../common/settings_nsuserdefaults.hpp"
#import <OpenGLES/EAGL.h>
#import <QuartzCore/QuartzCore.h>
#import <GLKit/GLKit.h>
#import <CoreLocation/CoreLocation.h>
#include <llmr/llmr.hpp>
#include <llmr/platform/platform.hpp>
#include <llmr/map/tile.hpp>
@interface MBXViewController () <CLLocationManagerDelegate>
@interface MBXViewController () <UIGestureRecognizerDelegate, CLLocationManagerDelegate>
@property (nonatomic) EAGLContext *context;
@property (nonatomic) CGPoint center;
@property (nonatomic) CGFloat scale;
@property (nonatomic) CGFloat angle;
@property (nonatomic) CGFloat quickZoomStart;
@property (nonatomic) MVKMapView *mapView;
@property (nonatomic) BOOL debug;
@property (nonatomic) UIView *palette;
@property (nonatomic) CLLocationManager *locationManager;
@@ -27,12 +17,7 @@
@implementation MBXViewController
class View;
llmr::Map *map = nullptr;
View *viewObj = nullptr;
llmr::Settings_NSUserDefaults *settings = nullptr;
MBXViewController *viewController = nullptr;
#pragma mark - Setup
@@ -42,117 +27,64 @@ MBXViewController *viewController = nullptr;
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restoreState) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restoreState:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
return self;
}
- (void)loadView
{
[super loadView];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context)
{
NSLog(@"Failed to create OpenGL ES context");
}
GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:self.context];
view.enableSetNeedsDisplay = NO;
view.drawableStencilFormat = GLKViewDrawableStencilFormat8;
view.drawableDepthFormat = GLKViewDrawableDepthFormat16;
[view bindDrawable];
self.view = view;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupMap];
[self setupInteraction];
self.mapView = [[MVKMapView alloc] initWithFrame:self.view.bounds];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.mapView];
settings = new llmr::Settings_NSUserDefaults();
[self restoreState:nil];
[self setupDebugUI];
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
map->start();
}
- (void)setupMap
{
viewObj = new View(self);
map = new llmr::Map(*viewObj);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
// Load settings
settings = new llmr::Settings_NSUserDefaults();
[self restoreState];
}
- (void)saveState
- (void)saveState:(NSNotification *)notification
{
if (map && settings)
if (self.mapView && settings)
{
map->getLonLatZoom(settings->longitude, settings->latitude, settings->zoom);
settings->angle = map->getAngle();
settings->debug = map->getDebug();
settings->longitude = self.mapView.centerCoordinate.longitude;
settings->latitude = self.mapView.centerCoordinate.latitude;
settings->zoom = self.mapView.zoomLevel;
settings->angle = self.mapView.direction;
settings->debug = self.mapView.isDebugActive;
settings->save();
}
}
- (void)restoreState
- (void)restoreState:(NSNotification *)notification
{
if (map && settings) {
if (self.mapView && settings) {
settings->load();
map->setLonLatZoom(settings->longitude, settings->latitude, settings->zoom);
map->setAngle(settings->angle);
map->setDebug(settings->debug);
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(settings->latitude, settings->longitude) zoomLevel:settings->zoom animated:NO];
self.mapView.direction = settings->angle;
[self.mapView setDebugActive:settings->debug];
}
}
- (void)setupInteraction
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
pinch.delegate = self;
[self.view addGestureRecognizer:pinch];
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotateGesture:)];
rotate.delegate = self;
[self.view addGestureRecognizer:rotate];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTapGesture:)];
twoFingerTap.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:twoFingerTap];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
UILongPressGestureRecognizer *quickZoom = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleQuickZoomGesture:)];
quickZoom.numberOfTapsRequired = 1;
quickZoom.minimumPressDuration = 0.25;
[self.view addGestureRecognizer:quickZoom];
}
}
#pragma clang diagnostic pop
- (void)setupDebugUI
{
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[self.mapView addGestureRecognizer:singleTap];
NSArray *selectorNames = @[ @"unrotate", @"resetPosition", @"toggleDebug", @"toggleRaster", @"locateUser" ];
CGFloat buttonSize = 40;
CGFloat bufferSize = 20;
@@ -182,32 +114,19 @@ MBXViewController *viewController = nullptr;
- (NSUInteger)supportedInterfaceOrientations
{
// We support everything; it's up to the embedding application to limit the orientations.
return UIInterfaceOrientationMaskAll;
}
#pragma mark - Rendering delegates
#pragma mark - Debugging UI
- (void)swap
- (void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap
{
if (map->needsSwap())
if (singleTap.state == UIGestureRecognizerStateEnded)
{
[(GLKView *)self.view display];
map->swapped();
[self togglePalette];
}
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
GLKView *view = (GLKView *)self.view;
CGRect rect = [view bounds];
map->resize(rect.size.width, rect.size.height, view.contentScaleFactor, view.drawableWidth, view.drawableHeight);
}
#pragma mark - Debugging UI
- (void)togglePalette
{
if (self.palette.alpha < 1)
@@ -232,24 +151,24 @@ MBXViewController *viewController = nullptr;
- (void)unrotate
{
map->resetNorth();
[self.mapView resetNorth];
}
- (void)resetPosition
{
map->resetPosition();
[self.mapView resetPosition];
}
- (void)toggleDebug
{
map->toggleDebug();
[self.mapView toggleDebug];
self.debug = ! self.debug;
}
- (void)toggleRaster
{
map->toggleRaster();
[self.mapView toggleRaster];
}
- (void)locateUser
@@ -269,223 +188,24 @@ MBXViewController *viewController = nullptr;
if (settings)
{
// Save settings
[self saveState];
[self saveState:nil];
delete settings;
settings = nullptr;
}
if (map)
{
delete map;
map = nullptr;
}
if (viewObj)
{
delete viewObj;
viewObj = nullptr;
}
if ([[EAGLContext currentContext] isEqual:self.context])
{
[EAGLContext setCurrentContext:nil];
}
}
#pragma mark - UI gestures
- (void)cancelPreviousActions
{
map->cancelTransitions();
[self.locationManager stopUpdatingLocation];
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)pan
{
[self cancelPreviousActions];
if (pan.state == UIGestureRecognizerStateBegan)
{
self.center = CGPointMake(0, 0);
}
else if (pan.state == UIGestureRecognizerStateChanged)
{
CGPoint delta = CGPointMake([pan translationInView:pan.view].x - self.center.x,
[pan translationInView:pan.view].y - self.center.y);
map->moveBy(delta.x, delta.y);
self.center = CGPointMake(self.center.x + delta.x, self.center.y + delta.y);
}
else if (pan.state == UIGestureRecognizerStateEnded)
{
if ([pan velocityInView:pan.view].x < 50 && [pan velocityInView:pan.view].y < 50)
{
return;
}
CGPoint finalCenter = CGPointMake(self.center.x + (0.1 * [pan velocityInView:pan.view].x),
self.center.y + (0.1 * [pan velocityInView:pan.view].y));
CGFloat duration = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad ? 0.3 : 0.5);
map->moveBy(finalCenter.x - self.center.x, finalCenter.y - self.center.y, duration);
}
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
{
[self cancelPreviousActions];
if (pinch.state == UIGestureRecognizerStateBegan)
{
map->startScaling();
self.scale = map->getScale();
}
else if (pinch.state == UIGestureRecognizerStateChanged)
{
CGFloat tolerance = 2.5;
CGFloat adjustment = 0;
if (pinch.scale > 1)
{
adjustment = (pinch.scale / tolerance) - (1 / tolerance);
}
else
{
adjustment = (-1 / pinch.scale) / tolerance + (1 / tolerance);
}
CGFloat newZoom = log2f(self.scale) + adjustment;
map->scaleBy(powf(2, newZoom) / map->getScale(), [pinch locationInView:pinch.view].x, [pinch locationInView:pinch.view].y);
}
else if (pinch.state == UIGestureRecognizerStateEnded)
{
map->stopScaling();
if (fabsf(pinch.velocity) < 20)
{
return;
}
CGFloat finalZoom = log2f(map->getScale()) + (0.01 * pinch.velocity);
double scale = map->getScale();
double new_scale = powf(2, finalZoom);
CGFloat duration = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad ? 0.3 : 0.5);
map->scaleBy(new_scale / scale, [pinch locationInView:pinch.view].x, [pinch locationInView:pinch.view].y, duration);
}
else if (pinch.state == UIGestureRecognizerStateCancelled)
{
map->stopScaling();
}
}
- (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
{
[self cancelPreviousActions];
if (rotate.state == UIGestureRecognizerStateBegan)
{
map->startRotating();
self.angle = map->getAngle();
}
else if (rotate.state == UIGestureRecognizerStateChanged)
{
map->setAngle(self.angle + rotate.rotation, [rotate locationInView:rotate.view].x, [rotate locationInView:rotate.view].y);
}
else if (rotate.state == UIGestureRecognizerStateEnded || rotate.state == UIGestureRecognizerStateCancelled)
{
map->stopRotating();
}
}
- (void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap
{
if (singleTap.state == UIGestureRecognizerStateEnded)
{
[self togglePalette];
}
}
- (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
{
[self cancelPreviousActions];
if (doubleTap.state == UIGestureRecognizerStateEnded)
{
map->scaleBy(2, [doubleTap locationInView:doubleTap.view].x, [doubleTap locationInView:doubleTap.view].y, 0.3);
}
}
- (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
{
[self cancelPreviousActions];
if (twoFingerTap.state == UIGestureRecognizerStateEnded)
{
map->scaleBy(0.5, [twoFingerTap locationInView:twoFingerTap.view].x, [twoFingerTap locationInView:twoFingerTap.view].y, 0.3);
}
}
- (void)handleQuickZoomGesture:(UILongPressGestureRecognizer *)quickZoom
{
[self cancelPreviousActions];
if (quickZoom.state == UIGestureRecognizerStateBegan)
{
self.scale = map->getScale();
self.quickZoomStart = [quickZoom locationInView:quickZoom.view].y;
}
else if (quickZoom.state == UIGestureRecognizerStateChanged)
{
CGFloat distance = self.quickZoomStart - [quickZoom locationInView:quickZoom.view].y;
CGFloat newZoom = log2f(self.scale) + (distance / 100);
map->scaleBy(powf(2, newZoom) / map->getScale(), self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
NSArray *validSimultaneousGestures = @[ [UIPanGestureRecognizer class], [UIPinchGestureRecognizer class], [UIRotationGestureRecognizer class] ];
return ([validSimultaneousGestures containsObject:[gestureRecognizer class]] && [validSimultaneousGestures containsObject:[otherGestureRecognizer class]]);
}
#pragma mark - User location
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *latestLocation = locations.lastObject;
map->setLonLatZoom(latestLocation.coordinate.longitude, latestLocation.coordinate.latitude, 17, 0.3);
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(latestLocation.coordinate.latitude, latestLocation.coordinate.longitude) zoomLevel:17 animated:YES];
}
class View : public llmr::View {
public:
View(MBXViewController *controller) : controller(controller) {}
virtual ~View() {}
void make_active() {
[EAGLContext setCurrentContext:controller.context];
}
void swap() {
[controller performSelectorOnMainThread:@selector(swap) withObject:nil waitUntilDone:NO];
}
private:
MBXViewController *controller = nullptr;
};
#pragma clang diagnostic pop
@end

1
ios/MVKMapKit Submodule

Submodule ios/MVKMapKit added at 172218fa97

View File

@@ -14,6 +14,8 @@
"./MBXAppDelegate.m",
"./MBXViewController.h",
"./MBXViewController.mm",
"./MVKMapKit/MVKMapKit/MVKMapView.h",
"./MVKMapKit/MVKMapKit/MVKMapView.mm",
"../common/settings_nsuserdefaults.hpp",
"../common/settings_nsuserdefaults.mm",
"../common/foundation_request.h",
@@ -29,7 +31,6 @@
'$(SDKROOT)/System/Library/Frameworks/UIKit.framework',
'$(SDKROOT)/System/Library/Frameworks/OpenGLES.framework',
'$(SDKROOT)/System/Library/Frameworks/GLKit.framework',
'$(SDKROOT)/System/Library/Frameworks/QuartzCore.framework',
'$(SDKROOT)/System/Library/Frameworks/CoreLocation.framework'
],
},

View File

@@ -0,0 +1,18 @@
#include <llmr/geometry/icon_buffer.hpp>
#include <llmr/platform/gl.hpp>
#include <cmath>
using namespace llmr;
void IconVertexBuffer::add(vertex_type x, vertex_type y, uint16_t tx, uint16_t ty) {
void *data = addElement();
vertex_type *vertices = static_cast<vertex_type *>(data);
vertices[0] = x;
vertices[1] = y;
uint16_t *texture = static_cast<uint16_t *>(data);
texture[2] = tx;
texture[3] = ty;
}

View File

@@ -1,12 +0,0 @@
#include <llmr/geometry/point_buffer.hpp>
#include <llmr/platform/gl.hpp>
#include <cmath>
using namespace llmr;
void PointVertexBuffer::add(vertex_type x, vertex_type y) {
vertex_type *vertices = static_cast<vertex_type *>(addElement());
vertices[0] = x;
vertices[1] = y;
}

View File

@@ -0,0 +1,259 @@
#include <llmr/geometry/sprite_atlas.hpp>
#include <llmr/platform/gl.hpp>
#include <llmr/platform/platform.hpp>
#include <llmr/util/math.hpp>
#include <llmr/util/std.hpp>
#include <llmr/style/sprite.hpp>
#include <cassert>
#include <cmath>
#include <algorithm>
using namespace llmr;
SpriteAtlas::SpriteAtlas(dimension width, dimension height)
: width(width),
height(height),
bin(width, height),
dirty(true) {
}
bool SpriteAtlas::resize(const float newRatio) {
if (pixelRatio == newRatio) return false;
std::lock_guard<std::mutex> lock(mtx);
const float oldRatio = pixelRatio;
pixelRatio = newRatio;
if (data) {
char *old_data = data;
data = nullptr;
allocate();
dimension w = static_cast<dimension>(width * newRatio);
dimension h = static_cast<dimension>(height * newRatio);
float s = std::pow(oldRatio / newRatio, 2);
// Basic image scaling. TODO: Replace this with better image scaling.
uint32_t *img_new = reinterpret_cast<uint32_t *>(data);
uint32_t *img_old = reinterpret_cast<uint32_t *>(old_data);
for (size_t i = 0, length = w * h; i < length; i++) {
img_new[i] = img_old[static_cast<size_t>(s * i)];
}
free(old_data);
dirty = true;
}
return dirty;
}
void copy_bitmap(const uint32_t *src, const int src_stride, const int src_x, const int src_y,
uint32_t *dst, const int dst_stride, const int dst_x, const int dst_y,
const int width, const int height) {
const int stride = width * sizeof(uint32_t);
src += src_y * src_stride + src_x;
dst += dst_y * dst_stride + dst_x;
for (int y = 0; y < height; y++, src += src_stride, dst += dst_stride) {
memcpy(dst, src, stride);
}
}
void draw_circle(uint32_t *dst, const int dst_stride, const int dst_x, const int dst_y,
const int width, const int height, const float blur,
const uint8_t r = 0xFF, const uint8_t g = 0xFF, const uint8_t b = 0xFF) {
const int sprite_stride = dst_stride;
const int radius = util::min(width, height);
for (int y = 0; y < height; y++) {
const int img_y = (dst_y + y) * sprite_stride + dst_x;
for (int x = 0; x < height; x++) {
const float dist = util::length(float(x) / radius - 0.5f, float(y) / radius - 0.5f);
const float t = util::smoothstep(0.5f, 0.5f - blur, dist);
const uint8_t alpha = t * 255;
uint32_t color = (uint32_t(r * t) << 0) |
(uint32_t(g * t) << 8) |
(uint32_t(b * t) << 16) |
(uint32_t(alpha) << 24);
dst[img_y + x] = color;
}
}
}
Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(size_t width, size_t height) {
// We have to allocate a new area in the bin, and store an empty image in it.
// Add a 1px border around every image.
Rect<dimension> rect = bin.allocate(width + 2 * buffer, height + 2 * buffer);
if (rect.w == 0) {
return rect;
}
rect.x += buffer;
rect.y += buffer;
rect.w -= 2 * buffer;
rect.h -= 2 * buffer;
return rect;
}
Rect<SpriteAtlas::dimension> SpriteAtlas::getIcon(const int size, const std::string &name) {
std::lock_guard<std::mutex> lock(mtx);
const std::string id = name + "-" + std::to_string(size);
auto rect_it = images.find(id);
if (rect_it != images.end()) {
return rect_it->second;
}
Rect<dimension> rect = allocateImage(size, size);
if (rect.w == 0) {
fprintf(stderr, "sprite atlas bitmap overflow\n");
return rect;
}
images.emplace(id, rect);
allocate();
// Draw an antialiased circle.
draw_circle(
reinterpret_cast<uint32_t *>(data),
width * pixelRatio,
rect.x * pixelRatio,
rect.y * pixelRatio,
size * pixelRatio,
size * pixelRatio,
1.0f / size / pixelRatio
);
uninitialized.emplace(id);
dirty = true;
return rect;
}
Rect<SpriteAtlas::dimension> SpriteAtlas::getImage(const std::string &name, const Sprite &sprite) {
std::lock_guard<std::mutex> lock(mtx);
auto rect_it = images.find(name);
if (rect_it != images.end()) {
return rect_it->second;
}
const SpritePosition &pos = sprite.getSpritePosition(name);
if (!pos.width || !pos.height) {
return Rect<dimension> { 0, 0, 0, 0 };
}
Rect<dimension> rect = allocateImage(pos.width / pos.pixelRatio, pos.height / pos.pixelRatio);
if (rect.w == 0) {
fprintf(stderr, "sprite atlas bitmap overflow\n");
return rect;
}
images.emplace(name, rect);
copy(rect, pos, sprite);
dirty = true;
return rect;
}
void SpriteAtlas::allocate() {
if (!data) {
dimension w = static_cast<dimension>(width * pixelRatio);
dimension h = static_cast<dimension>(height * pixelRatio);
data = (char *)calloc(w * h, sizeof(uint32_t));
}
}
void SpriteAtlas::copy(const Rect<dimension> &dst, const SpritePosition &src, const Sprite &sprite) {
if (!sprite.raster) return;
const uint32_t *src_img = reinterpret_cast<const uint32_t *>(sprite.raster->getData());
allocate();
uint32_t *dst_img = reinterpret_cast<uint32_t *>(data);
copy_bitmap(
/* source buffer */ src_img,
/* source stride */ sprite.raster->getWidth(),
/* source x */ src.x,
/* source y */ src.y,
/* dest buffer */ dst_img,
/* dest stride */ width * pixelRatio,
/* dest x */ dst.x * pixelRatio,
/* dest y */ dst.y * pixelRatio,
/* icon dimension */ src.width,
/* icon dimension */ src.height
);
}
void SpriteAtlas::update(const Sprite &sprite) {
if (!sprite.isLoaded()) return;
SpriteAtlas &atlas = *this;
std::erase_if(uninitialized, [&sprite, &atlas](const std::string &name) {
Rect<dimension> dst = atlas.getImage(name, sprite);
const SpritePosition &src = sprite.getSpritePosition(name);
if (src.width == dst.w * atlas.pixelRatio && src.height == dst.h * atlas.pixelRatio && src.pixelRatio == atlas.pixelRatio) {
atlas.copy(dst, src, sprite);
return true;
} else {
fprintf(stderr, "sprite icon dimension mismatch\n");
return false;
}
});
}
void SpriteAtlas::bind(bool linear) {
if (!texture) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
} else {
glBindTexture(GL_TEXTURE_2D, texture);
}
GLuint filter_val = linear ? GL_LINEAR : GL_NEAREST;
if (filter_val != filter) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_val);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_val);
filter = filter_val;
}
if (dirty) {
std::lock_guard<std::mutex> lock(mtx);
allocate();
glTexImage2D(
GL_TEXTURE_2D, // GLenum target
0, // GLint level
GL_RGBA, // GLint internalformat
width * pixelRatio, // GLsizei width
height * pixelRatio, // GLsizei height
0, // GLint border
GL_RGBA, // GLenum format
GL_UNSIGNED_BYTE, // GLenum type
data // const GLvoid * data
);
// platform::show_color_debug_image("Sprite Atlas", data, width, height, width * pixelRatio, height * pixelRatio);
dirty = false;
}
};
SpriteAtlas::~SpriteAtlas() {
std::lock_guard<std::mutex> lock(mtx);
if (data) {
free(data);
data = nullptr;
}
}

View File

@@ -20,6 +20,7 @@ Map::Map(View& view)
texturepool(),
style(),
glyphAtlas(1024, 1024),
spriteAtlas(512, 512),
painter(*this),
loop(uv_loop_new()) {
@@ -175,7 +176,16 @@ void Map::resize(uint16_t width, uint16_t height, float ratio) {
}
void Map::resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height) {
bool changed = false;
if (transform.resize(width, height, ratio, fb_width, fb_height)) {
changed = true;
}
if (spriteAtlas.resize(ratio)) {
changed = true;
}
if (changed) {
update();
}
}
@@ -393,6 +403,11 @@ void Map::prepare() {
style.sprite->load(kSpriteURL);
}
// Allow the sprite atlas to potentially pull new sprite images if needed.
if (style.sprite && style.sprite->isLoaded()) {
spriteAtlas.update(*style.sprite);
}
updateTiles();
updateClippingIDs();
@@ -464,6 +479,8 @@ void Map::render() {
if (transform.needsTransition()) {
update();
}
glFlush();
}
void Map::renderLayers(const std::vector<LayerDescription>& layers, RenderPass pass) {
@@ -520,9 +537,9 @@ void Map::renderLayer(const LayerDescription& layer_desc, RenderPass pass) {
if (pass == Opaque) return;
if (is_invisible(style.computed.lines, layer_desc)) return;
break;
case BucketType::Point:
case BucketType::Icon:
if (pass == Opaque) return;
if (is_invisible(style.computed.points, layer_desc)) return;
if (is_invisible(style.computed.icons, layer_desc)) return;
break;
case BucketType::Text:
if (pass == Opaque) return;

View File

@@ -3,7 +3,7 @@
#include <llmr/style/style.hpp>
#include <llmr/renderer/fill_bucket.hpp>
#include <llmr/renderer/line_bucket.hpp>
#include <llmr/renderer/point_bucket.hpp>
#include <llmr/renderer/icon_bucket.hpp>
#include <llmr/renderer/text_bucket.hpp>
#include <llmr/renderer/raster_bucket.hpp>
#include <llmr/util/raster.hpp>
@@ -14,11 +14,12 @@
using namespace llmr;
TileParser::TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas)
TileParser::TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas, SpriteAtlas &spriteAtlas)
: vector_data(pbf((const uint8_t *)data.data(), data.size())),
tile(tile),
style(style),
glyphAtlas(glyphAtlas),
spriteAtlas(spriteAtlas),
placement(tile.id.z) {
parseGlyphs();
parseStyleLayers(style.layers);
@@ -91,8 +92,8 @@ std::unique_ptr<Bucket> TileParser::createBucket(const BucketDescription& bucket
return createFillBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Line) {
return createLineBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Point) {
return createPointBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Icon) {
return createIconBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Text) {
return createTextBucket(layer, bucket_desc);
} else {
@@ -120,6 +121,15 @@ void TileParser::addBucketFeatures(Bucket& bucket, const VectorTileLayer& layer,
}
}
template <class Bucket, typename ...Args>
void TileParser::addBucketFeatures(Bucket& bucket, const VectorTileLayer& layer, const BucketDescription& bucket_desc, Args&& ...args) {
FilteredVectorTileLayer filtered_layer(layer, bucket_desc);
for (const pbf& feature_pbf : filtered_layer) {
if (obsolete()) return;
bucket->addFeature({ feature_pbf, layer }, std::forward<Args>(args)...);
}
}
std::unique_ptr<Bucket> TileParser::createFillBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc) {
std::unique_ptr<FillBucket> bucket = std::make_unique<FillBucket>(
tile.fillVertexBuffer, tile.triangleElementsBuffer, tile.lineElementsBuffer, bucket_desc);
@@ -134,10 +144,10 @@ std::unique_ptr<Bucket> TileParser::createLineBucket(const VectorTileLayer& laye
return obsolete() ? nullptr : std::move(bucket);
}
std::unique_ptr<Bucket> TileParser::createPointBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc) {
std::unique_ptr<PointBucket> bucket = std::make_unique<PointBucket>(
tile.pointVertexBuffer, bucket_desc);
addBucketFeatures(bucket, layer, bucket_desc);
std::unique_ptr<Bucket> TileParser::createIconBucket(const VectorTileLayer& layer, const BucketDescription& bucket_desc) {
std::unique_ptr<IconBucket> bucket = std::make_unique<IconBucket>(
tile.iconVertexBuffer, bucket_desc);
addBucketFeatures(bucket, layer, bucket_desc, spriteAtlas);
return obsolete() ? nullptr : std::move(bucket);
}
@@ -164,12 +174,6 @@ std::unique_ptr<Bucket> TileParser::createTextBucket(const VectorTileLayer& laye
std::unique_ptr<TextBucket> bucket = std::make_unique<TextBucket>(
tile.textVertexBuffer, tile.triangleElementsBuffer, bucket_desc, placement);
FilteredVectorTileLayer filtered_layer(layer, bucket_desc);
for (const pbf& feature_pbf : filtered_layer) {
if (obsolete()) return nullptr;
bucket->addFeature({ feature_pbf, layer }, faces, shaping);
}
addBucketFeatures(bucket, layer, bucket_desc, faces, shaping);
return std::move(bucket);
}

View File

@@ -302,7 +302,7 @@ void FilteredVectorTileLayer::iterator::operator++() {
}
} else if (feature_pbf.tag == 3) { // geometry type
switch (feature_pbf.varint()) {
case 1 /* Point */: type = BucketType::Point; break;
case 1 /* Point */: type = BucketType::Icon; break;
case 2 /* LineString */: type = BucketType::Line; break;
case 3 /* Polygon */: type = BucketType::Fill; break;
default: type = BucketType::None; break;

View File

@@ -22,7 +22,7 @@ void VectorTileData::parse() {
// Parsing creates state that is encapsulated in TileParser. While parsing,
// the TileParser object writes results into this objects. All other state
// is going to be discarded afterwards.
TileParser parser(data, *this, map.getStyle(), map.getGlyphAtlas());
TileParser parser(data, *this, map.getStyle(), map.getGlyphAtlas(), map.getSpriteAtlas());
} catch (const std::exception& ex) {
fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what());
cancel();

View File

@@ -1,10 +1,11 @@
#include <llmr/renderer/point_bucket.hpp>
#include <llmr/geometry/point_buffer.hpp>
#include <llmr/renderer/icon_bucket.hpp>
#include <llmr/geometry/icon_buffer.hpp>
#include <llmr/geometry/elements_buffer.hpp>
#include <llmr/geometry/geometry.hpp>
#include <llmr/renderer/painter.hpp>
#include <llmr/style/style.hpp>
#include <llmr/style/sprite.hpp>
#include <llmr/map/vector_tile.hpp>
#include <llmr/platform/gl.hpp>
@@ -15,43 +16,56 @@ struct geometry_too_long_exception : std::exception {};
using namespace llmr;
PointBucket::PointBucket(PointVertexBuffer& vertexBuffer,
IconBucket::IconBucket(IconVertexBuffer& vertexBuffer,
const BucketDescription& bucket_desc)
: geometry(bucket_desc.geometry),
vertexBuffer(vertexBuffer),
vertex_start(vertexBuffer.index()) {
}
void PointBucket::addGeometry(pbf& geom) {
void IconBucket::addFeature(const VectorTileFeature &feature, SpriteAtlas &sprite_atlas) {
auto field_it = feature.properties.find(geometry.field);
if (field_it == feature.properties.end()) {
// fprintf(stderr, "feature doesn't contain field '%s'\n", geometry.field.c_str());
return;
}
std::string field = toString(field_it->second);
const Rect<uint16_t> rect = sprite_atlas.getIcon(geometry.size, field);
const uint16_t tx = rect.x + rect.w / 2;
const uint16_t ty = rect.y + rect.h / 2;
Geometry::command cmd;
pbf geom = feature.geometry;
Geometry geometry(geom);
int32_t x, y;
while ((cmd = geometry.next(x, y)) != Geometry::end) {
if (cmd == Geometry::move_to) {
vertexBuffer.add(x, y);
vertexBuffer.add(x, y, tx, ty);
} else {
fprintf(stderr, "other command than move_to in point geometry\n");
fprintf(stderr, "other command than move_to in icon geometry\n");
}
}
vertex_end = vertexBuffer.index();
}
void PointBucket::render(Painter& painter, const std::string& layer_name, const Tile::ID& id) {
painter.renderPoint(*this, layer_name, id);
void IconBucket::render(Painter& painter, const std::string& layer_name, const Tile::ID& id) {
painter.renderIcon(*this, layer_name, id);
}
bool PointBucket::hasData() const {
bool IconBucket::hasData() const {
return vertex_end > 0;
}
void PointBucket::drawPoints(PointShader& shader) {
void IconBucket::drawIcons(IconShader& shader) {
char *vertex_index = BUFFER_OFFSET(vertex_start * vertexBuffer.itemSize);
array.bind(shader, vertexBuffer, vertex_index);
glDrawArrays(GL_POINTS, 0, (GLsizei)(vertex_end - vertex_start));
}
void PointBucket::drawPoints(DotShader& shader) {
void IconBucket::drawIcons(DotShader& shader) {
char *vertex_index = BUFFER_OFFSET(vertex_start * vertexBuffer.itemSize);
array.bind(shader, vertexBuffer, vertex_index);
glDrawArrays(GL_POINTS, 0, (GLsizei)(vertex_end - vertex_start));

View File

@@ -29,7 +29,7 @@ void Painter::setup() {
setupShaders();
assert(pointShader);
assert(iconShader);
assert(plainShader);
assert(outlineShader);
assert(lineShader);
@@ -63,7 +63,7 @@ void Painter::setupShaders() {
lineShader = std::make_unique<LineShader>();
linejoinShader = std::make_unique<LinejoinShader>();
patternShader = std::make_unique<PatternShader>();
pointShader = std::make_unique<PointShader>();
iconShader = std::make_unique<IconShader>();
rasterShader = std::make_unique<RasterShader>();
textShader = std::make_unique<TextShader>();
dotShader = std::make_unique<DotShader>();

View File

@@ -2,6 +2,7 @@
#include <llmr/renderer/fill_bucket.hpp>
#include <llmr/map/map.hpp>
#include <llmr/style/sprite.hpp>
#include <llmr/geometry/sprite_atlas.hpp>
using namespace llmr;
@@ -73,16 +74,17 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons
if ((fill_color[3] >= 1.0f) == (pass == Opaque)) {
auto &sprite = map.getStyle().sprite;
if (properties.image.size() && sprite && sprite->isLoaded()) {
// Draw texture fill
ImagePosition imagePos = sprite->getPosition(properties.image, true);
if (properties.image.size() && sprite) {
auto &spriteAtlas = map.getSpriteAtlas();
Rect<uint16_t> imagePos = spriteAtlas.getImage(properties.image, *sprite);
float factor = 8.0 / std::pow(2, map.getState().getIntegerZoom() - id.z);
float mix = std::fmod(map.getState().getZoom(), 1.0);
std::array<float, 2> imageSize = {{
imagePos.size.x * factor,
imagePos.size.y *factor
imagePos.w * factor,
imagePos.h * factor
}
};
@@ -96,11 +98,17 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons
patternShader->setMatrix(vtxMatrix);
patternShader->setOffset(offset);
patternShader->setPatternSize(imageSize);
patternShader->setPatternTopLeft({{ imagePos.tl.x, imagePos.tl.y }});
patternShader->setPatternBottomRight({{ imagePos.br.x, imagePos.br.y }});
patternShader->setPatternTopLeft({{
float(imagePos.x) / spriteAtlas.getWidth(),
float(imagePos.y) / spriteAtlas.getHeight(),
}});
patternShader->setPatternBottomRight({{
float(imagePos.x + imagePos.w) / spriteAtlas.getWidth(),
float(imagePos.y + imagePos.h) / spriteAtlas.getHeight(),
}});
patternShader->setColor(fill_color);
patternShader->setMix(mix);
sprite->raster.bind(true);
spriteAtlas.bind(true);
// Draw the actual triangles into the color & stencil buffer.
glDepthRange(strata + strata_epsilon, 1.0f);

View File

@@ -0,0 +1,52 @@
#include <llmr/renderer/painter.hpp>
#include <llmr/renderer/icon_bucket.hpp>
#include <llmr/map/map.hpp>
#include <llmr/style/sprite.hpp>
#include <llmr/util/math.hpp>
using namespace llmr;
void Painter::renderIcon(IconBucket& bucket, const std::string& layer_name, const Tile::ID& id) {
// Abort early.
if (!bucket.hasData()) return;
if (pass == Opaque) return;
auto icon_properties = map.getStyle().computed.icons;
auto icon_properties_it = icon_properties.find(layer_name);
if (icon_properties_it == icon_properties.end()) return;
const IconProperties& properties = icon_properties_it->second;
if (!properties.enabled) return;
Color color = properties.color;
color[0] *= properties.opacity;
color[1] *= properties.opacity;
color[2] *= properties.opacity;
color[3] *= properties.opacity;
const mat4 &vtxMatrix = translatedMatrix(properties.translate, id, properties.translateAnchor);
SpriteAtlas &spriteAtlas = map.getSpriteAtlas();
useProgram(iconShader->program);
iconShader->setMatrix(vtxMatrix);
iconShader->setColor(color);
iconShader->setImage(0);
iconShader->setRatio(map.getState().getPixelRatio());
iconShader->setDimension({{
spriteAtlas.getTextureWidth(),
spriteAtlas.getTextureHeight(),
}});
spriteAtlas.bind(map.getState().isChanging());
const float iconSize = bucket.geometry.size * map.getState().getPixelRatio();
iconShader->setSize(iconSize);
#ifndef GL_ES_VERSION_2_0
glPointSize(iconSize);
glEnable(GL_POINT_SPRITE);
#endif
glDepthRange(strata, 1.0f);
bucket.drawIcons(*iconShader);
}

View File

@@ -1,78 +0,0 @@
#include <llmr/renderer/painter.hpp>
#include <llmr/renderer/point_bucket.hpp>
#include <llmr/map/map.hpp>
#include <llmr/style/sprite.hpp>
using namespace llmr;
void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, const Tile::ID& id) {
// Abort early.
if (!bucket.hasData()) return;
if (pass == Opaque) return;
auto point_properties = map.getStyle().computed.points;
auto point_properties_it = point_properties.find(layer_name);
if (point_properties_it == point_properties.end()) return;
const PointProperties& properties = point_properties_it->second;
if (!properties.enabled) return;
Color color = properties.color;
color[0] *= properties.opacity;
color[1] *= properties.opacity;
color[2] *= properties.opacity;
color[3] *= properties.opacity;
auto &sprite = map.getStyle().sprite;
ImagePosition imagePos;
if (properties.image.length() && sprite && sprite->isLoaded()) {
std::string sized_image = properties.image;
if (properties.size) {
sized_image.append("-");
sized_image.append(std::to_string(static_cast<int>(std::round(properties.size))));
}
imagePos = sprite->getPosition(sized_image, false);
}
const mat4 &vtxMatrix = translatedMatrix(properties.translate, id, properties.translateAnchor);
if (!imagePos.size) {
useProgram(dotShader->program);
dotShader->setMatrix(vtxMatrix);
dotShader->setColor(color);
const float pointSize = (properties.radius ? properties.radius * 2 : 8) * map.getState().getPixelRatio();
#if defined(GL_ES_VERSION_2_0)
dotShader->setSize(pointSize);
#else
glPointSize(pointSize);
glEnable(GL_POINT_SPRITE);
#endif
dotShader->setBlur((properties.blur ? properties.blur : 1.5) / pointSize);
glDepthRange(strata, 1.0f);
bucket.drawPoints(*dotShader);
} else {
useProgram(pointShader->program);
pointShader->setMatrix(vtxMatrix);
pointShader->setColor(color);
pointShader->setImage(0);
pointShader->setPointTopLeft({{ imagePos.tl.x, imagePos.tl.y }});
pointShader->setPointBottomRight({{ imagePos.br.x, imagePos.br.y }});
sprite->raster.bind(map.getState().isChanging());
const float pointSize = (properties.size ? properties.size : (imagePos.size.x / map.getState().getPixelRatio())) * 1.4142135623730951 * map.getState().getPixelRatio();
#if defined(GL_ES_VERSION_2_0)
pointShader->setSize(pointSize);
#else
glPointSize(pointSize);
glEnable(GL_POINT_SPRITE);
#endif
glDepthRange(strata, 1.0f);
bucket.drawPoints(*pointShader);
}
}

View File

@@ -28,7 +28,7 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons
}
// If layerStyle.size > bucket.info.fontSize then labels may collide
float fontSize = std::fmin(properties.size, bucket.geom_desc.font_size);
float fontSize = std::fmin(properties.size, bucket.geom_desc.size);
matrix::scale(exMatrix, exMatrix, fontSize / 24.0f, fontSize / 24.0f, 1.0f);
const mat4 &vtxMatrix = translatedMatrix(properties.translate, id, properties.translateAnchor);
@@ -45,7 +45,7 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons
float angle = std::round((map.getState().getAngle() + rotate) / M_PI * 128);
// adjust min/max zooms for variable font sies
float zoomAdjust = log(fontSize / bucket.geom_desc.font_size) / log(2);
float zoomAdjust = log(fontSize / bucket.geom_desc.size) / log(2);
textShader->setAngle((int32_t)(angle + 256) % 256);
textShader->setFlip(bucket.geom_desc.path == TextPathType::Curve ? 1 : 0);

View File

@@ -102,7 +102,7 @@ void TextBucket::addGlyphs(const PlacedGlyphs &glyphs, float placementZoom,
void TextBucket::addFeature(const VectorTileFeature &feature,
const IndexedFaces &faces,
const std::map<Value, Shaping> &shapings) {
auto it_prop = feature.properties.find(geom_desc.text_field);
auto it_prop = feature.properties.find(geom_desc.field);
if (it_prop == feature.properties.end()) {
// feature does not have the correct property
return;

View File

@@ -23,7 +23,7 @@ DotShader::DotShader()
u_size = glGetUniformLocation(program, "u_size");
u_blur = glGetUniformLocation(program, "u_blur");
// fprintf(stderr, "PointShader:\n");
// fprintf(stderr, "DotShader:\n");
// fprintf(stderr, " - u_matrix: %d\n", u_matrix);
// fprintf(stderr, " - u_color: %d\n", u_color);
// fprintf(stderr, " - u_size: %d\n", u_size);
@@ -32,7 +32,7 @@ DotShader::DotShader()
void DotShader::bind(char *offset) {
glEnableVertexAttribArray(a_pos);
glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset);
glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset);
}
void DotShader::setColor(const std::array<float, 4>& new_color) {

View File

@@ -0,0 +1,11 @@
uniform sampler2D u_image;
uniform vec2 u_dimension;
uniform vec4 u_color;
uniform float u_size;
varying vec2 v_tex;
void main() {
vec2 pos = (v_tex + (gl_PointCoord - 0.5) * u_size) / u_dimension;
gl_FragColor = texture2D(u_image, pos);
}

View File

@@ -1,9 +1,14 @@
attribute vec2 a_pos;
attribute vec2 a_tex;
uniform mat4 u_matrix;
uniform float u_size;
uniform float u_ratio;
varying vec2 v_tex;
void main() {
gl_Position = u_matrix * vec4(a_pos, 0, 1);
gl_PointSize = u_size;
v_tex = a_tex * u_ratio;
}

View File

@@ -0,0 +1,78 @@
#include <llmr/shader/icon_shader.hpp>
#include <llmr/shader/shaders.hpp>
#include <llmr/platform/gl.hpp>
#include <cstdio>
using namespace llmr;
IconShader::IconShader()
: Shader(
shaders[ICON_SHADER].vertex,
shaders[ICON_SHADER].fragment
) {
if (!valid) {
fprintf(stderr, "invalid icon shader\n");
return;
}
a_pos = glGetAttribLocation(program, "a_pos");
a_tex = glGetAttribLocation(program, "a_tex");
u_matrix = glGetUniformLocation(program, "u_matrix");
u_color = glGetUniformLocation(program, "u_color");
u_size = glGetUniformLocation(program, "u_size");
u_ratio = glGetUniformLocation(program, "u_ratio");
u_dimension = glGetUniformLocation(program, "u_dimension");
// fprintf(stderr, "IconShader:\n");
// fprintf(stderr, " - u_matrix: %d\n", u_matrix);
// fprintf(stderr, " - u_color: %d\n", u_color);
// fprintf(stderr, " - u_size: %d\n", u_size);
// fprintf(stderr, " - u_ratio: %d\n", u_ratio);
// fprintf(stderr, " - u_dimension: %d\n", u_dimension);
// fprintf(stderr, " - u_image: %d\n", u_image);
}
void IconShader::bind(char *offset) {
glEnableVertexAttribArray(a_pos);
glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset);
glEnableVertexAttribArray(a_tex);
glVertexAttribPointer(a_tex, 2, GL_UNSIGNED_SHORT, false, 8, offset + 4);
}
void IconShader::setImage(int32_t new_image) {
if (image != new_image) {
glUniform1i(u_image, new_image);
image = new_image;
}
}
void IconShader::setColor(const std::array<float, 4>& new_color) {
if (color != new_color) {
glUniform4fv(u_color, 1, new_color.data());
color = new_color;
}
}
void IconShader::setSize(float new_size) {
if (size != new_size) {
glUniform1f(u_size, new_size);
size = new_size;
}
}
void IconShader::setRatio(float new_ratio) {
if (ratio != new_ratio) {
glUniform1f(u_ratio, new_ratio);
ratio = new_ratio;
}
}
void IconShader::setDimension(const std::array<float, 2>& new_dimension) {
if (dimension != new_dimension) {
glUniform2fv(u_dimension, 1, new_dimension.data());
dimension = new_dimension;
}
}

View File

@@ -1,21 +0,0 @@
#define root2 1.4142135623730951
uniform sampler2D u_image;
uniform vec2 u_tl;
uniform vec2 u_br;
uniform vec4 u_color;
uniform vec2 pos;
uniform float inbounds;
uniform vec4 color;
void main() {
vec2 pos = (gl_PointCoord * 2.0 - 1.0) * root2 / 2.0 + 0.5;
float inbounds = step(0.0, pos.x) * step(0.0, pos.y) *
(1.0 - step(1.0, pos.x)) * (1.0 - step(1.0, pos.y));
vec4 color = texture2D(u_image, mix(u_tl, u_br, pos)) * inbounds;
gl_FragColor = color * u_color;
}

View File

@@ -1,74 +0,0 @@
#include <llmr/shader/point_shader.hpp>
#include <llmr/shader/shaders.hpp>
#include <llmr/platform/gl.hpp>
#include <cstdio>
using namespace llmr;
PointShader::PointShader()
: Shader(
shaders[POINT_SHADER].vertex,
shaders[POINT_SHADER].fragment
) {
if (!valid) {
fprintf(stderr, "invalid point shader\n");
return;
}
a_pos = glGetAttribLocation(program, "a_pos");
u_matrix = glGetUniformLocation(program, "u_matrix");
u_color = glGetUniformLocation(program, "u_color");
u_size = glGetUniformLocation(program, "u_size");
u_point_tl = glGetUniformLocation(program, "u_tl");
u_point_br = glGetUniformLocation(program, "u_br");
// fprintf(stderr, "PointShader:\n");
// fprintf(stderr, " - u_matrix: %d\n", u_matrix);
// fprintf(stderr, " - u_color: %d\n", u_color);
// fprintf(stderr, " - u_size: %d\n", u_size);
// fprintf(stderr, " - u_point_tl: %d\n", u_point_tl);
// fprintf(stderr, " - u_point_br: %d\n", u_point_br);
// fprintf(stderr, " - u_image: %d\n", u_image);
}
void PointShader::bind(char *offset) {
glEnableVertexAttribArray(a_pos);
glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset);
}
void PointShader::setImage(int32_t new_image) {
if (image != new_image) {
glUniform1i(u_image, new_image);
image = new_image;
}
}
void PointShader::setColor(const std::array<float, 4>& new_color) {
if (color != new_color) {
glUniform4fv(u_color, 1, new_color.data());
color = new_color;
}
}
void PointShader::setSize(float new_size) {
if (size != new_size) {
glUniform1f(u_size, new_size);
size = new_size;
}
}
void PointShader::setPointTopLeft(const std::array<float, 2>& new_point_tl) {
if (point_tl != new_point_tl) {
glUniform2fv(u_point_tl, 1, new_point_tl.data());
point_tl = new_point_tl;
}
}
void PointShader::setPointBottomRight(const std::array<float, 2>& new_point_br) {
if (point_br != new_point_br) {
glUniform2fv(u_point_br, 1, new_point_br.data());
point_br = new_point_br;
}
}

View File

@@ -10,6 +10,10 @@ const shader_source llmr::shaders[SHADER_COUNT] = {
"#version 120\nattribute vec2 a_pos;\nuniform mat4 u_matrix;\nuniform float u_size;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n gl_PointSize = u_size;\n}\n\n",
"#version 120\nuniform vec4 u_color;\nuniform float u_blur;\nvoid main ()\n{\n vec2 x_1;\n x_1 = (gl_PointCoord - 0.5);\n float tmpvar_2;\n tmpvar_2 = clamp (((\n sqrt(dot (x_1, x_1))\n - 0.5) / (\n (0.5 - u_blur)\n - 0.5)), 0.0, 1.0);\n gl_FragColor = (u_color * (tmpvar_2 * (tmpvar_2 * \n (3.0 - (2.0 * tmpvar_2))\n )));\n}\n\n",
},
{
"#version 120\nattribute vec2 a_pos;\nattribute vec2 a_tex;\nuniform mat4 u_matrix;\nuniform float u_size;\nuniform float u_ratio;\nvarying vec2 v_tex;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n gl_PointSize = u_size;\n v_tex = (a_tex * u_ratio);\n}\n\n",
"#version 120\nuniform sampler2D u_image;\nuniform vec2 u_dimension;\nuniform float u_size;\nvarying vec2 v_tex;\nvoid main ()\n{\n gl_FragColor = texture2D (u_image, ((v_tex + (\n (gl_PointCoord - 0.5)\n * u_size)) / u_dimension));\n}\n\n",
},
{
"#version 120\nattribute vec2 a_pos;\nattribute vec2 a_extrude;\nattribute float a_linesofar;\nuniform mat4 u_matrix;\nuniform mat4 u_exmatrix;\nuniform float u_ratio;\nuniform vec2 u_linewidth;\nvarying vec2 v_normal;\nvarying float v_linesofar;\nvoid main ()\n{\n vec2 normal_1;\n vec2 tmpvar_2;\n tmpvar_2 = (vec2(mod (a_pos, 2.0)));\n normal_1.x = tmpvar_2.x;\n normal_1.y = sign((tmpvar_2.y - 0.5));\n v_normal = normal_1;\n vec4 tmpvar_3;\n tmpvar_3.zw = vec2(0.0, 0.0);\n tmpvar_3.xy = ((u_linewidth.x * a_extrude) * 0.015873);\n vec4 tmpvar_4;\n tmpvar_4.zw = vec2(0.0, 1.0);\n tmpvar_4.xy = floor((a_pos * 0.5));\n gl_Position = ((u_matrix * tmpvar_4) + (u_exmatrix * tmpvar_3));\n v_linesofar = (a_linesofar * u_ratio);\n}\n\n",
"#version 120\nuniform vec2 u_linewidth;\nuniform vec4 u_color;\nuniform vec2 u_dasharray;\nvarying vec2 v_normal;\nvarying float v_linesofar;\nvoid main ()\n{\n float tmpvar_1;\n tmpvar_1 = (sqrt(dot (v_normal, v_normal)) * u_linewidth.x);\n float tmpvar_2;\n tmpvar_2 = (float(mod (v_linesofar, (u_dasharray.x + u_dasharray.y))));\n gl_FragColor = (u_color * (clamp (\n min ((tmpvar_1 - (u_linewidth.y - 1.0)), (u_linewidth.x - tmpvar_1))\n , 0.0, 1.0) * max (\n float((-(u_dasharray.y) >= 0.0))\n , \n clamp (min (tmpvar_2, (u_dasharray.x - tmpvar_2)), 0.0, 1.0)\n )));\n}\n\n",
@@ -30,10 +34,6 @@ const shader_source llmr::shaders[SHADER_COUNT] = {
"#version 120\nattribute vec2 a_pos;\nuniform mat4 u_matrix;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n}\n\n",
"#version 120\nuniform vec4 u_color;\nvoid main ()\n{\n gl_FragColor = u_color;\n}\n\n",
},
{
"#version 120\nattribute vec2 a_pos;\nuniform mat4 u_matrix;\nuniform float u_size;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n gl_PointSize = u_size;\n}\n\n",
"#version 120\nuniform sampler2D u_image;\nuniform vec2 u_tl;\nuniform vec2 u_br;\nuniform vec4 u_color;\nvoid main ()\n{\n vec2 tmpvar_1;\n tmpvar_1 = (((\n ((gl_PointCoord * 2.0) - 1.0)\n * 1.41421) / 2.0) + 0.5);\n gl_FragColor = ((texture2D (u_image, mix (u_tl, u_br, tmpvar_1)) * (\n ((float((tmpvar_1.x >= 0.0)) * float((tmpvar_1.y >= 0.0))) * (1.0 - float((tmpvar_1.x >= 1.0))))\n * \n (1.0 - float((tmpvar_1.y >= 1.0)))\n )) * u_color);\n}\n\n",
},
{
"#version 120\nuniform mat4 u_matrix;\nattribute vec2 a_pos;\nvarying vec2 v_pos;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n v_pos = (a_pos / 4096.0);\n}\n\n",
"#version 120\nuniform sampler2D u_image;\nuniform float u_opacity;\nvarying vec2 v_pos;\nvoid main ()\n{\n gl_FragColor = (texture2D (u_image, v_pos) * u_opacity);\n}\n\n",

View File

@@ -10,6 +10,10 @@ const shader_source llmr::shaders[SHADER_COUNT] = {
"precision highp float;\nattribute vec2 a_pos;\nuniform mat4 u_matrix;\nuniform float u_size;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n gl_PointSize = u_size;\n}\n\n",
"precision highp float;\nuniform vec4 u_color;\nuniform float u_blur;\nvoid main ()\n{\n mediump vec2 x_1;\n x_1 = (gl_PointCoord - 0.5);\n mediump float tmpvar_2;\n tmpvar_2 = clamp (((\n sqrt(dot (x_1, x_1))\n - 0.5) / (\n (0.5 - u_blur)\n - 0.5)), 0.0, 1.0);\n gl_FragColor = (u_color * (tmpvar_2 * (tmpvar_2 * \n (3.0 - (2.0 * tmpvar_2))\n )));\n}\n\n",
},
{
"precision highp float;\nattribute vec2 a_pos;\nattribute vec2 a_tex;\nuniform mat4 u_matrix;\nuniform float u_size;\nuniform float u_ratio;\nvarying vec2 v_tex;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n gl_PointSize = u_size;\n v_tex = (a_tex * u_ratio);\n}\n\n",
"precision highp float;\nuniform sampler2D u_image;\nuniform vec2 u_dimension;\nuniform float u_size;\nvarying vec2 v_tex;\nvoid main ()\n{\n mediump vec2 tmpvar_1;\n tmpvar_1 = ((v_tex + (\n (gl_PointCoord - 0.5)\n * u_size)) / u_dimension);\n lowp vec4 tmpvar_2;\n tmpvar_2 = texture2D (u_image, tmpvar_1);\n gl_FragColor = tmpvar_2;\n}\n\n",
},
{
"precision highp float;\nattribute vec2 a_pos;\nattribute vec2 a_extrude;\nattribute float a_linesofar;\nuniform mat4 u_matrix;\nuniform mat4 u_exmatrix;\nuniform float u_ratio;\nuniform vec2 u_linewidth;\nvarying vec2 v_normal;\nvarying float v_linesofar;\nvoid main ()\n{\n vec2 normal_1;\n vec2 tmpvar_2;\n tmpvar_2 = (vec2(mod (a_pos, 2.0)));\n normal_1.x = tmpvar_2.x;\n normal_1.y = sign((tmpvar_2.y - 0.5));\n v_normal = normal_1;\n vec4 tmpvar_3;\n tmpvar_3.zw = vec2(0.0, 0.0);\n tmpvar_3.xy = ((u_linewidth.x * a_extrude) * 0.015873);\n vec4 tmpvar_4;\n tmpvar_4.zw = vec2(0.0, 1.0);\n tmpvar_4.xy = floor((a_pos * 0.5));\n gl_Position = ((u_matrix * tmpvar_4) + (u_exmatrix * tmpvar_3));\n v_linesofar = (a_linesofar * u_ratio);\n}\n\n",
"precision highp float;\nuniform vec2 u_linewidth;\nuniform vec4 u_color;\nuniform vec2 u_dasharray;\nvarying vec2 v_normal;\nvarying float v_linesofar;\nvoid main ()\n{\n float tmpvar_1;\n tmpvar_1 = (sqrt(dot (v_normal, v_normal)) * u_linewidth.x);\n float tmpvar_2;\n tmpvar_2 = (float(mod (v_linesofar, (u_dasharray.x + u_dasharray.y))));\n gl_FragColor = (u_color * (clamp (\n min ((tmpvar_1 - (u_linewidth.y - 1.0)), (u_linewidth.x - tmpvar_1))\n , 0.0, 1.0) * max (\n float((-(u_dasharray.y) >= 0.0))\n , \n clamp (min (tmpvar_2, (u_dasharray.x - tmpvar_2)), 0.0, 1.0)\n )));\n}\n\n",
@@ -30,10 +34,6 @@ const shader_source llmr::shaders[SHADER_COUNT] = {
"precision highp float;\nattribute vec2 a_pos;\nuniform mat4 u_matrix;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n}\n\n",
"precision highp float;\nuniform vec4 u_color;\nvoid main ()\n{\n gl_FragColor = u_color;\n}\n\n",
},
{
"precision highp float;\nattribute vec2 a_pos;\nuniform mat4 u_matrix;\nuniform float u_size;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n gl_PointSize = u_size;\n}\n\n",
"precision highp float;\nuniform sampler2D u_image;\nuniform vec2 u_tl;\nuniform vec2 u_br;\nuniform vec4 u_color;\nvoid main ()\n{\n mediump vec2 tmpvar_1;\n tmpvar_1 = (((\n ((gl_PointCoord * 2.0) - 1.0)\n * 1.41421) / 2.0) + 0.5);\n mediump vec2 tmpvar_2;\n tmpvar_2 = mix (u_tl, u_br, tmpvar_1);\n lowp vec4 tmpvar_3;\n tmpvar_3 = texture2D (u_image, tmpvar_2);\n gl_FragColor = ((tmpvar_3 * (\n ((float((tmpvar_1.x >= 0.0)) * float((tmpvar_1.y >= 0.0))) * (1.0 - float((tmpvar_1.x >= 1.0))))\n * \n (1.0 - float((tmpvar_1.y >= 1.0)))\n )) * u_color);\n}\n\n",
},
{
"precision highp float;\nuniform mat4 u_matrix;\nattribute vec2 a_pos;\nvarying vec2 v_pos;\nvoid main ()\n{\n vec4 tmpvar_1;\n tmpvar_1.zw = vec2(0.0, 1.0);\n tmpvar_1.xy = a_pos;\n gl_Position = (u_matrix * tmpvar_1);\n v_pos = (a_pos / 4096.0);\n}\n\n",
"precision highp float;\nuniform sampler2D u_image;\nuniform float u_opacity;\nvarying vec2 v_pos;\nvoid main ()\n{\n lowp vec4 tmpvar_1;\n tmpvar_1 = (texture2D (u_image, v_pos) * u_opacity);\n gl_FragColor = tmpvar_1;\n}\n\n",

View File

@@ -14,6 +14,6 @@ std::ostream& llmr::operator<<(std::ostream& os, const BucketDescription& bucket
os << " - cap: " << (uint32_t)bucket.geometry.cap << std::endl;
os << " - join: " << (uint32_t)bucket.geometry.join << std::endl;
os << " - font: " << bucket.geometry.font << std::endl;
os << " - font_size: " << bucket.geometry.font_size << std::endl;
os << " - size: " << bucket.geometry.size << std::endl;
return os;
}

View File

@@ -5,6 +5,7 @@
#include <string>
#include <llmr/platform/platform.hpp>
#include <llmr/util/uv.hpp>
#include <llmr/util/std.hpp>
#include <rapidjson/document.h>
@@ -18,48 +19,43 @@ SpritePosition::SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t
pixelRatio(pixelRatio) {
}
ImagePosition::ImagePosition(const vec2<uint16_t>& size, vec2<float> tl, vec2<float> br)
: size(size),
tl(tl),
br(br) {}
Sprite::Sprite(Map &map, float pixelRatio)
: pixelRatio(pixelRatio),
raster(map.getTexturepool()),
raster(),
map(map),
loaded(false) {
}
void Sprite::load(const std::string& base_url) {
std::shared_ptr<Sprite> sprite = shared_from_this();
std::shared_ptr<Sprite> sprite = shared_from_this();
std::string suffix = (pixelRatio > 1 ? "@2x" : "");
std::string suffix = (pixelRatio > 1 ? "@2x" : "");
platform::request_http(base_url + suffix + ".json", [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->body.swap(res->body);
sprite->asyncParseJSON();
} else {
fprintf(stderr, "failed to load sprite\n");
}
}, map.getLoop());
platform::request_http(base_url + suffix + ".json", [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->body.swap(res->body);
sprite->asyncParseJSON();
} else {
fprintf(stderr, "failed to load sprite\n");
}
}, map.getLoop());
platform::request_http(base_url + suffix + ".png", [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->image.swap(res->body);
sprite->asyncParseImage();
} else {
fprintf(stderr, "failed to load sprite image\n");
}
}, map.getLoop());
platform::request_http(base_url + suffix + ".png", [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->image.swap(res->body);
sprite->asyncParseImage();
} else {
fprintf(stderr, "failed to load sprite image\n");
}
}, map.getLoop());
}
void Sprite::complete(std::shared_ptr<Sprite> &sprite) {
if (sprite->raster.isLoaded() && sprite->pos.size()) {
sprite->loaded = true;
sprite->map.update();
fprintf(stderr, "sprite loaded\n");
}
if (sprite->raster && sprite->pos.size()) {
sprite->loaded = true;
sprite->map.update();
fprintf(stderr, "sprite loaded\n");
}
}
bool Sprite::isLoaded() const {
@@ -75,7 +71,7 @@ void Sprite::asyncParseJSON() {
}
void Sprite::parseImage(std::shared_ptr<Sprite> &sprite) {
sprite->raster.load(sprite->image);
sprite->raster = std::make_unique<util::Image>(sprite->image);
sprite->image.clear();
}
@@ -101,37 +97,14 @@ void Sprite::parseJSON(std::shared_ptr<Sprite> &sprite) {
if (value.HasMember("width")) width = value["width"].GetInt();
if (value.HasMember("height")) height = value["height"].GetInt();
if (value.HasMember("pixelRatio")) pixelRatio = value["pixelRatio"].GetInt();
sprite->pos.insert({ name, { x, y, width, height, pixelRatio } });
sprite->pos.emplace(name, SpritePosition { x, y, width, height, pixelRatio });
}
}
}
}
ImagePosition Sprite::getPosition(const std::string& name, bool repeating) {
if (!isLoaded()) return {};
// `repeating` indicates that the image will be used in a repeating pattern
// repeating pattern images are assumed to have a 1px padding that mirrors the opposite edge
// positions for repeating images are adjusted to exclude the edge
int8_t offset = repeating ? 1 : 0;
const SpritePosition &Sprite::getSpritePosition(const std::string& name) const {
if (!isLoaded()) return empty;
auto it = pos.find(name);
if (it == pos.end()) return {};
SpritePosition& pos = it->second;
return {
{
pos.width,
pos.height
},
{
(float)(pos.x + offset) / raster.width,
(float)(pos.y + offset) / raster.height
},
{
(float)(pos.x + pos.width - 2 * offset) / raster.width,
(float)(pos.y + pos.height - 2 * offset) / raster.height
}
};
return it == pos.end() ? empty : it->second;
}

View File

@@ -14,7 +14,9 @@ Style::Style() {
void Style::reset() {
computed.fills.clear();
computed.lines.clear();
computed.points.clear();
computed.icons.clear();
computed.texts.clear();
computed.rasters.clear();
}
void Style::cascade(float z) {
@@ -69,24 +71,24 @@ void Style::cascade(float z) {
stroke.opacity = layer.opacity.evaluate<float>(z);
}
// Cascade point classes
for (const auto& point_pair : sheetClass.point) {
const std::string& layer_name = point_pair.first;
const llmr::PointClass& layer = point_pair.second;
// Cascade icon classes
for (const auto& icon_pair : sheetClass.icon) {
const std::string& layer_name = icon_pair.first;
const llmr::IconClass& layer = icon_pair.second;
// TODO: This should be restricted to point styles that have actual
// TODO: This should be restricted to icon styles that have actual
// values so as to not override with default values.
llmr::PointProperties& point = computed.points[layer_name];
point.enabled = layer.enabled.evaluate<bool>(z);
point.translate = {{ layer.translate[0].evaluate<float>(z),
llmr::IconProperties& icon = computed.icons[layer_name];
icon.enabled = layer.enabled.evaluate<bool>(z);
icon.translate = {{ layer.translate[0].evaluate<float>(z),
layer.translate[1].evaluate<float>(z) }};
point.translateAnchor = layer.translateAnchor;
point.color = layer.color;
point.size = layer.size.evaluate<float>(z);
point.opacity = layer.opacity.evaluate<float>(z);
point.image = layer.image;
point.radius = layer.radius.evaluate<float>(z);
point.blur = layer.blur.evaluate<float>(z);
icon.translateAnchor = layer.translateAnchor;
icon.color = layer.color;
icon.size = layer.size.evaluate<float>(z);
icon.opacity = layer.opacity.evaluate<float>(z);
icon.image = layer.image;
icon.radius = layer.radius.evaluate<float>(z);
icon.blur = layer.blur.evaluate<float>(z);
}
// Cascade text classes

View File

@@ -83,15 +83,15 @@ BucketDescription StyleParser::parseBucket(JSVal value) {
} else {
throw Style::exception("font stack must be a string");
}
} else if (name == "fontSize") {
} else if (name == "fontSize" || name == "size") {
if (value.IsNumber()) {
bucket.geometry.font_size = value.GetDouble();
bucket.geometry.size = value.GetDouble();
} else {
throw Style::exception("font size must be a number");
}
} else if (name == "text_field") {
} else if (name == "text_field" || name == "icon") {
if (value.IsString()) {
bucket.geometry.text_field = { value.GetString(), value.GetStringLength() };
bucket.geometry.field = { value.GetString(), value.GetStringLength() };
} else {
throw Style::exception("text field must be a string");
}
@@ -269,8 +269,8 @@ void StyleParser::parseClass(const std::string& name, JSVal value, ClassDescript
class_desc.fill.insert({ name, std::forward<FillClass>(parseFillClass(value)) });
} else if (type == BucketType::Line) {
class_desc.line.insert({ name, std::forward<LineClass>(parseLineClass(value)) });
} else if (type == BucketType::Point) {
class_desc.point.insert({ name, std::forward<PointClass>(parsePointClass(value)) });
} else if (type == BucketType::Icon) {
class_desc.icon.insert({ name, std::forward<IconClass>(parseIconClass(value)) });
} else if (type == BucketType::Text) {
class_desc.text.insert({ name, std::forward<TextClass>(parseTextClass(value)) });
} else if (type == BucketType::Raster) {
@@ -512,8 +512,8 @@ LineClass StyleParser::parseLineClass(JSVal value) {
return klass;
}
PointClass StyleParser::parsePointClass(JSVal value) {
PointClass klass;
IconClass StyleParser::parseIconClass(JSVal value) {
IconClass klass;
if (value.HasMember("enabled")) {
klass.enabled = parseFunction(value["enabled"]);

View File

@@ -278,7 +278,7 @@ void Placement::addFeature(TextBucket& bucket,
const float textMinDistance = info.textMinDistance;
const float rotate = info.rotate;
const float fontScale =
(tileExtent / util::tileSize) / (glyphSize / info.font_size);
(tileExtent / util::tileSize) / (glyphSize / info.size);
const float advance = measureText(faces, shaping);
Anchors anchors;

View File

@@ -1,6 +1,8 @@
#include <llmr/util/image.hpp>
#include <png.h>
#include <cassert>
std::string llmr::util::compress_png(int width, int height, void *rgba, bool flip) {
png_voidp error_ptr = 0;
@@ -48,3 +50,127 @@ std::string llmr::util::compress_png(int width, int height, void *rgba, bool fli
return result;
}
using namespace llmr::util;
struct Buffer {
Buffer(const std::string& data)
: data(data.data()), length(data.size()) {}
const char *const data = 0;
const size_t length = 0;
size_t pos = 0;
};
void readCallback(png_structp png, png_bytep data, png_size_t length) {
Buffer *reader = static_cast<Buffer *>(png_get_io_ptr(png));
// Read `length` bytes into `data`.
if (reader->pos + length > reader->length) {
png_error(png, "Read Error");
} else {
memcpy(data, reader->data + reader->pos, length);
reader->pos += length;
}
}
void errorHandler(png_structp, png_const_charp error_msg) {
throw std::runtime_error(error_msg);
}
void warningHandler(png_structp, png_const_charp error_msg) {
fprintf(stderr, "PNG: %s\n", error_msg);
}
Image::Image(const std::string &data) {
Buffer buffer(data);
if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) {
fprintf(stderr, "image is not a valid PNG image\n");
return;
}
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, errorHandler, warningHandler);
assert(png);
png_infop info = png_create_info_struct(png);
assert(info);
int depth, color, interlace;
try {
png_set_read_fn(png, (png_voidp)&buffer, readCallback);
png_read_info(png, info);
png_get_IHDR(png, info, (png_uint_32*)&width, (png_uint_32*)&height, &depth, &color, &interlace, nullptr, nullptr);
bool alpha = (color & PNG_COLOR_MASK_ALPHA) || png_get_valid(png, info, PNG_INFO_tRNS);
// From http://trac.mapnik.org/browser/trunk/src/png_reader.cpp
if (color == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png);
if (color == PNG_COLOR_TYPE_GRAY)
png_set_expand(png);
if (png_get_valid(png, info, PNG_INFO_tRNS))
png_set_expand(png);
if (depth == 16)
png_set_strip_16(png);
if (depth < 8)
png_set_packing(png);
if (color == PNG_COLOR_TYPE_GRAY ||
color == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
if (interlace == PNG_INTERLACE_ADAM7)
png_set_interlace_handling(png);
// Always add an alpha channel.
if (!alpha) {
png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER);
}
double gamma;
if (png_get_gAMA(png, info, &gamma))
png_set_gamma(png, 2.2, gamma);
png_read_update_info(png, info);
png_size_t rowbytes = png_get_rowbytes(png, info);
assert(width * 4 == rowbytes);
img = (char *)malloc(width * height * 4);
char *surface = img;
assert(surface);
struct ptrs {
ptrs(size_t count) : rows(new png_bytep[count]) {}
~ptrs() { delete[] rows; }
png_bytep *rows = nullptr;
} pointers(height);
for (unsigned i = 0; i < height; ++i) {
pointers.rows[i] = (png_bytep)(surface + (i * rowbytes));
}
// Read image data
png_read_image(png, pointers.rows);
png_read_end(png, nullptr);
png_destroy_read_struct(&png, &info, nullptr);
} catch (std::exception& e) {
fprintf(stderr, "loading PNG failed: %s\n", e.what());
png_destroy_read_struct(&png, &info, nullptr);
if (img) {
free(img);
img = nullptr;
}
width = 0;
height = 0;
}
}
Image::~Image() {
if (img) {
free(img);
img = nullptr;
}
}

View File

@@ -8,6 +8,7 @@
#include <llmr/platform/gl.hpp>
#include <llmr/util/time.hpp>
#include <llmr/util/uv.hpp>
#include <llmr/util/std.hpp>
#include <png.h>
@@ -18,9 +19,7 @@ Raster::Raster(Texturepool &texturepool)
{}
Raster::~Raster() {
if (img && !textured) {
free(img);
} else if (textured) {
if (textured) {
texturepool.removeTextureID(texture);
}
}
@@ -31,130 +30,17 @@ bool Raster::isLoaded() const {
}
bool Raster::load(const std::string &data) {
loadImage(data);
img = std::make_unique<util::Image>(data);
width = img->getWidth();
height = img->getHeight();
std::lock_guard<std::mutex> lock(mtx);
if (img) {
if (img->getData()) {
loaded = true;
}
return loaded;
}
struct Buffer {
Buffer(const std::string& data)
: data(data.data()), length(data.size()) {}
const char *const data = 0;
const size_t length = 0;
size_t pos = 0;
};
void readCallback(png_structp png, png_bytep data, png_size_t length) {
Buffer *reader = static_cast<Buffer *>(png_get_io_ptr(png));
// Read `length` bytes into `data`.
if (reader->pos + length > reader->length) {
png_error(png, "Read Error");
} else {
memcpy(data, reader->data + reader->pos, length);
reader->pos += length;
}
}
void errorHandler(png_structp, png_const_charp error_msg) {
throw std::runtime_error(error_msg);
}
void warningHandler(png_structp, png_const_charp error_msg) {
fprintf(stderr, "PNG: %s\n", error_msg);
}
void Raster::loadImage(const std::string& data) {
std::lock_guard<std::mutex> lock(mtx);
Buffer buffer(data);
if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) {
fprintf(stderr, "image is not a valid PNG image\n");
return;
}
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, errorHandler, warningHandler);
assert(png);
png_infop info = png_create_info_struct(png);
assert(info);
int depth, color, interlace;
try {
png_set_read_fn(png, (png_voidp)&buffer, readCallback);
png_read_info(png, info);
png_get_IHDR(png, info, (png_uint_32*)&width, (png_uint_32*)&height, &depth, &color, &interlace, nullptr, nullptr);
bool alpha = (color & PNG_COLOR_MASK_ALPHA) || png_get_valid(png, info, PNG_INFO_tRNS);
// From http://trac.mapnik.org/browser/trunk/src/png_reader.cpp
if (color == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png);
if (color == PNG_COLOR_TYPE_GRAY)
png_set_expand(png);
if (png_get_valid(png, info, PNG_INFO_tRNS))
png_set_expand(png);
if (depth == 16)
png_set_strip_16(png);
if (depth < 8)
png_set_packing(png);
if (color == PNG_COLOR_TYPE_GRAY ||
color == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
if (interlace == PNG_INTERLACE_ADAM7)
png_set_interlace_handling(png);
// Always add an alpha channel.
if (!alpha) {
png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER);
}
double gamma;
if (png_get_gAMA(png, info, &gamma))
png_set_gamma(png, 2.2, gamma);
png_read_update_info(png, info);
png_size_t rowbytes = png_get_rowbytes(png, info);
assert(width * 4 == rowbytes);
img = (char *)malloc(width * height * 4);
char *surface = img;
assert(surface);
struct ptrs {
ptrs(size_t count) : rows(new png_bytep[count]) {}
~ptrs() { delete[] rows; }
png_bytep *rows = nullptr;
} pointers(height);
for (unsigned i = 0; i < height; ++i) {
pointers.rows[i] = (png_bytep)(surface + (i * rowbytes));
}
// Read image data
png_read_image(png, pointers.rows);
png_read_end(png, nullptr);
png_destroy_read_struct(&png, &info, nullptr);
} catch (std::exception& e) {
fprintf(stderr, "loading PNG failed: %s\n", e.what());
png_destroy_read_struct(&png, &info, nullptr);
if (img) {
free(img);
img = nullptr;
}
width = 0;
height = 0;
}
}
void Raster::bind(bool linear) {
if (!width || !height) {
@@ -167,9 +53,8 @@ void Raster::bind(bool linear) {
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
free(img);
img = nullptr;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData());
img.reset();
textured = true;
} else if (textured) {
glBindTexture(GL_TEXTURE_2D, texture);