2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 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 GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 =========================================================
30 ONLY SAVE OUT PLANES THAT ARE ACTUALLY USED AS NODES
32 =========================================================
35 int planeused[MAX_MAP_PLANES];
41 There is no oportunity to discard planes, because all of the original
42 brushes will be saved in the map.
45 void EmitPlanes( void ){
49 int planetranslate[MAX_MAP_PLANES];
52 for ( i = 0 ; i < nummapplanes ; i++, mp++ )
54 dp = &dplanes[numplanes];
55 planetranslate[i] = numplanes;
56 VectorCopy( mp->normal, dp->normal );
64 //========================================================
66 void EmitMarkFace( dleaf_t *leaf_p, face_t *f ){
74 EmitMarkFace( leaf_p, f->split[0] );
75 EmitMarkFace( leaf_p, f->split[1] );
79 facenum = f->outputnumber;
80 if ( facenum == -1 ) {
81 return; // degenerate face
84 if ( facenum < 0 || facenum >= numfaces ) {
85 Error( "Bad leafface" );
87 for ( i = leaf_p->firstleafface ; i < numleaffaces ; i++ )
88 if ( dleaffaces[i] == facenum ) {
91 if ( i == numleaffaces ) {
92 if ( numleaffaces >= MAX_MAP_LEAFFACES ) {
93 Error( "MAX_MAP_LEAFFACES" );
96 dleaffaces[numleaffaces] = facenum;
108 void EmitLeaf( node_t *node ){
118 if ( numleafs >= MAX_MAP_LEAFS ) {
119 Error( "MAX_MAP_LEAFS" );
122 leaf_p = &dleafs[numleafs];
125 leaf_p->contents = node->contents;
126 leaf_p->cluster = node->cluster;
127 leaf_p->area = node->area;
130 // write bounding box info
132 VectorCopy( (short) node->mins, leaf_p->mins );
133 VectorCopy( (short) node->maxs, leaf_p->maxs );
136 // write the leafbrushes
138 leaf_p->firstleafbrush = numleafbrushes;
139 for ( b = node->brushlist ; b ; b = b->next )
141 if ( numleafbrushes >= MAX_MAP_LEAFBRUSHES ) {
142 Error( "MAX_MAP_LEAFBRUSHES" );
145 brushnum = b->original - mapbrushes;
146 for ( i = leaf_p->firstleafbrush ; i < numleafbrushes ; i++ )
147 if ( dleafbrushes[i] == brushnum ) {
150 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
164 leaf_p->firstleafface = numleaffaces;
166 for ( p = node->portals ; p ; p = p->next[s] )
168 s = ( p->nodes[1] == node );
171 continue; // not a visible portal
174 EmitMarkFace( leaf_p, f );
177 leaf_p->numleaffaces = numleaffaces - leaf_p->firstleafface;
186 void EmitFace( face_t *f ){
191 f->outputnumber = -1;
193 if ( f->numpoints < 3 ) {
194 return; // degenerated
196 if ( f->merged || f->split[0] || f->split[1] ) {
197 return; // not a final face
200 // save output number so leaffaces can use
201 f->outputnumber = numfaces;
203 if ( numfaces >= MAX_MAP_FACES ) {
204 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" );
223 dsurfedges[numsurfedges] = e;
233 int EmitDrawNode_r( node_t *node ){
238 if ( node->planenum == PLANENUM_LEAF ) {
244 if ( numnodes == MAX_MAP_NODES ) {
245 Error( "MAX_MAP_NODES" );
247 n = &dnodes[numnodes];
250 VectorCopy( (short) node->mins, n->mins );
251 VectorCopy( (short) node->maxs, n->maxs );
253 planeused[node->planenum]++;
254 planeused[node->planenum ^ 1]++;
256 if ( node->planenum & 1 ) {
257 Error( "WriteDrawNodes_r: odd planenum" );
259 n->planenum = node->planenum;
260 n->firstface = numfaces;
262 if ( !node->faces ) {
269 for ( f = node->faces ; f ; f = f->next )
272 n->numfaces = numfaces - n->firstface;
276 // recursively output the other nodes
278 for ( i = 0 ; i < 2 ; i++ )
280 if ( node->children[i]->planenum == PLANENUM_LEAF ) {
281 n->children[i] = -( numleafs + 1 );
282 EmitLeaf( node->children[i] );
286 n->children[i] = numnodes;
287 EmitDrawNode_r( node->children[i] );
294 //=========================================================
302 void WriteBSP( node_t *headnode ){
308 Sys_FPrintf( SYS_VRB, "--- WriteBSP ---\n" );
311 dmodels[nummodels].headnode = EmitDrawNode_r( headnode );
312 EmitAreaPortals( headnode );
314 Sys_FPrintf( SYS_VRB, "%5i nodes with faces\n", c_facenodes );
315 Sys_FPrintf( SYS_VRB, "%5i nodes without faces\n", c_nofaces );
316 Sys_FPrintf( SYS_VRB, "%5i faces\n", numfaces - oldfaces );
319 //===========================================================
326 void SetModelNumbers( void ){
332 for ( i = 1 ; i < num_entities ; i++ )
334 if ( entities[i].numbrushes ) {
335 sprintf( value, "*%i", models );
337 SetKeyValue( &entities[i], "model", value );
348 #define MAX_SWITCHED_LIGHTS 32
349 void SetLightStyles( void ){
355 char lighttargets[MAX_SWITCHED_LIGHTS][64];
358 // any light that is controlled (has a targetname)
359 // must have a unique style number generated for it
362 for ( i = 1 ; i < num_entities ; i++ )
366 t = ValueForKey( e, "classname" );
367 if ( Q_strncasecmp( t, "light", 5 ) ) {
370 t = ValueForKey( e, "targetname" );
375 // find this targetname
376 for ( j = 0 ; j < stylenum ; j++ )
377 if ( !strcmp( lighttargets[j], t ) ) {
380 if ( j == stylenum ) {
381 if ( stylenum == MAX_SWITCHED_LIGHTS ) {
382 Error( "stylenum == MAX_SWITCHED_LIGHTS" );
384 strcpy( lighttargets[j], t );
387 sprintf( value, "%i", 32 + j );
388 SetKeyValue( e, "style", value );
393 //===========================================================
400 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" );
425 cp = &dbrushsides[numbrushsides];
427 cp->planenum = b->original_sides[j].planenum;
428 cp->texinfo = b->original_sides[j].texinfo;
431 // add any axis planes not contained in the brush to bevel off corners
432 for ( x = 0 ; x < 3 ; x++ )
433 for ( s = -1 ; s <= 1 ; s += 2 )
436 VectorCopy( vec3_origin, normal );
437 normal[x] = (float) s;
444 planenum = FindFloatPlane( normal, dist );
445 for ( i = 0 ; i < b->numsides ; i++ )
446 if ( b->original_sides[i].planenum == planenum ) {
449 if ( i == b->numsides ) {
450 if ( numbrushsides >= MAX_MAP_BRUSHSIDES ) {
451 Error( "MAX_MAP_BRUSHSIDES" );
454 dbrushsides[numbrushsides].planenum = planenum;
455 dbrushsides[numbrushsides].texinfo =
456 dbrushsides[numbrushsides - 1].texinfo;
466 //===========================================================
473 void BeginBSPFile( void ){
474 // these values may actually be initialized
475 // if the file existed when loaded, so clear them explicitly
485 // edge 0 is not used, because 0 can't be negated
488 // leave vertex 0 as an error
491 // leave leaf 0 as an error
493 dleafs[0].contents = CONTENTS_SOLID;
502 void EndBSPFile( void ){
516 sprintf( path, "%s/pics/pop.lmp", gamedir );
517 len = LoadFile( path, &buf );
518 memcpy( dpop, buf, sizeof( dpop ) );
523 sprintf( path, "%s.bsp", source );
524 Sys_Printf( "Writing %s\n", path );
525 WriteBSPFile( path );
535 extern int firstmodeledge;
536 extern int firstmodelface;
537 void BeginModel( void ){
545 if ( nummodels == MAX_MAP_MODELS ) {
546 Error( "MAX_MAP_MODELS" );
548 mod = &dmodels[nummodels];
550 mod->firstface = numfaces;
552 firstmodleaf = numleafs;
553 firstmodeledge = numedges;
554 firstmodelface = numfaces;
559 e = &entities[entity_num];
561 start = e->firstbrush;
562 end = start + e->numbrushes;
563 ClearBounds( mins, maxs );
565 for ( j = start ; j < end ; j++ )
568 if ( !b->numsides ) {
569 continue; // not a real brush (origin brush)
571 AddPointToBounds( b->mins, mins, maxs );
572 AddPointToBounds( b->maxs, mins, maxs );
575 VectorCopy( mins, mod->mins );
576 VectorCopy( maxs, mod->maxs );
585 void EndModel( void ){
588 mod = &dmodels[nummodels];
590 mod->numfaces = numfaces - mod->firstface;