Skip to content

Commit 756f74c

Browse files
committed
Add support for rendering labels from Markers
1 parent f6a31f7 commit 756f74c

File tree

3 files changed

+72
-30
lines changed

3 files changed

+72
-30
lines changed

core/src/labels/labels.cpp

+52-23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "tile/tileCache.h"
1313
#include "labels/labelSet.h"
1414
#include "labels/textLabel.h"
15+
#include "marker/marker.h"
1516

1617
#include "glm/glm.hpp"
1718
#include "glm/gtc/matrix_transform.hpp"
@@ -30,9 +31,41 @@ Labels::~Labels() {}
3031
// return (int) MIN(floor(((log(-_zoom + (_maxZoom + 2)) / log(_maxZoom + 2) * (_maxZoom )) * 0.5)), MAX_LOD);
3132
// }
3233

34+
void Labels::processLabelUpdate(StyledMesh* mesh, Tile* tile,
35+
const glm::mat4& mvp, const glm::vec2& screen,
36+
float dt, float dz, bool drawAll,
37+
bool onlyTransitions, bool isProxy) {
38+
39+
if (!mesh) { return; }
40+
auto labelMesh = dynamic_cast<const LabelSet*>(mesh);
41+
if (!labelMesh) { return; }
42+
43+
for (auto& label : labelMesh->getLabels()) {
44+
if (!label->update(mvp, screen, dz, drawAll)) {
45+
// skip dead labels
46+
continue;
47+
}
48+
49+
if (onlyTransitions) {
50+
if (label->occludedLastFrame()) { label->occlude(); }
51+
52+
if (label->visibleState() || !label->canOcclude()) {
53+
m_needUpdate |= label->evalState(dt);
54+
label->pushTransform();
55+
}
56+
} else if (label->canOcclude()) {
57+
m_labels.emplace_back(label.get(), tile, isProxy);
58+
} else {
59+
m_needUpdate |= label->evalState(dt);
60+
label->pushTransform();
61+
}
62+
}
63+
}
64+
3365
void Labels::updateLabels(const View& _view, float _dt,
3466
const std::vector<std::unique_ptr<Style>>& _styles,
3567
const std::vector<std::shared_ptr<Tile>>& _tiles,
68+
const std::vector<std::unique_ptr<Marker>>& _markers,
3669
bool _onlyTransitions) {
3770

3871
// Keep labels for debugDraw
@@ -60,30 +93,22 @@ void Labels::updateLabels(const View& _view, float _dt,
6093

6194
for (const auto& style : _styles) {
6295
const auto& mesh = tile->getMesh(*style);
63-
if (!mesh) { continue; }
96+
processLabelUpdate(mesh.get(), tile.get(), mvp, screenSize, _dt, dz,
97+
drawAllLabels, _onlyTransitions, proxyTile);
98+
}
99+
}
64100

65-
auto labelMesh = dynamic_cast<const LabelSet*>(mesh.get());
66-
if (!labelMesh) { continue; }
67-
for (auto& label : labelMesh->getLabels()) {
68-
if (!label->update(mvp, screenSize, dz, drawAllLabels)) {
69-
// skip dead labels
70-
continue;
71-
}
101+
for (const auto& marker : _markers) {
72102

73-
if (_onlyTransitions) {
74-
if (label->occludedLastFrame()) { label->occlude(); }
75-
76-
if (label->visibleState() || !label->canOcclude()) {
77-
m_needUpdate |= label->evalState(_dt);
78-
label->pushTransform();
79-
}
80-
} else if (label->canOcclude()) {
81-
m_labels.emplace_back(label.get(), tile.get(), proxyTile);
82-
} else {
83-
m_needUpdate |= label->evalState(_dt);
84-
label->pushTransform();
85-
}
86-
}
103+
glm::mat4 mvp = _view.getViewProjectionMatrix() * marker->modelMatrix();
104+
105+
for (const auto& style : _styles) {
106+
107+
if (marker->styleId() != style->getID()) { continue; }
108+
109+
const auto& mesh = marker->mesh();
110+
processLabelUpdate(mesh, nullptr, mvp, screenSize, _dt, dz,
111+
drawAllLabels, _onlyTransitions, false);
87112
}
88113
}
89114
}
@@ -182,6 +207,9 @@ bool Labels::labelComparator(const LabelEntry& _a, const LabelEntry& _b) {
182207
if (_a.priority != _b.priority) {
183208
return _a.priority < _b.priority;
184209
}
210+
if (!_a.tile || !_b.tile) {
211+
return (bool)_a.tile;
212+
}
185213
if (_a.tile->getID().z != _b.tile->getID().z) {
186214
return _a.tile->getID().z > _b.tile->getID().z;
187215
}
@@ -323,10 +351,11 @@ bool Labels::withinRepeatDistance(Label *_label) {
323351
void Labels::updateLabelSet(const View& _view, float _dt,
324352
const std::vector<std::unique_ptr<Style>>& _styles,
325353
const std::vector<std::shared_ptr<Tile>>& _tiles,
354+
const std::vector<std::unique_ptr<Marker>>& _markers,
326355
TileCache& _cache) {
327356

328357
/// Collect and update labels from visible tiles
329-
updateLabels(_view, _dt, _styles, _tiles, false);
358+
updateLabels(_view, _dt, _styles, _tiles, _markers, false);
330359

331360
sortLabels();
332361

core/src/labels/labels.h

+16-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace Tangram {
2020

2121
class FontContext;
22+
class Marker;
2223
class Tile;
2324
class View;
2425
class Style;
@@ -34,11 +35,17 @@ class Labels {
3435

3536
void drawDebug(RenderState& rs, const View& _view);
3637

37-
void updateLabelSet(const View& _view, float _dt, const std::vector<std::unique_ptr<Style>>& _styles,
38-
const std::vector<std::shared_ptr<Tile>>& _tiles, TileCache& _cache);
38+
void updateLabelSet(const View& _view, float _dt,
39+
const std::vector<std::unique_ptr<Style>>& _styles,
40+
const std::vector<std::shared_ptr<Tile>>& _tiles,
41+
const std::vector<std::unique_ptr<Marker>>& _markers,
42+
TileCache& _cache);
3943

40-
PERF_TRACE void updateLabels(const View& _view, float _dt, const std::vector<std::unique_ptr<Style>>& _styles,
41-
const std::vector<std::shared_ptr<Tile>>& _tiles, bool _onlyTransitions = true);
44+
PERF_TRACE void updateLabels(const View& _view, float _dt,
45+
const std::vector<std::unique_ptr<Style>>& _styles,
46+
const std::vector<std::shared_ptr<Tile>>& _tiles,
47+
const std::vector<std::unique_ptr<Marker>>& _markers,
48+
bool _onlyTransitions = true);
4249

4350
const std::vector<TouchItem>& getFeaturesAtPoint(const View& _view, float _dt,
4451
const std::vector<std::unique_ptr<Style>>& _styles,
@@ -66,6 +73,11 @@ class Labels {
6673

6774
PERF_TRACE bool withinRepeatDistance(Label *_label);
6875

76+
void processLabelUpdate(StyledMesh* mesh, Tile* tile,
77+
const glm::mat4& mvp, const glm::vec2& screen,
78+
float dt, float dz, bool drawAll,
79+
bool onlyTransitions, bool isProxy);
80+
6981
bool m_needUpdate;
7082

7183
isect2d::ISect2D<glm::vec2> m_isect2d;

core/src/tangram.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,21 @@ bool Map::update(float _dt) {
296296
impl->tileManager.updateTileSets(viewState, impl->view.getVisibleTiles());
297297

298298
auto& tiles = impl->tileManager.getVisibleTiles();
299+
auto& markers = impl->markerManager.markers();
299300

300301
if (impl->view.changedOnLastUpdate() ||
301302
impl->tileManager.hasTileSetChanged()) {
302303

303304
for (const auto& tile : tiles) {
304305
tile->update(_dt, impl->view);
305306
}
306-
for (const auto& marker : impl->markerManager.markers()) {
307+
for (const auto& marker : markers) {
307308
marker->update(_dt, impl->view);
308309
}
309-
impl->labels.updateLabelSet(impl->view, _dt, impl->scene->styles(), tiles,
310+
impl->labels.updateLabelSet(impl->view, _dt, impl->scene->styles(), tiles, markers,
310311
*impl->tileManager.getTileCache());
311312
} else {
312-
impl->labels.updateLabels(impl->view, _dt, impl->scene->styles(), tiles);
313+
impl->labels.updateLabels(impl->view, _dt, impl->scene->styles(), tiles, markers);
313314
}
314315
}
315316

0 commit comments

Comments
 (0)