2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
29 =========================================================
31 ONLY SAVE OUT PLANES THAT ARE ACTUALLY USED AS NODES
33 =========================================================
36 int planeused[MAX_MAP_PLANES];
42 There is no oportunity to discard planes, because all of the original
43 brushes will be saved in the map.
46 void EmitPlanes (void)
51 int planetranslate[MAX_MAP_PLANES];
54 for (i=0 ; i<nummapplanes ; i++, mp++)
56 dp = &dplanes[numplanes];
57 planetranslate[i] = numplanes;
58 VectorCopy ( mp->normal, dp->normal);
66 //========================================================
68 void EmitMarkFace (dleaf_t *leaf_p, face_t *f)
78 EmitMarkFace (leaf_p, f->split[0]);
79 EmitMarkFace (leaf_p, f->split[1]);
83 facenum = f->outputnumber;
85 return; // degenerate face
87 if (facenum < 0 || facenum >= numfaces)
88 Error ("Bad leafface");
89 for (i=leaf_p->firstleafface ; i<numleaffaces ; i++)
90 if (dleaffaces[i] == facenum)
91 break; // merged out face
92 if (i == numleaffaces)
94 if (numleaffaces >= MAX_MAP_LEAFFACES)
95 Error ("MAX_MAP_LEAFFACES");
97 dleaffaces[numleaffaces] = facenum;
109 void EmitLeaf (node_t *node)
120 if (numleafs >= MAX_MAP_LEAFS)
121 Error ("MAX_MAP_LEAFS");
123 leaf_p = &dleafs[numleafs];
126 leaf_p->contents = node->contents;
127 leaf_p->cluster = node->cluster;
128 leaf_p->area = node->area;
131 // write bounding box info
133 VectorCopy (node->mins, leaf_p->mins);
134 VectorCopy (node->maxs, leaf_p->maxs);
137 // write the leafbrushes
139 leaf_p->firstleafbrush = numleafbrushes;
140 for (b=node->brushlist ; b ; b=b->next)
142 if (numleafbrushes >= MAX_MAP_LEAFBRUSHES)
143 Error ("MAX_MAP_LEAFBRUSHES");
145 brushnum = b->original - mapbrushes;
146 for (i=leaf_p->firstleafbrush ; i<numleafbrushes ; i++)
147 if (dleafbrushes[i] == brushnum)
149 if (i == numleafbrushes)
151 dleafbrushes[numleafbrushes] = brushnum;
155 leaf_p->numleafbrushes = numleafbrushes - leaf_p->firstleafbrush;
158 // write the leaffaces
160 if (leaf_p->contents & CONTENTS_SOLID)
161 return; // no leaffaces in solids
163 leaf_p->firstleafface = numleaffaces;
165 for (p = node->portals ; p ; p = p->next[s])
167 s = (p->nodes[1] == node);
170 continue; // not a visible portal
172 EmitMarkFace (leaf_p, f);
175 leaf_p->numleaffaces = numleaffaces - leaf_p->firstleafface;
184 void EmitFace (face_t *f)
190 f->outputnumber = -1;
192 if (f->numpoints < 3)
194 return; // degenerated
196 if (f->merged || f->split[0] || f->split[1])
198 return; // not a final face
201 // save output number so leaffaces can use
202 f->outputnumber = numfaces;
204 if (numfaces >= MAX_MAP_FACES)
205 Error ("numfaces == MAX_MAP_FACES");
206 df = &dfaces[numfaces];
209 // planenum is used by qlight, but not quake
210 df->planenum = f->planenum & (~1);
211 df->side = f->planenum & 1;
213 df->firstedge = numsurfedges;
214 df->numedges = f->numpoints;
215 df->texinfo = f->texinfo;
216 for (i=0 ; i<f->numpoints ; i++)
218 // e = GetEdge (f->pts[i], f->pts[(i+1)%f->numpoints], f);
219 e = GetEdge2 (f->vertexnums[i], f->vertexnums[(i+1)%f->numpoints], f);
220 if (numsurfedges >= MAX_MAP_SURFEDGES)
221 Error ("numsurfedges == MAX_MAP_SURFEDGES");
222 dsurfedges[numsurfedges] = e;
232 int EmitDrawNode_r (node_t *node)
238 if (node->planenum == PLANENUM_LEAF)
245 if (numnodes == MAX_MAP_NODES)
246 Error ("MAX_MAP_NODES");
247 n = &dnodes[numnodes];
250 VectorCopy (node->mins, n->mins);
251 VectorCopy (node->maxs, n->maxs);
253 planeused[node->planenum]++;
254 planeused[node->planenum^1]++;
256 if (node->planenum & 1)
257 Error ("WriteDrawNodes_r: odd planenum");
258 n->planenum = node->planenum;
259 n->firstface = numfaces;
266 for (f=node->faces ; f ; f=f->next)
269 n->numfaces = numfaces - n->firstface;
273 // recursively output the other nodes
275 for (i=0 ; i<2 ; i++)
277 if (node->children[i]->planenum == PLANENUM_LEAF)
279 n->children[i] = -(numleafs + 1);
280 EmitLeaf (node->children[i]);
284 n->children[i] = numnodes;
285 EmitDrawNode_r (node->children[i]);
292 //=========================================================
300 void WriteBSP (node_t *headnode)
307 qprintf ("--- WriteBSP ---\n");
310 dmodels[nummodels].headnode = EmitDrawNode_r (headnode);
311 EmitAreaPortals (headnode);
313 qprintf ("%5i nodes with faces\n", c_facenodes);
314 qprintf ("%5i nodes without faces\n", c_nofaces);
315 qprintf ("%5i faces\n", numfaces-oldfaces);
318 //===========================================================
325 void SetModelNumbers (void)
332 for (i=1 ; i<num_entities ; i++)
334 if (entities[i].numbrushes)
336 sprintf (value, "*%i", models);
338 SetKeyValue (&entities[i], "model", value);
349 #define MAX_SWITCHED_LIGHTS 32
350 void SetLightStyles (void)
357 char lighttargets[MAX_SWITCHED_LIGHTS][64];
360 // any light that is controlled (has a targetname)
361 // must have a unique style number generated for it
364 for (i=1 ; i<num_entities ; i++)
368 t = ValueForKey (e, "classname");
369 if (Q_strncasecmp (t, "light", 5))
371 t = ValueForKey (e, "targetname");
375 // find this targetname
376 for (j=0 ; j<stylenum ; j++)
377 if (!strcmp (lighttargets[j], t))
381 if (stylenum == MAX_SWITCHED_LIGHTS)
382 Error ("stylenum == MAX_SWITCHED_LIGHTS");
383 strcpy (lighttargets[j], t);
386 sprintf (value, "%i", 32 + j);
387 SetKeyValue (e, "style", value);
392 //===========================================================
399 void EmitBrushes (void)
401 int i, j, bnum, s, x;
410 numbrushes = nummapbrushes;
412 for (bnum=0 ; bnum<nummapbrushes ; bnum++)
414 b = &mapbrushes[bnum];
415 db = &dbrushes[bnum];
417 db->contents = b->contents;
418 db->firstside = numbrushsides;
419 db->numsides = b->numsides;
420 for (j=0 ; j<b->numsides ; j++)
422 if (numbrushsides == MAX_MAP_BRUSHSIDES)
423 Error ("MAX_MAP_BRUSHSIDES");
424 cp = &dbrushsides[numbrushsides];
426 cp->planenum = b->original_sides[j].planenum;
427 cp->texinfo = b->original_sides[j].texinfo;
430 // add any axis planes not contained in the brush to bevel off corners
431 for (x=0 ; x<3 ; x++)
432 for (s=-1 ; s<=1 ; s+=2)
435 VectorCopy (vec3_origin, normal);
441 planenum = FindFloatPlane (normal, dist);
442 for (i=0 ; i<b->numsides ; i++)
443 if (b->original_sides[i].planenum == planenum)
445 if (i == b->numsides)
447 if (numbrushsides >= MAX_MAP_BRUSHSIDES)
448 Error ("MAX_MAP_BRUSHSIDES");
450 dbrushsides[numbrushsides].planenum = planenum;
451 dbrushsides[numbrushsides].texinfo =
452 dbrushsides[numbrushsides-1].texinfo;
462 //===========================================================
469 void BeginBSPFile (void)
471 // these values may actually be initialized
472 // if the file existed when loaded, so clear them explicitly
482 // edge 0 is not used, because 0 can't be negated
485 // leave vertex 0 as an error
488 // leave leaf 0 as an error
490 dleafs[0].contents = CONTENTS_SOLID;
499 void EndBSPFile (void)
512 sprintf (path, "%s/pics/pop.lmp", gamedir);
513 len = LoadFile (path, &buf);
514 memcpy (dpop, buf, sizeof(dpop));
519 sprintf (path, "%s.bsp", source);
520 printf ("Writing %s\n", path);
531 extern int firstmodeledge;
532 extern int firstmodelface;
533 void BeginModel (void)
542 if (nummodels == MAX_MAP_MODELS)
543 Error ("MAX_MAP_MODELS");
544 mod = &dmodels[nummodels];
546 mod->firstface = numfaces;
548 firstmodleaf = numleafs;
549 firstmodeledge = numedges;
550 firstmodelface = numfaces;
555 e = &entities[entity_num];
557 start = e->firstbrush;
558 end = start + e->numbrushes;
559 ClearBounds (mins, maxs);
561 for (j=start ; j<end ; j++)
565 continue; // not a real brush (origin brush)
566 AddPointToBounds (b->mins, mins, maxs);
567 AddPointToBounds (b->maxs, mins, maxs);
570 VectorCopy (mins, mod->mins);
571 VectorCopy (maxs, mod->maxs);
584 mod = &dmodels[nummodels];
586 mod->numfaces = numfaces - mod->firstface;