1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#include <mbgl/geometry/line_atlas.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/hash.hpp>
#include <cmath>
namespace mbgl {
LineAtlas::LineAtlas(const Size size)
: image(size),
dirty(true) {
}
LineAtlas::~LineAtlas() = default;
LinePatternPos LineAtlas::getDashPosition(const std::vector<float>& dasharray,
LinePatternCap patternCap) {
size_t key = patternCap == LinePatternCap::Round ? std::numeric_limits<size_t>::min()
: std::numeric_limits<size_t>::max();
for (const float part : dasharray) {
util::hash_combine<float>(key, part);
}
// Note: We're not handling hash collisions here.
const auto it = positions.find(key);
if (it == positions.end()) {
auto inserted = positions.emplace(key, addDash(dasharray, patternCap));
assert(inserted.second);
return inserted.first->second;
} else {
return it->second;
}
}
LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatternCap patternCap) {
const uint8_t n = patternCap == LinePatternCap::Round ? 7 : 0;
const uint8_t dashheight = 2 * n + 1;
const uint8_t offset = 128;
if (dasharray.size() < 2) {
return LinePatternPos();
}
if (nextRow + dashheight > image.size.height) {
Log::Warning(Event::OpenGL, "line atlas bitmap overflow");
return LinePatternPos();
}
float length = 0;
for (const float part : dasharray) {
length += part;
}
float stretch = image.size.width / length;
float halfWidth = stretch * 0.5;
// If dasharray has an odd length, both the first and last parts
// are dashes and should be joined seamlessly.
bool oddLength = dasharray.size() % 2 == 1;
for (int y = -n; y <= n; y++) {
int row = nextRow + n + y;
int index = image.size.width * row;
float left = 0;
float right = dasharray[0];
unsigned int partIndex = 1;
if (oddLength) {
left -= dasharray.back();
}
for (uint32_t x = 0; x < image.size.width; x++) {
while (right < x / stretch) {
left = right;
if (partIndex >= dasharray.size()) {
return LinePatternPos();
}
right = right + dasharray[partIndex];
if (oddLength && partIndex == dasharray.size() - 1) {
right += dasharray.front();
}
partIndex++;
}
float distLeft = fabs(x - left * stretch);
float distRight = fabs(x - right * stretch);
float dist = fmin(distLeft, distRight);
bool inside = (partIndex % 2) == 1;
int signedDistance;
if (patternCap == LinePatternCap::Round) {
float distMiddle = n ? (float)y / n * (halfWidth + 1) : 0;
if (inside) {
float distEdge = halfWidth - fabs(distMiddle);
signedDistance = sqrt(dist * dist + distEdge * distEdge);
} else {
signedDistance = halfWidth - sqrt(dist * dist + distMiddle * distMiddle);
}
} else {
signedDistance = int((inside ? 1 : -1) * dist);
}
image.data[index + x] = fmax(0, fmin(255, signedDistance + offset));
}
}
LinePatternPos position;
position.y = (0.5 + nextRow + n) / image.size.height;
position.height = (2.0 * n) / image.size.height;
position.width = length;
nextRow += dashheight;
dirty = true;
return position;
}
Size LineAtlas::getSize() const {
return image.size;
}
void LineAtlas::upload(gl::Context& context, uint8_t unit) {
if (!texture) {
texture = context.createTexture(image, unit);
} else if (dirty) {
context.updateTexture(*texture, image, unit);
}
dirty = false;
}
void LineAtlas::bind(gl::Context& context, uint8_t unit) {
upload(context, unit);
context.bindTexture(*texture, unit, gfx::TextureFilterType::Linear, gfx::TextureMipMapType::No,
gfx::TextureWrapType::Repeat, gfx::TextureWrapType::Clamp);
}
} // namespace mbgl
|