/
blockmapvisual.cpp
606 lines (504 loc) · 18.7 KB
/
blockmapvisual.cpp
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/** @file render/blockmapvisual.cpp Graphical Blockmap Visual.
* @ingroup world
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <cmath>
#include <de/concurrency.h>
#include "de_base.h"
#include "de_console.h"
#include "de_graphics.h"
#include "de_render.h"
#include "de_play.h"
#include "de_ui.h"
#include "Face"
#include "HEdge"
#include "world/blockmap.h"
#include "world/map.h"
#include "BspLeaf"
#include "render/blockmapvisual.h"
using namespace de;
byte bmapShowDebug = 0; // 1 = mobjs, 2 = lines, 3 = BSP leafs, 4 = polyobjs. cvar
float bmapDebugSize = 1.5f; // cvar
static int drawMobj(mobj_t *mo, void * /*parameters*/)
{
if(mo->validCount != validCount)
{
vec2f_t start; V2f_Set(start, mo->origin[VX] - mo->radius, mo->origin[VY] - mo->radius);
vec2f_t end; V2f_Set(end, mo->origin[VX] + mo->radius, mo->origin[VY] + mo->radius);
glVertex2f(start[VX], start[VY]);
glVertex2f( end[VX], start[VY]);
glVertex2f( end[VX], end[VY]);
glVertex2f(start[VX], end[VY]);
mo->validCount = validCount;
}
return false; // Continue iteration.
}
static int drawLine(Line *line, void * /*parameters*/)
{
if(line->validCount() != validCount)
{
glVertex2f(line->fromOrigin().x, line->fromOrigin().y);
glVertex2f( line->toOrigin().x, line->toOrigin().y);
line->setValidCount(validCount);
}
return false; // Continue iteration.
}
static int drawBspLeaf(BspLeaf *bspLeaf, void * /*parameters*/)
{
if(bspLeaf->hasPoly() && bspLeaf->validCount() != validCount)
{
float const scale = de::max(bmapDebugSize, 1.f);
float const width = (DENG_GAMEVIEW_WIDTH / 16) / scale;
float length, dx, dy, normal[2], unit[2];
vec2f_t start, end;
Face const &face = bspLeaf->poly();
HEdge *hedge = face.hedge();
do
{
V2f_Set(start, hedge->origin().x, hedge->origin().y);
V2f_Set(end, hedge->twin().origin().x, hedge->twin().origin().y);
glBegin(GL_LINES);
glVertex2fv(start);
glVertex2fv(end);
glEnd();
dx = end[VX] - start[VX];
dy = end[VY] - start[VY];
length = sqrt(dx * dx + dy * dy);
if(length > 0)
{
unit[VX] = dx / length;
unit[VY] = dy / length;
normal[VX] = -unit[VY];
normal[VY] = unit[VX];
GL_BindTextureUnmanaged(GL_PrepareLSTexture(LST_DYNAMIC));
glEnable(GL_TEXTURE_2D);
GL_BlendOp(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBegin(GL_QUADS);
glTexCoord2f(0.75f, 0.5f);
glVertex2fv(start);
glTexCoord2f(0.75f, 0.5f);
glVertex2fv(end);
glTexCoord2f(0.75f, 1);
glVertex2f(end[VX] - normal[VX] * width,
end[VY] - normal[VY] * width);
glTexCoord2f(0.75f, 1);
glVertex2f(start[VX] - normal[VX] * width,
start[VY] - normal[VY] * width);
glEnd();
glDisable(GL_TEXTURE_2D);
GL_BlendMode(BM_NORMAL);
}
// Draw a bounding box for the leaf's face geometry.
V2f_Set(start, face.aaBox().minX, face.aaBox().minY);
V2f_Set(end, face.aaBox().maxX, face.aaBox().maxY);
glBegin(GL_LINES);
glVertex2f(start[VX], start[VY]);
glVertex2f( end[VX], start[VY]);
glVertex2f( end[VX], start[VY]);
glVertex2f( end[VX], end[VY]);
glVertex2f( end[VX], end[VY]);
glVertex2f(start[VX], end[VY]);
glVertex2f(start[VX], end[VY]);
glVertex2f(start[VX], start[VY]);
glEnd();
} while((hedge = &hedge->next()) != face.hedge());
bspLeaf->setValidCount(validCount);
}
return false; // Continue iteration.
}
static int drawCellLines(Blockmap const &bmap, BlockmapCell const &cell, void *parameters)
{
glBegin(GL_LINES);
bmap.iterate(cell, (int (*)(void*,void*)) drawLine, parameters);
glEnd();
return false; // Continue iteration.
}
static int drawCellPolyobjLines(void *object, void *parameters)
{
Polyobj *po = (Polyobj *)object;
foreach(Line *line, po->lines())
{
if(int result = drawLine(line, parameters))
return result;
}
return false; // Continue iteration.
}
static int drawCellPolyobjs(Blockmap const &bmap, BlockmapCell const &cell, void *parameters)
{
glBegin(GL_LINES);
bmap.iterate(cell, (int (*)(void*,void*)) drawCellPolyobjLines, parameters);
glEnd();
return false; // Continue iteration.
}
static int drawCellMobjs(Blockmap const &bmap, BlockmapCell const &cell, void *parameters)
{
glBegin(GL_QUADS);
bmap.iterate(cell, (int (*)(void*,void*)) drawMobj, parameters);
glEnd();
return false; // Continue iteration.
}
static int drawCellBspLeafs(Blockmap const &bmap, BlockmapCell const &cell, void *parameters)
{
bmap.iterate(cell, (int (*)(void*,void*)) drawBspLeaf, parameters);
return false; // Continue iteration.
}
static void drawBackground(Blockmap const &bmap)
{
BlockmapCell const &bmapDimensions = bmap.dimensions();
// Scale modelview matrix so we can express cell geometry
// using a cell-sized unit coordinate space.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScalef(bmap.cellWidth(), bmap.cellHeight(), 1);
/*
* Draw the translucent quad which represents the "used" cells.
*/
glColor4f(.25f, .25f, .25f, .66f);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(bmapDimensions.x, 0);
glVertex2f(bmapDimensions.x, bmapDimensions.y);
glVertex2f(0, bmapDimensions.y);
glEnd();
/*
* Draw the "null cells" over the top.
*/
glColor4f(0, 0, 0, .95f);
BlockmapCell cell;
for(cell.y = 0; cell.y < bmapDimensions.y; ++cell.y)
for(cell.x = 0; cell.x < bmapDimensions.x; ++cell.x)
{
if(bmap.cellElementCount(cell)) continue;
glBegin(GL_QUADS);
glVertex2f(cell.x, cell.y);
glVertex2f(cell.x + 1, cell.y);
glVertex2f(cell.x + 1, cell.y + 1);
glVertex2f(cell.x, cell.y + 1);
glEnd();
}
// Restore previous GL state.
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
static void drawCellInfo(Point2Raw const *_origin, char const *info)
{
DENG_ASSERT(_origin != 0);
glEnable(GL_TEXTURE_2D);
FR_SetFont(fontFixed);
FR_LoadDefaultAttrib();
FR_SetShadowOffset(UI_SHADOW_OFFSET, UI_SHADOW_OFFSET);
FR_SetShadowStrength(UI_SHADOW_STRENGTH);
Size2Raw size(16 + FR_TextWidth(info),
16 + FR_SingleLineHeight(info));
Point2Raw origin = *_origin;
origin.x -= size.width / 2;
UI_GradientEx(&origin, &size, 6, UI_Color(UIC_BG_MEDIUM), UI_Color(UIC_BG_LIGHT), .5f, .5f);
UI_DrawRectEx(&origin, &size, 6, false, UI_Color(UIC_BRD_HI), NULL, .5f, -1);
origin.x += 8;
origin.y += size.height / 2;
UI_SetColor(UI_Color(UIC_TEXT));
UI_TextOutEx2(info, &origin, UI_Color(UIC_TITLE), 1, ALIGN_LEFT, DTF_ONLY_SHADOW);
glDisable(GL_TEXTURE_2D);
}
static void drawBlockmapInfo(Point2Raw const *_origin, Blockmap const *blockmap)
{
DENG_ASSERT(blockmap != 0);
glEnable(GL_TEXTURE_2D);
Point2Raw origin = *_origin;
FR_SetFont(fontFixed);
FR_LoadDefaultAttrib();
FR_SetShadowOffset(UI_SHADOW_OFFSET, UI_SHADOW_OFFSET);
FR_SetShadowStrength(UI_SHADOW_STRENGTH);
Size2Raw size;
size.width = 16 + FR_TextWidth("(+000.0, +000.0) (+000.0, +000.0)");
int th = FR_SingleLineHeight("Info");
size.height = th * 4 + 16;
origin.x -= size.width;
origin.y -= size.height;
UI_GradientEx(&origin, &size, 6, UI_Color(UIC_BG_MEDIUM), UI_Color(UIC_BG_LIGHT), .5f, .5f);
UI_DrawRectEx(&origin, &size, 6, false, UI_Color(UIC_BRD_HI), NULL, .5f, -1);
origin.x += 8;
origin.y += 8 + th/2;
UI_TextOutEx2("Blockmap", &origin, UI_Color(UIC_TITLE), 1, ALIGN_LEFT, DTF_ONLY_SHADOW);
origin.y += th;
Vector2ui const &bmapDimensions = blockmap->dimensions();
char buf[80];
dd_snprintf(buf, 80, "Dimensions:(%u, %u) #%li", bmapDimensions.x, bmapDimensions.y,
(long) bmapDimensions.y * bmapDimensions.x);
UI_TextOutEx2(buf, &origin, UI_Color(UIC_TEXT), 1, ALIGN_LEFT, DTF_ONLY_SHADOW);
origin.y += th;
dd_snprintf(buf, 80, "Cell dimensions:(%.3f, %.3f)", blockmap->cellWidth(), blockmap->cellHeight());
UI_TextOutEx2(buf, &origin, UI_Color(UIC_TEXT), 1, ALIGN_LEFT, DTF_ONLY_SHADOW);
origin.y += th;
dd_snprintf(buf, 80, "(%+06.0f, %+06.0f) (%+06.0f, %+06.0f)",
blockmap->bounds().minX, blockmap->bounds().minY,
blockmap->bounds().maxX, blockmap->bounds().maxY);
UI_TextOutEx2(buf, &origin, UI_Color(UIC_TEXT), 1, ALIGN_LEFT, DTF_ONLY_SHADOW);
glDisable(GL_TEXTURE_2D);
}
static void drawCellInfoBox(Blockmap const *blockmap, Point2Raw const *origin,
char const *objectTypeName, BlockmapCell const &cell)
{
uint count = blockmap->cellElementCount(cell);
char info[160];
dd_snprintf(info, 160, "Cell:(%u, %u) %s:#%u", cell.x, cell.y, objectTypeName, count);
drawCellInfo(origin, info);
}
/**
* @param bmap Blockmap to be rendered.
* @param followMobj Mobj to center/focus the visual on. Can be @c NULL.
* @param cellDrawer Blockmap cell content drawing callback. Can be @a NULL.
*/
static void drawBlockmap(Blockmap const &bmap, mobj_t *followMobj,
int (*cellDrawer) (Blockmap const &bmap, BlockmapCell const &cell, void *parameters))
{
BlockmapCellBlock vCellBlock;
BlockmapCell vCell;
BlockmapCell const &dimensions = bmap.dimensions();
Vector2d const &cellDimensions = bmap.cellDimensions();
if(followMobj)
{
// Determine the followed Mobj's blockmap coords.
bool didClip;
vCell = bmap.toCell(followMobj->origin, &didClip);
if(didClip)
followMobj = 0; // Outside the blockmap.
if(followMobj)
{
// Determine the extended blockmap coords for the followed
// Mobj's "touch" range.
float const radius = followMobj->radius + DDMOBJ_RADIUS_MAX * 2;
AABoxd aaBox;
vec2d_t start; V2d_Set(start, followMobj->origin[VX] - radius, followMobj->origin[VY] - radius);
vec2d_t end; V2d_Set(end, followMobj->origin[VX] + radius, followMobj->origin[VY] + radius);
V2d_InitBox(aaBox.arvec2, start);
V2d_AddToBox(aaBox.arvec2, end);
vCellBlock = bmap.toCellBlock(aaBox);
}
}
if(followMobj)
{
// Orient on the center of the followed Mobj.
glTranslated(-(vCell.x * cellDimensions.x), -(vCell.y * cellDimensions.y), 0);
}
else
{
// Orient on center of the Blockmap.
glTranslated(-(cellDimensions.x * dimensions.x)/2,
-(cellDimensions.y * dimensions.y)/2, 0);
}
// First we'll draw a background showing the "null" cells.
drawBackground(bmap);
if(followMobj)
{
// Highlight cells the followed Mobj "touches".
glBegin(GL_QUADS);
BlockmapCell cell;
for(cell.y = vCellBlock.min.y; cell.y <= vCellBlock.max.y; ++cell.y)
for(cell.x = vCellBlock.min.x; cell.x <= vCellBlock.max.x; ++cell.x)
{
if(cell.x == vCell.x && cell.y == vCell.y)
{
// The cell the followed Mobj is actually in.
glColor4f(.66f, .66f, 1, .66f);
}
else
{
// A cell within the followed Mobj's extended collision range.
glColor4f(.33f, .33f, .66f, .33f);
}
vec2d_t start; V2d_Set(start, cell.x * cellDimensions.x, cell.y * cellDimensions.y);
vec2d_t end; V2d_Set(end, cellDimensions.x, cellDimensions.y);
V2d_Sum(end, end, start);
glVertex2d(start[VX], start[VY]);
glVertex2d( end[VX], start[VY]);
glVertex2d( end[VX], end[VY]);
glVertex2d(start[VX], end[VY]);
}
glEnd();
}
/**
* Draw the Gridmap visual.
* @note Gridmap uses a cell unit size of [width:1,height:1], so we need to
* scale it up so it aligns correctly.
*/
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScaled(cellDimensions.x, cellDimensions.y, 1);
bmap.gridmap().drawDebugVisual();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
/*
* Draw the blockmap-linked data.
* Translate the modelview matrix so that objects can be drawn using
* the map space coordinates directly.
*/
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslated(-bmap.origin().x, -bmap.origin().y, 0);
if(cellDrawer)
{
if(followMobj)
{
/*
* Draw cell contents color coded according to their range from the
* followed Mobj.
*/
// First, the cells outside the "touch" range (crimson).
validCount++;
glColor4f(.33f, 0, 0, .75f);
BlockmapCell cell;
for(cell.y = 0; cell.y < dimensions.y; ++cell.y)
for(cell.x = 0; cell.x < dimensions.x; ++cell.x)
{
if(cell.x >= vCellBlock.min.x && cell.x <= vCellBlock.max.x &&
cell.y >= vCellBlock.min.y && cell.y <= vCellBlock.max.y) continue;
if(!bmap.cellElementCount(cell)) continue;
cellDrawer(bmap, cell, 0/*no params*/);
}
// Next, the cells within the "touch" range (orange).
validCount++;
glColor3f(1, .5f, 0);
for(cell.y = vCellBlock.min.y; cell.y <= vCellBlock.max.y; ++cell.y)
for(cell.x = vCellBlock.min.x; cell.x <= vCellBlock.max.x; ++cell.x)
{
if(cell.x == vCell.x && cell.y == vCell.y) continue;
if(!bmap.cellElementCount(cell)) continue;
cellDrawer(bmap, cell, 0/*no params*/);
}
// Lastly, the cell the followed Mobj is in (yellow).
validCount++;
glColor3f(1, 1, 0);
if(bmap.cellElementCount(vCell))
{
cellDrawer(bmap, vCell, 0/*no params*/);
}
}
else
{
// Draw all cells without color coding.
validCount++;
glColor4f(.33f, 0, 0, .75f);
BlockmapCell cell;
for(cell.y = 0; cell.y < dimensions.y; ++cell.y)
for(cell.x = 0; cell.x < dimensions.x; ++cell.x)
{
if(!bmap.cellElementCount(cell)) continue;
cellDrawer(bmap, cell, 0/*no params*/);
}
}
}
/*
* Draw the followed mobj, if any.
*/
if(followMobj)
{
validCount++;
glColor3f(0, 1, 0);
glBegin(GL_QUADS);
drawMobj(followMobj, NULL/*no params*/);
glEnd();
}
// Undo the map coordinate space translation.
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void Rend_BlockmapDebug()
{
int (*cellDrawer) (Blockmap const &blockmap, BlockmapCell const &cell, void *parameters);
char const *objectTypeName;
mobj_t *followMobj = 0;
Blockmap const *blockmap;
Point2Raw origin;
float scale;
// Disabled?
if(!bmapShowDebug || bmapShowDebug > 4) return;
if(!App_World().hasMap()) return;
Map &map = App_World().map();
switch(bmapShowDebug)
{
default: // Mobjs.
blockmap = &map.mobjBlockmap();
cellDrawer = drawCellMobjs;
objectTypeName = "Mobjs";
break;
case 2: // Lines.
blockmap = &map.lineBlockmap();
cellDrawer = drawCellLines;
objectTypeName = "Lines";
break;
case 3: // BSP leafs.
blockmap = &map.bspLeafBlockmap();
cellDrawer = drawCellBspLeafs;
objectTypeName = "BSP Leafs";
break;
case 4: // Polyobjs.
blockmap = &map.polyobjBlockmap();
cellDrawer = drawCellPolyobjs;
objectTypeName = "Polyobjs";
break;
}
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
/*
* Draw the blockmap.
*/
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, DENG_GAMEVIEW_WIDTH, DENG_GAMEVIEW_HEIGHT, 0, -1, 1);
// Orient on the center of the window.
glTranslatef((DENG_GAMEVIEW_WIDTH / 2), (DENG_GAMEVIEW_HEIGHT / 2), 0);
// Uniform scaling factor for this visual.
scale = bmapDebugSize / de::max(DENG_GAMEVIEW_HEIGHT / 100, 1);
glScalef(scale, -scale, 1);
// If possible we'll tailor what we draw relative to the viewPlayer.
if(viewPlayer && viewPlayer->shared.mo)
followMobj = viewPlayer->shared.mo;
// Draw!
drawBlockmap(*blockmap, followMobj, cellDrawer);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
/*
* Draw HUD info.
*/
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, DENG_GAMEVIEW_WIDTH, DENG_GAMEVIEW_HEIGHT, 0, -1, 1);
if(followMobj)
{
// About the cell the followed Mobj is in.
bool didClip;
BlockmapCell cell = blockmap->toCell(followMobj->origin, &didClip);
if(!didClip)
{
origin.x = DENG_GAMEVIEW_WIDTH / 2;
origin.y = 30;
drawCellInfoBox(blockmap, &origin, objectTypeName, cell);
}
}
// About the Blockmap itself.
origin.x = DENG_GAMEVIEW_WIDTH - 10;
origin.y = DENG_GAMEVIEW_HEIGHT - 10;
drawBlockmapInfo(&origin, blockmap);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}