]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/writebsp.c
support non-uniform -scale
[divverent/netradiant.git] / tools / quake3 / q3map2 / writebsp.c
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22 ----------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define WRITEBSP_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 EmitShader()
43 emits a bsp shader entry
44 */
45
46 int     EmitShader( const char *shader, int *contentFlags, int *surfaceFlags )
47 {
48         int                             i;
49         shaderInfo_t    *si;
50         
51         
52         /* handle special cases */
53         if( shader == NULL )
54                 shader = "noshader";
55         
56         /* try to find an existing shader */
57         for( i = 0; i < numBSPShaders; i++ )
58         {
59                 /* ydnar: handle custom surface/content flags */
60                 if( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags )
61                         continue;
62                 if( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags )
63                         continue;
64                 
65                 /* compare name */
66                 if( !Q_stricmp( shader, bspShaders[ i ].shader ) )
67                         return i;
68         }
69
70         // i == numBSPShaders
71         
72         /* get shaderinfo */
73         si = ShaderInfoForShader( shader );
74         
75         /* emit a new shader */
76         AUTOEXPAND_BY_REALLOC_BSP(Shaders, 1024);
77
78         numBSPShaders++;
79         strcpy( bspShaders[ i ].shader, shader );
80         bspShaders[ i ].surfaceFlags = si->surfaceFlags;
81         bspShaders[ i ].contentFlags = si->contentFlags;
82         
83         /* handle custom content/surface flags */
84         if( surfaceFlags != NULL )
85                 bspShaders[ i ].surfaceFlags = *surfaceFlags;
86         if( contentFlags != NULL )
87                 bspShaders[ i ].contentFlags = *contentFlags;
88         
89         /* recursively emit any damage shaders */
90         if( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' )
91         {
92                 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
93                 EmitShader( si->damageShader, NULL, NULL );
94         }
95         
96         /* return it */
97         return i;
98 }
99
100
101
102 /*
103 EmitPlanes()
104 there is no oportunity to discard planes, because all of the original
105 brushes will be saved in the map
106 */
107
108 void EmitPlanes( void )
109 {
110         int                     i;
111         bspPlane_t      *bp;
112         plane_t         *mp;
113         
114         
115         /* walk plane list */
116         mp = mapplanes;
117         for( i = 0; i < nummapplanes; i++, mp++ )
118         {
119                 bp = &bspPlanes[ numBSPPlanes ];
120                 VectorCopy( mp->normal, bp->normal );
121                 bp->dist = mp->dist;
122                 numBSPPlanes++;
123         }
124         
125         /* emit some statistics */
126         Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
127 }
128
129
130
131 /*
132 EmitLeaf()
133 emits a leafnode to the bsp file
134 */
135
136 void EmitLeaf( node_t *node )
137 {
138         bspLeaf_t               *leaf_p;
139         brush_t                 *b;
140         drawSurfRef_t   *dsr;
141
142         
143         /* check limits */
144         if( numBSPLeafs >= MAX_MAP_LEAFS )
145                 Error( "MAX_MAP_LEAFS" );
146
147         leaf_p = &bspLeafs[numBSPLeafs];
148         numBSPLeafs++;
149
150         leaf_p->cluster = node->cluster;
151         leaf_p->area = node->area;
152
153         /* emit bounding box */
154         VectorCopy( node->mins, leaf_p->mins );
155         VectorCopy( node->maxs, leaf_p->maxs );
156         
157         /* emit leaf brushes */
158         leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
159         for( b = node->brushlist; b; b = b->next )
160         {
161                 /* something is corrupting brushes */
162                 if( (size_t) b < 256 )
163                 {
164                         Sys_Printf( "WARNING: Node brush list corrupted (0x%08X)\n", b );
165                         break;
166                 }
167                 //%     if( b->guard != 0xDEADBEEF )
168                 //%             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 );
169                 
170                 AUTOEXPAND_BY_REALLOC_BSP(LeafBrushes, 1024);
171                 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
172                 numBSPLeafBrushes++;
173         }
174         
175         leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
176         
177         /* emit leaf surfaces */
178         if( node->opaque )
179                 return;
180         
181         /* add the drawSurfRef_t drawsurfs */
182         leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
183         for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
184         {
185                 AUTOEXPAND_BY_REALLOC_BSP(LeafSurfaces, 1024);
186                 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
187                 numBSPLeafSurfaces++;                   
188         }
189         
190         leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
191 }
192
193
194 /*
195 EmitDrawNode_r()
196 recursively emit the bsp nodes
197 */
198
199 int EmitDrawNode_r( node_t *node )
200 {
201         bspNode_t       *n;
202         int                     i, n0;
203         
204         
205         /* check for leafnode */
206         if( node->planenum == PLANENUM_LEAF )
207         {
208                 EmitLeaf( node );
209                 return -numBSPLeafs;
210         }
211         
212         /* emit a node */
213         AUTOEXPAND_BY_REALLOC_BSP(Nodes, 1024);
214         n0 = numBSPNodes;
215         n = &bspNodes[ n0 ];
216         numBSPNodes++;
217         
218         VectorCopy (node->mins, n->mins);
219         VectorCopy (node->maxs, n->maxs);
220
221         if (node->planenum & 1)
222                 Error ("WriteDrawNodes_r: odd planenum");
223         n->planeNum = node->planenum;
224
225         //
226         // recursively output the other nodes
227         //      
228         for (i=0 ; i<2 ; i++)
229         {
230                 if (node->children[i]->planenum == PLANENUM_LEAF)
231                 {
232                         n->children[i] = -(numBSPLeafs + 1);
233                         EmitLeaf (node->children[i]);
234                 }
235                 else
236                 {
237                         n->children[i] = numBSPNodes;   
238                         EmitDrawNode_r (node->children[i]);
239                         // n may have become invalid here, so...
240                         n = &bspNodes[ n0 ];
241                 }
242         }
243
244         return n - bspNodes;
245 }
246
247
248
249 /*
250 ============
251 SetModelNumbers
252 ============
253 */
254 void SetModelNumbers (void)
255 {
256         int             i;
257         int             models;
258         char    value[10];
259
260         models = 1;
261         for ( i=1 ; i<numEntities ; i++ ) {
262                 if ( entities[i].brushes || entities[i].patches ) {
263                         sprintf ( value, "*%i", models );
264                         models++;
265                         SetKeyValue (&entities[i], "model", value);
266                 }
267         }
268
269 }
270
271
272
273
274 /*
275 SetLightStyles()
276 sets style keys for entity lights
277 */
278
279 void SetLightStyles( void )
280 {
281         int                     i, j, style, numStyles;
282         qboolean        keepLights;
283         const char      *t;
284         entity_t        *e;
285         epair_t         *ep, *next;
286         char            value[ 10 ];
287         char            lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
288         int                     lightStyles[ MAX_SWITCHED_LIGHTS ];
289         
290         
291         /* ydnar: determine if we keep lights in the bsp */
292         t = ValueForKey( &entities[ 0 ], "_keepLights" );
293         keepLights = (t[ 0 ] == '1') ? qtrue : qfalse;
294         
295         /* any light that is controlled (has a targetname) must have a unique style number generated for it */
296         numStyles = 0;
297         for( i = 1; i < numEntities; i++ )
298         {
299                 e = &entities[ i ];
300
301                 t = ValueForKey( e, "classname" );
302                 if( Q_strncasecmp( t, "light", 5 ) )
303                         continue;
304                 t = ValueForKey( e, "targetname" );
305                 if( t[ 0 ] == '\0' )
306                 {
307                         /* ydnar: strip the light from the BSP file */
308                         if( keepLights == qfalse )
309                         {
310                                 ep = e->epairs;
311                                 while( ep != NULL )
312                                 {
313                                         next = ep->next;
314                                         free( ep->key );
315                                         free( ep->value );
316                                         free( ep );
317                                         ep = next;
318                                 }
319                                 e->epairs = NULL;
320                                 numStrippedLights++;
321                         }
322                         
323                         /* next light */
324                         continue;
325                 }
326                 
327                 /* get existing style */
328                 style = IntForKey( e, "style" );
329                 if( style < LS_NORMAL || style > LS_NONE )
330                         Error( "Invalid lightstyle (%d) on entity %d", style, i );
331                 
332                 /* find this targetname */
333                 for( j = 0; j < numStyles; j++ )
334                         if( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) )
335                                 break;
336                 
337                 /* add a new style */
338                 if( j >= numStyles )
339                 {
340                         if( numStyles == MAX_SWITCHED_LIGHTS )
341                                 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
342                         strcpy( lightTargets[ j ], t );
343                         lightStyles[ j ] = style;
344                         numStyles++;
345                 }
346                 
347                 /* set explicit style */
348                 sprintf( value, "%d", 32 + j );
349                 SetKeyValue( e, "style", value );
350                 
351                 /* set old style */
352                 if( style != LS_NORMAL )
353                 {
354                         sprintf( value, "%d", style );
355                         SetKeyValue( e, "switch_style", value );
356                 }
357         }
358         
359         /* emit some statistics */
360         Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
361 }
362
363
364
365 /*
366 BeginBSPFile()
367 starts a new bsp file
368 */
369
370 void BeginBSPFile( void )
371 {
372         /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
373         numBSPModels = 0;
374         numBSPNodes = 0;
375         numBSPBrushSides = 0;
376         numBSPLeafSurfaces = 0;
377         numBSPLeafBrushes = 0;
378         
379         /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
380         numBSPLeafs = 1;
381         
382         
383         /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
384         numBSPDrawIndexes = 6;
385         bspDrawIndexes[ 0 ] = 0;
386         bspDrawIndexes[ 1 ] = 1;
387         bspDrawIndexes[ 2 ] = 2;
388         bspDrawIndexes[ 3 ] = 0;
389         bspDrawIndexes[ 4 ] = 2;
390         bspDrawIndexes[ 5 ] = 3;
391 }
392
393
394
395 /*
396 EndBSPFile()
397 finishes a new bsp and writes to disk
398 */
399
400 void EndBSPFile( void )
401 {
402         char    path[ 1024 ];
403         
404
405         Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
406
407         EmitPlanes();
408         
409         numBSPEntities = numEntities;
410         UnparseEntities();
411         
412         /* write the surface extra file */
413         WriteSurfaceExtraFile( source );
414         
415         /* write the bsp */
416         sprintf( path, "%s.bsp", source );
417         Sys_Printf( "Writing %s\n", path );
418         WriteBSPFile( path );
419 }
420
421
422
423 /*
424 EmitBrushes()
425 writes the brush list to the bsp
426 */
427
428 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes )
429 {
430         int                             j;
431         brush_t                 *b;
432         bspBrush_t              *db;
433         bspBrushSide_t  *cp;
434         
435         
436         /* set initial brush */
437         if( firstBrush != NULL )
438                 *firstBrush = numBSPBrushes;
439         if( numBrushes != NULL )
440                 *numBrushes = 0;
441         
442         /* walk list of brushes */
443         for( b = brushes; b != NULL; b = b->next )
444         {
445                 /* check limits */
446                 AUTOEXPAND_BY_REALLOC_BSP(Brushes, 1024);
447                 
448                 /* get bsp brush */
449                 b->outputNum = numBSPBrushes;
450                 db = &bspBrushes[ numBSPBrushes ];
451                 numBSPBrushes++;
452                 if( numBrushes != NULL )
453                         (*numBrushes)++;
454                 
455                 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
456                 db->firstSide = numBSPBrushSides;
457                 
458                 /* walk sides */
459                 db->numSides = 0;
460                 for( j = 0; j < b->numsides; j++ )
461                 {
462                         /* set output number to bogus initially */
463                         b->sides[ j ].outputNum = -1;
464                         
465                         /* check count */
466                         AUTOEXPAND_BY_REALLOC_BSP(BrushSides, 1024);
467                         
468                         /* emit side */
469                         b->sides[ j ].outputNum = numBSPBrushSides;
470                         cp = &bspBrushSides[ numBSPBrushSides ];
471                         db->numSides++;
472                         numBSPBrushSides++;
473                         cp->planeNum = b->sides[ j ].planenum;
474                         
475                         /* emit shader */
476                         if( b->sides[ j ].shaderInfo )
477                                 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
478                         else
479                                 cp->shaderNum = EmitShader( NULL, NULL, NULL );
480                 }
481         }
482 }
483
484
485
486 /*
487 EmitFogs() - ydnar
488 turns map fogs into bsp fogs
489 */
490
491 void EmitFogs( void )
492 {
493         int                     i, j;
494         
495         
496         /* setup */
497         numBSPFogs = numMapFogs;
498         
499         /* walk list */
500         for( i = 0; i < numMapFogs; i++ )
501         {
502                 /* set shader */
503                 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
504                 
505                 /* global fog doesn't have an associated brush */
506                 if( mapFogs[ i ].brush == NULL )
507                 {
508                         bspFogs[ i ].brushNum = -1;
509                         bspFogs[ i ].visibleSide = -1;
510                 }
511                 else
512                 {
513                         /* set brush */
514                         bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
515                         
516                         /* try to use forced visible side */
517                         if( mapFogs[ i ].visibleSide >= 0 )
518                         {
519                                 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
520                                 continue;
521                         }
522                         
523                         /* find visible side */
524                         for( j = 0; j < 6; j++ )
525                         {
526                                 if( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL )
527                                 {
528                                         Sys_Printf( "Fog %d has visible side %d\n", i, j );
529                                         bspFogs[ i ].visibleSide = j;
530                                         break;
531                                 }
532                         }
533                 }
534         }
535 }
536
537
538
539 /*
540 BeginModel()
541 sets up a new brush model
542 */
543
544 void BeginModel( void )
545 {
546         bspModel_t      *mod;
547         brush_t         *b;
548         entity_t        *e;
549         vec3_t          mins, maxs;
550         vec3_t          lgMins, lgMaxs;         /* ydnar: lightgrid mins/maxs */
551         parseMesh_t     *p;
552         int                     i;
553         
554         
555         /* test limits */
556         AUTOEXPAND_BY_REALLOC_BSP(Models, 256);
557         
558         /* get model and entity */
559         mod = &bspModels[ numBSPModels ];
560         e = &entities[ mapEntityNum ];
561         
562         /* ydnar: lightgrid mins/maxs */
563         ClearBounds( lgMins, lgMaxs );
564         
565         /* bound the brushes */
566         ClearBounds( mins, maxs );
567         for ( b = e->brushes; b; b = b->next )
568         {
569                 /* ignore non-real brushes (origin, etc) */
570                 if( b->numsides == 0 )
571                         continue;
572                 AddPointToBounds( b->mins, mins, maxs );
573                 AddPointToBounds( b->maxs, mins, maxs );
574                 
575                 /* ydnar: lightgrid bounds */
576                 if( b->compileFlags & C_LIGHTGRID )
577                 {
578                         AddPointToBounds( b->mins, lgMins, lgMaxs );
579                         AddPointToBounds( b->maxs, lgMins, lgMaxs );
580                 }
581         }
582         
583         /* bound patches */
584         for( p = e->patches; p; p = p->next )
585         {
586                 for( i = 0; i < (p->mesh.width * p->mesh.height); i++ )
587                         AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
588         }
589         
590         /* ydnar: lightgrid mins/maxs */
591         if( lgMins[ 0 ] < 99999 )
592         {
593                 /* use lightgrid bounds */
594                 VectorCopy( lgMins, mod->mins );
595                 VectorCopy( lgMaxs, mod->maxs );
596         }
597         else
598         {
599                 /* use brush/patch bounds */
600                 VectorCopy( mins, mod->mins );
601                 VectorCopy( maxs, mod->maxs );
602         }
603         
604         /* note size */
605         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 ] );
606         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 ] );
607         
608         /* set firsts */
609         mod->firstBSPSurface = numBSPDrawSurfaces;
610         mod->firstBSPBrush = numBSPBrushes;
611 }
612
613
614
615
616 /*
617 EndModel()
618 finish a model's processing
619 */
620
621 void EndModel( entity_t *e, node_t *headnode )
622 {
623         bspModel_t      *mod;
624         
625         
626         /* note it */
627         Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
628         
629         /* emit the bsp */
630         mod = &bspModels[ numBSPModels ];
631         EmitDrawNode_r( headnode );
632         
633         /* set surfaces and brushes */
634         mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
635         mod->firstBSPBrush = e->firstBrush;
636         mod->numBSPBrushes = e->numBrushes;
637         
638         /* increment model count */
639         numBSPModels++;
640 }
641
642
643