unlimit MAX_MAP_DRAW_INDEXES
[xonotic/netradiant.git] / tools / quake3 / q3map2 / q3map2.h
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 #ifndef Q3MAP2_H
33 #define Q3MAP2_H
34
35
36
37 /* version */
38 #ifndef Q3MAP_VERSION
39 #error no Q3MAP_VERSION defined
40 #endif
41 #define Q3MAP_MOTD              "Your map saw the pretty lights from q3map2's BFG"
42
43
44
45
46 /* -------------------------------------------------------------------------------
47
48 dependencies
49
50 ------------------------------------------------------------------------------- */
51
52 /* platform-specific */
53 #if defined( __linux__ ) || defined( __APPLE__ )
54         #define Q_UNIX
55 #endif
56
57 #ifdef Q_UNIX
58         #include <unistd.h>
59         #include <pwd.h>
60         #include <limits.h>
61 #endif
62
63 #ifdef WIN32
64         #include <windows.h>
65 #endif
66
67
68 /* general */
69 #include "version.h"                    /* ttimo: might want to guard that if built outside of the GtkRadiant tree */
70
71 #include "cmdlib.h"
72 #include "mathlib.h"
73 #include "md5lib.h"
74 #include "ddslib.h"
75
76 #include "picomodel.h"
77
78 #include "scriplib.h"
79 #include "polylib.h"
80 #include "imagelib.h"
81 #include "qthreads.h"
82 #include "inout.h"
83 #include "vfs.h"
84 #include "png.h"
85 #include "md4.h"
86 #include "radiant_jpeglib.h"
87 #include <stdlib.h>
88
89
90
91 /* -------------------------------------------------------------------------------
92
93 port-related hacks
94
95 ------------------------------------------------------------------------------- */
96
97 #define MAC_STATIC_HACK                 0
98 #if defined( __APPLE__ ) && MAC_STATIC_HACK 
99         #define MAC_STATIC                      static
100 #else
101         #define MAC_STATIC                      
102 #endif
103
104 #if 1
105         #ifdef WIN32
106                 #define Q_stricmp                       stricmp
107                 #define Q_strncasecmp           strnicmp
108         #else
109                 #define Q_stricmp                       strcasecmp
110                 #define Q_strncasecmp           strncasecmp
111         #endif
112 #endif
113
114 /* macro version */
115 #define VectorMA( a, s, b, c )  ((c)[ 0 ] = (a)[ 0 ] + (s) * (b)[ 0 ], (c)[ 1 ] = (a)[ 1 ] + (s) * (b)[ 1 ], (c)[ 2 ] = (a)[ 2 ] + (s) * (b)[ 2 ])
116
117
118
119 /* -------------------------------------------------------------------------------
120
121 constants
122
123 ------------------------------------------------------------------------------- */
124
125 /* temporary hacks and tests (please keep off in SVN to prevent anyone's legacy map from screwing up) */
126 /* 2011-01-10 TTimo says we should turn these on in SVN, so turning on now */
127 #define Q3MAP2_EXPERIMENTAL_HIGH_PRECISION_MATH_FIXES   1
128 #define Q3MAP2_EXPERIMENTAL_SNAP_NORMAL_FIX             1
129 #define Q3MAP2_EXPERIMENTAL_SNAP_PLANE_FIX              1
130
131 /* general */
132 #define MAX_QPATH                               64
133
134 #define MAX_IMAGES                              512
135 #define DEFAULT_IMAGE                   "*default"
136
137 #define MAX_MODELS                              512
138
139 #define DEF_BACKSPLASH_FRACTION 0.05f   /* 5% backsplash by default */
140 #define DEF_BACKSPLASH_DISTANCE 23
141
142 #define DEF_RADIOSITY_BOUNCE    1.0f    /* ydnar: default to 100% re-emitted light */
143
144 #define MAX_SHADER_INFO                 8192
145 #define MAX_CUST_SURFACEPARMS   256
146
147 #define SHADER_MAX_VERTEXES             1000
148 #define SHADER_MAX_INDEXES              (6 * SHADER_MAX_VERTEXES)
149
150 #define MAX_JITTERS                             256
151
152
153 /* epair parsing (note case-sensitivity directive) */
154 #define CASE_INSENSITIVE_EPAIRS 1
155
156 #if CASE_INSENSITIVE_EPAIRS
157         #define EPAIR_STRCMP            Q_stricmp
158 #else
159         #define EPAIR_STRCMP            strcmp
160 #endif
161
162
163 /* ydnar: compiler flags, because games have widely varying content/surface flags */
164 #define C_SOLID                                 0x00000001
165 #define C_TRANSLUCENT                   0x00000002
166 #define C_STRUCTURAL                    0x00000004
167 #define C_HINT                                  0x00000008
168 #define C_NODRAW                                0x00000010
169 #define C_LIGHTGRID                             0x00000020
170 #define C_ALPHASHADOW                   0x00000040
171 #define C_LIGHTFILTER                   0x00000080
172 #define C_VERTEXLIT                             0x00000100
173 #define C_LIQUID                                0x00000200
174 #define C_FOG                                   0x00000400
175 #define C_SKY                                   0x00000800
176 #define C_ORIGIN                                0x00001000
177 #define C_AREAPORTAL                    0x00002000
178 #define C_ANTIPORTAL                    0x00004000      /* like hint, but doesn't generate portals */
179 #define C_SKIP                                  0x00008000      /* like hint, but skips this face (doesn't split bsp) */
180 #define C_NOMARKS                               0x00010000      /* no decals */
181 #define C_DETAIL                                0x08000000      /* THIS MUST BE THE SAME AS IN RADIANT! */
182
183
184 /* shadow flags */
185 #define WORLDSPAWN_CAST_SHADOWS 1
186 #define WORLDSPAWN_RECV_SHADOWS 1
187 #define ENTITY_CAST_SHADOWS             0
188 #define ENTITY_RECV_SHADOWS             1
189
190
191 /* bsp */
192 #define MAX_PATCH_SIZE                  32
193 #define MAX_BRUSH_SIDES                 1024
194 #define MAX_BUILD_SIDES                 1024
195
196 #define MAX_EXPANDED_AXIS               128
197
198 #define CLIP_EPSILON                    0.1f
199 #define PLANESIDE_EPSILON               0.001f
200 #define PLANENUM_LEAF                   -1
201
202 #define HINT_PRIORITY                   1000            /* ydnar: force hint splits first and antiportal/areaportal splits last */
203 #define ANTIPORTAL_PRIORITY             -1000
204 #define AREAPORTAL_PRIORITY             -1000
205 #define DETAIL_PRIORITY         -3000
206
207 #define PSIDE_FRONT                             1
208 #define PSIDE_BACK                              2
209 #define PSIDE_BOTH                              (PSIDE_FRONT | PSIDE_BACK)
210 #define PSIDE_FACING                    4
211
212 #define BPRIMIT_UNDEFINED               0
213 #define BPRIMIT_OLDBRUSHES              1
214 #define BPRIMIT_NEWBRUSHES              2
215
216
217 /* vis */
218 #define VIS_HEADER_SIZE                 8
219
220 #define SEPERATORCACHE                  /* seperator caching helps a bit */
221
222 #define PORTALFILE                              "PRT1"
223
224 #define MAX_PORTALS                             0x20000 /* same as MAX_MAP_PORTALS */
225 #define MAX_SEPERATORS                  MAX_POINTS_ON_WINDING
226 #define MAX_POINTS_ON_FIXED_WINDING     24      /* ydnar: increased this from 12 at the expense of more memory */
227 #define MAX_PORTALS_ON_LEAF             1024
228
229
230 /* light */
231 #define EMIT_POINT                              0
232 #define EMIT_AREA                               1
233 #define EMIT_SPOT                               2
234 #define EMIT_SUN                                3
235
236 #define LIGHT_ATTEN_LINEAR              1
237 #define LIGHT_ATTEN_ANGLE               2
238 #define LIGHT_ATTEN_DISTANCE    4
239 #define LIGHT_TWOSIDED                  8
240 #define LIGHT_GRID                              16
241 #define LIGHT_SURFACES                  32
242 #define LIGHT_DARK                              64              /* probably never use this */
243 #define LIGHT_FAST                              256
244 #define LIGHT_FAST_TEMP                 512
245 #define LIGHT_FAST_ACTUAL               (LIGHT_FAST | LIGHT_FAST_TEMP)
246 #define LIGHT_NEGATIVE                  1024
247 #define LIGHT_UNNORMALIZED              2048    /* vortex: do not normalize _color */
248
249 #define LIGHT_SUN_DEFAULT               (LIGHT_ATTEN_ANGLE | LIGHT_GRID | LIGHT_SURFACES)
250 #define LIGHT_AREA_DEFAULT              (LIGHT_ATTEN_ANGLE | LIGHT_ATTEN_DISTANCE | LIGHT_GRID | LIGHT_SURFACES)        /* q3a and wolf are the same */
251 #define LIGHT_Q3A_DEFAULT               (LIGHT_ATTEN_ANGLE | LIGHT_ATTEN_DISTANCE | LIGHT_GRID | LIGHT_SURFACES | LIGHT_FAST)
252 #define LIGHT_WOLF_DEFAULT              (LIGHT_ATTEN_LINEAR | LIGHT_ATTEN_DISTANCE | LIGHT_GRID | LIGHT_SURFACES | LIGHT_FAST)
253
254 #define MAX_TRACE_TEST_NODES    256
255 #define DEFAULT_INHIBIT_RADIUS  1.5f
256
257 #define LUXEL_EPSILON                   0.125f
258 #define VERTEX_EPSILON                  -0.125f
259 #define GRID_EPSILON                    0.0f
260
261 #define DEFAULT_LIGHTMAP_SAMPLE_SIZE    16
262 #define DEFAULT_LIGHTMAP_MIN_SAMPLE_SIZE        0
263 #define DEFAULT_LIGHTMAP_SAMPLE_OFFSET  1.0f
264 #define DEFAULT_SUBDIVIDE_THRESHOLD             1.0f
265
266 #define EXTRA_SCALE                             2       /* -extrawide = -super 2 */
267 #define EXTRAWIDE_SCALE                 2       /* -extrawide = -super 2 -filter */
268
269 #define CLUSTER_UNMAPPED                -1
270 #define CLUSTER_OCCLUDED                -2
271 #define CLUSTER_FLOODED                 -3
272
273 #define VERTEX_LUXEL_SIZE               3
274 #define BSP_LUXEL_SIZE                  3
275 #define RAD_LUXEL_SIZE                  3
276 #define SUPER_LUXEL_SIZE                4
277 #define SUPER_FLAG_SIZE                 4
278 #define FLAG_FORCE_SUBSAMPLING 1
279 #define FLAG_ALREADY_SUBSAMPLED 2
280 #define SUPER_ORIGIN_SIZE               3
281 #define SUPER_NORMAL_SIZE               4
282 #define SUPER_DELUXEL_SIZE              3
283 #define BSP_DELUXEL_SIZE                3
284 #define SUPER_FLOODLIGHT_SIZE   4
285
286 #define VERTEX_LUXEL( s, v )    (vertexLuxels[ s ] + ((v) * VERTEX_LUXEL_SIZE))
287 #define RAD_VERTEX_LUXEL( s, v )(radVertexLuxels[ s ] + ((v) * VERTEX_LUXEL_SIZE))
288 #define BSP_LUXEL( s, x, y )    (lm->bspLuxels[ s ] + ((((y) * lm->w) + (x)) * BSP_LUXEL_SIZE))
289 #define RAD_LUXEL( s, x, y )    (lm->radLuxels[ s ] + ((((y) * lm->w) + (x)) * RAD_LUXEL_SIZE))
290 #define SUPER_LUXEL( s, x, y )  (lm->superLuxels[ s ] + ((((y) * lm->sw) + (x)) * SUPER_LUXEL_SIZE))
291 #define SUPER_FLAG( x, y )      (lm->superFlags + ((((y) * lm->sw) + (x)) * SUPER_FLAG_SIZE))
292 #define SUPER_DELUXEL( x, y )   (lm->superDeluxels + ((((y) * lm->sw) + (x)) * SUPER_DELUXEL_SIZE))
293 #define BSP_DELUXEL( x, y )             (lm->bspDeluxels + ((((y) * lm->w) + (x)) * BSP_DELUXEL_SIZE))
294 #define SUPER_CLUSTER( x, y )   (lm->superClusters + (((y) * lm->sw) + (x)))
295 #define SUPER_ORIGIN( x, y )    (lm->superOrigins + ((((y) * lm->sw) + (x)) * SUPER_ORIGIN_SIZE))
296 #define SUPER_NORMAL( x, y )    (lm->superNormals + ((((y) * lm->sw) + (x)) * SUPER_NORMAL_SIZE))
297 #define SUPER_DIRT( x, y )              (lm->superNormals + ((((y) * lm->sw) + (x)) * SUPER_NORMAL_SIZE) + 3)   /* stash dirtyness in normal[ 3 ] */
298 #define SUPER_FLOODLIGHT( x, y )        (lm->superFloodLight + ((((y) * lm->sw) + (x)) * SUPER_FLOODLIGHT_SIZE) )
299
300
301
302 /* -------------------------------------------------------------------------------
303
304 abstracted bsp file
305
306 ------------------------------------------------------------------------------- */
307
308 #define EXTERNAL_LIGHTMAP               "lm_%04d.tga"
309
310 #define MAX_LIGHTMAPS                   4                       /* RBSP */
311 #define MAX_LIGHT_STYLES                64
312 #define MAX_SWITCHED_LIGHTS             32
313 #define LS_NORMAL                               0x00
314 #define LS_UNUSED                               0xFE
315 #define LS_NONE                                 0xFF
316
317 #define MAX_LIGHTMAP_SHADERS    256
318
319 /* ok to increase these at the expense of more memory */
320 #define MAX_MAP_AREAS                   0x100           /* MAX_MAP_AREA_BYTES in q_shared must match! */
321 #define MAX_MAP_FOGS                    30                      //& 0x100       /* RBSP (32 - world fog - goggles) */
322 #define MAX_MAP_LEAFS                   0x20000
323 #define MAX_MAP_PORTALS                 0x20000
324 #define MAX_MAP_LIGHTING                0x800000
325 #define MAX_MAP_LIGHTGRID               0x100000        //%     0x800000 /* ydnar: set to points, not bytes */
326 #define MAX_MAP_VISCLUSTERS     0x4000 // <= MAX_MAP_LEAFS
327 #define MAX_MAP_VISIBILITY              (VIS_HEADER_SIZE + MAX_MAP_VISCLUSTERS * (((MAX_MAP_VISCLUSTERS + 63) & ~63) >> 3))
328
329 #define MAX_MAP_DRAW_SURFS              0x20000
330
331 #define MAX_MAP_ADVERTISEMENTS  30
332
333 /* key / value pair sizes in the entities lump */
334 #define MAX_KEY                                 32
335 #define MAX_VALUE                               1024
336
337 /* the editor uses these predefined yaw angles to orient entities up or down */
338 #define ANGLE_UP                                -1
339 #define ANGLE_DOWN                              -2
340
341 #define LIGHTMAP_WIDTH                  128
342 #define LIGHTMAP_HEIGHT                 128
343
344 #define MIN_WORLD_COORD                 (-65536)
345 #define MAX_WORLD_COORD                 (65536)
346 #define WORLD_SIZE                              (MAX_WORLD_COORD - MIN_WORLD_COORD)
347
348
349 typedef void                                    (*bspFunc)( const char * );
350
351
352 typedef struct
353 {
354         int                     offset, length;
355 }
356 bspLump_t;
357
358
359 typedef struct
360 {
361         char            ident[ 4 ];
362         int                     version;
363         
364         bspLump_t       lumps[ 100 ];   /* theoretical maximum # of bsp lumps */
365 }
366 bspHeader_t;
367
368
369 typedef struct
370 {
371         float           mins[ 3 ], maxs[ 3 ];
372         int                     firstBSPSurface, numBSPSurfaces;
373         int                     firstBSPBrush, numBSPBrushes;
374 }
375 bspModel_t;
376
377
378 typedef struct
379 {
380         char            shader[ MAX_QPATH ];
381         int                     surfaceFlags;
382         int                     contentFlags;
383 }
384 bspShader_t;
385
386
387 /* planes x^1 is allways the opposite of plane x */
388
389 typedef struct
390 {
391         float           normal[ 3 ];
392         float           dist;
393 }
394 bspPlane_t;
395
396
397 typedef struct
398 {
399         int                     planeNum;
400         int                     children[ 2 ];          /* negative numbers are -(leafs+1), not nodes */
401         int                     mins[ 3 ];                      /* for frustom culling */
402         int                     maxs[ 3 ];
403 }
404 bspNode_t;
405
406
407 typedef struct
408 {
409         int                     cluster;                        /* -1 = opaque cluster (do I still store these?) */
410         int                     area;
411         
412         int                     mins[ 3 ];                      /* for frustum culling */
413         int                     maxs[ 3 ];
414         
415         int                     firstBSPLeafSurface;
416         int                     numBSPLeafSurfaces;
417         
418         int                     firstBSPLeafBrush;
419         int                     numBSPLeafBrushes;
420 }
421 bspLeaf_t;
422
423
424 typedef struct
425 {
426         int                     planeNum;                       /* positive plane side faces out of the leaf */
427         int                     shaderNum;
428         int                     surfaceNum;                     /* RBSP */
429 }
430 bspBrushSide_t;
431
432
433 typedef struct
434 {
435         int                     firstSide;
436         int                     numSides;
437         int                     shaderNum;                      /* the shader that determines the content flags */
438 }
439 bspBrush_t;
440
441
442 typedef struct
443 {
444         char            shader[ MAX_QPATH ];
445         int                     brushNum;
446         int                     visibleSide;            /* the brush side that ray tests need to clip against (-1 == none) */
447 }
448 bspFog_t;
449
450
451 typedef struct
452 {
453         vec3_t          xyz;
454         float           st[ 2 ];
455         float           lightmap[ MAX_LIGHTMAPS ][ 2 ]; /* RBSP */
456         vec3_t          normal;
457         byte            color[ MAX_LIGHTMAPS ][ 4 ];    /* RBSP */
458 }
459 bspDrawVert_t;
460
461
462 typedef enum
463 {
464         MST_BAD,
465         MST_PLANAR,
466         MST_PATCH,
467         MST_TRIANGLE_SOUP,
468         MST_FLARE,
469         MST_FOLIAGE
470 }
471 bspSurfaceType_t;
472
473
474 typedef struct bspGridPoint_s
475 {
476         byte            ambient[ MAX_LIGHTMAPS ][ 3 ];
477         byte            directed[ MAX_LIGHTMAPS ][ 3 ];
478         byte            styles[ MAX_LIGHTMAPS ];
479         byte            latLong[ 2 ];
480 }
481 bspGridPoint_t;
482
483
484 typedef struct
485 {
486         int                     shaderNum;
487         int                     fogNum;
488         int                     surfaceType;
489         
490         int                     firstVert;
491         int                     numVerts;
492         
493         int                     firstIndex;
494         int                     numIndexes;
495         
496         byte            lightmapStyles[ MAX_LIGHTMAPS ];                                                /* RBSP */
497         byte            vertexStyles[ MAX_LIGHTMAPS ];                                                  /* RBSP */
498         int                     lightmapNum[ MAX_LIGHTMAPS ];                                                   /* RBSP */
499         int                     lightmapX[ MAX_LIGHTMAPS ], lightmapY[ MAX_LIGHTMAPS ]; /* RBSP */
500         int                     lightmapWidth, lightmapHeight;
501         
502         vec3_t          lightmapOrigin;
503         vec3_t          lightmapVecs[ 3 ];      /* on patches, [ 0 ] and [ 1 ] are lodbounds */
504         
505         int                     patchWidth;
506         int                     patchHeight;
507 }
508 bspDrawSurface_t;
509
510
511 /* advertisements */
512 typedef struct {
513         int                     cellId;
514         vec3_t          normal;
515         vec3_t          rect[4];
516         char            model[ MAX_QPATH ];
517 } bspAdvertisement_t;
518
519
520 /* -------------------------------------------------------------------------------
521
522 general types
523
524 ------------------------------------------------------------------------------- */
525
526 /* ydnar: for smaller structs */
527 typedef unsigned char   qb_t;
528
529
530 /* ydnar: for q3map_tcMod */
531 typedef float   tcMod_t[ 3 ][ 3 ];
532
533
534 /* ydnar: for multiple game support */
535 typedef struct surfaceParm_s
536 {
537         char            *name;
538         int                     contentFlags, contentFlagsClear;
539         int                     surfaceFlags, surfaceFlagsClear;
540         int                     compileFlags, compileFlagsClear;
541 }
542 surfaceParm_t;
543
544 typedef enum
545 {
546         MINIMAP_MODE_GRAY,
547         MINIMAP_MODE_BLACK,
548         MINIMAP_MODE_WHITE
549 }
550 miniMapMode_t;
551
552 typedef struct game_s
553 {
554         char                            *arg;                                                   /* -game matches this */
555         char                            *gamePath;                                              /* main game data dir */
556         char                            *homeBasePath;                                  /* home sub-dir on unix */
557         char                            *magic;                                                 /* magic word for figuring out base path */
558         char                            *shaderPath;                                    /* shader directory */
559         int                                     maxLMSurfaceVerts;                              /* default maximum meta surface verts */
560         int                                     maxSurfaceVerts;                                /* default maximum surface verts */
561         int                                     maxSurfaceIndexes;                              /* default maximum surface indexes (tris * 3) */
562         qboolean                        emitFlares;                                             /* when true, emit flare surfaces */
563         char                            *flareShader;                                   /* default flare shader (MUST BE SET) */
564         qboolean                        wolfLight;                                              /* when true, lights work like wolf q3map  */
565         int                                     lightmapSize;                                   /* bsp lightmap width/height */
566         float                           lightmapGamma;                                  /* default lightmap gamma */
567         qboolean                        lightmapsRGB;                                   /* default lightmap sRGB mode */
568         qboolean                        texturesRGB;                                    /* default texture sRGB mode */
569         qboolean                        colorsRGB;                                      /* default color sRGB mode */
570         float                           lightmapExposure;                               /* default lightmap exposure */
571         float                           lightmapCompensate;                             /* default lightmap compensate value */
572         float                           gridScale;                                              /* vortex: default lightgrid scale (affects both directional and ambient spectres) */
573         float                           gridAmbientScale;                               /* vortex: default lightgrid ambient spectre scale */
574         qboolean                        lightAngleHL;                                   /* jal: use half-lambert curve for light angle attenuation */
575         qboolean                        noStyles;                                               /* use lightstyles hack or not */
576         qboolean                        keepLights;                                             /* keep light entities on bsp */
577         int                                     patchSubdivisions;                              /* default patch subdivisions tolerance */
578         qboolean                        patchShadows;                                   /* patch casting enabled */
579         qboolean                        deluxeMap;                                              /* compile deluxemaps */
580         int                                     deluxeMode;                                             /* deluxemap mode (0 - modelspace, 1 - tangentspace with renormalization, 2 - tangentspace without renormalization) */
581         int                 miniMapSize;                    /* minimap size */
582         float               miniMapSharpen;                 /* minimap sharpening coefficient */
583         float               miniMapBorder;                  /* minimap border amount */
584         qboolean            miniMapKeepAspect;              /* minimap keep aspect ratio by letterboxing */
585         miniMapMode_t       miniMapMode;                    /* minimap mode */
586         char                *miniMapNameFormat;             /* minimap name format */
587         char                            *bspIdent;                                              /* 4-letter bsp file prefix */
588         int                                     bspVersion;                                             /* bsp version to use */
589         qboolean                        lumpSwap;                                               /* cod-style len/ofs order */
590         bspFunc                         load, write;                                    /* load/write function pointers */
591         surfaceParm_t           surfaceParms[ 128 ];                    /* surfaceparm array */
592 }
593 game_t;
594
595
596 typedef struct image_s
597 {
598         char                            *name, *filename;
599         int                                     refCount;
600         int                                     width, height;
601         byte                            *pixels;
602 }
603 image_t;
604
605
606 typedef struct sun_s
607 {
608         struct sun_s            *next;
609         vec3_t                          direction, color;
610         float                           photons, deviance, filterRadius;
611         int                                     numSamples, style;
612 }
613 sun_t;
614
615
616 typedef struct surfaceModel_s 
617 {
618         struct surfaceModel_s   *next;
619         char                            model[ MAX_QPATH ];
620         float                           density, odds;
621         float                           minScale, maxScale;
622         float                           minAngle, maxAngle;
623         qboolean                        oriented;
624 }
625 surfaceModel_t;
626
627
628 /* ydnar/sd: foliage stuff for wolf et (engine-supported optimization of the above) */
629 typedef struct foliage_s 
630 {
631         struct foliage_s        *next;
632         char                            model[ MAX_QPATH ];
633         float                           scale, density, odds;
634         qboolean                        inverseAlpha;
635 }
636 foliage_t;
637
638 typedef struct foliageInstance_s
639 {
640         vec3_t                          xyz, normal;
641 }
642 foliageInstance_t;
643
644
645 typedef struct remap_s
646 {
647         struct remap_s          *next;
648         char                            from[ 1024 ];
649         char                            to[ MAX_QPATH ];
650 }
651 remap_t;
652
653 typedef struct skinfile_s
654 {
655         struct skinfile_s       *next;
656         char                            name[ 1024 ];
657         char                            to[ MAX_QPATH ];
658 }
659 skinfile_t;
660
661
662 /* wingdi.h hack, it's the same: 0 */
663 #undef CM_NONE
664
665 typedef enum
666 {
667         CM_NONE,
668         CM_VOLUME,
669         CM_COLOR_SET,
670         CM_ALPHA_SET,
671         CM_COLOR_SCALE,
672         CM_ALPHA_SCALE,
673         CM_COLOR_DOT_PRODUCT,
674         CM_ALPHA_DOT_PRODUCT,
675         CM_COLOR_DOT_PRODUCT_SCALE,
676         CM_ALPHA_DOT_PRODUCT_SCALE,
677         CM_COLOR_DOT_PRODUCT_2,
678         CM_ALPHA_DOT_PRODUCT_2,
679         CM_COLOR_DOT_PRODUCT_2_SCALE,
680         CM_ALPHA_DOT_PRODUCT_2_SCALE
681 }
682 colorModType_t;
683
684
685 typedef struct colorMod_s
686 {
687         struct colorMod_s       *next;
688         colorModType_t          type;
689         vec_t                           data[ 16 ];
690 }
691 colorMod_t;
692
693
694 typedef enum
695 {
696         IM_NONE,
697         IM_OPAQUE,
698         IM_MASKED,
699         IM_BLEND
700 }
701 implicitMap_t;
702
703
704 typedef struct shaderInfo_s
705 {
706         char                            shader[ MAX_QPATH ];
707         int                                     surfaceFlags;
708         int                                     contentFlags;
709         int                                     compileFlags;
710         float                           value;                                                  /* light value */
711         
712         char                            *flareShader;                                   /* for light flares */
713         char                            *damageShader;                                  /* ydnar: sof2 damage shader name */
714         char                            *backShader;                                    /* for surfaces that generate different front and back passes */
715         char                            *cloneShader;                                   /* ydnar: for cloning of a surface */
716         char                            *remapShader;                                   /* ydnar: remap a shader in final stage */
717         char                            *deprecateShader;                               /* vortex: shader is deprecated and replaced by this on use */
718
719         surfaceModel_t          *surfaceModel;                                  /* ydnar: for distribution of models */
720         foliage_t                       *foliage;                                               /* ydnar/splash damage: wolf et foliage */
721         
722         float                           subdivisions;                                   /* from a "tesssize xxx" */
723         float                           backsplashFraction;                             /* floating point value, usually 0.05 */
724         float                           backsplashDistance;                             /* default 16 */
725         float                           lightSubdivide;                                 /* default 999 */
726         float                           lightFilterRadius;                              /* ydnar: lightmap filtering/blurring radius for lights created by this shader (default: 0) */
727         
728         int                                     lightmapSampleSize;                             /* lightmap sample size */
729         float                           lightmapSampleOffset;                   /* ydnar: lightmap sample offset (default: 1.0) */
730         
731         float                           bounceScale;                                    /* ydnar: radiosity re-emission [0,1.0+] */
732         float                           offset;                                                 /* ydnar: offset in units */
733         float                           shadeAngleDegrees;                              /* ydnar: breaking angle for smooth shading (degrees) */
734         
735         vec3_t                          mins, maxs;                                             /* ydnar: for particle studio vertexDeform move support */
736         
737         qb_t                            legacyTerrain;                                  /* ydnar: enable legacy terrain crutches */
738         qb_t                            indexed;                                                /* ydnar: attempt to use indexmap (terrain alphamap style) */
739         qb_t                            forceMeta;                                              /* ydnar: force metasurface path */
740         qb_t                            noClip;                                                 /* ydnar: don't clip into bsp, preserve original face winding */
741         qb_t                            noFast;                                                 /* ydnar: supress fast lighting for surfaces with this shader */
742         qb_t                            invert;                                                 /* ydnar: reverse facing */
743         qb_t                            nonplanar;                                              /* ydnar: for nonplanar meta surface merging */
744         qb_t                            tcGen;                                                  /* ydnar: has explicit texcoord generation */
745         vec3_t                          vecs[ 2 ];                                              /* ydnar: explicit texture vectors for [0,1] texture space */
746         tcMod_t                         mod;                                                    /* ydnar: q3map_tcMod matrix for djbob :) */
747         vec3_t                          lightmapAxis;                                   /* ydnar: explicit lightmap axis projection */
748         colorMod_t                      *colorMod;                                              /* ydnar: q3map_rgb/color/alpha/Set/Mod support */
749         
750         int                                     furNumLayers;                                   /* ydnar: number of fur layers */
751         float                           furOffset;                                              /* ydnar: offset of each layer */
752         float                           furFade;                                                /* ydnar: alpha fade amount per layer */
753
754         qb_t                            splotchFix;                                             /* ydnar: filter splotches on lightmaps */
755         
756         qb_t                            hasPasses;                                              /* false if the shader doesn't define any rendering passes */
757         qb_t                            globalTexture;                                  /* don't normalize texture repeats */
758         qb_t                            twoSided;                                               /* cull none */
759         qb_t                            autosprite;                                             /* autosprite shaders will become point lights instead of area lights */
760         qb_t                            polygonOffset;                                  /* ydnar: don't face cull this or against this */
761         qb_t                            patchShadows;                                   /* have patches casting shadows when using -light for this surface */
762         qb_t                            vertexShadows;                                  /* shadows will be casted at this surface even when vertex lit */
763         qb_t                            forceSunlight;                                  /* force sun light at this surface even tho we might not calculate shadows in vertex lighting */
764         qb_t                            notjunc;                                                /* don't use this surface for tjunction fixing */
765         qb_t                            fogParms;                                               /* ydnar: has fogparms */
766         qb_t                            noFog;                                                  /* ydnar: supress fogging */
767         qb_t                            clipModel;                                              /* ydnar: solid model hack */
768         qb_t                            noVertexLight;                                  /* ydnar: leave vertex color alone */
769         qb_t                            noDirty;                                                /* jal: do not apply the dirty pass to this surface */
770
771         byte                            styleMarker;                                    /* ydnar: light styles hack */
772         
773         float                           vertexScale;                                    /* vertex light scale */
774         
775         char                            skyParmsImageBase[ MAX_QPATH ]; /* ydnar: for skies */
776         
777         char                            editorImagePath[ MAX_QPATH ];   /* use this image to generate texture coordinates */
778         char                            lightImagePath[ MAX_QPATH ];    /* use this image to generate color / averageColor */
779         char                            normalImagePath[ MAX_QPATH ];   /* ydnar: normalmap image for bumpmapping */
780         
781         implicitMap_t           implicitMap;                                    /* ydnar: enemy territory implicit shaders */
782         char                            implicitImagePath[ MAX_QPATH ];
783         
784         image_t                         *shaderImage;
785         image_t                         *lightImage;
786         image_t                         *normalImage;
787         
788         float                           skyLightValue;                                  /* ydnar */
789         int                                     skyLightIterations;                             /* ydnar */
790         sun_t                           *sun;                                                   /* ydnar */
791         
792         vec3_t                          color;                                                  /* normalized color */
793         vec3_t                          averageColor;
794         byte                            lightStyle;                                     
795
796         /* vortex: per-surface floodlight */
797         float                           floodlightDirectionScale;
798         vec3_t                          floodlightRGB; 
799         float                           floodlightIntensity;
800         float                           floodlightDistance;
801         
802         qb_t                            lmMergable;                                             /* ydnar */
803         int                                     lmCustomWidth, lmCustomHeight;  /* ydnar */
804         float                           lmBrightness;                                   /* ydnar */
805         float                           lmFilterRadius;                                 /* ydnar: lightmap filtering/blurring radius for this shader (default: 0) */
806         
807         int                                     shaderWidth, shaderHeight;              /* ydnar */
808         float                           stFlat[ 2 ];
809         
810         vec3_t                          fogDir;                                                 /* ydnar */
811         
812         char                            *shaderText;                                    /* ydnar */
813         qb_t                            custom;
814         qb_t                            finished;
815 }
816 shaderInfo_t;
817
818
819
820 /* -------------------------------------------------------------------------------
821
822 bsp structures
823
824 ------------------------------------------------------------------------------- */
825
826 typedef struct face_s
827 {
828         struct face_s           *next;
829         int                                     planenum;
830         int                                     priority;
831         //qboolean                      checked;
832         int                                     compileFlags;
833         winding_t                       *w;
834 }
835 face_t;
836
837
838 typedef struct plane_s
839 {
840         vec3_t                          normal;
841         vec_t                           dist;
842         int                                     type;
843         int                 counter;
844         int                                     hash_chain;
845 }
846 plane_t;
847
848
849 typedef struct side_s
850 {
851         int                                     planenum;
852         
853         int                                     outputNum;                      /* set when the side is written to the file list */
854         
855         float                           texMat[ 2 ][ 3 ];       /* brush primitive texture matrix */
856         float                           vecs[ 2 ][ 4 ];         /* old-style texture coordinate mapping */
857
858         winding_t                       *winding;
859         winding_t                       *visibleHull;           /* convex hull of all visible fragments */
860
861         shaderInfo_t            *shaderInfo;
862
863         int                                     contentFlags;           /* from shaderInfo */
864         int                                     surfaceFlags;           /* from shaderInfo */
865         int                                     compileFlags;           /* from shaderInfo */
866         int                                     value;                          /* from shaderInfo */
867
868         qboolean                        visible;                        /* choose visble planes first */
869         qboolean                        bevel;                          /* don't ever use for bsp splitting, and don't bother making windings for it */
870         qboolean                        culled;                         /* ydnar: face culling */
871 }
872 side_t;
873
874
875 typedef struct sideRef_s
876 {
877         struct sideRef_s        *next;
878         side_t                          *side;
879 }
880 sideRef_t;
881
882
883 /* ydnar: generic index mapping for entities (natural extension of terrain texturing) */
884 typedef struct indexMap_s
885 {
886         int                                     w, h, numLayers;
887         char                            name[ MAX_QPATH ], shader[ MAX_QPATH ];
888         float                           offsets[ 256 ];
889         byte                            *pixels;
890 }
891 indexMap_t;
892
893
894 typedef struct brush_s
895 {
896         struct brush_s          *next;
897         struct brush_s          *nextColorModBrush;     /* ydnar: colorMod volume brushes go here */
898         struct brush_s          *original;                      /* chopped up brushes will reference the originals */
899         
900         int                                     entityNum, brushNum;/* editor numbering */
901         int                                     outputNum;                      /* set when the brush is written to the file list */
902         
903         /* ydnar: for shadowcasting entities */
904         int                                     castShadows;
905         int                                     recvShadows;
906         
907         shaderInfo_t            *contentShader;
908         shaderInfo_t            *celShader;                     /* :) */
909         
910         /* ydnar: gs mods */
911         int                                     lightmapSampleSize; /* jal : entity based _lightmapsamplesize */
912         float                           lightmapScale;
913         float                           shadeAngleDegrees; /* jal : entity based _shadeangle */
914         vec3_t                          eMins, eMaxs;
915         indexMap_t                      *im;
916
917         int                                     contentFlags;
918         int                                     compileFlags;           /* ydnar */
919         qboolean                        detail;
920         qboolean                        opaque;
921
922         int                                     portalareas[ 2 ];
923
924         vec3_t                          mins, maxs;
925         int                                     numsides;
926         
927         side_t                          sides[ 6 ];                     /* variably sized */
928 }
929 brush_t;
930
931
932 typedef struct fog_s
933 {
934         shaderInfo_t            *si;
935         brush_t                         *brush;
936         int                                     visibleSide;            /* the brush side that ray tests need to clip against (-1 == none) */
937 }
938 fog_t;
939
940
941 typedef struct
942 {
943         int                                     width, height;
944         bspDrawVert_t           *verts;
945 }
946 mesh_t;
947
948
949 typedef struct parseMesh_s
950 {
951         struct parseMesh_s      *next;
952         
953         int                                     entityNum, brushNum;    /* ydnar: editor numbering */
954         
955         /* ydnar: for shadowcasting entities */
956         int                                     castShadows;
957         int                                     recvShadows;
958         
959         mesh_t                          mesh;
960         shaderInfo_t            *shaderInfo;
961         shaderInfo_t            *celShader;                             /* :) */
962         
963         /* ydnar: gs mods */
964         int                                     lightmapSampleSize;             /* jal : entity based _lightmapsamplesize */
965         float                           lightmapScale;
966         vec3_t                          eMins, eMaxs;
967         indexMap_t                      *im;
968         
969         /* grouping */
970         qboolean                        grouped;
971         float                           longestCurve;
972         int                                     maxIterations;
973 }
974 parseMesh_t;
975
976
977 /*
978         ydnar: the drawsurf struct was extended to allow for:
979         - non-convex planar surfaces
980         - non-planar brushface surfaces
981         - lightmapped terrain
982         - planar patches
983 */
984
985 typedef enum
986 {
987         /* ydnar: these match up exactly with bspSurfaceType_t */
988         SURFACE_BAD,
989         SURFACE_FACE,
990         SURFACE_PATCH,
991         SURFACE_TRIANGLES,
992         SURFACE_FLARE,
993         SURFACE_FOLIAGE,        /* wolf et */
994         
995         /* ydnar: compiler-relevant surface types */
996         SURFACE_FORCED_META,
997         SURFACE_META,
998         SURFACE_FOGHULL,
999         SURFACE_DECAL,
1000         SURFACE_SHADER,
1001         
1002         NUM_SURFACE_TYPES
1003 }
1004 surfaceType_t;
1005
1006 char                    *surfaceTypes[ NUM_SURFACE_TYPES ]
1007 #ifndef MAIN_C
1008                                 ;
1009 #else
1010                                 =
1011                                 {
1012                                         "SURFACE_BAD",
1013                                         "SURFACE_FACE",
1014                                         "SURFACE_PATCH",
1015                                         "SURFACE_TRIANGLES",
1016                                         "SURFACE_FLARE",
1017                                         "SURFACE_FOLIAGE",
1018                                         "SURFACE_FORCED_META",
1019                                         "SURFACE_META",
1020                                         "SURFACE_FOGHULL",
1021                                         "SURFACE_DECAL",
1022                                         "SURFACE_SHADER"
1023                                 };
1024 #endif
1025
1026
1027 /* ydnar: this struct needs an overhaul (again, heh) */
1028 typedef struct mapDrawSurface_s
1029 {
1030         surfaceType_t           type;
1031         qboolean                        planar;
1032         int                                     outputNum;                      /* ydnar: to match this sort of thing up */
1033         
1034         qboolean                        fur;                            /* ydnar: this is kind of a hack, but hey... */
1035         qboolean                        skybox;                         /* ydnar: yet another fun hack */
1036         qboolean                        backSide;                       /* ydnar: q3map_backShader support */
1037         
1038         struct mapDrawSurface_s *parent;                /* ydnar: for cloned (skybox) surfaces to share lighting data */
1039         struct mapDrawSurface_s *clone;                 /* ydnar: for cloned surfaces */
1040         struct mapDrawSurface_s *cel;                   /* ydnar: for cloned cel surfaces */
1041         
1042         shaderInfo_t            *shaderInfo;
1043         shaderInfo_t            *celShader;
1044         brush_t                         *mapBrush;
1045         parseMesh_t                     *mapMesh;
1046         sideRef_t                       *sideRef;
1047         
1048         int                                     fogNum;
1049         
1050         int                                     numVerts;                       /* vertexes and triangles */
1051         bspDrawVert_t           *verts;
1052         int                                     numIndexes;
1053         int                                     *indexes;
1054         
1055         int                                     planeNum;
1056         vec3_t                          lightmapOrigin;         /* also used for flares */
1057         vec3_t                          lightmapVecs[ 3 ];      /* also used for flares */
1058         int                                     lightStyle;                     /* used for flares */
1059         
1060         /* ydnar: per-surface (per-entity, actually) lightmap sample size scaling */
1061         float                           lightmapScale;
1062
1063         /* jal: per-surface (per-entity, actually) shadeangle */
1064         float                           shadeAngleDegrees;
1065         
1066         /* ydnar: surface classification */
1067         vec3_t                          mins, maxs;
1068         vec3_t                          lightmapAxis;
1069         int                                     sampleSize;
1070         
1071         /* ydnar: shadow group support */
1072         int                                     castShadows, recvShadows;
1073         
1074         /* ydnar: texture coordinate range monitoring for hardware with limited texcoord precision (in texel space) */
1075         float                           bias[ 2 ];
1076         int                                     texMins[ 2 ], texMaxs[ 2 ], texRange[ 2 ];
1077                 
1078         /* ydnar: for patches */
1079         float                           longestCurve;
1080         int                                     maxIterations;
1081         int                                     patchWidth, patchHeight;
1082         vec3_t                          bounds[ 2 ];
1083
1084         /* ydnar/sd: for foliage */
1085         int                                     numFoliageInstances;
1086         
1087         /* ydnar: editor/useful numbering */
1088         int                                     entityNum;
1089         int                                     surfaceNum;
1090 }
1091 mapDrawSurface_t;
1092
1093
1094 typedef struct drawSurfRef_s
1095 {
1096         struct drawSurfRef_s    *nextRef;
1097         int                                     outputNum;
1098 }
1099 drawSurfRef_t;
1100
1101
1102 /* ydnar: metasurfaces are constructed from lists of metatriangles so they can be merged in the best way */
1103 typedef struct metaTriangle_s
1104 {
1105         shaderInfo_t            *si;
1106         side_t                          *side;
1107         int                                     entityNum, surfaceNum, planeNum, fogNum, sampleSize, castShadows, recvShadows;
1108         float                           shadeAngleDegrees;
1109         vec4_t                          plane;
1110         vec3_t                          lightmapAxis;
1111         int                                     indexes[ 3 ];
1112 }
1113 metaTriangle_t;
1114
1115
1116 typedef struct epair_s
1117 {
1118         struct epair_s          *next;
1119         char                            *key, *value;
1120 }
1121 epair_t;
1122
1123
1124 typedef struct
1125 {
1126         vec3_t                          origin;
1127         brush_t                         *brushes, *lastBrush, *colorModBrushes;
1128         parseMesh_t                     *patches;
1129         int                                     mapEntityNum, firstDrawSurf;
1130         int                                     firstBrush, numBrushes;         /* only valid during BSP compile */
1131         epair_t                         *epairs;
1132         vec3_t                          originbrush_origin;
1133 }
1134 entity_t;
1135
1136
1137 typedef struct node_s
1138 {
1139         /* both leafs and nodes */
1140         int                                     planenum;               /* -1 = leaf node */
1141         struct node_s           *parent;
1142         vec3_t                          mins, maxs;             /* valid after portalization */
1143         brush_t                         *volume;                /* one for each leaf/node */
1144
1145         /* nodes only */
1146         side_t                          *side;                  /* the side that created the node */
1147         struct node_s           *children[ 2 ];
1148         int                                     compileFlags;   /* ydnar: hint, antiportal */
1149         int                                     tinyportals;
1150         vec3_t                          referencepoint;
1151
1152         /* leafs only */
1153         qboolean                        opaque;                 /* view can never be inside */
1154         qboolean                        areaportal;
1155         qboolean                        skybox;                 /* ydnar: a skybox leaf */
1156         qboolean                        sky;                    /* ydnar: a sky leaf */
1157         int                                     cluster;                /* for portalfile writing */
1158         int                                     area;                   /* for areaportals */
1159         brush_t                         *brushlist;             /* fragments of all brushes in this leaf */
1160         drawSurfRef_t           *drawSurfReferences;
1161
1162         int                                     occupied;               /* 1 or greater can reach entity */
1163         entity_t                        *occupant;              /* for leak file testing */
1164
1165         struct portal_s         *portals;               /* also on nodes during construction */
1166
1167         qboolean            has_structural_children;
1168 }
1169 node_t;
1170
1171
1172 typedef struct portal_s
1173 {
1174         plane_t                         plane;
1175         node_t                          *onnode;                /* NULL = outside box */
1176         node_t                          *nodes[ 2 ];    /* [ 0 ] = front side of plane */
1177         struct portal_s         *next[ 2 ];
1178         winding_t                       *winding;
1179
1180         qboolean                        sidefound;              /* false if ->side hasn't been checked */
1181         int                                     compileFlags;   /* from original face that caused the split */
1182         side_t                          *side;                  /* NULL = non-visible */
1183 }
1184 portal_t;
1185
1186
1187 typedef struct
1188 {
1189         node_t                          *headnode;
1190         node_t                          outside_node;
1191         vec3_t                          mins, maxs;
1192 }
1193 tree_t;
1194
1195
1196
1197 /* -------------------------------------------------------------------------------
1198
1199 vis structures
1200
1201 ------------------------------------------------------------------------------- */
1202
1203 typedef struct
1204 {
1205         vec3_t                          normal;
1206         float                           dist;
1207 }
1208 visPlane_t;
1209
1210
1211 typedef struct
1212 {
1213         int                                     numpoints;
1214         vec3_t                          points[ MAX_POINTS_ON_FIXED_WINDING     ];              /* variable sized */
1215
1216 fixedWinding_t;
1217
1218
1219 typedef struct passage_s
1220 {
1221         struct passage_s        *next;
1222         byte                            cansee[ 1 ];    /* all portals that can be seen through this passage */
1223 } passage_t;
1224
1225
1226 typedef enum
1227 {
1228         stat_none,
1229         stat_working,
1230         stat_done
1231 }
1232 vstatus_t;
1233
1234
1235 typedef struct
1236 {
1237         int                                     num;
1238         qboolean                        hint;                   /* true if this portal was created from a hint splitter */
1239         qboolean                        removed;
1240         visPlane_t                      plane;                  /* normal pointing into neighbor */
1241         int                                     leaf;                   /* neighbor */
1242         
1243         vec3_t                          origin;                 /* for fast clip testing */
1244         float                           radius;
1245
1246         fixedWinding_t          *winding;
1247         vstatus_t                       status;
1248         byte                            *portalfront;   /* [portals], preliminary */
1249         byte                            *portalflood;   /* [portals], intermediate */
1250         byte                            *portalvis;             /* [portals], final */
1251
1252         int                                     nummightsee;    /* bit count on portalflood for sort */
1253         passage_t                       *passages;              /* there are just as many passages as there */
1254                                                                                 /* are portals in the leaf this portal leads */
1255 }
1256 vportal_t;
1257
1258
1259 typedef struct leaf_s
1260 {
1261         int                                     numportals;
1262         int                                     merged;
1263         vportal_t                       *portals[MAX_PORTALS_ON_LEAF];
1264 }
1265 leaf_t;
1266
1267         
1268 typedef struct pstack_s
1269 {
1270         byte                            mightsee[ MAX_PORTALS / 8 ];
1271         struct pstack_s         *next;
1272         leaf_t                          *leaf;
1273         vportal_t                       *portal;                /* portal exiting */
1274         fixedWinding_t          *source;
1275         fixedWinding_t          *pass;
1276
1277         fixedWinding_t          windings[ 3 ];  /* source, pass, temp in any order */
1278         int                                     freewindings[ 3 ];
1279
1280         visPlane_t                      portalplane;
1281         int depth;
1282 #ifdef SEPERATORCACHE
1283         visPlane_t                      seperators[ 2 ][ MAX_SEPERATORS ];
1284         int                                     numseperators[ 2 ];
1285 #endif
1286 }
1287 pstack_t;
1288
1289
1290 typedef struct
1291 {
1292         vportal_t                       *base;
1293         int                                     c_chains;
1294         pstack_t                        pstack_head;
1295 }
1296 threaddata_t;
1297
1298
1299
1300 /* -------------------------------------------------------------------------------
1301
1302 light structures
1303
1304 ------------------------------------------------------------------------------- */
1305
1306 /* ydnar: new light struct with flags */
1307 typedef struct light_s
1308 {
1309         struct light_s          *next;
1310         
1311         int                                     type;
1312         int                                     flags;                  /* ydnar: condensed all the booleans into one flags int */
1313         shaderInfo_t            *si;
1314         
1315         vec3_t                          origin;
1316         vec3_t                          normal;                 /* for surfaces, spotlights, and suns */
1317         float                           dist;                   /* plane location along normal */
1318         
1319         float                           photons;
1320         int                                     style;
1321         vec3_t                          color;
1322         float                           radiusByDist;   /* for spotlights */
1323         float                           fade;                   /* ydnar: from wolf, for linear lights */
1324         float                           angleScale;             /* ydnar: stolen from vlight for K */
1325         float                           extraDist;              /* "extra dimension" distance of the light, to kill hot spots */
1326
1327         float                           add;                    /* ydnar: used for area lights */
1328         float                           envelope;               /* ydnar: units until falloff < tolerance */
1329         float                           envelope2;              /* ydnar: envelope squared (tiny optimization) */
1330         vec3_t                          mins, maxs;             /* ydnar: pvs envelope */
1331         int                                     cluster;                /* ydnar: cluster light falls into */
1332         
1333         winding_t                       *w;
1334         vec3_t                          emitColor;              /* full out-of-gamut value */
1335         
1336         float                           falloffTolerance;       /* ydnar: minimum attenuation threshold */
1337         float                           filterRadius;   /* ydnar: lightmap filter radius in world units, 0 == default */
1338 }
1339 light_t;
1340
1341
1342 typedef struct
1343 {
1344         /* constant input */
1345         qboolean                        testOcclusion, forceSunlight, testAll;
1346         int                                     recvShadows;
1347         
1348         int                                     numSurfaces;
1349         int                                     *surfaces;
1350         
1351         int                                     numLights;
1352         light_t                         **lights;
1353         
1354         qboolean                        twoSided;
1355         
1356         /* per-sample input */
1357         int                                     cluster;
1358         vec3_t                          origin, normal;
1359         vec_t                           inhibitRadius;  /* sphere in which occluding geometry is ignored */
1360         
1361         /* per-light input */
1362         light_t                         *light;
1363         vec3_t                          end;
1364         
1365         /* calculated input */
1366         vec3_t                          displacement, direction;
1367         vec_t                           distance;
1368         
1369         /* input and output */
1370         vec3_t                          color;                  /* starts out at full color, may be reduced if transparent surfaces are crossed */
1371         vec3_t                          colorNoShadow;  /* result color with no shadow casting */
1372         vec3_t                          directionContribution; /* result contribution to the deluxe map */
1373
1374         /* output */
1375         vec3_t                          hit;
1376         int                                     compileFlags;   /* for determining surface compile flags traced through */
1377         qboolean                        passSolid;
1378         qboolean                        opaque;
1379         vec_t                   forceSubsampling; /* needs subsampling (alphashadow), value = max color contribution possible from it */
1380         
1381         /* working data */
1382         int                                     numTestNodes;
1383         int                                     testNodes[ MAX_TRACE_TEST_NODES ]; 
1384 }
1385 trace_t;
1386
1387
1388
1389 /* must be identical to bspDrawVert_t except for float color! */
1390 typedef struct
1391 {
1392         vec3_t                          xyz;
1393         float                           st[ 2 ];
1394         float                           lightmap[ MAX_LIGHTMAPS ][ 2 ];
1395         vec3_t                          normal;
1396         float                           color[ MAX_LIGHTMAPS ][ 4 ];
1397 }
1398 radVert_t;
1399
1400
1401 typedef struct
1402 {
1403         int                                     numVerts;
1404         radVert_t                       verts[ MAX_POINTS_ON_WINDING ];
1405 }
1406 radWinding_t;
1407
1408
1409 /* crutch for poor local allocations in win32 smp */
1410 typedef struct
1411 {
1412         vec_t                           dists[ MAX_POINTS_ON_WINDING + 4 ];
1413         int                                     sides[ MAX_POINTS_ON_WINDING + 4 ];
1414 }
1415 clipWork_t;
1416
1417
1418 /* ydnar: new lightmap handling code */
1419 typedef struct outLightmap_s
1420 {
1421         int                                     lightmapNum, extLightmapNum;
1422         int                                     customWidth, customHeight;
1423         int                                     numLightmaps;
1424         int                                     freeLuxels;
1425         int                                     numShaders;
1426         shaderInfo_t            *shaders[ MAX_LIGHTMAP_SHADERS ];
1427         byte                            *lightBits;
1428         byte                            *bspLightBytes;
1429         byte                            *bspDirBytes;
1430 }
1431 outLightmap_t;
1432
1433
1434 typedef struct rawLightmap_s
1435 {
1436         qboolean                                finished, splotchFix, wrap[ 2 ];
1437         int                                             customWidth, customHeight;
1438         float                                   brightness;
1439         float                                   filterRadius;
1440         
1441         int                                             firstLightSurface, numLightSurfaces;    /* index into lightSurfaces */
1442         int                                             numLightClusters, *lightClusters;
1443         
1444         int                                             sampleSize, actualSampleSize, axisNum;
1445
1446         /* vortex: per-surface floodlight */
1447         float                                   floodlightDirectionScale;
1448         vec3_t                                  floodlightRGB; 
1449         float                                   floodlightIntensity;
1450         float                                   floodlightDistance;
1451
1452         int                                             entityNum;
1453         int                                             recvShadows;
1454         vec3_t                                  mins, maxs, axis, origin, *vecs;
1455         float                                   *plane;
1456         int                                             w, h, sw, sh, used;
1457         
1458         qboolean                                solid[ MAX_LIGHTMAPS ];
1459         vec3_t                                  solidColor[ MAX_LIGHTMAPS ];
1460         
1461         int                                             numStyledTwins;
1462         struct rawLightmap_s    *twins[ MAX_LIGHTMAPS ];
1463
1464         int                                             outLightmapNums[ MAX_LIGHTMAPS ];
1465         int                                             twinNums[ MAX_LIGHTMAPS ];
1466         int                                             lightmapX[ MAX_LIGHTMAPS ], lightmapY[ MAX_LIGHTMAPS ];
1467         byte                                    styles[ MAX_LIGHTMAPS ];
1468         float                                   *bspLuxels[ MAX_LIGHTMAPS ];
1469         float                                   *radLuxels[ MAX_LIGHTMAPS ];
1470         float                                   *superLuxels[ MAX_LIGHTMAPS ];
1471         unsigned char                           *superFlags;
1472         float                                   *superOrigins;
1473         float                                   *superNormals;
1474         int                                             *superClusters;
1475         
1476         float                                   *superDeluxels; /* average light direction */
1477         float                                   *bspDeluxels;
1478         float                                   *superFloodLight;
1479 }
1480 rawLightmap_t;
1481
1482
1483 typedef struct rawGridPoint_s
1484 {
1485         vec3_t                          ambient[ MAX_LIGHTMAPS ];
1486         vec3_t                          directed[ MAX_LIGHTMAPS ];
1487         vec3_t                          dir;
1488         byte                            styles[ MAX_LIGHTMAPS ];
1489 }
1490 rawGridPoint_t;
1491
1492
1493 typedef struct surfaceInfo_s
1494 {
1495         int                                     modelindex;
1496         shaderInfo_t            *si;
1497         rawLightmap_t           *lm;
1498         int                                     parentSurfaceNum, childSurfaceNum;
1499         int                                     entityNum, castShadows, recvShadows, sampleSize, patchIterations;
1500         float                           longestCurve;
1501         float                           *plane;
1502         vec3_t                          axis, mins, maxs;
1503         qboolean                        hasLightmap, approximated;
1504         int                                     firstSurfaceCluster, numSurfaceClusters;
1505 }
1506 surfaceInfo_t;
1507
1508 /* -------------------------------------------------------------------------------
1509
1510 prototypes
1511
1512 ------------------------------------------------------------------------------- */
1513
1514 /* main.c */
1515 vec_t                                           Random( void );
1516 int                                                     BSPInfo( int count, char **fileNames );
1517 int                                                     ScaleBSPMain( int argc, char **argv );
1518 int                                                     ConvertMain( int argc, char **argv );
1519
1520
1521 /* path_init.c */
1522 game_t                                          *GetGame( char *arg );
1523 void                                            InitPaths( int *argc, char **argv );
1524
1525
1526 /* bsp.c */
1527 int                                                     BSPMain( int argc, char **argv );
1528
1529
1530 /* convert_map.c */
1531 int                                                     ConvertBSPToMap( char *bspName );
1532 int                                                     ConvertBSPToMap_BP( char *bspName );
1533
1534
1535 /* convert_ase.c */
1536 int                                                     ConvertBSPToASE( char *bspName );
1537
1538 /* convert_obj.c */
1539 int                                                     ConvertBSPToOBJ( char *bspName );
1540
1541
1542 /* brush.c */
1543 sideRef_t                                       *AllocSideRef( side_t *side, sideRef_t *next );
1544 int                                                     CountBrushList( brush_t *brushes );
1545 brush_t                                         *AllocBrush( int numsides );
1546 void                                            FreeBrush( brush_t *brushes );
1547 void                                            FreeBrushList( brush_t *brushes );
1548 brush_t                                         *CopyBrush( brush_t *brush );
1549 qboolean                                        BoundBrush( brush_t *brush );
1550 qboolean                                        CreateBrushWindings( brush_t *brush );
1551 brush_t                                         *BrushFromBounds( vec3_t mins, vec3_t maxs );
1552 vec_t                                           BrushVolume( brush_t *brush );
1553 void                                            WriteBSPBrushMap( char *name, brush_t *list );
1554
1555 void                                            FilterDetailBrushesIntoTree( entity_t *e, tree_t *tree );
1556 void                                            FilterStructuralBrushesIntoTree( entity_t *e, tree_t *tree );
1557
1558 int                                                     BoxOnPlaneSide( vec3_t mins, vec3_t maxs, plane_t *plane );
1559 qboolean                                        WindingIsTiny( winding_t *w );
1560
1561 void                                            SplitBrush( brush_t *brush, int planenum, brush_t **front, brush_t **back);
1562
1563 tree_t                                          *AllocTree( void );
1564 node_t                                          *AllocNode( void );
1565
1566
1567 /* mesh.c */
1568 void                                            LerpDrawVert( bspDrawVert_t *a, bspDrawVert_t *b, bspDrawVert_t *out );
1569 void                                            LerpDrawVertAmount( bspDrawVert_t *a, bspDrawVert_t *b, float amount, bspDrawVert_t *out );
1570 void                                            FreeMesh( mesh_t *m );
1571 mesh_t                                          *CopyMesh( mesh_t *mesh );
1572 void                                            PrintMesh( mesh_t *m );
1573 mesh_t                                          *TransposeMesh( mesh_t *in );
1574 void                                            InvertMesh( mesh_t *m );
1575 mesh_t                                          *SubdivideMesh( mesh_t in, float maxError, float minLength );
1576 int                                                     IterationsForCurve( float len, int subdivisions );
1577 mesh_t                                          *SubdivideMesh2( mesh_t in, int iterations );
1578 mesh_t                                          *SubdivideMeshQuads( mesh_t *in, float minLength, int maxsize, int *widthtable, int *heighttable );
1579 mesh_t                                          *RemoveLinearMeshColumnsRows( mesh_t *in );
1580 void                                            MakeMeshNormals( mesh_t in );
1581 void                                            PutMeshOnCurve( mesh_t in );
1582
1583 void                                            MakeNormalVectors( vec3_t forward, vec3_t right, vec3_t up );
1584
1585
1586 /* map.c */
1587 void                                            LoadMapFile( char *filename, qboolean onlyLights, qboolean noCollapseGroups );
1588 int                                                     FindFloatPlane( vec3_t normal, vec_t dist, int numPoints, vec3_t *points );
1589 int                                                     PlaneTypeForNormal( vec3_t normal );
1590 void                                            AddBrushBevels( void );
1591 brush_t                                         *FinishBrush(qboolean noCollapseGroups);
1592
1593
1594 /* portals.c */
1595 void                                            MakeHeadnodePortals( tree_t *tree );
1596 void                                            MakeNodePortal( node_t *node );
1597 void                                            SplitNodePortals( node_t *node );
1598
1599 qboolean                                        PortalPassable( portal_t *p );
1600
1601 qboolean                                        FloodEntities( tree_t *tree );
1602 void                                            FillOutside( node_t *headnode);
1603 void                                            FloodAreas( tree_t *tree);
1604 face_t                                          *VisibleFaces( entity_t *e, tree_t *tree );
1605 void                                            FreePortal( portal_t *p );
1606
1607 void                                            MakeTreePortals( tree_t *tree );
1608
1609
1610 /* leakfile.c */
1611 xmlNodePtr                                      LeakFile( tree_t *tree );
1612
1613
1614 /* prtfile.c */
1615 void                                            NumberClusters( tree_t *tree );
1616 void                                            WritePortalFile( tree_t *tree );
1617
1618
1619 /* writebsp.c */
1620 void                                            SetModelNumbers( void );
1621 void                                            SetLightStyles( void );
1622
1623 int                                                     EmitShader( const char *shader, int *contentFlags, int *surfaceFlags );
1624
1625 void                                            BeginBSPFile( void );
1626 void                                            EndBSPFile( qboolean do_write );
1627 void                                            EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes );
1628 void                                            EmitFogs( void );
1629
1630 void                                            BeginModel( void );
1631 void                                            EndModel( entity_t *e, node_t *headnode );
1632
1633
1634 /* tree.c */
1635 void                                            FreeTree( tree_t *tree );
1636 void                                            FreeTree_r( node_t *node );
1637 void                                            PrintTree_r( node_t *node, int depth );
1638 void                                            FreeTreePortals_r( node_t *node );
1639
1640
1641 /* patch.c */
1642 void                                            ParsePatch( qboolean onlyLights );
1643 mesh_t                                          *SubdivideMesh( mesh_t in, float maxError, float minLength );
1644 void                                            PatchMapDrawSurfs( entity_t *e );
1645 void                                            TriangulatePatchSurface( entity_t *e , mapDrawSurface_t *ds );
1646
1647
1648 /* tjunction.c */
1649 void                                            FixTJunctions( entity_t *e );
1650
1651
1652 /* fog.c */
1653 winding_t                                       *WindingFromDrawSurf( mapDrawSurface_t *ds );
1654 void                                            FogDrawSurfaces( entity_t *e );
1655 int                                                     FogForPoint( vec3_t point, float epsilon );
1656 int                                                     FogForBounds( vec3_t mins, vec3_t maxs, float epsilon );
1657 void                                            CreateMapFogs( void );
1658
1659
1660 /* facebsp.c */
1661 face_t                                          *MakeStructuralBSPFaceList( brush_t *list );
1662 face_t                                          *MakeVisibleBSPFaceList( brush_t *list );
1663 tree_t                                          *FaceBSP( face_t *list );
1664
1665
1666 /* model.c */
1667 void                                            PicoPrintFunc( int level, const char *str );
1668 void                                            PicoLoadFileFunc( const char *name, byte **buffer, int *bufSize );
1669 picoModel_t                                     *FindModel( const char *name, int frame );
1670 picoModel_t                                     *LoadModel( const char *name, int frame );
1671 void                                            InsertModel( const char *name, int skin, int frame, m4x4_t transform, remap_t *remap, shaderInfo_t *celShader, int eNum, int castShadows, int recvShadows, int spawnFlags, float lightmapScale, int lightmapSampleSize, float shadeAngle );
1672 void                                            AddTriangleModels( entity_t *e );
1673
1674
1675 /* surface.c */
1676 mapDrawSurface_t                        *AllocDrawSurface( surfaceType_t type );
1677 void                                            FinishSurface( mapDrawSurface_t *ds );
1678 void                                            StripFaceSurface( mapDrawSurface_t *ds );
1679 void                                            MaxAreaFaceSurface( mapDrawSurface_t *ds );
1680 qboolean                                        CalcSurfaceTextureRange( mapDrawSurface_t *ds );
1681 qboolean                                        CalcLightmapAxis( vec3_t normal, vec3_t axis );
1682 void                                            ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds );
1683 void                                            ClassifyEntitySurfaces( entity_t *e );
1684 void                                            TidyEntitySurfaces( entity_t *e );
1685 mapDrawSurface_t                        *CloneSurface( mapDrawSurface_t *src, shaderInfo_t *si );
1686 mapDrawSurface_t                        *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si );
1687 qboolean                                        IsTriangleDegenerate( bspDrawVert_t *points, int a, int b, int c );
1688 void                                            ClearSurface( mapDrawSurface_t *ds );
1689 void                                            AddEntitySurfaceModels( entity_t *e );
1690 mapDrawSurface_t                        *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w );
1691 mapDrawSurface_t                        *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh );
1692 mapDrawSurface_t                        *DrawSurfaceForFlare( int entNum, vec3_t origin, vec3_t normal, vec3_t color, const char *flareShader, int lightStyle );
1693 mapDrawSurface_t                        *DrawSurfaceForShader( char *shader );
1694 void                                            ClipSidesIntoTree( entity_t *e, tree_t *tree );
1695 void                                            MakeDebugPortalSurfs( tree_t *tree );
1696 void                                            MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader );
1697 void                                            SubdivideFaceSurfaces( entity_t *e, tree_t *tree );
1698 void                                            AddEntitySurfaceModels( entity_t *e );
1699 int                                                     AddSurfaceModels( mapDrawSurface_t *ds );
1700 void                                            FilterDrawsurfsIntoTree( entity_t *e, tree_t *tree );
1701 void                                            EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds );
1702 void                                            EmitTriangleSurface( mapDrawSurface_t *ds );
1703
1704
1705 /* surface_fur.c */
1706 void                                            Fur( mapDrawSurface_t *src );
1707
1708
1709 /* surface_foliage.c */
1710 void                                            Foliage( mapDrawSurface_t *src );
1711
1712
1713 /* ydnar: surface_meta.c */
1714 void                                            ClearMetaTriangles( void );
1715 int                                                     FindMetaTriangle( metaTriangle_t *src, bspDrawVert_t *a, bspDrawVert_t *b, bspDrawVert_t *c, int planeNum );
1716 void                                            MakeEntityMetaTriangles( entity_t *e );
1717 void                                            FixMetaTJunctions( void );
1718 void                                            SmoothMetaTriangles( void );
1719 void                                            MergeMetaTriangles( void );
1720 void                                            EmitMetaStats(); // vortex: print meta statistics even in no-verbose mode
1721
1722
1723 /* surface_extra.c */
1724 void                                            SetDefaultSampleSize( int sampleSize );
1725
1726 void                                            SetSurfaceExtra( mapDrawSurface_t *ds, int num );
1727
1728 shaderInfo_t                            *GetSurfaceExtraShaderInfo( int num );
1729 int                                                     GetSurfaceExtraParentSurfaceNum( int num );
1730 int                                                     GetSurfaceExtraEntityNum( int num );
1731 int                                                     GetSurfaceExtraCastShadows( int num );
1732 int                                                     GetSurfaceExtraRecvShadows( int num );
1733 int                                                     GetSurfaceExtraSampleSize( int num );
1734 int                                                     GetSurfaceExtraMinSampleSize( int num );
1735 float                                           GetSurfaceExtraLongestCurve( int num );
1736 void                                            GetSurfaceExtraLightmapAxis( int num, vec3_t lightmapAxis );
1737
1738 void                                            WriteSurfaceExtraFile( const char *path );
1739 void                                            LoadSurfaceExtraFile( const char *path );
1740
1741
1742 /* decals.c */
1743 void                                            ProcessDecals( void );
1744 void                                            MakeEntityDecals( entity_t *e );
1745
1746 /* map.c */
1747 void                                            TextureAxisFromPlane( plane_t *pln, vec3_t xv, vec3_t yv );
1748
1749 /* brush_primit.c */
1750 void                                            ComputeAxisBase( vec3_t normal, vec3_t texX, vec3_t texY);
1751
1752
1753 /* vis.c */
1754 fixedWinding_t                          *NewFixedWinding( int points );
1755 int                                                     VisMain( int argc, char **argv );
1756
1757 /* visflow.c */
1758 int                                                     CountBits( byte *bits, int numbits );
1759 void                                            PassageFlow( int portalnum );
1760 void                                            CreatePassages( int portalnum );
1761 void                                            PassageMemory( void );
1762 void                                            BasePortalVis( int portalnum );
1763 void                                            BetterPortalVis( int portalnum );
1764 void                                            PortalFlow( int portalnum );
1765 void                                            PassagePortalFlow( int portalnum );
1766
1767
1768
1769 /* light.c  */
1770 float                                           PointToPolygonFormFactor( const vec3_t point, const vec3_t normal, const winding_t *w );
1771 int                                                     LightContributionToSample( trace_t *trace );
1772 void                                            LightingAtSample( trace_t *trace, byte styles[ MAX_LIGHTMAPS ], vec3_t colors[ MAX_LIGHTMAPS ] );
1773 int                                                     LightContributionToPoint( trace_t *trace );
1774 int                                                     LightMain( int argc, char **argv );
1775
1776
1777 /* light_trace.c */
1778 void                                            SetupTraceNodes( void );
1779 void                                            TraceLine( trace_t *trace );
1780 float                                           SetupTrace( trace_t *trace );
1781
1782
1783 /* light_bounce.c */
1784 qboolean                                        RadSampleImage( byte *pixels, int width, int height, float st[ 2 ], float color[ 4 ] );
1785 void                                            RadLightForTriangles( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw );
1786 void                                            RadLightForPatch( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw );
1787 void                                            RadCreateDiffuseLights( void );
1788 void                                            RadFreeLights();
1789
1790
1791 /* light_ydnar.c */
1792 void                                            ColorToBytes( const float *color, byte *colorBytes, float scale );
1793 void                                            SmoothNormals( void );
1794
1795 void                                            MapRawLightmap( int num );
1796
1797 void                                            SetupDirt();
1798 float                                           DirtForSample( trace_t *trace );
1799 void                                            DirtyRawLightmap( int num );
1800
1801 void                                            SetupFloodLight();
1802 void                                            FloodlightRawLightmaps();
1803 void                                            FloodlightIlluminateLightmap( rawLightmap_t *lm );
1804 float                                           FloodLightForSample( trace_t *trace , float floodLightDistance, qboolean floodLightLowQuality);
1805 void                                            FloodLightRawLightmap( int num );
1806
1807 void                                            IlluminateRawLightmap( int num );
1808 void                                            IlluminateVertexes( int num );
1809
1810 void                                            SetupBrushesFlags( int mask_any, int test_any, int mask_all, int test_all );
1811 void                                            SetupBrushes( void );
1812 void                                            SetupClusters( void );
1813 qboolean                                        ClusterVisible( int a, int b );
1814 qboolean                                        ClusterVisibleToPoint( vec3_t point, int cluster );
1815 int                                                     ClusterForPoint( vec3_t point );
1816 int                                                     ClusterForPointExt( vec3_t point, float epsilon );
1817 int                                                     ClusterForPointExtFilter( vec3_t point, float epsilon, int numClusters, int *clusters );
1818 int                                                     ShaderForPointInLeaf( vec3_t point, int leafNum, float epsilon, int wantContentFlags, int wantSurfaceFlags, int *contentFlags, int *surfaceFlags );
1819 void                                            SetupEnvelopes( qboolean forGrid, qboolean fastFlag );
1820 void                                            FreeTraceLights( trace_t *trace );
1821 void                                            CreateTraceLightsForBounds( vec3_t mins, vec3_t maxs, vec3_t normal, int numClusters, int *clusters, int flags, trace_t *trace );
1822 void                                            CreateTraceLightsForSurface( int num, trace_t *trace );
1823
1824
1825 /* lightmaps_ydnar.c */
1826 void                                            ExportLightmaps( void );
1827
1828 int                                                     ExportLightmapsMain( int argc, char **argv );
1829 int                                                     ImportLightmapsMain( int argc, char **argv );
1830
1831 void                                            SetupSurfaceLightmaps( void );
1832 void                                            StitchSurfaceLightmaps( void );
1833 void                                            StoreSurfaceLightmaps( void );
1834
1835
1836 /* image.c */
1837 void                                            ImageFree( image_t *image );
1838 image_t                                         *ImageFind( const char *filename );
1839 image_t                                         *ImageLoad( const char *filename );
1840
1841
1842 /* shaders.c */
1843 void                                            ColorMod( colorMod_t *am, int numVerts, bspDrawVert_t *drawVerts );
1844
1845 void                                            TCMod( tcMod_t mod, float st[ 2 ] );
1846 void                                            TCModIdentity( tcMod_t mod );
1847 void                                            TCModMultiply( tcMod_t a, tcMod_t b, tcMod_t out );
1848 void                                            TCModTranslate( tcMod_t mod, float s, float t );
1849 void                                            TCModScale( tcMod_t mod, float s, float t );
1850 void                                            TCModRotate( tcMod_t mod, float euler );
1851
1852 qboolean                                        ApplySurfaceParm( char *name, int *contentFlags, int *surfaceFlags, int *compileFlags );
1853
1854 void                                            BeginMapShaderFile( const char *mapFile );
1855 void                                            WriteMapShaderFile( void );
1856 shaderInfo_t                            *CustomShader( shaderInfo_t *si, char *find, char *replace );
1857 void                                            EmitVertexRemapShader( char *from, char *to );
1858
1859 void                                            LoadShaderInfo( void );
1860 shaderInfo_t                            *ShaderInfoForShader( const char *shader );
1861 shaderInfo_t                            *ShaderInfoForShaderNull( const char *shader );
1862
1863
1864 /* bspfile_abstract.c */
1865 void                                            SetGridPoints( int n );
1866 void                                            SetDrawVerts( int n );
1867 void                                            IncDrawVerts();
1868 void                                            SetDrawSurfaces(int n);
1869 void                                            SetDrawSurfacesBuffer();
1870 void                                            BSPFilesCleanup();
1871
1872 void                                            SwapBlock( int *block, int size );
1873
1874 int                                                     GetLumpElements( bspHeader_t *header, int lump, int size );
1875 void                                            *GetLump( bspHeader_t *header, int lump );
1876 int                                                     CopyLump( bspHeader_t *header, int lump, void *dest, int size );
1877 int                                                     CopyLump_Allocate( bspHeader_t *header, int lump, void **dest, int size, int *allocationVariable );
1878 void                                            AddLump( FILE *file, bspHeader_t *header, int lumpNum, const void *data, int length );
1879
1880 void                                            LoadBSPFile( const char *filename );
1881 void                                            WriteBSPFile( const char *filename );
1882 void                                            PrintBSPFileSizes( void );
1883
1884 epair_t                                         *ParseEPair( void );
1885 void                                            ParseEntities( void );
1886 void                                            UnparseEntities( void );
1887 void                                            PrintEntity( const entity_t *ent );
1888 void                                            SetKeyValue( entity_t *ent, const char *key, const char *value );
1889 qboolean                                        KeyExists( const entity_t *ent, const char *key ); /* VorteX: check if key exists */
1890 const char                                      *ValueForKey( const entity_t *ent, const char *key );
1891 int                                                     IntForKey( const entity_t *ent, const char *key );
1892 vec_t                                           FloatForKey( const entity_t *ent, const char *key );
1893 void                                            GetVectorForKey( const entity_t *ent, const char *key, vec3_t vec );
1894 entity_t                                        *FindTargetEntity( const char *target );
1895 void                                            GetEntityShadowFlags( const entity_t *ent, const entity_t *ent2, int *castShadows, int *recvShadows );
1896 void InjectCommandLine(char **argv, int beginArgs, int endArgs);
1897                 
1898
1899
1900 /* bspfile_ibsp.c */
1901 void                                            LoadIBSPFile( const char *filename );
1902 void                                            WriteIBSPFile( const char *filename );
1903
1904
1905 /* bspfile_rbsp.c */
1906 void                                            LoadRBSPFile( const char *filename );
1907 void                                            WriteRBSPFile( const char *filename );
1908
1909
1910
1911 /* -------------------------------------------------------------------------------
1912
1913 bsp/general global variables
1914
1915 ------------------------------------------------------------------------------- */
1916
1917 #ifdef MAIN_C
1918         #define Q_EXTERN
1919         #define Q_ASSIGN( a )   = a
1920 #else
1921         #define Q_EXTERN extern
1922         #define Q_ASSIGN( a )   
1923 #endif
1924
1925 /* game support */
1926 Q_EXTERN game_t                         games[]
1927 #ifndef MAIN_C
1928                                                         ;
1929 #else
1930                                                         =
1931                                                         {
1932                                                                 #include "game_quake3.h"
1933                                                                 ,
1934                                                                 #include "game_quakelive.h"/* most be after game_quake3.h as they share defines! */
1935                                                                 ,
1936                                                                 #include "game_nexuiz.h"/* most be after game_quake3.h as they share defines! */
1937                                                                 ,
1938                                                                 #include "game_xonotic.h"/* most be after game_quake3.h as they share defines! */
1939                                                                 ,
1940                                                                 #include "game_tremulous.h" /*LinuxManMikeC: must be after game_quake3.h, depends on #define's set in it */
1941                                                                 ,
1942                                                                 #include "game_tenebrae.h"
1943                                                                 ,
1944                                                                 #include "game_wolf.h"
1945                                                                 ,
1946                                                                 #include "game_wolfet.h"/* most be after game_wolf.h as they share defines! */
1947                                                                 ,
1948                                                                 #include "game_etut.h"
1949                                                                 ,
1950                                                                 #include "game_ef.h"
1951                                                                 ,
1952                                                                 #include "game_sof2.h"
1953                                                                 ,
1954                                                                 #include "game_jk2.h"   /* most be after game_sof2.h as they share defines! */
1955                                                                 ,
1956                                                                 #include "game_ja.h"    /* most be after game_jk2.h as they share defines! */
1957                                                                 ,
1958                                                                 #include "game_qfusion.h"       /* qfusion game */
1959                                                                 ,
1960                                                                 #include "game_reaction.h" /* must be after game_quake3.h */
1961                                                                 ,
1962                                                                 #include "game_darkplaces.h"    /* vortex: darkplaces q1 engine */
1963                                                                 ,
1964                                                                 #include "game_dq.h"    /* vortex: deluxe quake game ( darkplaces q1 engine) */
1965                                                                 ,
1966                                                                 #include "game_prophecy.h"      /* vortex: prophecy game ( darkplaces q1 engine) */
1967                                                                 ,
1968                                                                 #include "game__null.h" /* null game (must be last item) */
1969                                                         };
1970 #endif
1971 Q_EXTERN game_t                         *game Q_ASSIGN( &games[ 0 ] );
1972
1973
1974 /* general */
1975 Q_EXTERN int                            numImages Q_ASSIGN( 0 );
1976 Q_EXTERN image_t                        images[ MAX_IMAGES ];
1977
1978 Q_EXTERN int                            numPicoModels Q_ASSIGN( 0 );
1979 Q_EXTERN picoModel_t            *picoModels[ MAX_MODELS ];
1980
1981 Q_EXTERN shaderInfo_t           *shaderInfo Q_ASSIGN( NULL );
1982 Q_EXTERN int                            numShaderInfo Q_ASSIGN( 0 );
1983 Q_EXTERN int                            numVertexRemaps Q_ASSIGN( 0 );
1984
1985 Q_EXTERN surfaceParm_t          custSurfaceParms[ MAX_CUST_SURFACEPARMS ];
1986 Q_EXTERN int                            numCustSurfaceParms Q_ASSIGN( 0 );
1987
1988 Q_EXTERN char                           mapName[ MAX_QPATH ];   /* ydnar: per-map custom shaders for larger lightmaps */
1989 Q_EXTERN char                           mapShaderFile[ 1024 ];
1990 Q_EXTERN qboolean                       warnImage Q_ASSIGN( qtrue );
1991
1992 /* ydnar: sinusoid samples */
1993 Q_EXTERN float                          jitters[ MAX_JITTERS ];
1994
1995
1996 /* commandline arguments */
1997 Q_EXTERN qboolean                       verbose;
1998 Q_EXTERN qboolean                       verboseEntities Q_ASSIGN( qfalse );
1999 Q_EXTERN qboolean                       force Q_ASSIGN( qfalse );
2000 Q_EXTERN qboolean                       infoMode Q_ASSIGN( qfalse );
2001 Q_EXTERN qboolean                       useCustomInfoParms Q_ASSIGN( qfalse );
2002 Q_EXTERN qboolean                       noprune Q_ASSIGN( qfalse );
2003 Q_EXTERN qboolean                       leaktest Q_ASSIGN( qfalse );
2004 Q_EXTERN qboolean                       nodetail Q_ASSIGN( qfalse );
2005 Q_EXTERN qboolean                       nosubdivide Q_ASSIGN( qfalse );
2006 Q_EXTERN qboolean                       notjunc Q_ASSIGN( qfalse );
2007 Q_EXTERN qboolean                       fulldetail Q_ASSIGN( qfalse );
2008 Q_EXTERN qboolean                       nowater Q_ASSIGN( qfalse );
2009 Q_EXTERN qboolean                       noCurveBrushes Q_ASSIGN( qfalse );
2010 Q_EXTERN qboolean                       fakemap Q_ASSIGN( qfalse );
2011 Q_EXTERN qboolean                       coplanar Q_ASSIGN( qfalse );
2012 Q_EXTERN qboolean                       nofog Q_ASSIGN( qfalse );
2013 Q_EXTERN qboolean                       noHint Q_ASSIGN( qfalse );                              /* ydnar */
2014 Q_EXTERN qboolean                       renameModelShaders Q_ASSIGN( qfalse );  /* ydnar */
2015 Q_EXTERN qboolean                       skyFixHack Q_ASSIGN( qfalse );                  /* ydnar */
2016 Q_EXTERN qboolean                       bspAlternateSplitWeights Q_ASSIGN( qfalse );                    /* 27 */
2017 Q_EXTERN qboolean                       deepBSP Q_ASSIGN( qfalse );                     /* div0 */
2018 Q_EXTERN qboolean                       maxAreaFaceSurface Q_ASSIGN( qfalse );                  /* divVerent */
2019
2020 Q_EXTERN int                            patchSubdivisions Q_ASSIGN( 8 );                /* ydnar: -patchmeta subdivisions */
2021
2022 Q_EXTERN int                            maxLMSurfaceVerts Q_ASSIGN( 64 );               /* ydnar */
2023 Q_EXTERN int                            maxSurfaceVerts Q_ASSIGN( 999 );                /* ydnar */
2024 Q_EXTERN int                            maxSurfaceIndexes Q_ASSIGN( 6000 );             /* ydnar */
2025 Q_EXTERN float                          npDegrees Q_ASSIGN( 0.0f );                             /* ydnar: nonplanar degrees */
2026 Q_EXTERN int                            bevelSnap Q_ASSIGN( 0 );                                /* ydnar: bevel plane snap */
2027 Q_EXTERN int                            texRange Q_ASSIGN( 0 );
2028 Q_EXTERN qboolean                       flat Q_ASSIGN( qfalse );
2029 Q_EXTERN qboolean                       meta Q_ASSIGN( qfalse );
2030 Q_EXTERN qboolean                       patchMeta Q_ASSIGN( qfalse );
2031 Q_EXTERN qboolean                       emitFlares Q_ASSIGN( qfalse );
2032 Q_EXTERN qboolean                       debugSurfaces Q_ASSIGN( qfalse );
2033 Q_EXTERN qboolean                       debugInset Q_ASSIGN( qfalse );
2034 Q_EXTERN qboolean                       debugPortals Q_ASSIGN( qfalse );
2035 Q_EXTERN qboolean           lightmapTriangleCheck Q_ASSIGN(qfalse);
2036 Q_EXTERN qboolean           lightmapExtraVisClusterNudge Q_ASSIGN(qfalse);
2037 Q_EXTERN qboolean           lightmapFill Q_ASSIGN(qfalse);
2038 Q_EXTERN int                            metaAdequateScore Q_ASSIGN( -1 );
2039 Q_EXTERN int                            metaGoodScore Q_ASSIGN( -1 );
2040 Q_EXTERN float                          metaMaxBBoxDistance Q_ASSIGN( -1 );
2041
2042 #if Q3MAP2_EXPERIMENTAL_SNAP_NORMAL_FIX
2043 // Increasing the normalEpsilon to compensate for new logic in SnapNormal(), where
2044 // this epsilon is now used to compare against 0 components instead of the 1 or -1
2045 // components.  Unfortunately, normalEpsilon is also used in PlaneEqual().  So changing
2046 // this will affect anything that calls PlaneEqual() as well (which are, at the time
2047 // of this writing, FindFloatPlane() and AddBrushBevels()).
2048 Q_EXTERN double                         normalEpsilon Q_ASSIGN(0.00005);
2049 #else
2050 Q_EXTERN double                         normalEpsilon Q_ASSIGN( 0.00001 );
2051 #endif
2052
2053 #if Q3MAP2_EXPERIMENTAL_HIGH_PRECISION_MATH_FIXES
2054 // NOTE: This distanceEpsilon is too small if parts of the map are at maximum world
2055 // extents (in the range of plus or minus 2^16).  The smallest epsilon at values
2056 // close to 2^16 is about 0.007, which is greater than distanceEpsilon.  Therefore,
2057 // maps should be constrained to about 2^15, otherwise slightly undesirable effects
2058 // may result.  The 0.01 distanceEpsilon used previously is just too coarse in my
2059 // opinion.  The real fix for this problem is to have 64 bit distances and then make
2060 // this epsilon even smaller, or to constrain world coordinates to plus minus 2^15
2061 // (or even 2^14).
2062 Q_EXTERN double                         distanceEpsilon Q_ASSIGN(0.005);
2063 #else
2064 Q_EXTERN double                         distanceEpsilon Q_ASSIGN( 0.01 );
2065 #endif
2066
2067
2068 /* bsp */
2069 Q_EXTERN int                            numMapEntities Q_ASSIGN( 0 );
2070
2071 Q_EXTERN int                            blockSize[ 3 ]                                  /* should be the same as in radiant */
2072 #ifndef MAIN_C
2073                                                         ;
2074 #else
2075                                                         = { 1024, 1024, 1024 };
2076 #endif
2077
2078 Q_EXTERN char                           name[ 1024 ];
2079 Q_EXTERN char                           source[ 1024 ];
2080 Q_EXTERN char                           outbase[ 32 ];
2081
2082 Q_EXTERN int                            sampleSize;                                             /* lightmap sample size in units */
2083 Q_EXTERN int                            minSampleSize;                  /* minimum sample size to use at all */
2084 Q_EXTERN int                            sampleScale;                                    /* vortex: lightmap sample scale (ie quality)*/
2085
2086 Q_EXTERN int                            mapEntityNum Q_ASSIGN( 0 );
2087
2088 Q_EXTERN int                            entitySourceBrushes;
2089
2090 Q_EXTERN plane_t                        *mapplanes Q_ASSIGN(NULL);      /* mapplanes[ num ^ 1 ] will always be the mirror or mapplanes[ num ] */
2091 Q_EXTERN int                            nummapplanes Q_ASSIGN(0);               /* nummapplanes will always be even */
2092 Q_EXTERN int                            allocatedmapplanes Q_ASSIGN(0);
2093 Q_EXTERN int                            numMapPatches;
2094 Q_EXTERN vec3_t                         mapMins, mapMaxs;
2095
2096 Q_EXTERN int                            defaultFogNum Q_ASSIGN( -1 );   /* ydnar: cleaner fog handling */
2097 Q_EXTERN int                            numMapFogs Q_ASSIGN( 0 );
2098 Q_EXTERN fog_t                          mapFogs[ MAX_MAP_FOGS ];
2099
2100 Q_EXTERN entity_t                       *mapEnt;
2101 Q_EXTERN brush_t                        *buildBrush;
2102 Q_EXTERN int                            numActiveBrushes;
2103 Q_EXTERN int                            g_bBrushPrimit;
2104
2105 Q_EXTERN int                            numStrippedLights Q_ASSIGN( 0 );
2106
2107
2108 /* surface stuff */
2109 Q_EXTERN mapDrawSurface_t       *mapDrawSurfs Q_ASSIGN( NULL );
2110 Q_EXTERN int                            numMapDrawSurfs;
2111
2112 Q_EXTERN int                            numSurfacesByType[ NUM_SURFACE_TYPES ];
2113 Q_EXTERN int                            numClearedSurfaces;
2114 Q_EXTERN int                            numStripSurfaces;
2115 Q_EXTERN int                            numMaxAreaSurfaces;
2116 Q_EXTERN int                            numFanSurfaces;
2117 Q_EXTERN int                            numMergedSurfaces;
2118 Q_EXTERN int                            numMergedVerts;
2119
2120 Q_EXTERN int                            numRedundantIndexes;
2121
2122 Q_EXTERN int                            numSurfaceModels Q_ASSIGN( 0 );
2123
2124 Q_EXTERN byte                           debugColors[ 12 ][ 3 ]
2125 #ifndef MAIN_C
2126                                                         ;
2127 #else
2128                                                         =
2129                                                         {
2130                                                                 { 255, 0, 0 },
2131                                                                 { 192, 128, 128 },
2132                                                                 { 255, 255, 0 },
2133                                                                 { 192, 192, 128 },
2134                                                                 { 0, 255, 255 },
2135                                                                 { 128, 192, 192 },
2136                                                                 { 0, 0, 255 },
2137                                                                 { 128, 128, 192 },
2138                                                                 { 255, 0, 255 },
2139                                                                 { 192, 128, 192 },
2140                                                                 { 0, 255, 0 },
2141                                                                 { 128, 192, 128 }
2142                                                         };
2143 #endif
2144
2145 Q_EXTERN qboolean                       skyboxPresent Q_ASSIGN( qfalse );
2146 Q_EXTERN int                            skyboxArea Q_ASSIGN( -1 );
2147 Q_EXTERN m4x4_t                         skyboxTransform;
2148
2149
2150
2151 /* -------------------------------------------------------------------------------
2152
2153 vis global variables
2154
2155 ------------------------------------------------------------------------------- */
2156
2157 /* commandline arguments */
2158 Q_EXTERN qboolean                       fastvis;
2159 Q_EXTERN qboolean                       noPassageVis;
2160 Q_EXTERN qboolean                       passageVisOnly;
2161 Q_EXTERN qboolean                       mergevis;
2162 Q_EXTERN qboolean                       mergevisportals;
2163 Q_EXTERN qboolean                       nosort;
2164 Q_EXTERN qboolean                       saveprt;
2165 Q_EXTERN qboolean                       hint;   /* ydnar */
2166 Q_EXTERN char                           inbase[ MAX_QPATH ];
2167 Q_EXTERN char                           globalCelShader[ MAX_QPATH ];
2168
2169 Q_EXTERN float                          farPlaneDist;   /* rr2do2, rf, mre, ydnar all contributed to this one... */
2170
2171 Q_EXTERN int                            numportals;
2172 Q_EXTERN int                            portalclusters;
2173
2174 Q_EXTERN vportal_t                      *portals;
2175 Q_EXTERN leaf_t                         *leafs;
2176
2177 Q_EXTERN vportal_t                      *faces;
2178 Q_EXTERN leaf_t                         *faceleafs;
2179
2180 Q_EXTERN int                            numfaces;
2181
2182 Q_EXTERN int                            c_portaltest, c_portalpass, c_portalcheck;
2183 Q_EXTERN int                            c_portalskip, c_leafskip;
2184 Q_EXTERN int                            c_vistest, c_mighttest;
2185 Q_EXTERN int                            c_chains;
2186
2187 Q_EXTERN byte                           *vismap, *vismap_p, *vismap_end;
2188
2189 Q_EXTERN int                            testlevel;
2190
2191 Q_EXTERN byte                           *uncompressed;
2192
2193 Q_EXTERN int                            leafbytes, leaflongs;
2194 Q_EXTERN int                            portalbytes, portallongs;
2195
2196 Q_EXTERN vportal_t                      *sorted_portals[ MAX_MAP_PORTALS * 2 ];
2197
2198
2199
2200 /* -------------------------------------------------------------------------------
2201
2202 light global variables
2203
2204 ------------------------------------------------------------------------------- */
2205
2206 /* commandline arguments */
2207 Q_EXTERN qboolean                       wolfLight Q_ASSIGN( qfalse );
2208 Q_EXTERN float                          extraDist Q_ASSIGN( 0.0f );
2209 Q_EXTERN qboolean                       loMem Q_ASSIGN( qfalse );
2210 Q_EXTERN qboolean                       noStyles Q_ASSIGN( qfalse );
2211 Q_EXTERN qboolean                       keepLights Q_ASSIGN( qfalse );
2212
2213 Q_EXTERN int                            sampleSize Q_ASSIGN( DEFAULT_LIGHTMAP_SAMPLE_SIZE );
2214 Q_EXTERN int                            minSampleSize Q_ASSIGN( DEFAULT_LIGHTMAP_MIN_SAMPLE_SIZE );
2215 Q_EXTERN qboolean                       noVertexLighting Q_ASSIGN( qfalse );
2216 Q_EXTERN qboolean                       noGridLighting Q_ASSIGN( qfalse );
2217
2218 Q_EXTERN qboolean                       noTrace Q_ASSIGN( qfalse );
2219 Q_EXTERN qboolean                       noSurfaces Q_ASSIGN( qfalse );
2220 Q_EXTERN qboolean                       patchShadows Q_ASSIGN( qfalse );
2221 Q_EXTERN qboolean                       cpmaHack Q_ASSIGN( qfalse );
2222
2223 Q_EXTERN qboolean                       deluxemap Q_ASSIGN( qfalse );
2224 Q_EXTERN qboolean                       debugDeluxemap Q_ASSIGN( qfalse );
2225 Q_EXTERN int                            deluxemode Q_ASSIGN( 0 );       /* deluxemap format (0 - modelspace, 1 - tangentspace with renormalization, 2 - tangentspace without renormalization) */
2226
2227 Q_EXTERN qboolean                       fast Q_ASSIGN( qfalse );
2228 Q_EXTERN qboolean                       fastpoint Q_ASSIGN( qtrue );
2229 Q_EXTERN qboolean                       faster Q_ASSIGN( qfalse );
2230 Q_EXTERN qboolean                       fastgrid Q_ASSIGN( qfalse );
2231 Q_EXTERN qboolean                       fastbounce Q_ASSIGN( qfalse );
2232 Q_EXTERN qboolean                       cheap Q_ASSIGN( qfalse );
2233 Q_EXTERN qboolean                       cheapgrid Q_ASSIGN( qfalse );
2234 Q_EXTERN int                            bounce Q_ASSIGN( 0 );
2235 Q_EXTERN qboolean                       bounceOnly Q_ASSIGN( qfalse );
2236 Q_EXTERN qboolean                       bouncing Q_ASSIGN( qfalse );
2237 Q_EXTERN qboolean                       bouncegrid Q_ASSIGN( qfalse );
2238 Q_EXTERN qboolean                       normalmap Q_ASSIGN( qfalse );
2239 Q_EXTERN qboolean                       trisoup Q_ASSIGN( qfalse );
2240 Q_EXTERN qboolean                       shade Q_ASSIGN( qfalse );
2241 Q_EXTERN float                          shadeAngleDegrees Q_ASSIGN( 0.0f );
2242 Q_EXTERN int                            superSample Q_ASSIGN( 0 );
2243 Q_EXTERN int                            lightSamples Q_ASSIGN( 1 );
2244 Q_EXTERN qboolean                       lightRandomSamples Q_ASSIGN( qfalse );
2245 Q_EXTERN int                            lightSamplesSearchBoxSize Q_ASSIGN( 1 );
2246 Q_EXTERN qboolean                       filter Q_ASSIGN( qfalse );
2247 Q_EXTERN qboolean                       dark Q_ASSIGN( qfalse );
2248 Q_EXTERN qboolean                       sunOnly Q_ASSIGN( qfalse );
2249 Q_EXTERN int                            approximateTolerance Q_ASSIGN( 0 );
2250 Q_EXTERN qboolean                       noCollapse Q_ASSIGN( qfalse );
2251 Q_EXTERN int                            lightmapSearchBlockSize Q_ASSIGN( 0 );
2252 Q_EXTERN qboolean                       exportLightmaps Q_ASSIGN( qfalse );
2253 Q_EXTERN qboolean                       externalLightmaps Q_ASSIGN( qfalse );
2254 Q_EXTERN int                            lmCustomSize Q_ASSIGN( LIGHTMAP_WIDTH );
2255 Q_EXTERN char *                         lmCustomDir Q_ASSIGN( NULL );
2256 Q_EXTERN int                            lmLimitSize Q_ASSIGN( 0 );
2257
2258 Q_EXTERN qboolean                       dirty Q_ASSIGN( qfalse );
2259 Q_EXTERN qboolean                       dirtDebug Q_ASSIGN( qfalse );
2260 Q_EXTERN int                            dirtMode Q_ASSIGN( 0 );
2261 Q_EXTERN float                          dirtDepth Q_ASSIGN( 128.0f );
2262 Q_EXTERN float                          dirtScale Q_ASSIGN( 1.0f );
2263 Q_EXTERN float                          dirtGain Q_ASSIGN( 1.0f );
2264
2265 /* 27: floodlighting */
2266 Q_EXTERN qboolean                                       debugnormals Q_ASSIGN( qfalse );
2267 Q_EXTERN qboolean                                       floodlighty Q_ASSIGN( qfalse );
2268 Q_EXTERN qboolean                                       floodlight_lowquality Q_ASSIGN( qfalse );
2269 Q_EXTERN vec3_t                                         floodlightRGB;
2270 Q_EXTERN float                                          floodlightIntensity Q_ASSIGN( 512.0f );
2271 Q_EXTERN float                                          floodlightDistance Q_ASSIGN( 1024.0f );
2272 Q_EXTERN float                                          floodlightDirectionScale Q_ASSIGN( 1.0f );
2273
2274 Q_EXTERN qboolean                       dump Q_ASSIGN( qfalse );
2275 Q_EXTERN qboolean                       debug Q_ASSIGN( qfalse );
2276 Q_EXTERN qboolean                       debugUnused Q_ASSIGN( qfalse );
2277 Q_EXTERN qboolean                       debugAxis Q_ASSIGN( qfalse );
2278 Q_EXTERN qboolean                       debugCluster Q_ASSIGN( qfalse );
2279 Q_EXTERN qboolean                       debugOrigin Q_ASSIGN( qfalse );
2280 Q_EXTERN qboolean                       lightmapBorder Q_ASSIGN( qfalse );
2281
2282 /* longest distance across the map */
2283 Q_EXTERN float                          maxMapDistance Q_ASSIGN( 0 );
2284
2285 /* for run time tweaking of light sources */
2286 Q_EXTERN float                          pointScale Q_ASSIGN( 7500.0f );
2287 Q_EXTERN float                          spotScale Q_ASSIGN( 7500.0f );
2288 Q_EXTERN float                          areaScale Q_ASSIGN( 0.25f );
2289 Q_EXTERN float                          skyScale Q_ASSIGN( 1.0f );
2290 Q_EXTERN float                          bounceScale Q_ASSIGN( 0.25f );
2291
2292 /* jal: alternative angle attenuation curve */
2293 Q_EXTERN qboolean                       lightAngleHL Q_ASSIGN( qfalse );
2294  
2295 /* vortex: gridscale and gridambientscale */
2296 Q_EXTERN float                          gridScale Q_ASSIGN( 1.0f );
2297 Q_EXTERN float                          gridAmbientScale Q_ASSIGN( 1.0f );
2298 Q_EXTERN float                          gridDirectionality Q_ASSIGN( 1.0f );
2299 Q_EXTERN float                          gridAmbientDirectionality Q_ASSIGN( 0.0f );
2300 Q_EXTERN qboolean                       inGrid Q_ASSIGN(0);
2301
2302 /* ydnar: lightmap gamma/compensation */
2303 Q_EXTERN float                          lightmapGamma Q_ASSIGN( 1.0f );
2304 Q_EXTERN float                          lightmapsRGB Q_ASSIGN( qfalse );
2305 Q_EXTERN float                          texturesRGB Q_ASSIGN( qfalse );
2306 Q_EXTERN float                          colorsRGB Q_ASSIGN( qfalse );
2307 Q_EXTERN float                          lightmapExposure Q_ASSIGN( 0.0f );
2308 Q_EXTERN float                          lightmapCompensate Q_ASSIGN( 1.0f );
2309
2310 /* ydnar: for runtime tweaking of falloff tolerance */
2311 Q_EXTERN float                          falloffTolerance Q_ASSIGN( 1.0f );
2312 Q_EXTERN qboolean                       exactPointToPolygon Q_ASSIGN( qtrue );
2313 Q_EXTERN float                          formFactorValueScale Q_ASSIGN( 3.0f );
2314 Q_EXTERN float                          linearScale Q_ASSIGN( 1.0f / 8000.0f );
2315
2316 // for .ase conversion
2317 Q_EXTERN qboolean                       shadersAsBitmap Q_ASSIGN( qfalse );
2318 Q_EXTERN qboolean                       lightmapsAsTexcoord Q_ASSIGN( qfalse );
2319
2320 Q_EXTERN light_t                        *lights;
2321 Q_EXTERN int                            numPointLights;
2322 Q_EXTERN int                            numSpotLights;
2323 Q_EXTERN int                            numSunLights;
2324 Q_EXTERN int                            numAreaLights;
2325
2326 /* ydnar: for luxel placement */
2327 Q_EXTERN int                            numSurfaceClusters, maxSurfaceClusters;
2328 Q_EXTERN int                            *surfaceClusters;
2329
2330 /* ydnar: for radiosity */
2331 Q_EXTERN int                            numDiffuseLights;
2332 Q_EXTERN int                            numBrushDiffuseLights;
2333 Q_EXTERN int                            numTriangleDiffuseLights;
2334 Q_EXTERN int                            numPatchDiffuseLights;
2335
2336 /* ydnar: general purpose extra copy of drawvert list */
2337 Q_EXTERN bspDrawVert_t          *yDrawVerts;
2338
2339 /* ydnar: for tracing statistics */
2340 Q_EXTERN int                            minSurfacesTested;
2341 Q_EXTERN int                            maxSurfacesTested;
2342 Q_EXTERN int                            totalSurfacesTested;
2343 Q_EXTERN int                            totalTraces;
2344
2345 Q_EXTERN FILE                           *dumpFile;
2346
2347 Q_EXTERN int                            c_visible, c_occluded;
2348 Q_EXTERN int                            c_subsampled;   /* ydnar */
2349
2350 Q_EXTERN int                            defaultLightSubdivide Q_ASSIGN( 999 );
2351
2352 Q_EXTERN vec3_t                         ambientColor;
2353 Q_EXTERN vec3_t                         minLight, minVertexLight, minGridLight;
2354
2355 Q_EXTERN int                            *entitySurface;
2356 Q_EXTERN vec3_t                         *surfaceOrigin;
2357
2358 Q_EXTERN vec3_t                         sunDirection;
2359 Q_EXTERN vec3_t                         sunLight;
2360
2361 /* tracing */
2362 Q_EXTERN int                            c_totalTrace;
2363 Q_EXTERN int                            c_cullTrace, c_testTrace;
2364 Q_EXTERN int                            c_testFacets;
2365
2366 /* ydnar: light optimization */
2367 Q_EXTERN float                          subdivideThreshold Q_ASSIGN( DEFAULT_SUBDIVIDE_THRESHOLD );
2368
2369 Q_EXTERN int                            numOpaqueBrushes, maxOpaqueBrush;
2370 Q_EXTERN byte                           *opaqueBrushes;
2371
2372 Q_EXTERN int                            numLights;
2373 Q_EXTERN int                            numCulledLights;
2374
2375 Q_EXTERN int                            gridBoundsCulled;
2376 Q_EXTERN int                            gridEnvelopeCulled;
2377
2378 Q_EXTERN int                            lightsBoundsCulled;
2379 Q_EXTERN int                            lightsEnvelopeCulled;
2380 Q_EXTERN int                            lightsPlaneCulled;
2381 Q_EXTERN int                            lightsClusterCulled;
2382
2383 /* ydnar: radiosity */
2384 Q_EXTERN float                          diffuseSubdivide Q_ASSIGN( 256.0f );
2385 Q_EXTERN float                          minDiffuseSubdivide Q_ASSIGN( 64.0f );
2386 Q_EXTERN int                            numDiffuseSurfaces Q_ASSIGN( 0 );
2387
2388 /* ydnar: list of surface information necessary for lightmap calculation */
2389 Q_EXTERN surfaceInfo_t          *surfaceInfos Q_ASSIGN( NULL );
2390
2391 /* ydnar: sorted list of surfaces */
2392 Q_EXTERN int                            *sortSurfaces Q_ASSIGN( NULL );
2393
2394 /* clumps of surfaces that share a raw lightmap */
2395 Q_EXTERN int                            numLightSurfaces Q_ASSIGN( 0 );
2396 Q_EXTERN int                            *lightSurfaces Q_ASSIGN( NULL );
2397
2398 /* raw lightmaps */
2399 Q_EXTERN int                            numRawSuperLuxels Q_ASSIGN( 0 );
2400 Q_EXTERN int                            numRawLightmaps Q_ASSIGN( 0 );
2401 Q_EXTERN rawLightmap_t          *rawLightmaps Q_ASSIGN( NULL );
2402 Q_EXTERN int                            *sortLightmaps Q_ASSIGN( NULL );
2403
2404 /* vertex luxels */
2405 Q_EXTERN float                          *vertexLuxels[ MAX_LIGHTMAPS ];
2406 Q_EXTERN float                          *radVertexLuxels[ MAX_LIGHTMAPS ];
2407
2408 /* bsp lightmaps */
2409 Q_EXTERN int                            numLightmapShaders Q_ASSIGN( 0 );
2410 Q_EXTERN int                            numSolidLightmaps Q_ASSIGN( 0 );
2411 Q_EXTERN int                            numOutLightmaps Q_ASSIGN( 0 );
2412 Q_EXTERN int                            numBSPLightmaps Q_ASSIGN( 0 );
2413 Q_EXTERN int                            numExtLightmaps Q_ASSIGN( 0 );
2414 Q_EXTERN outLightmap_t          *outLightmaps Q_ASSIGN( NULL );
2415
2416 /* vortex: per surface floodlight statictics */
2417 Q_EXTERN int                            numSurfacesFloodlighten Q_ASSIGN( 0 );
2418
2419 /* grid points */
2420 Q_EXTERN int                            numRawGridPoints Q_ASSIGN( 0 );
2421 Q_EXTERN rawGridPoint_t         *rawGridPoints Q_ASSIGN( NULL );
2422
2423 Q_EXTERN int                            numSurfsVertexLit Q_ASSIGN( 0 );
2424 Q_EXTERN int                            numSurfsVertexForced Q_ASSIGN( 0 );
2425 Q_EXTERN int                            numSurfsVertexApproximated Q_ASSIGN( 0 );
2426 Q_EXTERN int                            numSurfsLightmapped Q_ASSIGN( 0 );
2427 Q_EXTERN int                            numPlanarsLightmapped Q_ASSIGN( 0 );
2428 Q_EXTERN int                            numNonPlanarsLightmapped Q_ASSIGN( 0 );
2429 Q_EXTERN int                            numPatchesLightmapped Q_ASSIGN( 0 );
2430 Q_EXTERN int                            numPlanarPatchesLightmapped Q_ASSIGN( 0 );
2431
2432 Q_EXTERN int                            numLuxels Q_ASSIGN( 0 );
2433 Q_EXTERN int                            numLuxelsMapped Q_ASSIGN( 0 );
2434 Q_EXTERN int                            numLuxelsOccluded Q_ASSIGN( 0 );
2435 Q_EXTERN int                            numLuxelsIlluminated Q_ASSIGN( 0 );
2436 Q_EXTERN int                            numVertsIlluminated Q_ASSIGN( 0 );
2437
2438 /* lightgrid */
2439 Q_EXTERN vec3_t                         gridMins;
2440 Q_EXTERN int                            gridBounds[ 3 ];
2441 Q_EXTERN vec3_t                         gridSize
2442 #ifndef MAIN_C
2443                                                         ;
2444 #else
2445                                                         = { 64, 64, 128 };
2446 #endif
2447
2448
2449
2450 /* -------------------------------------------------------------------------------
2451
2452 abstracted bsp globals
2453
2454 ------------------------------------------------------------------------------- */
2455
2456 Q_EXTERN int                            numEntities Q_ASSIGN( 0 );
2457 Q_EXTERN int                            numBSPEntities Q_ASSIGN( 0 );
2458 Q_EXTERN int                            allocatedEntities Q_ASSIGN( 0 );
2459 Q_EXTERN entity_t*                      entities Q_ASSIGN(NULL);
2460
2461 Q_EXTERN int                            numBSPModels Q_ASSIGN( 0 );
2462 Q_EXTERN int                            allocatedBSPModels Q_ASSIGN( 0 );
2463 Q_EXTERN bspModel_t*            bspModels Q_ASSIGN(NULL);
2464
2465 Q_EXTERN int                            numBSPShaders Q_ASSIGN( 0 );
2466 Q_EXTERN int                            allocatedBSPShaders Q_ASSIGN( 0 );
2467 Q_EXTERN bspShader_t*           bspShaders Q_ASSIGN(0);
2468
2469 Q_EXTERN int                            bspEntDataSize Q_ASSIGN( 0 );
2470 Q_EXTERN int                            allocatedBSPEntData Q_ASSIGN( 0 );
2471 Q_EXTERN char                           *bspEntData Q_ASSIGN(0);
2472
2473 Q_EXTERN int                            numBSPLeafs Q_ASSIGN( 0 );
2474 Q_EXTERN bspLeaf_t                      bspLeafs[ MAX_MAP_LEAFS ];
2475
2476 Q_EXTERN int                            numBSPPlanes Q_ASSIGN( 0 );
2477 Q_EXTERN int                            allocatedBSPPlanes Q_ASSIGN(0);
2478 Q_EXTERN bspPlane_t                     *bspPlanes;
2479
2480 Q_EXTERN int                            numBSPNodes Q_ASSIGN( 0 );
2481 Q_EXTERN int                            allocatedBSPNodes Q_ASSIGN( 0 );
2482 Q_EXTERN bspNode_t*                     bspNodes Q_ASSIGN(NULL);
2483
2484 Q_EXTERN int                            numBSPLeafSurfaces Q_ASSIGN( 0 );
2485 Q_EXTERN int                            allocatedBSPLeafSurfaces Q_ASSIGN( 0 );
2486 Q_EXTERN int*                           bspLeafSurfaces Q_ASSIGN(NULL);
2487
2488 Q_EXTERN int                            numBSPLeafBrushes Q_ASSIGN( 0 );
2489 Q_EXTERN int                            allocatedBSPLeafBrushes Q_ASSIGN( 0 );
2490 Q_EXTERN int*                           bspLeafBrushes Q_ASSIGN(NULL);
2491
2492 Q_EXTERN int                            numBSPBrushes Q_ASSIGN( 0 );
2493 Q_EXTERN int                            allocatedBSPBrushes Q_ASSIGN( 0 );
2494 Q_EXTERN bspBrush_t*            bspBrushes Q_ASSIGN(NULL);
2495
2496 Q_EXTERN int                            numBSPBrushSides Q_ASSIGN( 0 );
2497 Q_EXTERN int                            allocatedBSPBrushSides Q_ASSIGN( 0 );
2498 Q_EXTERN bspBrushSide_t*        bspBrushSides Q_ASSIGN(NULL);
2499
2500 Q_EXTERN int                            numBSPLightBytes Q_ASSIGN( 0 );
2501 Q_EXTERN byte                           *bspLightBytes Q_ASSIGN( NULL );
2502
2503 //%     Q_EXTERN int                            numBSPGridPoints Q_ASSIGN( 0 );
2504 //%     Q_EXTERN byte                           *bspGridPoints Q_ASSIGN( NULL );
2505
2506 Q_EXTERN int                            numBSPGridPoints Q_ASSIGN( 0 );
2507 Q_EXTERN bspGridPoint_t         *bspGridPoints Q_ASSIGN( NULL );
2508
2509 Q_EXTERN int                            numBSPVisBytes Q_ASSIGN( 0 );
2510 Q_EXTERN byte                           bspVisBytes[ MAX_MAP_VISIBILITY ];
2511
2512 Q_EXTERN int                            numBSPDrawVerts Q_ASSIGN( 0 );
2513 Q_EXTERN bspDrawVert_t                  *bspDrawVerts Q_ASSIGN( NULL );
2514
2515 Q_EXTERN int                            numBSPDrawIndexes Q_ASSIGN( 0 );
2516 Q_EXTERN int                            allocatedBSPDrawIndexes Q_ASSIGN( 0 );
2517 Q_EXTERN int                            *bspDrawIndexes Q_ASSIGN(NULL);
2518
2519 Q_EXTERN int                            numBSPDrawSurfaces Q_ASSIGN( 0 );
2520 Q_EXTERN bspDrawSurface_t       *bspDrawSurfaces Q_ASSIGN( NULL );
2521
2522 Q_EXTERN int                            numBSPFogs Q_ASSIGN( 0 );
2523 Q_EXTERN bspFog_t                       bspFogs[ MAX_MAP_FOGS ];
2524
2525 Q_EXTERN int                            numBSPAds Q_ASSIGN( 0 );
2526 Q_EXTERN bspAdvertisement_t     bspAds[ MAX_MAP_ADVERTISEMENTS ];
2527
2528 #define AUTOEXPAND_BY_REALLOC(ptr, reqitem, allocated, def) \
2529         do \
2530         { \
2531                 if(reqitem >= allocated) \
2532                 { \
2533                         if(allocated == 0) \
2534                                 allocated = def; \
2535                         while(reqitem >= allocated && allocated) \
2536                                 allocated *= 2; \
2537                         if(!allocated || allocated > 2147483647 / (int)sizeof(*ptr)) \
2538                         { \
2539                                 Error(#ptr " over 2 GB"); \
2540                         } \
2541                         ptr = realloc(ptr, sizeof(*ptr) * allocated); \
2542                         if(!ptr) \
2543                                 Error(#ptr " out of memory"); \
2544                 } \
2545         } \
2546         while(0)
2547
2548 #define AUTOEXPAND_BY_REALLOC_BSP(suffix, def) AUTOEXPAND_BY_REALLOC(bsp##suffix, numBSP##suffix, allocatedBSP##suffix, def)
2549
2550 #define Image_LinearFloatFromsRGBFloat(c) (((c) <= 0.04045f) ? (c) * (1.0f / 12.92f) : (float)pow(((c) + 0.055f)*(1.0f/1.055f), 2.4f))
2551 #define Image_sRGBFloatFromLinearFloat(c) (((c) < 0.0031308f) ? (c) * 12.92f : 1.055f * (float)pow((c), 1.0f/2.4f) - 0.055f)
2552
2553 /* end marker */
2554 #endif