8
=========================================================
10
ONLY SAVE OUT PLANES THAT ARE ACTUALLY USED AS NODES
12
=========================================================
15
int planeused[MAX_MAP_PLANES];
21
There is no oportunity to discard planes, because all of the original
22
brushes will be saved in the map.
25
void EmitPlanes (void)
30
int planetranslate[MAX_MAP_PLANES];
33
for (i=0 ; i<nummapplanes ; i++, mp++)
35
dp = &dplanes[numplanes];
36
planetranslate[i] = numplanes;
37
VectorCopy ( mp->normal, dp->normal);
45
//========================================================
47
void EmitMarkFace (dleaf_t *leaf_p, face_t *f)
57
EmitMarkFace (leaf_p, f->split[0]);
58
EmitMarkFace (leaf_p, f->split[1]);
62
facenum = f->outputnumber;
64
return; // degenerate face
66
if (facenum < 0 || facenum >= numfaces)
67
Error ("Bad leafface");
68
for (i=leaf_p->firstleafface ; i<numleaffaces ; i++)
69
if (dleaffaces[i] == facenum)
70
break; // merged out face
71
if (i == numleaffaces)
73
if (numleaffaces >= MAX_MAP_LEAFFACES)
74
Error ("MAX_MAP_LEAFFACES");
76
dleaffaces[numleaffaces] = facenum;
87
void EmitLeaf (node_t *node)
98
if (numleafs >= MAX_MAP_LEAFS)
99
Error ("MAX_MAP_LEAFS");
101
leaf_p = &dleafs[numleafs];
104
leaf_p->contents = node->contents;
105
leaf_p->cluster = node->cluster;
106
leaf_p->area = node->area;
109
// write bounding box info
111
VectorCopy (node->mins, leaf_p->mins);
112
VectorCopy (node->maxs, leaf_p->maxs);
115
// write the leafbrushes
117
leaf_p->firstleafbrush = numleafbrushes;
118
for (b=node->brushlist ; b ; b=b->next)
120
if (numleafbrushes >= MAX_MAP_LEAFBRUSHES)
121
Error ("MAX_MAP_LEAFBRUSHES");
123
brushnum = b->original - mapbrushes;
124
for (i=leaf_p->firstleafbrush ; i<numleafbrushes ; i++)
125
if (dleafbrushes[i] == brushnum)
127
if (i == numleafbrushes)
129
dleafbrushes[numleafbrushes] = brushnum;
133
leaf_p->numleafbrushes = numleafbrushes - leaf_p->firstleafbrush;
136
// write the leaffaces
138
if (leaf_p->contents & CONTENTS_SOLID)
139
return; // no leaffaces in solids
141
leaf_p->firstleafface = numleaffaces;
143
for (p = node->portals ; p ; p = p->next[s])
145
s = (p->nodes[1] == node);
148
continue; // not a visible portal
150
EmitMarkFace (leaf_p, f);
153
leaf_p->numleaffaces = numleaffaces - leaf_p->firstleafface;
162
void EmitFace (face_t *f)
168
f->outputnumber = -1;
170
if (f->numpoints < 3)
172
return; // degenerated
174
if (f->merged || f->split[0] || f->split[1])
176
return; // not a final face
179
// save output number so leaffaces can use
180
f->outputnumber = numfaces;
182
if (numfaces >= MAX_MAP_FACES)
183
Error ("numfaces == MAX_MAP_FACES");
184
df = &dfaces[numfaces];
187
// planenum is used by qlight, but not quake
188
df->planenum = f->planenum & (~1);
189
df->side = f->planenum & 1;
191
df->firstedge = numsurfedges;
192
df->numedges = f->numpoints;
193
df->texinfo = f->texinfo;
194
for (i=0 ; i<f->numpoints ; i++)
196
// e = GetEdge (f->pts[i], f->pts[(i+1)%f->numpoints], f);
197
e = GetEdge2 (f->vertexnums[i], f->vertexnums[(i+1)%f->numpoints], f);
198
if (numsurfedges >= MAX_MAP_SURFEDGES)
199
Error ("numsurfedges == MAX_MAP_SURFEDGES");
200
dsurfedges[numsurfedges] = e;
210
int EmitDrawNode_r (node_t *node)
216
if (node->planenum == PLANENUM_LEAF)
223
if (numnodes == MAX_MAP_NODES)
224
Error ("MAX_MAP_NODES");
225
n = &dnodes[numnodes];
228
VectorCopy (node->mins, n->mins);
229
VectorCopy (node->maxs, n->maxs);
231
planeused[node->planenum]++;
232
planeused[node->planenum^1]++;
234
if (node->planenum & 1)
235
Error ("WriteDrawNodes_r: odd planenum");
236
n->planenum = node->planenum;
237
n->firstface = numfaces;
244
for (f=node->faces ; f ; f=f->next)
247
n->numfaces = numfaces - n->firstface;
251
// recursively output the other nodes
253
for (i=0 ; i<2 ; i++)
255
if (node->children[i]->planenum == PLANENUM_LEAF)
257
n->children[i] = -(numleafs + 1);
258
EmitLeaf (node->children[i]);
262
n->children[i] = numnodes;
263
EmitDrawNode_r (node->children[i]);
270
//=========================================================
278
void WriteBSP (node_t *headnode)
285
qprintf ("--- WriteBSP ---\n");
288
dmodels[nummodels].headnode = EmitDrawNode_r (headnode);
290
// Workaround for bug in Quake 2. Make sure there is at least one leaf
296
for (i=numleafs,l=dleafs;i;i--,l++)
308
EmitAreaPortals (headnode);
310
qprintf ("%5i nodes with faces\n", c_facenodes);
311
qprintf ("%5i nodes without faces\n", c_nofaces);
312
qprintf ("%5i faces\n", numfaces-oldfaces);
315
//===========================================================
322
void SetModelNumbers (void)
329
for (i=1 ; i<num_entities ; i++)
331
if (entities[i].numbrushes)
333
sprintf (value, "*%i", models);
335
SetKeyValue (&entities[i], "model", value);
346
#define MAX_SWITCHED_LIGHTS 32
347
void SetLightStyles (void)
354
char lighttargets[MAX_SWITCHED_LIGHTS][64];
357
// any light that is controlled (has a targetname)
358
// must have a unique style number generated for it
361
for (i=1 ; i<num_entities ; i++)
365
t = ValueForKey (e, "classname");
366
if (Q_strncasecmp (t, "light", 5))
368
t = ValueForKey (e, "targetname");
372
// find this targetname
373
for (j=0 ; j<stylenum ; j++)
374
if (!strcmp (lighttargets[j], t))
378
if (stylenum == MAX_SWITCHED_LIGHTS)
379
Error ("stylenum == MAX_SWITCHED_LIGHTS");
380
strcpy (lighttargets[j], t);
383
sprintf (value, "%i", 32 + j);
384
SetKeyValue (e, "style", value);
389
//===========================================================
396
void EmitBrushes (void)
398
int i, j, bnum, s, x;
407
numbrushes = nummapbrushes;
409
for (bnum=0 ; bnum<nummapbrushes ; bnum++)
411
b = &mapbrushes[bnum];
412
db = &dbrushes[bnum];
414
db->contents = b->contents;
415
db->firstside = numbrushsides;
416
db->numsides = b->numsides;
417
for (j=0 ; j<b->numsides ; j++)
419
if (numbrushsides == MAX_MAP_BRUSHSIDES)
420
Error ("MAX_MAP_BRUSHSIDES");
421
cp = &dbrushsides[numbrushsides];
423
cp->planenum = b->original_sides[j].planenum;
424
cp->texinfo = b->original_sides[j].texinfo;
427
// add any axis planes not contained in the brush to bevel off corners
428
for (x=0 ; x<3 ; x++)
429
for (s=-1 ; s<=1 ; s+=2)
432
VectorCopy (vec3_origin, normal);
438
planenum = FindFloatPlane (normal, dist);
439
for (i=0 ; i<b->numsides ; i++)
440
if (b->original_sides[i].planenum == planenum)
442
if (i == b->numsides)
444
if (numbrushsides >= MAX_MAP_BRUSHSIDES)
445
Error ("MAX_MAP_BRUSHSIDES");
447
dbrushsides[numbrushsides].planenum = planenum;
448
dbrushsides[numbrushsides].texinfo =
449
dbrushsides[numbrushsides-1].texinfo;
458
//===========================================================
465
void BeginBSPFile (void)
467
// these values may actually be initialized
468
// if the file existed when loaded, so clear them explicitly
478
// edge 0 is not used, because 0 can't be negated
481
// leave vertex 0 as an error
484
// leave leaf 0 as an error
486
dleafs[0].contents = CONTENTS_SOLID;
495
void EndBSPFile (void)
510
sprintf (path, "%s/pics/pop.lmp", gamedir);
511
len = LoadFile (path, &buf);
512
memcpy (dpop, buf, sizeof(dpop));
517
sprintf (path, "%s.bsp", source);
518
printf ("Writing %s\n", path);
529
extern int firstmodeledge;
530
extern int firstmodelface;
531
void BeginModel (void)
540
if (nummodels == MAX_MAP_MODELS)
541
Error ("MAX_MAP_MODELS");
542
mod = &dmodels[nummodels];
544
mod->firstface = numfaces;
546
firstmodleaf = numleafs;
547
firstmodeledge = numedges;
548
firstmodelface = numfaces;
553
e = &entities[entity_num];
555
start = e->firstbrush;
556
end = start + e->numbrushes;
557
ClearBounds (mins, maxs);
559
for (j=start ; j<end ; j++)
563
continue; // not a real brush (origin brush)
564
AddPointToBounds (b->mins, mins, maxs);
565
AddPointToBounds (b->maxs, mins, maxs);
568
VectorCopy (mins, mod->mins);
569
VectorCopy (maxs, mod->maxs);
582
mod = &dmodels[nummodels];
584
mod->numfaces = numfaces - mod->firstface;