/
bsp_map.c
531 lines (427 loc) · 15.8 KB
/
bsp_map.c
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
/**\file bsp_map.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*
* 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
*/
// HEADER FILES ------------------------------------------------------------
#include <stdlib.h>
#include <math.h>
#include "de_base.h"
#include "de_console.h"
#include "de_bsp.h"
#include "de_misc.h"
#include "de_play.h"
#include "de_edit.h"
#include "de_refresh.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// CODE --------------------------------------------------------------------
static void hardenSidedefHEdgeList(GameMap* map, sidedef_t* side, HEdge* hedge,
bsp_hedge_t* bspHEdge)
{
uint count;
bsp_hedge_t* first, *other;
if(!side)
return;
// Have we already processed this side?
if(side->hedges)
return;
// Find the first hedge.
first = bspHEdge;
while(first->prevOnSide)
first = first->prevOnSide;
// Count the hedges for this side.
count = 0;
other = first;
while(other)
{
other = other->nextOnSide;
count++;
}
// Allocate the final side hedge table.
side->hedgeCount = count;
side->hedges =
Z_Malloc(sizeof(HEdge*) * (side->hedgeCount + 1), PU_MAPSTATIC, 0);
count = 0;
other = first;
while(other)
{
side->hedges[count++] = &map->hedges[other->index];
other = other->nextOnSide;
}
side->hedges[count] = NULL; // Terminate.
}
static int C_DECL hEdgeCompare(const void* p1, const void* p2)
{
const bsp_hedge_t* a = ((const bsp_hedge_t**) p1)[0];
const bsp_hedge_t* b = ((const bsp_hedge_t**) p2)[0];
if(a->index == b->index)
return 0;
if(a->index < b->index)
return -1;
return 1;
}
typedef struct {
size_t curIdx;
bsp_hedge_t*** indexPtr;
boolean write;
} hedgecollectorparams_t;
static boolean hEdgeCollector(binarytree_t* tree, void* data)
{
if(BinaryTree_IsLeaf(tree))
{
hedgecollectorparams_t* params = (hedgecollectorparams_t*) data;
bspleafdata_t* leaf = (bspleafdata_t*) BinaryTree_GetData(tree);
bsp_hedge_t* hEdge;
for(hEdge = leaf->hEdges; hEdge; hEdge = hEdge->next)
{
if(params->indexPtr)
{ // Write mode.
(*params->indexPtr)[params->curIdx++] = hEdge;
}
else
{ // Count mode.
if(hEdge->index == -1)
Con_Error("HEdge %p never reached a subsector!", hEdge);
params->curIdx++;
}
}
}
return true; // Continue traversal.
}
static void buildHEdgesFromBSPHEdges(GameMap* dest, binarytree_t* rootNode)
{
uint i;
bsp_hedge_t** index;
hedgecollectorparams_t params;
//
// First we need to build a sorted index of the used hedges.
//
// Pass 1: Count the number of used hedges.
params.curIdx = 0;
params.indexPtr = NULL;
BinaryTree_InOrder(rootNode, hEdgeCollector, ¶ms);
if(!(params.curIdx > 0))
Con_Error("buildHEdgesFromBSPHEdges: No hedges?");
// Allocate the sort buffer.
index = M_Malloc(sizeof(bsp_hedge_t*) * params.curIdx);
// Pass 2: Collect ptrs the hedges and insert into the index.
params.curIdx = 0;
params.indexPtr = &index;
BinaryTree_InOrder(rootNode, hEdgeCollector, ¶ms);
// Sort the half-edges into ascending index order.
qsort(index, params.curIdx, sizeof(bsp_hedge_t*), hEdgeCompare);
dest->numHEdges = (uint) params.curIdx;
dest->hedges = Z_Calloc(dest->numHEdges * sizeof(HEdge), PU_MAPSTATIC, 0);
for(i = 0; i < dest->numHEdges; ++i)
{
HEdge* hedge = &dest->hedges[i];
bsp_hedge_t* bspHEdge = index[i];
hedge->header.type = DMU_HEDGE;
hedge->HE_v1 = &dest->vertexes[bspHEdge->v[0]->buildData.index - 1];
hedge->HE_v2 = &dest->vertexes[bspHEdge->v[1]->buildData.index - 1];
hedge->side = bspHEdge->side;
if(bspHEdge->lineDef)
hedge->lineDef = &dest->lineDefs[bspHEdge->lineDef->buildData.index - 1];
if(bspHEdge->twin)
hedge->twin = &dest->hedges[bspHEdge->twin->index];
hedge->flags = 0;
if(hedge->lineDef)
{
linedef_t* ldef = hedge->lineDef;
vertex_t* vtx = hedge->lineDef->L_v(hedge->side);
if(ldef->L_side(hedge->side))
hedge->HE_frontsector = ldef->L_side(hedge->side)->sector;
if(ldef->L_frontside && ldef->L_backside)
{
hedge->HE_backsector = ldef->L_side(hedge->side ^ 1)->sector;
}
else
{
hedge->HE_backsector = 0;
}
hedge->offset = P_AccurateDistance(hedge->HE_v1pos[VX] - vtx->V_pos[VX],
hedge->HE_v1pos[VY] - vtx->V_pos[VY]);
}
else
{
hedge->lineDef = NULL;
hedge->HE_frontsector = NULL;
hedge->HE_backsector = NULL;
}
if(hedge->lineDef)
hardenSidedefHEdgeList(dest, HEDGE_SIDEDEF(hedge), hedge, bspHEdge);
hedge->angle =
bamsAtan2((int) (hedge->HE_v2pos[VY] - hedge->HE_v1pos[VY]),
(int) (hedge->HE_v2pos[VX] - hedge->HE_v1pos[VX])) << FRACBITS;
// Calculate the length of the segment. We need this for
// the texture coordinates. -jk
hedge->length = P_AccurateDistance(hedge->HE_v2pos[VX] - hedge->HE_v1pos[VX],
hedge->HE_v2pos[VY] - hedge->HE_v1pos[VY]);
if(hedge->length == 0)
hedge->length = 0.01f; // Hmm...
// Calculate the tangent space surface vectors.
// Front first
if(hedge->lineDef && HEDGE_SIDEDEF(hedge))
{
sidedef_t* side = HEDGE_SIDEDEF(hedge);
surface_t* surface = &side->SW_topsurface;
surface->normal[VY] = (hedge->HE_v1pos[VX] - hedge->HE_v2pos[VX]) / hedge->length;
surface->normal[VX] = (hedge->HE_v2pos[VY] - hedge->HE_v1pos[VY]) / hedge->length;
surface->normal[VZ] = 0;
V3_BuildTangents(surface->tangent, surface->bitangent, surface->normal);
// All surfaces of a sidedef have the same tangent space vectors.
memcpy(side->SW_middletangent, surface->tangent, sizeof(surface->tangent));
memcpy(side->SW_middlebitangent, surface->bitangent, sizeof(surface->bitangent));
memcpy(side->SW_middlenormal, surface->normal, sizeof(surface->normal));
memcpy(side->SW_bottomtangent, surface->tangent, sizeof(surface->tangent));
memcpy(side->SW_bottombitangent, surface->bitangent, sizeof(surface->bitangent));
memcpy(side->SW_bottomnormal, surface->normal, sizeof(surface->normal));
}
}
// Free temporary storage
M_Free(index);
}
static void hardenSubsectorHEdgeList(GameMap* dest, subsector_t* ssec,
bsp_hedge_t* list, size_t hedgeCount)
{
size_t i;
bsp_hedge_t* cur;
HEdge** hedges;
hedges = Z_Malloc(sizeof(HEdge*) * (hedgeCount + 1), PU_MAPSTATIC, 0);
for(cur = list, i = 0; cur; cur = cur->next, ++i)
hedges[i] = &dest->hedges[cur->index];
hedges[hedgeCount] = NULL; // Terminate.
if(i != hedgeCount)
Con_Error("hardenSubsectorHEdgeList: Miscounted?");
ssec->hedges = hedges;
}
static void hardenLeaf(GameMap* map, subsector_t* dest,
const bspleafdata_t* src)
{
HEdge** segp;
boolean found;
size_t hEdgeCount;
bsp_hedge_t* hEdge;
hEdge = src->hEdges;
hEdgeCount = 0;
do
{
hEdgeCount++;
} while((hEdge = hEdge->next) != NULL);
dest->header.type = DMU_SUBSECTOR;
dest->hedgeCount = (uint) hEdgeCount;
dest->shadows = NULL;
dest->vertices = NULL;
hardenSubsectorHEdgeList(map, dest, src->hEdges, hEdgeCount);
// Determine which sector this subsector belongs to.
segp = dest->hedges;
found = false;
while(*segp)
{
HEdge* hedge = *segp;
if(!found && hedge->lineDef && HEDGE_SIDEDEF(hedge))
{
sidedef_t* side = HEDGE_SIDEDEF(hedge);
dest->sector = side->sector;
found = true;
}
hedge->subsector = dest;
segp++;
}
if(!dest->sector)
{
Con_Message("hardenLeaf: Warning orphan subsector %p.\n", dest);
}
}
typedef struct {
GameMap* dest;
uint subsectorCurIndex;
uint nodeCurIndex;
} hardenbspparams_t;
static boolean C_DECL hardenNode(binarytree_t* tree, void* data)
{
binarytree_t* right, *left;
bspnodedata_t* nodeData;
hardenbspparams_t* params;
node_t* node;
if(BinaryTree_IsLeaf(tree))
return true; // Continue iteration.
nodeData = BinaryTree_GetData(tree);
params = (hardenbspparams_t*) data;
node = ¶ms->dest->nodes[nodeData->index = params->nodeCurIndex++];
node->header.type = DMU_NODE;
node->partition.x = nodeData->partition.x;
node->partition.y = nodeData->partition.y;
node->partition.dX = nodeData->partition.dX;
node->partition.dY = nodeData->partition.dY;
node->bBox[RIGHT][BOXTOP] = nodeData->bBox[RIGHT][BOXTOP];
node->bBox[RIGHT][BOXBOTTOM] = nodeData->bBox[RIGHT][BOXBOTTOM];
node->bBox[RIGHT][BOXLEFT] = nodeData->bBox[RIGHT][BOXLEFT];
node->bBox[RIGHT][BOXRIGHT] = nodeData->bBox[RIGHT][BOXRIGHT];
node->bBox[LEFT][BOXTOP] = nodeData->bBox[LEFT][BOXTOP];
node->bBox[LEFT][BOXBOTTOM] = nodeData->bBox[LEFT][BOXBOTTOM];
node->bBox[LEFT][BOXLEFT] = nodeData->bBox[LEFT][BOXLEFT];
node->bBox[LEFT][BOXRIGHT] = nodeData->bBox[LEFT][BOXRIGHT];
right = BinaryTree_GetChild(tree, RIGHT);
if(right)
{
if(BinaryTree_IsLeaf(right))
{
bspleafdata_t* leaf = (bspleafdata_t*) BinaryTree_GetData(right);
uint idx = params->subsectorCurIndex++;
node->children[RIGHT] = idx | NF_SUBSECTOR;
hardenLeaf(params->dest, ¶ms->dest->subsectors[idx], leaf);
}
else
{
bspnodedata_t* data = (bspnodedata_t*) BinaryTree_GetData(right);
node->children[RIGHT] = data->index;
}
}
left = BinaryTree_GetChild(tree, LEFT);
if(left)
{
if(BinaryTree_IsLeaf(left))
{
bspleafdata_t* leaf = (bspleafdata_t*) BinaryTree_GetData(left);
uint idx = params->subsectorCurIndex++;
node->children[LEFT] = idx | NF_SUBSECTOR;
hardenLeaf(params->dest, ¶ms->dest->subsectors[idx], leaf);
}
else
{
bspnodedata_t* data = (bspnodedata_t*) BinaryTree_GetData(left);
node->children[LEFT] = data->index;
}
}
return true; // Continue iteration.
}
static boolean C_DECL countNode(binarytree_t* tree, void* data)
{
if(!BinaryTree_IsLeaf(tree))
(*((uint*) data))++;
return true; // Continue iteration.
}
static boolean C_DECL countSSec(binarytree_t* tree, void* data)
{
if(BinaryTree_IsLeaf(tree))
(*((uint*) data))++;
return true; // Continue iteration.
}
static void hardenBSP(GameMap* dest, binarytree_t* rootNode)
{
dest->numNodes = 0;
BinaryTree_PostOrder(rootNode, countNode, &dest->numNodes);
if(dest->numNodes != 0)
dest->nodes = Z_Calloc(dest->numNodes * sizeof(node_t), PU_MAPSTATIC, 0);
else
dest->nodes = 0;
dest->numSubsectors = 0;
BinaryTree_PostOrder(rootNode, countSSec, &dest->numSubsectors);
dest->subsectors = Z_Calloc(dest->numSubsectors * sizeof(subsector_t), PU_MAPSTATIC, 0);
if(!rootNode)
return;
if(BinaryTree_IsLeaf(rootNode))
{
hardenLeaf(dest, &dest->subsectors[0], (bspleafdata_t*) BinaryTree_GetData(rootNode));
return;
}
{ hardenbspparams_t p;
p.dest = dest;
p.subsectorCurIndex = 0;
p.nodeCurIndex = 0;
BinaryTree_PostOrder(rootNode, hardenNode, &p);
}
}
void BSP_InitForNodeBuild(GameMap* map)
{
uint i;
for(i = 0; i < map->numLineDefs; ++i)
{
linedef_t* l = &map->lineDefs[i];
vertex_t* start = l->v[0];
vertex_t* end = l->v[1];
start->buildData.refCount++;
end->buildData.refCount++;
l->buildData.mlFlags = 0;
// Check for zero-length line.
if((fabs(start->buildData.pos[VX] - end->buildData.pos[VX]) < DIST_EPSILON) &&
(fabs(start->buildData.pos[VY] - end->buildData.pos[VY]) < DIST_EPSILON))
l->buildData.mlFlags |= MLF_ZEROLENGTH;
if(l->inFlags & LF_POLYOBJ)
l->buildData.mlFlags |= MLF_POLYOBJ;
if(l->sideDefs[BACK] && l->sideDefs[FRONT])
{
l->buildData.mlFlags |= MLF_TWOSIDED;
if(l->sideDefs[BACK]->sector == l->sideDefs[FRONT]->sector)
l->buildData.mlFlags |= MLF_SELFREF;
}
}
}
static void hardenVertexes(GameMap* dest, vertex_t*** vertexes,
uint* numVertexes)
{
uint i;
dest->numVertexes = *numVertexes;
dest->vertexes =
Z_Calloc(dest->numVertexes * sizeof(vertex_t), PU_MAPSTATIC, 0);
for(i = 0; i < dest->numVertexes; ++i)
{
vertex_t* destV = &dest->vertexes[i];
vertex_t* srcV = (*vertexes)[i];
destV->header.type = DMU_VERTEX;
destV->numLineOwners = srcV->numLineOwners;
destV->lineOwners = srcV->lineOwners;
//// \fixme Add some rounding.
destV->V_pos[VX] = (float) srcV->buildData.pos[VX];
destV->V_pos[VY] = (float) srcV->buildData.pos[VY];
}
}
static void updateVertexLinks(GameMap* dest)
{
uint i;
for(i = 0; i < dest->numLineDefs; ++i)
{
linedef_t* line = &dest->lineDefs[i];
line->L_v1 = &dest->vertexes[line->L_v1->buildData.index - 1];
line->L_v2 = &dest->vertexes[line->L_v2->buildData.index - 1];
}
}
void SaveMap(GameMap* dest, void* rootNode, vertex_t*** vertexes,
uint* numVertexes)
{
uint startTime = Sys_GetRealTime();
binarytree_t* rn = (binarytree_t*) rootNode;
hardenVertexes(dest, vertexes, numVertexes);
updateVertexLinks(dest);
buildHEdgesFromBSPHEdges(dest, rn);
hardenBSP(dest, rn);
// How much time did we spend?
VERBOSE(Con_Message
("SaveMap: Done in %.2f seconds.\n",
(Sys_GetRealTime() - startTime) / 1000.0f));
}