forked from MUME/MMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacters.cpp
More file actions
549 lines (470 loc) · 19 KB
/
Copy pathCharacters.cpp
File metadata and controls
549 lines (470 loc) · 19 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2019 The MMapper Authors
#include "Characters.h"
#include "../configuration/configuration.h"
#include "../group/CGroupChar.h"
#include "../group/mmapper2group.h"
#include "../map/roomid.h"
#include "../mapdata/mapdata.h"
#include "../mapdata/roomselection.h"
#include "../opengl/LineRendering.h"
#include "../opengl/OpenGL.h"
#include "../opengl/OpenGLTypes.h"
#include "MapCanvasData.h"
#include "Textures.h"
#include "mapcanvas.h"
#include "prespammedpath.h"
#include <array>
#include <cmath>
#include <optional>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtx/norm.hpp>
#include <QtCore>
static constexpr float CHAR_ARROW_LINE_WIDTH = 2.f;
static constexpr float PATH_LINE_WIDTH = 0.1f;
static constexpr float PATH_POINT_SIZE = 8.f;
DistantObjectTransform DistantObjectTransform::construct(const glm::vec3 &pos,
const MapScreen &mapScreen,
const float marginPixels)
{
assert(marginPixels > 0.f);
const glm::vec3 viewCenter = mapScreen.getCenter();
const auto delta = pos - viewCenter;
const float radians = std::atan2(delta.y, delta.x);
const auto hint = mapScreen.getProxyLocation(pos, marginPixels);
const float degrees = glm::degrees(radians);
return DistantObjectTransform{hint, degrees};
}
bool CharacterBatch::isVisible(const Coordinate &c, float margin) const
{
return m_mapScreen.isRoomVisible(c, margin);
}
void CharacterBatch::drawCharacter(const Coordinate &c, const Color color, bool fill)
{
const Configuration::CanvasSettings &settings = getConfig().canvas;
const glm::vec3 roomCenter = c.to_vec3() + glm::vec3{0.5f, 0.5f, 0.f};
const int layerDifference = c.z - m_currentLayer;
auto &gl = getOpenGL();
gl.setColor(color);
// REVISIT: The margin probably needs to be modified for high-dpi.
const float marginPixels = MapScreen::DEFAULT_MARGIN_PIXELS;
const bool visible = isVisible(c, marginPixels / 2.f);
const bool isFar = m_scale <= settings.charBeaconScaleCutoff;
const bool wantBeacons = settings.drawCharBeacons && isFar;
if (!visible) {
static const bool useScreenSpacePlayerArrow = std::invoke([]() -> bool {
auto opt = utils::getEnvBool("MMAPPER_SCREEN_SPACE_ARROW");
return opt ? opt.value() : true;
});
const auto dot = DistantObjectTransform::construct(roomCenter, m_mapScreen, marginPixels);
// Player is distant
if (useScreenSpacePlayerArrow) {
gl.addScreenSpaceArrow(dot.offset, dot.rotationDegrees, color, fill);
} else {
gl.glPushMatrix();
gl.glTranslatef(dot.offset);
// NOTE: 180 degrees of additional rotation flips the arrow to point right instead of left.
gl.glRotateZ(dot.rotationDegrees + 180.f);
// NOTE: arrow is centered, so it doesn't need additional translation.
gl.drawArrow(fill, wantBeacons);
gl.glPopMatrix();
}
}
const bool differentLayer = layerDifference != 0;
if (differentLayer) {
const glm::vec3 centerOnCurrentLayer{static_cast<glm::vec2>(roomCenter),
static_cast<float>(m_currentLayer)};
// Draw any arrow on the current layer pointing in either up or down
// (this may not make sense graphically in an angled 3D view).
gl.glPushMatrix();
gl.glTranslatef(centerOnCurrentLayer);
// Arrow points up or down.
// REVISIT: billboard this in 3D?
gl.glRotateZ((layerDifference > 0) ? 90.f : 270.f);
gl.drawArrow(fill, false);
gl.glPopMatrix();
}
const bool beacon = visible && !differentLayer && wantBeacons;
gl.drawBox(c, fill, beacon, isFar);
}
void CharacterBatch::CharFakeGL::drawPathSegment(const glm::vec3 &p1,
const glm::vec3 &p2,
const Color color)
{
m_pathLines.emplace_back(color, p1);
m_pathLines.emplace_back(color, p2);
}
void CharacterBatch::drawPreSpammedPath(const Coordinate &c1,
const std::vector<Coordinate> &path,
const Color color)
{
if (path.empty()) {
return;
}
using TranslatedVerts = std::vector<glm::vec3>;
const auto verts = std::invoke([&c1, &path]() -> TranslatedVerts {
TranslatedVerts translated;
translated.reserve(path.size() + 1);
const auto add = [&translated](const Coordinate &c) -> void {
static const glm::vec3 PATH_OFFSET{0.5f, 0.5f, 0.f};
translated.push_back(c.to_vec3() + PATH_OFFSET);
};
add(c1);
for (const Coordinate &c2 : path) {
add(c2);
}
return translated;
});
auto &gl = getOpenGL();
// Generate vertices for the thick line
for (size_t i = 0; i < verts.size() - 1; ++i) {
const glm::vec3 p1 = verts[i];
const glm::vec3 p2 = verts[i + 1];
gl.drawPathSegment(p1, p2, color);
}
gl.drawPathPoint(color, verts.back());
}
void CharacterBatch::CharFakeGL::drawQuadCommon(const glm::vec2 &in_a,
const glm::vec2 &in_b,
const glm::vec2 &in_c,
const glm::vec2 &in_d,
const QuadOptsEnum options)
{
const auto &m = m_stack.top().modelView;
const auto transform = [&m](const glm::vec2 &vin) -> glm::vec3 {
const auto vtmp = m * glm::vec4(vin, 0.f, 1.f);
return glm::vec3{vtmp / vtmp.w};
};
const auto a = transform(in_a);
const auto b = transform(in_b);
const auto c = transform(in_c);
const auto d = transform(in_d);
if (::utils::isSet(options, QuadOptsEnum::FILL)) {
const auto color = m_color.withAlpha(FILL_ALPHA);
auto emitVert = [this, &color](const auto &x) -> void { m_charTris.emplace_back(color, x); };
auto emitTri = [&emitVert](const auto &v0, const auto &v1, const auto &v2) -> void {
emitVert(v0);
emitVert(v1);
emitVert(v2);
};
emitTri(a, b, c);
emitTri(a, c, d);
}
if (::utils::isSet(options, QuadOptsEnum::BEACON)) {
const auto color = m_color.withAlpha(BEACON_ALPHA);
const glm::vec3 heightOffset{0.f, 0.f, 50.f};
// H-----G
// |\ /|
// | D-C |
// | | | |
// | A-B |
// |/ \|
// E-----F
const auto e = a + heightOffset;
const auto f = b + heightOffset;
const auto g = c + heightOffset;
const auto h = d + heightOffset;
auto emitVert = [this, &color](const auto &x) -> void {
m_charBeaconQuads.emplace_back(color, x);
};
auto emitQuad =
[&emitVert](const auto &v0, const auto &v1, const auto &v2, const auto &v3) -> void {
emitVert(v0);
emitVert(v1);
emitVert(v2);
emitVert(v3);
};
// draw the *inner* faces
emitQuad(a, e, f, b);
emitQuad(b, f, g, c);
emitQuad(c, g, h, d);
emitQuad(d, h, e, a);
}
if (::utils::isSet(options, QuadOptsEnum::OUTLINE)) {
const auto color = m_color.withAlpha(LINE_ALPHA);
auto emitVert = [this, &color](const auto &x) -> void {
m_charLines.emplace_back(color, x);
};
auto emitLine = [&emitVert](const auto &v0, const auto &v1) -> void {
emitVert(v0);
emitVert(v1);
};
emitLine(a, b);
emitLine(b, c);
emitLine(c, d);
emitLine(d, a);
}
}
void CharacterBatch::CharFakeGL::drawBox(const Coordinate &coord,
bool fill,
bool beacon,
const bool isFar)
{
const bool dontFillRotatedQuads = true;
const bool shrinkRotatedQuads = false; // REVISIT: make this a user option?
// caution: multiple side effects in next statement.
const int numAlreadyInRoom = m_coordCounts[coord]++;
glPushMatrix();
glTranslatef(coord.to_vec3());
if (numAlreadyInRoom != 0) {
// NOTE: use of 45/PI here is NOT a botched conversion to radians;
// it's a value close to 15 degrees (~14.324) that is guaranteed
// to never perfectly overlap a regular axis-aligned square
// when multiplied by an integer.
static constexpr const float MAGIC_ANGLE = 45.f / float(M_PI);
const float degrees = static_cast<float>(numAlreadyInRoom) * MAGIC_ANGLE;
const glm::vec3 quadCenter{0.5f, 0.5f, 0.f};
glTranslatef(quadCenter);
if ((shrinkRotatedQuads)) {
// keeps the rotated squares bounded inside the outer square.
glScalef(0.7f, 0.7f, 1.f);
}
glRotateZ(degrees);
glTranslatef(-quadCenter);
if (dontFillRotatedQuads) {
fill = false; // avoid highlighting the room multiple times
}
beacon = false;
}
// d-c
// |/|
// a-b
const glm::vec2 a{0, 0};
const glm::vec2 b{1, 0};
const glm::vec2 c{1, 1};
const glm::vec2 d{0, 1};
if (isFar) {
const auto options = QuadOptsEnum::OUTLINE
| (fill ? QuadOptsEnum::FILL : QuadOptsEnum::NONE)
| (beacon ? QuadOptsEnum::BEACON : QuadOptsEnum::NONE);
drawQuadCommon(a, b, c, d, options);
} else {
/* ignoring fill for now; that'll require a different icon */
const auto &color = m_color;
const auto &m = m_stack.top().modelView;
const auto addTransformed = [this, &color, &m](const glm::vec2 &in_vert) -> void {
const auto tmp = m * glm::vec4(in_vert, 0.f, 1.f);
m_charRoomQuads.emplace_back(color, glm::vec3{in_vert, 0}, glm::vec3{tmp / tmp.w});
};
addTransformed(a);
addTransformed(b);
addTransformed(c);
addTransformed(d);
if (beacon) {
drawQuadCommon(a, b, c, d, QuadOptsEnum::BEACON);
}
}
glPopMatrix();
}
void CharacterBatch::CharFakeGL::drawArrow(const bool fill, const bool beacon)
{
// Generic topology:
// d
// /|
// a-c
// \|
// b
const glm::vec2 a{-0.5f, 0.f};
const glm::vec2 b{0.75f, -0.5f};
const glm::vec2 c{0.25f, 0.f};
const glm::vec2 d{0.75f, 0.5f};
const auto options = QuadOptsEnum::OUTLINE | (fill ? QuadOptsEnum::FILL : QuadOptsEnum::NONE)
| (beacon ? QuadOptsEnum::BEACON : QuadOptsEnum::NONE);
drawQuadCommon(a, b, c, d, options);
}
void CharacterBatch::CharFakeGL::reallyDrawCharacters(OpenGL &gl, const MapCanvasTextures &textures)
{
const auto blended_noDepth
= GLRenderState().withDepthFunction(std::nullopt).withBlend(BlendModeEnum::TRANSPARENCY);
// Cull the front faces, because the quads point towards the center of the room,
// and we don't want to draw over the entire terrain if we're inside the room.
if (!m_charBeaconQuads.empty()) {
gl.renderColoredQuads(m_charBeaconQuads, blended_noDepth.withCulling(CullingEnum::FRONT));
}
if (!m_charRoomQuads.empty()) {
gl.renderColoredTexturedQuads(m_charRoomQuads,
blended_noDepth.withTexture0(
textures.char_room_sel->getArrayPosition().array));
}
if (!m_charTris.empty()) {
gl.renderColoredTris(m_charTris, blended_noDepth);
}
if (!m_charLines.empty()) {
gl.renderColoredLines(m_charLines,
blended_noDepth.withLineParams(LineParams{CHAR_ARROW_LINE_WIDTH}));
}
if (!m_screenSpaceArrows.empty()) {
// FIXME: add an option to auto-scale to DPR.
const float dpr = gl.getDevicePixelRatio();
for (auto &v : m_screenSpaceArrows) {
v.vert *= dpr;
}
gl.renderFont3d(textures.char_arrows, m_screenSpaceArrows);
m_screenSpaceArrows.clear();
}
}
void CharacterBatch::CharFakeGL::reallyDrawPaths(OpenGL &gl)
{
const auto blended_noDepth
= GLRenderState().withDepthFunction(std::nullopt).withBlend(BlendModeEnum::TRANSPARENCY);
gl.renderPoints(m_pathPoints, blended_noDepth.withPointSize(PATH_POINT_SIZE));
if (!m_pathLines.empty()) {
gl.renderColoredLines(m_pathLines,
blended_noDepth.withLineParams(LineParams{PATH_LINE_WIDTH}));
}
}
void CharacterBatch::CharFakeGL::addScreenSpaceArrow(const glm::vec3 &pos,
const float degrees,
const Color color,
const bool fill)
{
std::array<glm::vec2, 4> texCoords{
glm::vec2{0, 0},
glm::vec2{1, 0},
glm::vec2{1, 1},
glm::vec2{0, 1},
};
const float scale = MapScreen::DEFAULT_MARGIN_PIXELS;
const float radians = glm::radians(degrees);
const glm::vec3 z{0, 0, 1};
const glm::mat4 rotation = glm::rotate(glm::mat4(1), radians, z);
for (size_t i = 0; i < 4; ++i) {
const glm::vec2 &tc = texCoords[i];
const auto tmp = rotation * glm::vec4(tc * 2.f - 1.f, 0, 1);
const glm::vec2 screenSpaceOffset = scale * glm::vec2(tmp) / tmp.w;
// solid |filled
// --------+--------
// outline | n/a
const glm::vec2 tcOffset = tc * 0.5f
+ (fill ? glm::vec2(0.5f, 0.5f) : glm::vec2(0.f, 0.0f));
m_screenSpaceArrows.emplace_back(pos, color, tcOffset, screenSpaceOffset);
}
}
void CharacterBatch::CharFakeGL::addName(const Coordinate &c,
const std::string &name,
const Color color,
const MapScreen &mapScreen)
{
const glm::vec3 roomCenter = c.to_vec3() + glm::vec3{0.5f, 0.5f, 0.f};
const float marginPixels = MapScreen::DEFAULT_MARGIN_PIXELS;
const bool visible = mapScreen.isRoomVisible(c, marginPixels / 2.f);
const auto &viewport = mapScreen.getViewport();
std::optional<glm::vec3> optScreen;
float verticalOffset;
if (visible) {
optScreen = viewport.project(roomCenter);
const auto optScreenTop = viewport.project(roomCenter + glm::vec3{0.f, 0.5f, 0.f});
if (optScreen && optScreenTop) {
verticalOffset = glm::distance(glm::vec2(*optScreen), glm::vec2(*optScreenTop)) + 2.f;
} else {
verticalOffset = 10.f;
}
} else {
const glm::vec3 proxyWorld = mapScreen.getProxyLocation(roomCenter, marginPixels);
optScreen = viewport.project(proxyWorld);
verticalOffset = 15.f;
}
if (!optScreen) {
return;
}
const float screenX = optScreen->x;
const float screenY = static_cast<float>(viewport.height()) - optScreen->y;
const float stackIdx = static_cast<float>(m_coordCounts[c] - 1);
// We store stackIdx in pos.z for now, and logical coordinates in pos.xy
m_names.emplace_back(glm::vec3{screenX, screenY - verticalOffset, stackIdx},
name,
textColor(color),
color.withAlpha(0.6f),
FontFormatFlags{FontFormatFlagEnum::HALIGN_CENTER});
}
void CharacterBatch::CharFakeGL::reallyDrawNames(OpenGL &gl, GLFont &font)
{
if (m_names.empty()) {
return;
}
const float dpr = gl.getDevicePixelRatio();
const float fontPhysicalHeight = static_cast<float>(font.getFontHeight());
for (auto &t : m_names) {
const float stackIdx = t.pos.z;
t.pos.x *= dpr;
t.pos.y = t.pos.y * dpr - stackIdx * fontPhysicalHeight;
t.pos.z = 0.f;
}
font.render2dTextImmediate(m_names);
m_names.clear();
}
void MapCanvas::paintCharacters()
{
if (m_data.isEmpty()) {
return;
}
CharacterBatch characterBatch{m_mapScreen, getCurrentLayer(), getTotalScaleFactor()};
// IIFE to abuse return to avoid duplicate else branches
std::invoke([this, &characterBatch]() -> void {
if (const std::optional<RoomId> opt_pos = m_data.getCurrentRoomId()) {
const auto &id = opt_pos.value();
if (const auto room = m_data.findRoomHandle(id)) {
const auto &pos = room.getPosition();
// draw the characters before the current position
characterBatch.incrementCount(pos);
drawGroupCharacters(characterBatch, room.getServerId());
characterBatch.resetCount(pos);
// paint char current position
const Color color{getConfig().groupManager.color};
characterBatch.drawCharacter(pos, color);
// paint prespam
const auto prespam = m_data.getPath(id, m_prespammedPath.getQueue());
characterBatch.drawPreSpammedPath(pos, prespam, color);
return;
} else {
// this can happen if the "current room" is deleted
// and we failed to clear it elsewhere.
m_data.clearSelectedRoom();
}
}
drawGroupCharacters(characterBatch, INVALID_SERVER_ROOMID);
});
characterBatch.reallyDraw(getOpenGL(), m_textures, getGLFont());
}
void MapCanvas::drawGroupCharacters(CharacterBatch &batch, ServerRoomId yourServerId)
{
if (m_data.isEmpty()) {
return;
}
RoomIdSet drawnRoomIds;
const Map &map = m_data.getCurrentMap();
for (const auto &pCharacter : m_groupManager.selectAll()) {
// Omit player so that they know group members are below them
if (pCharacter->isYou()) {
continue;
}
const CGroupChar &character = deref(pCharacter);
const ServerRoomId srvId = character.getServerId();
const auto &r = std::invoke([srvId, &map]() -> RoomHandle {
if (srvId != INVALID_SERVER_ROOMID) {
if (const auto &room = map.findRoomHandle(srvId)) {
return room;
}
}
return RoomHandle{};
});
// Do not draw the character if they're in an "Unknown" room
if (!r) {
continue;
}
const RoomId id = r.getId();
const auto &pos = r.getPosition();
const auto color = Color{character.getColor()};
const bool fill = !drawnRoomIds.contains(id);
batch.drawCharacter(pos, color, fill);
if (srvId != INVALID_SERVER_ROOMID && srvId != yourServerId) {
QString name = character.getLabel().isEmpty() ? character.getName().toQString()
: character.getLabel().toQString();
if (!name.isEmpty()) {
batch.drawName(pos, mmqt::toStdStringLatin1(name), color);
}
}
drawnRoomIds.insert(id);
}
}