-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwritebsp.c
699 lines (551 loc) · 16 KB
/
writebsp.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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant 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.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
----------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* marker */
#define WRITEBSP_C
/* dependencies */
//#include "q3map2.h"
#include "kmap2.h" //add hypov8
/*
EmitShader()
emits a bsp shader entry
*/
int EmitShader(const char *shader, int *contentFlags, int *surfaceFlags)
{
int i;
shaderInfo_t *si;
/* handle special cases */
if(shader == NULL)
shader = "noshader";
/* try to find an existing shader */
for(i = 0; i < numBSPShaders; i++)
{
/* ydnar: handle custom surface/content flags */
if(surfaceFlags != NULL && bspShaders[i].surfaceFlags != *surfaceFlags)
continue;
if(contentFlags != NULL && bspShaders[i].contentFlags != *contentFlags)
continue;
/* compare name */
if(!Q_stricmp(shader, bspShaders[i].shader))
return i;
}
/* get shaderinfo */
si = ShaderInfoForShader(shader);
/* emit a new shader */
AUTOEXPAND_BY_REALLOC_BSP(Shaders, 1024);
numBSPShaders++;
strcpy(bspShaders[i].shader, shader);
bspShaders[i].surfaceFlags = si->surfaceFlags;
bspShaders[i].contentFlags = si->contentFlags;
/* handle custom content/surface flags */
if(surfaceFlags != NULL)
bspShaders[i].surfaceFlags = *surfaceFlags;
if(contentFlags != NULL)
bspShaders[i].contentFlags = *contentFlags;
/* recursively emit any damage shaders */
if(si->damageShader != NULL && si->damageShader[0] != '\0')
{
Sys_FPrintf(SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader);
EmitShader(si->damageShader, NULL, NULL);
}
/* return it */
return i;
}
/*
EmitPlanes()
there is no oportunity to discard planes, because all of the original
brushes will be saved in the map
*/
void EmitPlanes(void)
{
int i;
bspPlane_t *bp;
plane_t *mp;
/* walk plane list */
mp = mapplanes;
for(i = 0; i < nummapplanes; i++, mp++)
{
AUTOEXPAND_BY_REALLOC_BSP(Planes, 1024);
bp = &bspPlanes[numBSPPlanes];
VectorCopy(mp->normal, bp->normal);
bp->dist = mp->dist;
numBSPPlanes++;
}
/* emit some statistics */
Sys_FPrintf(SYS_VRB, "%9d BSP planes\n", numBSPPlanes);
}
/*
EmitLeaf()
emits a leafnode to the bsp file
*/
void EmitLeaf(node_t * node)
{
bspLeaf_t *leaf_p;
brush_t *b;
drawSurfRef_t *dsr;
/* check limits */
if(numBSPLeafs >= MAX_MAP_LEAFS)
Error("MAX_MAP_LEAFS");
leaf_p = &bspLeafs[numBSPLeafs];
numBSPLeafs++;
leaf_p->cluster = node->cluster;
leaf_p->area = node->area;
/* emit bounding box */
VectorCopy(node->mins, leaf_p->mins);
VectorCopy(node->maxs, leaf_p->maxs);
/* emit leaf brushes */
leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
for(b = node->brushlist; b; b = b->next)
{
/* something is corrupting brushes */
if((size_t) b < 256)
{
Sys_Printf("WARNING: Node brush list corrupted (0x%08X)\n", b);
break;
}
//% if( b->guard != 0xDEADBEEF )
//% Sys_Printf( "Brush %6d: 0x%08X Guard: 0x%08X Next: 0x%08X Original: 0x%08X Sides: %d\n", b->brushNum, b, b, b->next, b->original, b->numsides );
AUTOEXPAND_BY_REALLOC_BSP(LeafBrushes, 1024);
bspLeafBrushes[numBSPLeafBrushes] = b->original->outputNum;
numBSPLeafBrushes++;
}
leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
/* emit leaf surfaces */
if(node->opaque)
return;
/* add the drawSurfRef_t drawsurfs */
leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
for(dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef)
{
AUTOEXPAND_BY_REALLOC_BSP(LeafSurfaces, 1024);
bspLeafSurfaces[numBSPLeafSurfaces] = dsr->outputNum;
numBSPLeafSurfaces++;
}
leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
}
/*
EmitDrawNode_r()
recursively emit the bsp nodes
*/
int EmitDrawNode_r(node_t * node)
{
bspNode_t *n;
int i, n0;
/* check for leafnode */
if(node->planenum == PLANENUM_LEAF)
{
EmitLeaf(node);
return -numBSPLeafs;
}
/* emit a node */
AUTOEXPAND_BY_REALLOC_BSP(Nodes, 1024);
n0 = numBSPNodes;
n = &bspNodes[n0];
numBSPNodes++;
VectorCopy(node->mins, n->mins);
VectorCopy(node->maxs, n->maxs);
if(node->planenum & 1)
Error("WriteDrawNodes_r: odd planenum");
n->planeNum = node->planenum;
//
// recursively output the other nodes
//
for(i = 0; i < 2; i++)
{
if(node->children[i]->planenum == PLANENUM_LEAF)
{
n->children[i] = -(numBSPLeafs + 1);
EmitLeaf(node->children[i]);
}
else
{
n->children[i] = numBSPNodes;
EmitDrawNode_r(node->children[i]);
// n may have become invalid here, so...
n = &bspNodes[n0];
}
}
return n - bspNodes;
}
/*
============
SetModelNumbers
============
*/
void SetModelNumbers(void)
{
int i;
entity_t *ent;
int models;
char value[10];
const char *classname;
const char *model;
models = 1;
for(i = 1; i < numEntities; i++)
{
ent = &entities[i];
classname = ValueForKey(ent, "classname");
model = ValueForKey(ent, "model");
if(ent->brushes || ent->patches ||
(inlineEntityModels && !ent->brushes && !ent->patches && model[0] != '\0' && Q_stricmp("misc_model", classname)))
{
sprintf(value, "*%i", models);
models++;
SetKeyValue(&entities[i], "model", value);
}
}
}
/*
SetLightStyles()
sets style keys for entity lights
*/
#if 0
void SetLightStyles(void)
{
int i, j, style, numStyles;
qboolean keepLights;
const char *t;
entity_t *e;
epair_t *ep, *next;
char value[10];
char lightTargets[MAX_SWITCHED_LIGHTS][64];
int lightStyles[MAX_SWITCHED_LIGHTS];
/* ydnar: determine if we keep lights in the bsp */
t = ValueForKey(&entities[0], "_keepLights");
keepLights = (t[0] == '1') ? qtrue : qfalse;
/* any light that is controlled (has a targetname) must have a unique style number generated for it */
numStyles = 0;
for(i = 1; i < numEntities; i++)
{
e = &entities[i];
t = ValueForKey(e, "classname");
if(Q_strncasecmp(t, "light", 5))
continue;
t = ValueForKey(e, "name");
if(t[0] == '\0')
{
/* ydnar: strip the light from the BSP file */
if(keepLights == qfalse)
{
ep = e->epairs;
while(ep != NULL)
{
next = ep->next;
free(ep->key);
free(ep->value);
free(ep);
ep = next;
}
e->epairs = NULL;
numStrippedLights++;
}
/* next light */
continue;
}
/* get existing style */
style = IntForKey(e, "style");
if(style < LS_NORMAL || style > LS_NONE)
Error("Invalid lightstyle (%d) on entity %d", style, i);
/* find this targetname */
for(j = 0; j < numStyles; j++)
if(lightStyles[j] == style && !strcmp(lightTargets[j], t))
break;
/* add a new style */
if(j >= numStyles)
{
if(numStyles == MAX_SWITCHED_LIGHTS)
Error("MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS);
strcpy(lightTargets[j], t);
lightStyles[j] = style;
numStyles++;
}
/* set explicit style */
sprintf(value, "%d", 32 + j);
SetKeyValue(e, "style", value);
/* set old style */
if(style != LS_NORMAL)
{
sprintf(value, "%d", style);
SetKeyValue(e, "switch_style", value);
}
}
/* emit some statistics */
Sys_FPrintf(SYS_VRB, "%9d light entities stripped\n", numStrippedLights);
}
#endif
/*
BeginBSPFile()
starts a new bsp file
*/
void BeginBSPFile(void)
{
/* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
numBSPModels = 0;
numBSPNodes = 0;
numBSPBrushSides = 0;
numBSPLeafSurfaces = 0;
numBSPLeafBrushes = 0;
/* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
numBSPLeafs = 1;
/* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
numBSPDrawIndexes = 6;
bspDrawIndexes[0] = 0;
bspDrawIndexes[1] = 1;
bspDrawIndexes[2] = 2;
bspDrawIndexes[3] = 0;
bspDrawIndexes[4] = 2;
bspDrawIndexes[5] = 3;
}
/*
EndBSPFile()
finishes a new bsp and writes to disk
*/
void EndBSPFile(void)
{
char path[1024];
Sys_FPrintf(SYS_VRB, "--- EndBSPFile ---\n");
EmitPlanes();
numBSPEntities = numEntities;
UnparseEntities();
/* write the surface extra file */
WriteSurfaceExtraFile(source);
/* write the bsp */
sprintf(path, "%s.bsp", source);
Sys_Printf("Writing %s\n", path);
WriteBSPFile(path);
}
/*
EmitBrushes()
writes the brush list to the bsp
*/
void EmitBrushes(brush_t * brushes, int *firstBrush, int *numBrushes)
{
int j;
brush_t *b;
bspBrush_t *db;
bspBrushSide_t *cp;
/* set initial brush */
if(firstBrush != NULL)
*firstBrush = numBSPBrushes;
if(numBrushes != NULL)
*numBrushes = 0;
/* walk list of brushes */
for(b = brushes; b != NULL; b = b->next)
{
/* check limits */
AUTOEXPAND_BY_REALLOC_BSP(Brushes, 1024);
/* get bsp brush */
b->outputNum = numBSPBrushes;
db = &bspBrushes[numBSPBrushes];
numBSPBrushes++;
if(numBrushes != NULL)
(*numBrushes)++;
db->shaderNum = EmitShader(b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags);
db->firstSide = numBSPBrushSides;
/* walk sides */
db->numSides = 0;
for(j = 0; j < b->numsides; j++)
{
/* set output number to bogus initially */
b->sides[j].outputNum = -1;
/* check count */
AUTOEXPAND_BY_REALLOC_BSP(BrushSides, 1024);
/* emit side */
b->sides[j].outputNum = numBSPBrushSides;
cp = &bspBrushSides[numBSPBrushSides];
db->numSides++;
numBSPBrushSides++;
cp->planeNum = b->sides[j].planenum;
/* emit shader */
if(b->sides[j].shaderInfo)
cp->shaderNum =
EmitShader(b->sides[j].shaderInfo->shader, &b->sides[j].shaderInfo->contentFlags,
&b->sides[j].shaderInfo->surfaceFlags);
else
cp->shaderNum = EmitShader(NULL, NULL, NULL);
}
}
}
/*
EmitFogs() - ydnar
turns map fogs into bsp fogs
*/
void EmitFogs(void)
{
int i, j;
/* setup */
numBSPFogs = numMapFogs;
/* walk list */
for(i = 0; i < numMapFogs; i++)
{
/* set shader */
strcpy(bspFogs[i].shader, mapFogs[i].si->shader);
/* global fog doesn't have an associated brush */
if(mapFogs[i].brush == NULL)
{
bspFogs[i].brushNum = -1;
bspFogs[i].visibleSide = -1;
}
else
{
/* set brush */
bspFogs[i].brushNum = mapFogs[i].brush->outputNum;
/* try to use forced visible side */
if(mapFogs[i].visibleSide >= 0)
{
bspFogs[i].visibleSide = mapFogs[i].visibleSide;
continue;
}
/* find visible side */
for(j = 0; j < 6; j++)
{
if(mapFogs[i].brush->sides[j].visibleHull != NULL)
{
Sys_Printf("Fog %d has visible side %d\n", i, j);
bspFogs[i].visibleSide = j;
break;
}
}
}
}
}
/*
BeginModel()
sets up a new brush model
*/
void BeginModel(void)
{
bspModel_t *mod;
/* test limits */
AUTOEXPAND_BY_REALLOC_BSP(Models, 256);
/* get model and entity */
mod = &bspModels[numBSPModels];
/* set firsts */
mod->firstBSPSurface = numBSPDrawSurfaces;
mod->firstBSPBrush = numBSPBrushes;
}
/*
EndModel()
finish a model's processing
*/
void EndModel(entity_t * e, node_t * headnode)
{
bspModel_t *mod;
bspDrawSurface_t *ds;
brush_t *b;
vec3_t mins, maxs;
vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
parseMesh_t *p;
const char *name;
const char *model;
const char *classname;
int i, j;
/* note it */
Sys_FPrintf(SYS_VRB, "--- EndModel ---\n");
/* emit the bsp */
mod = &bspModels[numBSPModels];
EmitDrawNode_r(headnode);
/* ydnar: lightgrid mins/maxs */
ClearBounds(lgMins, lgMaxs);
/* bound the brushes */
ClearBounds(mins, maxs);
//BoundsAdd(mins, maxs, headnode->mins, headnode->maxs);
for(b = e->brushes; b; b = b->next)
{
/* ignore non-real brushes (origin, etc) */
if(b->numsides == 0)
continue;
// Tr3B: ignore autogenerated clip brushes because their bounding sizes can be really huge
if(b->generatedClipBrush)
continue;
AddPointToBounds(b->mins, mins, maxs);
AddPointToBounds(b->maxs, mins, maxs);
/* ydnar: lightgrid bounds */
if(b->compileFlags & C_LIGHTGRID)
{
AddPointToBounds(b->mins, lgMins, lgMaxs);
AddPointToBounds(b->maxs, lgMins, lgMaxs);
}
}
/* bound patches */
for(p = e->patches; p; p = p->next)
{
for(i = 0; i < (p->mesh.width * p->mesh.height); i++)
AddPointToBounds(p->mesh.verts[i].xyz, mins, maxs);
}
/* Tr3B: bound triangle surfaces */
classname = ValueForKey(e, "classname");
name = ValueForKey(e, "name");
model = ValueForKey(e, "model");
#if 1
if(inlineEntityModels && !e->brushes && !e->patches && model[0] != '\0' && Q_stricmp("misc_model", classname))
{
qboolean noVerts = qtrue;
//Sys_FPrintf(SYS_STD, "calculating BSP bounds from draw surfaces for entity '%s' with model '%s'\n", name, model);
for(i = mod->firstBSPSurface; i < numBSPDrawSurfaces; i++)
{
ds = &bspDrawSurfaces[i];
if(!ds->numVerts)
{
continue; // leftover from a surface subdivision
}
// HACK: don't loop only through the vertices because they can contain bad data with .lwo models ...
for(j = 0; j < ds->numIndexes; j += 3)
{
AddPointToBounds(bspDrawVerts[ds->firstVert + bspDrawIndexes[j + ds->firstIndex]].xyz, mins, maxs);
noVerts = qfalse;
}
}
if(noVerts)
{
// : model could not be loaded or something else went wrong.
// reset to zero bounds so the entity doesn't mess up the server collision code
Sys_FPrintf(SYS_WRN, "WARNING: resetting BSP bounds for entity = '%s', classname = '%s', model = '%s'\n", name, classname, model);
VectorClear(mins);
VectorClear(maxs);
}
}
#endif
/* ydnar: lightgrid mins/maxs */
#if 0
// : don't mess with the model bounds ...
if(lgMins[0] < 99999)
{
/* use lightgrid bounds */
VectorCopy(lgMins, mod->mins);
VectorCopy(lgMaxs, mod->maxs);
}
else
#endif
{
/* use brush/patch bounds */
VectorCopy(mins, mod->mins);
VectorCopy(maxs, mod->maxs);
}
/* note size */
Sys_FPrintf(SYS_VRB, "BSP bounds: { %f %f %f } { %f %f %f }\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
Sys_FPrintf(SYS_VRB, "Lightgrid bounds: { %f %f %f } { %f %f %f }\n", lgMins[0], lgMins[1], lgMins[2], lgMaxs[0], lgMaxs[1],
lgMaxs[2]);
/* set surfaces and brushes */
mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
mod->firstBSPBrush = e->firstBrush;
mod->numBSPBrushes = e->numBrushes;
/* increment model count */
numBSPModels++;
}