]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/shaders.c
initial
[divverent/netradiant.git] / tools / quake3 / q3map2 / shaders.c
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22 ----------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define SHADERS_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 ColorMod()
43 routines for dealing with vertex color/alpha modification
44 */
45
46 void ColorMod( colorMod_t *cm, int numVerts, bspDrawVert_t *drawVerts )
47 {
48         int                             i, j, k;
49         float                   c;
50         vec4_t                  mult, add;
51         bspDrawVert_t   *dv;
52         colorMod_t              *cm2;
53         
54         
55         /* dummy check */
56         if( cm == NULL || numVerts < 1 || drawVerts == NULL )
57                 return;
58         
59         
60         /* walk vertex list */
61         for( i = 0; i < numVerts; i++ )
62         {
63                 /* get vertex */
64                 dv = &drawVerts[ i ];
65                 
66                 /* walk colorMod list */
67                 for( cm2 = cm; cm2 != NULL; cm2 = cm2->next )
68                 {
69                         /* default */
70                         VectorSet( mult, 1.0f, 1.0f, 1.0f );
71                         mult[ 3 ] = 1.0f;
72                         VectorSet( add, 0.0f, 0.0f, 0.0f );
73                         mult[ 3 ] = 0.0f;
74                         
75                         /* switch on type */
76                         switch( cm2->type )
77                         {
78                                 case CM_COLOR_SET:
79                                         VectorClear( mult );
80                                         VectorScale( cm2->data, 255.0f, add );
81                                         break;
82                                 
83                                 case CM_ALPHA_SET:
84                                         mult[ 3 ] = 0.0f;
85                                         add[ 3 ] = cm2->data[ 0 ] * 255.0f;
86                                         break;
87                                 
88                                 case CM_COLOR_SCALE:
89                                         VectorCopy( cm2->data, mult );
90                                         break;
91                                 
92                                 case CM_ALPHA_SCALE:
93                                         mult[ 3 ] = cm2->data[ 0 ];
94                                         break;
95                                 
96                                 case CM_COLOR_DOT_PRODUCT:
97                                         c = DotProduct( dv->normal, cm2->data );
98                                         VectorSet( mult, c, c, c );
99                                         break;
100                                 
101                                 case CM_ALPHA_DOT_PRODUCT:
102                                         mult[ 3 ] = DotProduct( dv->normal, cm2->data );
103                                         break;
104                                 
105                                 case CM_COLOR_DOT_PRODUCT_2:
106                                         c = DotProduct( dv->normal, cm2->data );
107                                         c *= c;
108                                         VectorSet( mult, c, c, c );
109                                         break;
110                                 
111                                 case CM_ALPHA_DOT_PRODUCT_2:
112                                         mult[ 3 ] = DotProduct( dv->normal, cm2->data );
113                                         mult[ 3 ] *= mult[ 3 ];
114                                         break;
115                                 
116                                 default:
117                                         break;
118                         }
119                         
120                         /* apply mod */
121                         for( j = 0; j < MAX_LIGHTMAPS; j++ )
122                         {
123                                 for( k = 0; k < 4; k++ )
124                                 {
125                                         c = (mult[ k ] * dv->color[ j ][ k ]) + add[ k ];
126                                         if( c < 0 )
127                                                 c = 0;
128                                         else if( c > 255 )
129                                                 c = 255;
130                                         dv->color[ j ][ k ] = c;
131                                 }
132                         }
133                 }
134         }
135 }
136
137
138
139 /*
140 TCMod*()
141 routines for dealing with a 3x3 texture mod matrix
142 */
143
144 void TCMod( tcMod_t mod, float st[ 2 ] )
145 {
146         float   old[ 2 ];
147         
148         
149         old[ 0 ] = st[ 0 ];
150         old[ 1 ] = st[ 1 ];
151         st[ 0 ] = (mod[ 0 ][ 0 ] * old[ 0 ]) + (mod[ 0 ][ 1 ] * old[ 1 ]) + mod[ 0 ][ 2 ];
152         st[ 1 ] = (mod[ 1 ][ 0 ] * old[ 0 ]) + (mod[ 1 ][ 1 ] * old[ 1 ]) + mod[ 1 ][ 2 ];
153 }
154
155
156 void TCModIdentity( tcMod_t mod )
157 {
158         mod[ 0 ][ 0 ] = 1.0f;   mod[ 0 ][ 1 ] = 0.0f;   mod[ 0 ][ 2 ] = 0.0f;
159         mod[ 1 ][ 0 ] = 0.0f;   mod[ 1 ][ 1 ] = 1.0f;   mod[ 1 ][ 2 ] = 0.0f;
160         mod[ 2 ][ 0 ] = 0.0f;   mod[ 2 ][ 1 ] = 0.0f;   mod[ 2 ][ 2 ] = 1.0f;   /* this row is only used for multiples, not transformation */
161 }
162
163
164 void TCModMultiply( tcMod_t a, tcMod_t b, tcMod_t out )
165 {
166         int             i;
167         
168         
169         for( i = 0; i < 3; i++ )
170         {
171                 out[ i ][ 0 ] = (a[ i ][ 0 ] * b[ 0 ][ 0 ]) + (a[ i ][ 1 ] * b[ 1 ][ 0 ]) + (a[ i ][ 2 ] * b[ 2 ][ 0 ]);
172                 out[ i ][ 1 ] = (a[ i ][ 0 ] * b[ 0 ][ 1 ]) + (a[ i ][ 1 ] * b[ 1 ][ 1 ]) + (a[ i ][ 2 ] * b[ 2 ][ 1 ]);
173                 out[ i ][ 2 ] = (a[ i ][ 0 ] * b[ 0 ][ 2 ]) + (a[ i ][ 1 ] * b[ 1 ][ 2 ]) + (a[ i ][ 2 ] * b[ 2 ][ 2 ]);
174         }
175 }
176
177
178 void TCModTranslate( tcMod_t mod, float s, float t )
179 {
180         mod[ 0 ][ 2 ] += s;
181         mod[ 1 ][ 2 ] += t;
182 }
183
184
185 void TCModScale( tcMod_t mod, float s, float t )
186 {
187         mod[ 0 ][ 0 ] *= s;
188         mod[ 1 ][ 1 ] *= t;
189 }
190
191
192 void TCModRotate( tcMod_t mod, float euler )
193 {
194         tcMod_t old, temp;
195         float   radians, sinv, cosv;
196         
197         
198         memcpy( old, mod, sizeof( tcMod_t ) );
199         TCModIdentity( temp );
200
201         radians = euler / 180 * Q_PI;
202         sinv = sin( radians );
203         cosv = cos( radians );
204
205         temp[ 0 ][ 0 ] = cosv;  temp[ 0 ][ 1 ] = -sinv;
206         temp[ 1 ][ 0 ] = sinv;  temp[ 1 ][ 1 ] = cosv;
207         
208         TCModMultiply( old, temp, mod );
209 }
210
211
212
213 /*
214 ApplySurfaceParm() - ydnar
215 applies a named surfaceparm to the supplied flags
216 */
217
218 qboolean ApplySurfaceParm( char *name, int *contentFlags, int *surfaceFlags, int *compileFlags )
219 {
220         int                             i, fake;
221         surfaceParm_t   *sp;
222         
223         
224         /* dummy check */
225         if( name == NULL )
226                 name = "";
227         if( contentFlags == NULL )
228                 contentFlags = &fake;
229         if( surfaceFlags == NULL )
230                 surfaceFlags = &fake;
231         if( compileFlags == NULL )
232                 compileFlags = &fake;
233         
234         /* walk the current game's surfaceparms */
235         sp = game->surfaceParms;
236         while( sp->name != NULL )
237         {
238                 /* match? */
239                 if( !Q_stricmp( name, sp->name ) )
240                 {
241                         /* clear and set flags */
242                         *contentFlags &= ~(sp->contentFlagsClear);
243                         *contentFlags |= sp->contentFlags;
244                         *surfaceFlags &= ~(sp->surfaceFlagsClear);
245                         *surfaceFlags |= sp->surfaceFlags;
246                         *compileFlags &= ~(sp->compileFlagsClear);
247                         *compileFlags |= sp->compileFlags;
248                         
249                         /* return ok */
250                         return qtrue;
251                 }
252                 
253                 /* next */
254                 sp++;
255         }
256         
257         /* check custom info parms */
258         for( i = 0; i < numCustSurfaceParms; i++ )
259         {
260                 /* get surfaceparm */
261                 sp = &custSurfaceParms[ i ];
262                 
263                 /* match? */
264                 if( !Q_stricmp( name, sp->name ) )
265                 {
266                         /* clear and set flags */
267                         *contentFlags &= ~(sp->contentFlagsClear);
268                         *contentFlags |= sp->contentFlags;
269                         *surfaceFlags &= ~(sp->surfaceFlagsClear);
270                         *surfaceFlags |= sp->surfaceFlags;
271                         *compileFlags &= ~(sp->compileFlagsClear);
272                         *compileFlags |= sp->compileFlags;
273                         
274                         /* return ok */
275                         return qtrue;
276                 }
277         }
278         
279         /* no matching surfaceparm found */
280         return qfalse;
281 }
282
283
284
285 /*
286 BeginMapShaderFile() - ydnar
287 erases and starts a new map shader script
288 */
289
290 void BeginMapShaderFile( const char *mapFile )
291 {
292         char    base[ 1024 ];
293         int             len;
294         
295
296         /* dummy check */
297         mapName[ 0 ] = '\0';
298         mapShaderFile[ 0 ] = '\0';
299         if( mapFile == NULL || mapFile[ 0 ] == '\0' )
300                 return;
301         
302         /* copy map name */
303         strcpy( base, mapFile );
304         StripExtension( base );
305         
306         /* extract map name */
307         len = strlen( base ) - 1;
308         while( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )
309                 len--;
310         strcpy( mapName, &base[ len + 1 ] );
311         base[ len ] = '\0';
312         if( len <= 0 )
313                 return;
314         
315         /* append ../scripts/q3map2_<mapname>.shader */
316         sprintf( mapShaderFile, "%s/../%s/q3map2_%s.shader", base, game->shaderPath, mapName );
317         Sys_FPrintf( SYS_VRB, "Map has shader script %s\n", mapShaderFile );
318         
319         /* remove it */
320         remove( mapShaderFile );
321         
322         /* stop making warnings about missing images */
323         warnImage = qfalse;
324 }
325
326
327
328 /*
329 WriteMapShaderFile() - ydnar
330 writes a shader to the map shader script
331 */
332
333 void WriteMapShaderFile( void )
334 {
335         FILE                    *file;
336         shaderInfo_t    *si;
337         int                             i, num;
338         
339         
340         /* dummy check */
341         if( mapShaderFile[ 0 ] == '\0' )
342                 return;
343         
344         /* are there any custom shaders? */
345         for( i = 0, num = 0; i < numShaderInfo; i++ )
346         {
347                 if( shaderInfo[ i ].custom ) 
348                         break;
349         }
350         if( i == numShaderInfo )
351                 return;
352         
353         /* note it */
354         Sys_FPrintf( SYS_VRB, "--- WriteMapShaderFile ---\n");
355         Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile );
356         
357         /* open shader file */
358         file = fopen( mapShaderFile, "w" );
359         if( file == NULL )
360         {
361                 Sys_Printf( "WARNING: Unable to open map shader file %s for writing\n", mapShaderFile );
362                 return;
363         }
364         
365         /* print header */
366         fprintf( file,
367                 "// Custom shader file for %s.bsp\n"
368                 "// Generated by Q3Map2 (ydnar)\n"
369                 "// Do not edit! This file is overwritten on recompiles.\n\n",
370                 mapName );
371         
372         /* walk the shader list */
373         for( i = 0, num = 0; i < numShaderInfo; i++ )
374         {
375                 /* get the shader and print it */
376                 si = &shaderInfo[ i ];
377                 if( si->custom == qfalse || si->shaderText == NULL || si->shaderText[ 0 ] == '\0' )
378                         continue;
379                 num++;
380
381                 /* print it to the file */
382                 fprintf( file, "%s%s\n", si->shader, si->shaderText );
383                 //Sys_Printf( "%s%s\n", si->shader, si->shaderText ); /* FIXME: remove debugging code */
384                 
385                 Sys_FPrintf( SYS_VRB, "." );
386         }
387         
388         /* close the shader */
389         fflush( file );
390         fclose( file );
391         
392         Sys_FPrintf( SYS_VRB, "\n" );
393         
394         /* print some stats */
395         Sys_Printf( "%9d custom shaders emitted\n", num );
396 }
397
398
399
400 /*
401 CustomShader() - ydnar
402 sets up a custom map shader
403 */
404
405 shaderInfo_t *CustomShader( shaderInfo_t *si, char *find, char *replace )
406 {
407         shaderInfo_t    *csi;
408         char                    shader[ MAX_QPATH ];
409         char                    *s;
410         int                             loc;
411         MHASH                   mh;
412         byte                    digest[ 16 ];
413         char                    *srcShaderText, temp[ 8192 ], shaderText[ 8192 ];       /* ydnar: fixme (make this bigger?) */
414         
415         
416         /* dummy check */
417         if( si == NULL )
418                 return ShaderInfoForShader( "default" );
419         
420         /* default shader text source */
421         srcShaderText = si->shaderText;
422         
423         /* et: implicitMap */
424         if( si->implicitMap == IM_OPAQUE )
425         {
426                 srcShaderText = temp;
427                 sprintf( temp, "\n"
428                         "{ // Q3Map2 defaulted (implicitMap)\n"
429                         "\t{\n"
430                         "\t\tmap $lightmap\n"
431                         "\t\trgbGen identity\n"
432                         "\t}\n"
433                         "\tq3map_styleMarker\n"
434                         "\t{\n"
435                         "\t\tmap %s\n"
436                         "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
437                         "\t\trgbGen identity\n"
438                         "\t}\n"
439                         "}\n",
440                         si->implicitImagePath );
441         }
442         
443         /* et: implicitMask */
444         else if( si->implicitMap == IM_MASKED )
445         {
446                 srcShaderText = temp;
447                 sprintf( temp, "\n"
448                         "{ // Q3Map2 defaulted (implicitMask)\n"
449                         "\tcull none\n"
450                         "\t{\n"
451                         "\t\tmap %s\n"
452                         "\t\talphaFunc GE128\n"
453                         "\t\tdepthWrite\n"
454                         "\t}\n"
455                         "\t{\n"
456                         "\t\tmap $lightmap\n"
457                         "\t\trgbGen identity\n"
458                         "\t\tdepthFunc equal\n"
459                         "\t}\n"
460                         "\tq3map_styleMarker\n"
461                         "\t{\n"
462                         "\t\tmap %s\n"
463                         "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
464                         "\t\tdepthFunc equal\n"
465                         "\t\trgbGen identity\n"
466                         "\t}\n"
467                         "}\n",
468                         si->implicitImagePath,
469                         si->implicitImagePath );
470         }
471         
472         /* et: implicitBlend */
473         else if( si->implicitMap == IM_BLEND )
474         {
475                 srcShaderText = temp;
476                 sprintf( temp, "\n"
477                         "{ // Q3Map2 defaulted (implicitBlend)\n"
478                         "\tcull none\n"
479                         "\t{\n"
480                         "\t\tmap %s\n"
481                         "\t\tblendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA\n"
482                         "\t}\n"
483                         "\t{\n"
484                         "\t\tmap $lightmap\n"
485                         "\t\trgbGen identity\n"
486                         "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
487                         "\t}\n"
488                         "\tq3map_styleMarker\n"
489                         "}\n",
490                         si->implicitImagePath );
491         }
492         
493         /* default shader text */
494         else if( srcShaderText == NULL )
495         {
496                 srcShaderText = temp;
497                 sprintf( temp, "\n"
498                         "{ // Q3Map2 defaulted\n"
499                         "\t{\n"
500                         "\t\tmap $lightmap\n"
501                         "\t\trgbGen identity\n"
502                         "\t}\n"
503                         "\tq3map_styleMarker\n"
504                         "\t{\n"
505                         "\t\tmap %s.tga\n"
506                         "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
507                         "\t\trgbGen identity\n"
508                         "\t}\n"
509                         "}\n",
510                         si->shader );
511         }
512         
513         /* error check */
514         if( (strlen( mapName ) + 1 + 32) > MAX_QPATH )
515                 Error( "Custom shader name length (%d) exceeded. Shorten your map name.\n", MAX_QPATH );
516         
517         /* do some bad find-replace */
518         s = strstr( srcShaderText, find );
519         if( s == NULL )
520                 //%     strcpy( shaderText, srcShaderText );
521                 return si;      /* testing just using the existing shader if this fails */
522         else
523         {
524                 /* substitute 'find' with 'replace' */
525                 loc = s - srcShaderText;
526                 strcpy( shaderText, srcShaderText );
527                 shaderText[ loc ] = '\0';
528                 strcat( shaderText, replace );
529                 strcat( shaderText, &srcShaderText[ loc + strlen( find ) ] );
530         }
531         
532         /* make md5 hash of the shader text */
533         mh = mhash_init( MHASH_MD5 );
534         if( !mh )
535                 Error( "Unable to initialize MD5 hash context" );
536         mhash( mh, shaderText, strlen( shaderText ) );
537         mhash_deinit( mh, digest );
538         
539         /* mangle hash into a shader name */
540         sprintf( shader, "%s/%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", mapName,
541                 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ], 
542                 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ], digest[ 15 ] );
543         
544         /* get shader */
545         csi = ShaderInfoForShader( shader );
546         
547         /* might be a preexisting shader */
548         if( csi->custom )
549                 return csi;
550         
551         /* clone the existing shader and rename */
552         memcpy( csi, si, sizeof( shaderInfo_t ) );
553         strcpy( csi->shader, shader );
554         csi->custom = qtrue;
555         
556         /* store new shader text */
557         csi->shaderText = safe_malloc( strlen( shaderText ) + 1 );
558         strcpy( csi->shaderText, shaderText );  /* LEAK! */
559         
560         /* return it */
561         return csi;
562 }
563
564
565
566 /*
567 EmitVertexRemapShader()
568 adds a vertexremapshader key/value pair to worldspawn
569 */
570
571 void EmitVertexRemapShader( char *from, char *to )
572 {
573         MHASH                   mh;
574         byte                    digest[ 16 ];
575         char                    key[ 64 ], value[ 256 ];
576         
577         
578         /* dummy check */
579         if( from == NULL || from[ 0 ] == '\0' ||
580                 to == NULL || to[ 0 ] == '\0' )
581                 return;
582         
583         /* build value */
584         sprintf( value, "%s;%s", from, to );
585         
586         /* make md5 hash */
587         mh = mhash_init( MHASH_MD5 );
588         if( !mh )
589                 Error( "Unable to initialize MD5 hash context" );
590         mhash( mh, value, strlen( value ) );
591         mhash_deinit( mh, digest );
592
593         /* make key (this is annoying, as vertexremapshader is precisely 17 characters,
594            which is one too long, so we leave off the last byte of the md5 digest) */
595         sprintf( key, "vertexremapshader%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
596                 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ], 
597                 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ] );       /* no: digest[ 15 ] */
598         
599         /* add key/value pair to worldspawn */
600         SetKeyValue( &entities[ 0 ], key, value );
601 }
602
603
604
605 /*
606 AllocShaderInfo()
607 allocates and initializes a new shader
608 */
609
610 static shaderInfo_t     *AllocShaderInfo( void )
611 {
612         shaderInfo_t    *si;
613         
614         
615         /* allocate? */
616         if( shaderInfo == NULL )
617         {
618                 shaderInfo = safe_malloc( sizeof( shaderInfo_t ) * MAX_SHADER_INFO );
619                 numShaderInfo = 0;
620         }
621         
622         /* bounds check */
623         if( numShaderInfo == MAX_SHADER_INFO )
624                 Error( "MAX_SHADER_INFO exceeded. Remove some PK3 files or shader scripts from shaderlist.txt and try again." );
625         si = &shaderInfo[ numShaderInfo ];
626         numShaderInfo++;
627         
628         /* ydnar: clear to 0 first */
629         memset( si, 0, sizeof( shaderInfo_t ) );
630         
631         /* set defaults */
632         ApplySurfaceParm( "default", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
633         
634         si->backsplashFraction = DEF_BACKSPLASH_FRACTION;
635         si->backsplashDistance = DEF_BACKSPLASH_DISTANCE;
636         
637         si->bounceScale = DEF_RADIOSITY_BOUNCE;
638         
639         si->lightStyle = LS_NORMAL;
640         
641         si->polygonOffset = qfalse;
642         
643         si->shadeAngleDegrees = 0.0f;
644         si->lightmapSampleSize = 0;
645         si->lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;
646         si->patchShadows = qfalse;
647         si->vertexShadows = qtrue;      /* ydnar: changed default behavior */
648         si->forceSunlight = qfalse;
649         si->vertexScale = 1.0;
650         si->notjunc = qfalse;
651         
652         /* ydnar: set texture coordinate transform matrix to identity */
653         TCModIdentity( si->mod );
654         
655         /* ydnar: lightmaps can now be > 128x128 in certain games or an externally generated tga */
656         si->lmCustomWidth = lmCustomSize;
657         si->lmCustomHeight = lmCustomSize;
658         
659         /* return to sender */
660         return si;
661 }
662
663
664
665 /*
666 FinishShader() - ydnar
667 sets a shader's width and height among other things
668 */
669
670 void FinishShader( shaderInfo_t *si )
671 {
672         int             x, y;
673         float   st[ 2 ], o[ 2 ], dist, bestDist;
674         vec4_t  color, bestColor, delta;
675         
676
677         /* don't double-dip */
678         if( si->finished )
679                 return;
680         
681         /* if they're explicitly set, copy from image size */
682         if( si->shaderWidth == 0 && si->shaderHeight == 0 )
683         {
684                 si->shaderWidth = si->shaderImage->width;
685                 si->shaderHeight = si->shaderImage->height;
686         }
687         
688         /* legacy terrain has explicit image-sized texture projection */
689         if( si->legacyTerrain && si->tcGen == qfalse )
690         {
691                 /* set xy texture projection */
692                 si->tcGen = qtrue;
693                 VectorSet( si->vecs[ 0 ], (1.0f / (si->shaderWidth * 0.5f)), 0, 0 );
694                 VectorSet( si->vecs[ 1 ], 0, (1.0f / (si->shaderHeight * 0.5f)), 0 );
695         }
696         
697         /* find pixel coordinates best matching the average color of the image */
698         bestDist = 99999999;
699         o[ 0 ] = 1.0f / si->shaderImage->width;
700         o[ 1 ] = 1.0f / si->shaderImage->height;
701         for( y = 0, st[ 1 ] = 0.0f; y < si->shaderImage->height; y++, st[ 1 ] += o[ 1 ] )
702         {
703                 for( x = 0, st[ 0 ] = 0.0f; x < si->shaderImage->width; x++, st[ 0 ] += o[ 0 ] )
704                 {
705                         /* sample the shader image */
706                         RadSampleImage( si->shaderImage->pixels, si->shaderImage->width, si->shaderImage->height, st, color );
707                         
708                         /* determine error squared */
709                         VectorSubtract( color, si->averageColor, delta );
710                         delta[ 3 ] = color[ 3 ] - si->averageColor[ 3 ];
711                         dist = delta[ 0 ] * delta[ 0 ] + delta[ 1 ] * delta[ 1 ] + delta[ 2 ] * delta[ 2 ] + delta[ 3 ] * delta[ 3 ];
712                         if( dist < bestDist )
713                         {
714                                 VectorCopy( color, bestColor );
715                                 bestColor[ 3 ] = color[ 3 ];
716                                 si->stFlat[ 0 ] = st[ 0 ];
717                                 si->stFlat[ 1 ] = st[ 1 ];
718                         }
719                 }
720         }
721         
722         /* set to finished */
723         si->finished = qtrue;
724 }
725
726
727
728 /*
729 LoadShaderImages()
730 loads a shader's images
731 ydnar: image.c made this a bit simpler
732 */
733
734 static void LoadShaderImages( shaderInfo_t *si )
735 {
736         int                     i, count;
737         float           color[ 4 ];
738         
739         
740         /* nodraw shaders don't need images */
741         if( si->compileFlags & C_NODRAW )
742                 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
743         else
744         {
745                 /* try to load editor image first */
746                 si->shaderImage = ImageLoad( si->editorImagePath );
747                 
748                 /* then try shadername */
749                 if( si->shaderImage == NULL )
750                         si->shaderImage = ImageLoad( si->shader );
751                 
752                 /* then try implicit image path (note: new behavior!) */
753                 if( si->shaderImage == NULL )
754                         si->shaderImage = ImageLoad( si->implicitImagePath );
755                 
756                 /* then try lightimage (note: new behavior!) */
757                 if( si->shaderImage == NULL )
758                         si->shaderImage = ImageLoad( si->lightImagePath );
759                 
760                 /* otherwise, use default image */
761                 if( si->shaderImage == NULL )
762                 {
763                         si->shaderImage = ImageLoad( DEFAULT_IMAGE );
764                         if( warnImage && strcmp( si->shader, "noshader" ) )
765                                 Sys_Printf( "WARNING: Couldn't find image for shader %s\n", si->shader );
766                 }
767                 
768                 /* load light image */
769                 si->lightImage = ImageLoad( si->lightImagePath );
770                 
771                 /* load normalmap image (ok if this is NULL) */
772                 si->normalImage = ImageLoad( si->normalImagePath );
773                 if( si->normalImage != NULL )
774                 {
775                         Sys_FPrintf( SYS_VRB, "Shader %s has\n"
776                                                                   "    NM %s\n", si->shader, si->normalImagePath );
777                 }
778         }
779         
780         /* if no light image, use shader image */
781         if( si->lightImage == NULL )
782                 si->lightImage = ImageLoad( si->shaderImage->name );
783         
784         /* create default and average colors */
785         count = si->lightImage->width * si->lightImage->height;
786         VectorClear( color );
787         color[ 3 ] = 0.0f;
788         for( i = 0; i < count; i++ )
789         {
790                 color[ 0 ] += si->lightImage->pixels[ i * 4 + 0 ];
791                 color[ 1 ] += si->lightImage->pixels[ i * 4 + 1 ];
792                 color[ 2 ] += si->lightImage->pixels[ i * 4 + 2 ];
793                 color[ 3 ] += si->lightImage->pixels[ i * 4 + 3 ];
794         }
795         
796         if( VectorLength( si->color ) <= 0.0f )
797         {
798                 ColorNormalize( color, si->color );
799                 VectorScale( color, (1.0f / count), si->averageColor );
800         }
801         else
802         {
803                 VectorCopy( si->color, si->averageColor );
804         }
805 }
806
807
808
809 /*
810 ShaderInfoForShader()
811 finds a shaderinfo for a named shader
812 */
813
814 shaderInfo_t *ShaderInfoForShader( const char *shaderName )
815 {
816         int                             i;
817         shaderInfo_t    *si;
818         char                    shader[ MAX_QPATH ];
819         
820         
821         /* dummy check */
822         if( shaderName == NULL || shaderName[ 0 ] == '\0' )
823         {
824                 Sys_Printf( "WARNING: Null or empty shader name\n" );
825                 shaderName = "missing";
826         }
827         
828         /* strip off extension */
829         strcpy( shader, shaderName );
830         StripExtension( shader );
831         
832         /* search for it */
833         for( i = 0; i < numShaderInfo; i++ )
834         {
835                 si = &shaderInfo[ i ];
836                 if( !Q_stricmp( shader, si->shader ) )
837                 {
838                         /* load image if necessary */
839                         if( si->finished == qfalse )
840                         {
841                                 LoadShaderImages( si );
842                                 FinishShader( si );
843                         }
844                         
845                         /* return it */
846                         return si;
847                 }
848         }
849         
850         /* allocate a default shader */
851         si = AllocShaderInfo();
852         strcpy( si->shader, shader );
853         LoadShaderImages( si );
854         FinishShader( si );
855         
856         /* return it */
857         return si;
858 }
859
860
861
862 /*
863 GetTokenAppend() - ydnar
864 gets a token and appends its text to the specified buffer
865 */
866
867 static int      oldScriptLine = 0;
868 static int      tabDepth = 0;
869
870 qboolean GetTokenAppend( char *buffer, qboolean crossline )
871 {
872         qboolean        r;
873         int                     i;
874         
875         
876         /* get the token */
877         r = GetToken( crossline );
878         if( r == qfalse || buffer == NULL || token[ 0 ] == '\0' )
879                 return r;
880         
881         /* pre-tabstops */
882         if( token[ 0 ] == '}' )
883                 tabDepth--;
884         
885         /* append? */
886         if( oldScriptLine != scriptline )
887         {
888                 strcat( buffer, "\n" );
889                 for( i = 0; i < tabDepth; i++ )
890                         strcat( buffer, "\t" );
891         }
892         else
893                 strcat( buffer, " " );
894         oldScriptLine = scriptline;
895         strcat( buffer, token );
896         
897         /* post-tabstops */
898         if( token[ 0 ] == '{' )
899                 tabDepth++;
900         
901         /* return */
902         return r;
903 }
904
905
906 void Parse1DMatrixAppend( char *buffer, int x, vec_t *m )
907 {
908         int             i;
909         
910         
911         if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, "(" ) )
912                 Error( "Parse1DMatrixAppend(): line %d: ( not found!", scriptline );
913         for( i = 0; i < x; i++ )
914         {
915                 if( !GetTokenAppend( buffer, qfalse ) )
916                         Error( "Parse1DMatrixAppend(): line %d: Number not found!", scriptline );
917                 m[ i ] = atof( token );
918         }
919         if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, ")" ) )
920                 Error( "Parse1DMatrixAppend(): line %d: ) not found!", scriptline );
921 }
922
923
924
925
926 /*
927 ParseShaderFile()
928 parses a shader file into discrete shaderInfo_t
929 */
930
931 static void ParseShaderFile( const char *filename )
932 {
933         int                             i, val;
934         shaderInfo_t    *si;
935         char                    *suffix, temp[ 1024 ];
936         char                    shaderText[ 8192 ];     /* ydnar: fixme (make this bigger?) */
937         
938         
939         /* init */
940         si = NULL;
941         shaderText[ 0 ] = '\0';
942         
943         /* load the shader */
944         LoadScriptFile( filename, 0 );
945         
946         /* tokenize it */
947         while( 1 )
948         {
949                 /* copy shader text to the shaderinfo */
950                 if( si != NULL && shaderText[ 0 ] != '\0' )
951                 {
952                         strcat( shaderText, "\n" );
953                         si->shaderText = safe_malloc( strlen( shaderText ) + 1 );
954                         strcpy( si->shaderText, shaderText );
955                         //%     if( VectorLength( si->vecs[ 0 ] ) )
956                         //%             Sys_Printf( "%s\n", shaderText );
957                 }
958                 
959                 /* ydnar: clear shader text buffer */
960                 shaderText[ 0 ] = '\0';
961                 
962                 /* test for end of file */
963                 if( !GetToken( qtrue ) )
964                         break;
965                 
966                 /* shader name is initial token */
967                 si = AllocShaderInfo();
968                 strcpy( si->shader, token );
969                 
970                 /* ignore ":q3map" suffix */
971                 suffix = strstr( si->shader, ":q3map" );
972                 if( suffix != NULL )
973                         *suffix = '\0';
974                 
975                 /* handle { } section */
976                 if( !GetTokenAppend( shaderText, qtrue ) )
977                         break;
978                 if( strcmp( token, "{" ) )
979                 {
980                         if( si != NULL )
981                                 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s\nLast known shader: %s",
982                                         filename, scriptline, token, si->shader );
983                         else
984                                 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s",
985                                         filename, scriptline, token );
986                 }
987                 
988                 while( 1 )
989                 {
990                         /* get the next token */
991                         if( !GetTokenAppend( shaderText, qtrue ) )
992                                 break;
993                         if( !strcmp( token, "}" ) )
994                                 break;
995                         
996                         
997                         /* -----------------------------------------------------------------
998                            shader stages (passes)
999                            ----------------------------------------------------------------- */
1000                         
1001                         /* parse stage directives */
1002                         if( !strcmp( token, "{" ) )
1003                         {
1004                                 si->hasPasses = qtrue;
1005                                 while( 1 )
1006                                 {
1007                                         if( !GetTokenAppend( shaderText, qtrue ) )
1008                                                 break;
1009                                         if( !strcmp( token, "}" ) )
1010                                                 break;
1011                                         
1012                                         /* only care about images if we don't have a editor/light image */
1013                                         if( si->editorImagePath[ 0 ] == '\0' && si->lightImagePath[ 0 ] == '\0' && si->implicitImagePath[ 0 ] == '\0' )
1014                                         {
1015                                                 /* digest any images */
1016                                                 if( !Q_stricmp( token, "map" ) ||
1017                                                         !Q_stricmp( token, "clampMap" ) ||
1018                                                         !Q_stricmp( token, "animMap" ) ||
1019                                                         !Q_stricmp( token, "clampAnimMap" ) ||
1020                                                         !Q_stricmp( token, "clampMap" ) ||
1021                                                         !Q_stricmp( token, "mapComp" ) ||
1022                                                         !Q_stricmp( token, "mapNoComp" ) )
1023                                                 {
1024                                                         /* skip one token for animated stages */
1025                                                         if( !Q_stricmp( token, "animMap" ) || !Q_stricmp( token, "clampAnimMap" ) )
1026                                                                 GetTokenAppend( shaderText, qfalse );
1027                                                         
1028                                                         /* get an image */
1029                                                         GetTokenAppend( shaderText, qfalse );
1030                                                         if( token[ 0 ] != '*' && token[ 0 ] != '$' )
1031                                                         {
1032                                                                 strcpy( si->lightImagePath, token );
1033                                                                 DefaultExtension( si->lightImagePath, ".tga" );
1034                                                                 
1035                                                                 /* debug code */
1036                                                                 //%     Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath );
1037                                                         }
1038                                                 }
1039                                         }
1040                                 }
1041                         }
1042                         
1043                         
1044                         /* -----------------------------------------------------------------
1045                            surfaceparm * directives
1046                            ----------------------------------------------------------------- */
1047                         
1048                         /* match surfaceparm */
1049                         else if( !Q_stricmp( token, "surfaceparm" ) )
1050                         {
1051                                 GetTokenAppend( shaderText, qfalse );
1052                                 if( ApplySurfaceParm( token, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1053                                         Sys_Printf( "WARNING: Unknown surfaceparm: \"%s\"\n", token );
1054                         }
1055                         
1056                         
1057                         /* -----------------------------------------------------------------
1058                            game-related shader directives
1059                            ----------------------------------------------------------------- */
1060                         
1061                         /* ydnar: fogparms (for determining fog volumes) */
1062                         else if( !Q_stricmp( token, "fogparms" ) )
1063                                 si->fogParms = qtrue;
1064                         
1065                         /* ydnar: polygonoffset (for no culling) */
1066                         else if( !Q_stricmp( token, "polygonoffset" ) )
1067                                 si->polygonOffset = qtrue;
1068                         
1069                         /* tesssize is used to force liquid surfaces to subdivide */
1070                         else if( !Q_stricmp( token, "tessSize" ) || !Q_stricmp( token, "q3map_tessSize" ) /* sof2 */ )
1071                         {
1072                                 GetTokenAppend( shaderText, qfalse );
1073                                 si->subdivisions = atof( token );
1074                         }
1075                         
1076                         /* cull none will set twoSided (ydnar: added disable too) */
1077                         else if ( !Q_stricmp( token, "cull" ) )
1078                         {
1079                                 GetTokenAppend( shaderText, qfalse );
1080                                 if( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "disable" ) || !Q_stricmp( token, "twosided" ) )
1081                                         si->twoSided = qtrue;
1082                         }
1083                         
1084                         /* deformVertexes autosprite[ 2 ]
1085                            we catch this so autosprited surfaces become point
1086                            lights instead of area lights */
1087                         else if( !Q_stricmp( token, "deformVertexes" ) )
1088                         {
1089                                 GetTokenAppend( shaderText, qfalse );
1090                                 
1091                                 /* deformVertexes autosprite(2) */
1092                                 if( !Q_strncasecmp( token, "autosprite", 10 ) )
1093                                 {
1094                                         /* set it as autosprite and detail */
1095                                         si->autosprite = qtrue;
1096                                         ApplySurfaceParm( "detail", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1097                                         
1098                                         /* ydnar: gs mods: added these useful things */
1099                                         si->noClip = qtrue;
1100                                         si->notjunc = qtrue;
1101                                 }
1102                                 
1103                                 /* deformVertexes move <x> <y> <z> <func> <base> <amplitude> <phase> <freq> (ydnar: for particle studio support) */
1104                                 if( !Q_stricmp( token, "move") )
1105                                 {
1106                                         vec3_t  amt, mins, maxs;
1107                                         float   base, amp;
1108                                         
1109                                         
1110                                         /* get move amount */
1111                                         GetTokenAppend( shaderText, qfalse );   amt[ 0 ] = atof( token );
1112                                         GetTokenAppend( shaderText, qfalse );   amt[ 1 ] = atof( token );
1113                                         GetTokenAppend( shaderText, qfalse );   amt[ 2 ] = atof( token );
1114                                         
1115                                         /* skip func */
1116                                         GetTokenAppend( shaderText, qfalse );
1117                                         
1118                                         /* get base and amplitude */
1119                                         GetTokenAppend( shaderText, qfalse );   base = atof( token );
1120                                         GetTokenAppend( shaderText, qfalse );   amp = atof( token );
1121                                         
1122                                         /* calculate */
1123                                         VectorScale( amt, base, mins );
1124                                         VectorMA( mins, amp, amt, maxs );
1125                                         VectorAdd( si->mins, mins, si->mins );
1126                                         VectorAdd( si->maxs, maxs, si->maxs );
1127                                 } 
1128                         }
1129                         
1130                         /* light <value> (old-style flare specification) */
1131                         else if( !Q_stricmp( token, "light" ) )
1132                         {
1133                                 GetTokenAppend( shaderText, qfalse );
1134                                 si->flareShader = game->flareShader;
1135                         }
1136                         
1137                         /* ydnar: damageShader <shader> <health> (sof2 mods) */
1138                         else if( !Q_stricmp( token, "damageShader" ) )
1139                         {
1140                                 GetTokenAppend( shaderText, qfalse );
1141                                 if( token[ 0 ] != '\0' )
1142                                 {
1143                                         si->damageShader = safe_malloc( strlen( token ) + 1 );
1144                                         strcpy( si->damageShader, token );
1145                                 }
1146                                 GetTokenAppend( shaderText, qfalse );   /* don't do anything with health */
1147                         }
1148                         
1149                         /* ydnar: enemy territory implicit shaders */
1150                         else if( !Q_stricmp( token, "implicitMap" ) )
1151                         {
1152                                 si->implicitMap = IM_OPAQUE;
1153                                 GetTokenAppend( shaderText, qfalse );
1154                                 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1155                                         sprintf( si->implicitImagePath, "%s.tga", si->shader );
1156                                 else
1157                                         strcpy( si->implicitImagePath, token );
1158                         }
1159
1160                         else if( !Q_stricmp( token, "implicitMask" ) )
1161                         {
1162                                 si->implicitMap = IM_MASKED;
1163                                 GetTokenAppend( shaderText, qfalse );
1164                                 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1165                                         sprintf( si->implicitImagePath, "%s.tga", si->shader );
1166                                 else
1167                                         strcpy( si->implicitImagePath, token );
1168                         }
1169
1170                         else if( !Q_stricmp( token, "implicitBlend" ) )
1171                         {
1172                                 si->implicitMap = IM_MASKED;
1173                                 GetTokenAppend( shaderText, qfalse );
1174                                 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1175                                         sprintf( si->implicitImagePath, "%s.tga", si->shader );
1176                                 else
1177                                         strcpy( si->implicitImagePath, token );
1178                         }
1179                         
1180                         
1181                         /* -----------------------------------------------------------------
1182                            image directives
1183                            ----------------------------------------------------------------- */
1184                         
1185                         /* qer_editorimage <image> */
1186                         else if( !Q_stricmp( token, "qer_editorImage" ) )
1187                         {
1188                                 GetTokenAppend( shaderText, qfalse );
1189                                 strcpy( si->editorImagePath, token );
1190                                 DefaultExtension( si->editorImagePath, ".tga" );
1191                         }
1192                         
1193                         /* ydnar: q3map_normalimage <image> (bumpmapping normal map) */
1194                         else if( !Q_stricmp( token, "q3map_normalImage" ) )
1195                         {
1196                                 GetTokenAppend( shaderText, qfalse );
1197                                 strcpy( si->normalImagePath, token );
1198                                 DefaultExtension( si->normalImagePath, ".tga" );
1199                         }
1200                         
1201                         /* q3map_lightimage <image> */
1202                         else if( !Q_stricmp( token, "q3map_lightImage" ) )
1203                         {
1204                                 GetTokenAppend( shaderText, qfalse );
1205                                 strcpy( si->lightImagePath, token );
1206                                 DefaultExtension( si->lightImagePath, ".tga" );
1207                         }
1208                         
1209                         /* ydnar: skyparms <outer image> <cloud height> <inner image> */
1210                         else if( !Q_stricmp( token, "skyParms" ) )
1211                         {
1212                                 /* get image base */
1213                                 GetTokenAppend( shaderText, qfalse );
1214                                 
1215                                 /* ignore bogus paths */
1216                                 if( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) )
1217                                 {
1218                                         strcpy( si->skyParmsImageBase, token );
1219                                         
1220                                         /* use top image as sky light image */
1221                                         if( si->lightImagePath[ 0 ] == '\0' )
1222                                                 sprintf( si->lightImagePath, "%s_up.tga", si->skyParmsImageBase );
1223                                 }
1224                                 
1225                                 /* skip rest of line */
1226                                 GetTokenAppend( shaderText, qfalse );
1227                                 GetTokenAppend( shaderText, qfalse );
1228                         }
1229                         
1230                         /* -----------------------------------------------------------------
1231                            q3map_* directives
1232                            ----------------------------------------------------------------- */
1233                         
1234                         /* q3map_sun <red> <green> <blue> <intensity> <degrees> <elevation>
1235                            color will be normalized, so it doesn't matter what range you use
1236                            intensity falls off with angle but not distance 100 is a fairly bright sun
1237                            degree of 0 = from the east, 90 = north, etc.  altitude of 0 = sunrise/set, 90 = noon
1238                            ydnar: sof2map has bareword 'sun' token, so we support that as well */
1239                         else if( !Q_stricmp( token, "sun" ) /* sof2 */ || !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) )
1240                         {
1241                                 float           a, b;
1242                                 sun_t           *sun;
1243                                 qboolean        ext;
1244                                 
1245                                 
1246                                 /* ydnar: extended sun directive? */
1247                                 if( !Q_stricmp( token, "q3map_sunext" ) )
1248                                         ext = qtrue;
1249                                 
1250                                 /* allocate sun */
1251                                 sun = safe_malloc( sizeof( *sun ) );
1252                                 memset( sun, 0, sizeof( *sun ) );
1253                                 
1254                                 /* set style */
1255                                 sun->style = si->lightStyle;
1256                                 
1257                                 /* get color */
1258                                 GetTokenAppend( shaderText, qfalse );
1259                                 sun->color[ 0 ] = atof( token );
1260                                 GetTokenAppend( shaderText, qfalse );
1261                                 sun->color[ 1 ] = atof( token );
1262                                 GetTokenAppend( shaderText, qfalse );
1263                                 sun->color[ 2 ] = atof( token );
1264                                 
1265                                 /* normalize it */
1266                                 VectorNormalize( sun->color, sun->color );
1267                                 
1268                                 /* scale color by brightness */
1269                                 GetTokenAppend( shaderText, qfalse );
1270                                 sun->photons = atof( token );
1271                                 
1272                                 /* get sun angle/elevation */
1273                                 GetTokenAppend( shaderText, qfalse );
1274                                 a = atof( token );
1275                                 a = a / 180.0f * Q_PI;
1276                                 
1277                                 GetTokenAppend( shaderText, qfalse );
1278                                 b = atof( token );
1279                                 b = b / 180.0f * Q_PI;
1280                                 
1281                                 sun->direction[ 0 ] = cos( a ) * cos( b );
1282                                 sun->direction[ 1 ] = sin( a ) * cos( b );
1283                                 sun->direction[ 2 ] = sin( b );
1284                                 
1285                                 /* get filter radius from shader */
1286                                 sun->filterRadius = si->lightFilterRadius;
1287                                 
1288                                 /* ydnar: get sun angular deviance/samples */
1289                                 if( ext && TokenAvailable() )
1290                                 {
1291                                         GetTokenAppend( shaderText, qfalse );
1292                                         sun->deviance = atof( token );
1293                                         sun->deviance = sun->deviance / 180.0f * Q_PI;
1294                                         
1295                                         GetTokenAppend( shaderText, qfalse );
1296                                         sun->numSamples = atoi( token );
1297                                 }
1298                                 
1299                                 /* store sun */
1300                                 sun->next = si->sun;
1301                                 si->sun = sun;
1302                                 
1303                                 /* apply sky surfaceparm */
1304                                 ApplySurfaceParm( "sky", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1305                                 
1306                                 /* don't process any more tokens on this line */
1307                                 continue;
1308                         }
1309
1310                         /* match q3map_ */
1311                         else if( !Q_strncasecmp( token, "q3map_", 6 ) )
1312                         {
1313                                 /* ydnar: q3map_baseShader <shader> (inherit this shader's parameters) */
1314                                 if( !Q_stricmp( token, "q3map_baseShader" ) )
1315                                 {
1316                                         shaderInfo_t    *si2;
1317                                         qboolean                oldWarnImage;
1318                                         
1319                                         
1320                                         /* get shader */
1321                                         GetTokenAppend( shaderText, qfalse );
1322                                         //%     Sys_FPrintf( SYS_VRB, "Shader %s has base shader %s\n", si->shader, token );
1323                                         oldWarnImage = warnImage;
1324                                         warnImage = qfalse;
1325                                         si2 = ShaderInfoForShader( token );
1326                                         warnImage = oldWarnImage;
1327                                         
1328                                         /* subclass it */
1329                                         if( si2 != NULL )
1330                                         {
1331                                                 /* preserve name */
1332                                                 strcpy( temp, si->shader );
1333                                                 
1334                                                 /* copy shader */
1335                                                 memcpy( si, si2, sizeof( *si ) );
1336                                                 
1337                                                 /* restore name and set to unfinished */
1338                                                 strcpy( si->shader, temp );
1339                                                 si->shaderWidth = 0;
1340                                                 si->shaderHeight = 0;
1341                                                 si->finished = qfalse;
1342                                         }
1343                                 }
1344                                 
1345                                 /* ydnar: q3map_surfacemodel <path to model> <density> <min scale> <max scale> <min angle> <max angle> <oriented (0 or 1)> */
1346                                 else if( !Q_stricmp( token, "q3map_surfacemodel" ) )
1347                                 {
1348                                         surfaceModel_t  *model;
1349                                         
1350                                         
1351                                         /* allocate new model and attach it */
1352                                         model = safe_malloc( sizeof( *model ) );
1353                                         memset( model, 0, sizeof( *model ) );
1354                                         model->next = si->surfaceModel;
1355                                         si->surfaceModel = model;
1356                                                 
1357                                         /* get parameters */
1358                                         GetTokenAppend( shaderText, qfalse );
1359                                         strcpy( model->model, token );
1360                                         
1361                                         GetTokenAppend( shaderText, qfalse );
1362                                         model->density = atof( token );
1363                                         GetTokenAppend( shaderText, qfalse );
1364                                         model->odds = atof( token );
1365                                         
1366                                         GetTokenAppend( shaderText, qfalse );
1367                                         model->minScale = atof( token );
1368                                         GetTokenAppend( shaderText, qfalse );
1369                                         model->maxScale = atof( token );
1370                                         
1371                                         GetTokenAppend( shaderText, qfalse );
1372                                         model->minAngle = atof( token );
1373                                         GetTokenAppend( shaderText, qfalse );
1374                                         model->maxAngle = atof( token );
1375                                         
1376                                         GetTokenAppend( shaderText, qfalse );
1377                                         model->oriented = (token[ 0 ] == '1' ? qtrue : qfalse);
1378                                 }
1379                                 
1380                                 /* ydnar/sd: q3map_foliage <path to model> <scale> <density> <odds> <invert alpha (1 or 0)> */
1381                                 else if( !Q_stricmp( token, "q3map_foliage" ) )
1382                                 {
1383                                         foliage_t       *foliage;
1384                                         
1385                                         
1386                                         /* allocate new foliage struct and attach it */
1387                                         foliage = safe_malloc( sizeof( *foliage ) );
1388                                         memset( foliage, 0, sizeof( *foliage ) );
1389                                         foliage->next = si->foliage;
1390                                         si->foliage = foliage;
1391                                         
1392                                         /* get parameters */
1393                                         GetTokenAppend( shaderText, qfalse );
1394                                         strcpy( foliage->model, token );
1395                                         
1396                                         GetTokenAppend( shaderText, qfalse );
1397                                         foliage->scale = atof( token );
1398                                         GetTokenAppend( shaderText, qfalse );
1399                                         foliage->density = atof( token );
1400                                         GetTokenAppend( shaderText, qfalse );
1401                                         foliage->odds = atof( token );
1402                                         GetTokenAppend( shaderText, qfalse );
1403                                         foliage->inverseAlpha = atoi( token );
1404                                 }
1405                                 
1406                                 /* ydnar: q3map_bounce <value> (fraction of light to re-emit during radiosity passes) */
1407                                 else if( !Q_stricmp( token, "q3map_bounce" ) || !Q_stricmp( token, "q3map_bounceScale" ) )
1408                                 {
1409                                         GetTokenAppend( shaderText, qfalse );
1410                                         si->bounceScale = atof( token );
1411                                 }
1412
1413                                 /* ydnar/splashdamage: q3map_skyLight <value> <iterations> */
1414                                 else if( !Q_stricmp( token, "q3map_skyLight" )  )
1415                                 {
1416                                         GetTokenAppend( shaderText, qfalse );
1417                                         si->skyLightValue = atof( token );
1418                                         GetTokenAppend( shaderText, qfalse );
1419                                         si->skyLightIterations = atoi( token );
1420                                         
1421                                         /* clamp */
1422                                         if( si->skyLightValue < 0.0f )
1423                                                 si->skyLightValue = 0.0f;
1424                                         if( si->skyLightIterations < 2 )
1425                                                 si->skyLightIterations = 2;
1426                                 }
1427                                 
1428                                 /* q3map_surfacelight <value> */
1429                                 else if( !Q_stricmp( token, "q3map_surfacelight" )  )
1430                                 {
1431                                         GetTokenAppend( shaderText, qfalse );
1432                                         si->value = atof( token );
1433                                 }
1434                                 
1435                                 /* q3map_lightStyle (sof2/jk2 lightstyle) */
1436                                 else if( !Q_stricmp( token, "q3map_lightStyle" ) )
1437                                 {
1438                                         GetTokenAppend( shaderText, qfalse );
1439                                         val = atoi( token );
1440                                         if( val < 0 )
1441                                                 val = 0;
1442                                         else if( val > LS_NONE )
1443                                                 val = LS_NONE;
1444                                         si->lightStyle = val;
1445                                 }
1446                                 
1447                                 /* wolf: q3map_lightRGB <red> <green> <blue> */
1448                                 else if( !Q_stricmp( token, "q3map_lightRGB" ) )
1449                                 {
1450                                         VectorClear( si->color );
1451                                         GetTokenAppend( shaderText, qfalse );
1452                                         si->color[ 0 ] = atof( token );
1453                                         GetTokenAppend( shaderText, qfalse );
1454                                         si->color[ 1 ] = atof( token );
1455                                         GetTokenAppend( shaderText, qfalse );
1456                                         si->color[ 2 ] = atof( token );
1457                                         ColorNormalize( si->color, si->color );
1458                                 }
1459                                 
1460                                 /* q3map_lightSubdivide <value> */
1461                                 else if( !Q_stricmp( token, "q3map_lightSubdivide" )  )
1462                                 {
1463                                         GetTokenAppend( shaderText, qfalse );
1464                                         si->lightSubdivide = atoi( token );
1465                                 }
1466                                 
1467                                 /* q3map_backsplash <percent> <distance> */
1468                                 else if( !Q_stricmp( token, "q3map_backsplash" ) )
1469                                 {
1470                                         GetTokenAppend( shaderText, qfalse );
1471                                         si->backsplashFraction = atof( token ) * 0.01f;
1472                                         GetTokenAppend( shaderText, qfalse );
1473                                         si->backsplashDistance = atof( token );
1474                                 }
1475                                 
1476                                 /* q3map_lightmapSampleSize <value> */
1477                                 else if( !Q_stricmp( token, "q3map_lightmapSampleSize" ) )
1478                                 {
1479                                         GetTokenAppend( shaderText, qfalse );
1480                                         si->lightmapSampleSize = atoi( token );
1481                                 }
1482                                 
1483                                 /* q3map_lightmapSampleSffset <value> */
1484                                 else if( !Q_stricmp( token, "q3map_lightmapSampleOffset" ) )
1485                                 {
1486                                         GetTokenAppend( shaderText, qfalse );
1487                                         si->lightmapSampleOffset = atof( token );
1488                                 }
1489                                 
1490                                 /* ydnar: q3map_lightmapFilterRadius <self> <other> */
1491                                 else if( !Q_stricmp( token, "q3map_lightmapFilterRadius" ) )
1492                                 {
1493                                         GetTokenAppend( shaderText, qfalse );
1494                                         si->lmFilterRadius = atof( token );
1495                                         GetTokenAppend( shaderText, qfalse );
1496                                         si->lightFilterRadius = atof( token );
1497                                 }
1498                                 
1499                                 /* ydnar: q3map_lightmapAxis [xyz] */
1500                                 else if( !Q_stricmp( token, "q3map_lightmapAxis" ) )
1501                                 {
1502                                         GetTokenAppend( shaderText, qfalse );
1503                                         if( !Q_stricmp( token, "x" ) )
1504                                                 VectorSet( si->lightmapAxis, 1, 0, 0 );
1505                                         else if( !Q_stricmp( token, "y" ) )
1506                                                 VectorSet( si->lightmapAxis, 0, 1, 0 );
1507                                         else if( !Q_stricmp( token, "z" ) )
1508                                                 VectorSet( si->lightmapAxis, 0, 0, 1 );
1509                                         else
1510                                         {
1511                                                 Sys_Printf( "WARNING: Unknown value for lightmap axis: %s\n", token );
1512                                                 VectorClear( si->lightmapAxis );
1513                                         }
1514                                 }
1515                                 
1516                                 /* ydnar: q3map_lightmapSize <width> <height> (for autogenerated shaders + external tga lightmaps) */
1517                                 else if( !Q_stricmp( token, "q3map_lightmapSize" ) )
1518                                 {
1519                                         GetTokenAppend( shaderText, qfalse );
1520                                         si->lmCustomWidth = atoi( token );
1521                                         GetTokenAppend( shaderText, qfalse );
1522                                         si->lmCustomHeight = atoi( token );
1523                                         
1524                                         /* must be a power of 2 */
1525                                         if( ((si->lmCustomWidth - 1) & si->lmCustomWidth) ||
1526                                                 ((si->lmCustomHeight - 1) & si->lmCustomHeight) )
1527                                         {
1528                                                 Sys_Printf( "WARNING: Non power-of-two lightmap size specified (%d, %d)\n",
1529                                                          si->lmCustomWidth, si->lmCustomHeight );
1530                                                 si->lmCustomWidth = lmCustomSize;
1531                                                 si->lmCustomHeight = lmCustomSize;
1532                                         }
1533                                 }
1534
1535                                 /* ydnar: q3map_lightmapBrightness N (for autogenerated shaders + external tga lightmaps) */
1536                                 else if( !Q_stricmp( token, "q3map_lightmapBrightness" ) || !Q_stricmp( token, "q3map_lightmapGamma" ) )
1537                                 {
1538                                         GetTokenAppend( shaderText, qfalse );
1539                                         si->lmBrightness = atof( token );
1540                                         if( si->lmBrightness < 0 )
1541                                                 si->lmBrightness = 1.0;
1542                                 }
1543                                 
1544                                 /* q3map_vertexScale (scale vertex lighting by this fraction) */
1545                                 else if( !Q_stricmp( token, "q3map_vertexScale" ) )
1546                                 {
1547                                         GetTokenAppend( shaderText, qfalse );
1548                                         si->vertexScale = atof( token );
1549                                 }
1550                                 
1551                                 /* q3map_noVertexLight */
1552                                 else if( !Q_stricmp( token, "q3map_noVertexLight" )  )
1553                                 {
1554                                         si->noVertexLight = qtrue;
1555                                 }
1556                                 
1557                                 /* q3map_flare[Shader] <shader> */
1558                                 else if( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) )
1559                                 {
1560                                         GetTokenAppend( shaderText, qfalse );
1561                                         if( token[ 0 ] != '\0' )
1562                                         {
1563                                                 si->flareShader = safe_malloc( strlen( token ) + 1 );
1564                                                 strcpy( si->flareShader, token );
1565                                         }
1566                                 }
1567                                 
1568                                 /* q3map_backShader <shader> */
1569                                 else if( !Q_stricmp( token, "q3map_backShader" ) )
1570                                 {
1571                                         GetTokenAppend( shaderText, qfalse );
1572                                         if( token[ 0 ] != '\0' )
1573                                         {
1574                                                 si->backShader = safe_malloc( strlen( token ) + 1 );
1575                                                 strcpy( si->backShader, token );
1576                                         }
1577                                 }
1578                                 
1579                                 /* ydnar: q3map_cloneShader <shader> */
1580                                 else if ( !Q_stricmp( token, "q3map_cloneShader" ) )
1581                                 {
1582                                         GetTokenAppend( shaderText, qfalse );
1583                                         if( token[ 0 ] != '\0' )
1584                                         {
1585                                                 si->cloneShader = safe_malloc( strlen( token ) + 1 );
1586                                                 strcpy( si->cloneShader, token );
1587                                         }
1588                                 }
1589                                 
1590                                 /* q3map_remapShader <shader> */
1591                                 else if( !Q_stricmp( token, "q3map_remapShader" ) )
1592                                 {
1593                                         GetTokenAppend( shaderText, qfalse );
1594                                         if( token[ 0 ] != '\0' )
1595                                         {
1596                                                 si->remapShader = safe_malloc( strlen( token ) + 1 );
1597                                                 strcpy( si->remapShader, token );
1598                                         }
1599                                 }
1600                                 
1601                                 /* ydnar: q3map_offset <value> */
1602                                 else if( !Q_stricmp( token, "q3map_offset" ) )
1603                                 {
1604                                         GetTokenAppend( shaderText, qfalse );
1605                                         si->offset = atof( token );
1606                                 }
1607                                 
1608                                 /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
1609                                 else if( !Q_stricmp( token, "q3map_fur" ) )
1610                                 {
1611                                         GetTokenAppend( shaderText, qfalse );
1612                                         si->furNumLayers = atoi( token );
1613                                         GetTokenAppend( shaderText, qfalse );
1614                                         si->furOffset = atof( token );
1615                                         GetTokenAppend( shaderText, qfalse );
1616                                         si->furFade = atof( token );
1617                                 }
1618                                 
1619                                 /* ydnar: gs mods: legacy support for terrain/terrain2 shaders */
1620                                 else if( !Q_stricmp( token, "q3map_terrain" ) )
1621                                 {
1622                                         /* team arena terrain is assumed to be nonplanar, with full normal averaging,
1623                                            passed through the metatriangle surface pipeline, with a lightmap axis on z */
1624                                         si->legacyTerrain = qtrue;
1625                                         si->noClip = qtrue;
1626                                         si->notjunc = qtrue;
1627                                         si->indexed = qtrue;
1628                                         si->nonplanar = qtrue;
1629                                         si->forceMeta = qtrue;
1630                                         si->shadeAngleDegrees = 179.0f;
1631                                         //%     VectorSet( si->lightmapAxis, 0, 0, 1 ); /* ydnar 2002-09-21: turning this off for better lightmapping of cliff faces */
1632                                 }
1633                                 
1634                                 /* ydnar: picomodel: q3map_forceMeta (forces brush faces and/or triangle models to go through the metasurface pipeline) */
1635                                 else if( !Q_stricmp( token, "q3map_forceMeta" ) )
1636                                 {
1637                                         si->forceMeta = qtrue;
1638                                 }
1639                                 
1640                                 /* ydnar: gs mods: q3map_shadeAngle <degrees> */
1641                                 else if( !Q_stricmp( token, "q3map_shadeAngle" ) )
1642                                 {
1643                                         GetTokenAppend( shaderText, qfalse );
1644                                         si->shadeAngleDegrees = atof( token );
1645                                 }
1646                                 
1647                                 /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
1648                                 else if( !Q_stricmp( token, "q3map_textureSize" ) )
1649                                 {
1650                                         GetTokenAppend( shaderText, qfalse );
1651                                         si->shaderWidth = atoi( token );
1652                                         GetTokenAppend( shaderText, qfalse );
1653                                         si->shaderHeight = atoi( token );
1654                                 }
1655                                 
1656                                 /* ydnar: gs mods: q3map_tcGen <style> <parameters> */
1657                                 else if( !Q_stricmp( token, "q3map_tcGen" ) )
1658                                 {
1659                                         si->tcGen = qtrue;
1660                                         GetTokenAppend( shaderText, qfalse );
1661                                         
1662                                         /* q3map_tcGen vector <s vector> <t vector> */
1663                                         if( !Q_stricmp( token, "vector" ) )
1664                                         {
1665                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1666                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1667                                         }
1668                                         
1669                                         /* q3map_tcGen ivector <1.0/s vector> <1.0/t vector> (inverse vector, easier for mappers to understand) */
1670                                         else if( !Q_stricmp( token, "ivector" ) )
1671                                         {
1672                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1673                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1674                                                 for( i = 0; i < 3; i++ )
1675                                                 {
1676                                                         si->vecs[ 0 ][ i ] = si->vecs[ 0 ][ i ] ? 1.0 / si->vecs[ 0 ][ i ] : 0;
1677                                                         si->vecs[ 1 ][ i ] = si->vecs[ 1 ][ i ] ? 1.0 / si->vecs[ 1 ][ i ] : 0;
1678                                                 }
1679                                         }
1680                                         else
1681                                         {
1682                                                 Sys_Printf( "WARNING: Unknown q3map_tcGen method: %s\n", token );
1683                                                 VectorClear( si->vecs[ 0 ] );
1684                                                 VectorClear( si->vecs[ 1 ] );
1685                                         }
1686                                 }
1687                                 
1688                                 /* ydnar: gs mods: q3map_[color|rgb|alpha][Gen|Mod] <style> <parameters> */
1689                                 else if( !Q_stricmp( token, "q3map_colorGen" ) || !Q_stricmp( token, "q3map_colorMod" ) ||
1690                                         !Q_stricmp( token, "q3map_rgbGen" ) || !Q_stricmp( token, "q3map_rgbMod" ) ||
1691                                         !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) )
1692                                 {
1693                                         colorMod_t      *cm, *cm2;
1694                                         int                     alpha;
1695                                         
1696                                         
1697                                         /* alphamods are colormod + 1 */
1698                                         alpha = (!Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" )) ? 1 : 0;
1699                                         
1700                                         /* allocate new colormod */
1701                                         cm = safe_malloc( sizeof( *cm ) );
1702                                         memset( cm, 0, sizeof( *cm ) );
1703                                         
1704                                         /* attach to shader */
1705                                         if( si->colorMod == NULL )
1706                                                 si->colorMod = cm;
1707                                         else
1708                                         {
1709                                                 for( cm2 = si->colorMod; cm2 != NULL; cm2 = cm2->next )
1710                                                 {
1711                                                         if( cm2->next == NULL )
1712                                                         {
1713                                                                 cm2->next = cm;
1714                                                                 break;
1715                                                         }
1716                                                 }
1717                                         }
1718                                         
1719                                         /* get type */
1720                                         GetTokenAppend( shaderText, qfalse );
1721                                         
1722                                         /* alpha set|const A */
1723                                         if( alpha && (!Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" )) )
1724                                         {
1725                                                 cm->type = CM_ALPHA_SET;
1726                                                 GetTokenAppend( shaderText, qfalse );
1727                                                 cm->data[ 0 ] = atof( token );
1728                                         }
1729                                         
1730                                         /* color|rgb set|const ( X Y Z ) */
1731                                         else if( !Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" ) )
1732                                         {
1733                                                 cm->type = CM_COLOR_SET;
1734                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1735                                         }
1736                                         
1737                                         /* alpha scale A */
1738                                         else if( alpha && !Q_stricmp( token, "scale" ) )
1739                                         {
1740                                                 cm->type = CM_ALPHA_SCALE;
1741                                                 GetTokenAppend( shaderText, qfalse );
1742                                                 cm->data[ 0 ] = atof( token );
1743                                         }
1744                                         
1745                                         /* color|rgb scale ( X Y Z ) */
1746                                         else if( !Q_stricmp( token, "scale" ) )
1747                                         {
1748                                                 cm->type = CM_COLOR_SCALE;
1749                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1750                                         }
1751                                         
1752                                         /* dotProduct ( X Y Z ) */
1753                                         else if( !Q_stricmp( token, "dotProduct" ) )
1754                                         {
1755                                                 cm->type = CM_COLOR_DOT_PRODUCT + alpha;
1756                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1757                                         }
1758                                         
1759                                         /* dotProduct2 ( X Y Z ) */
1760                                         else if( !Q_stricmp( token, "dotProduct2" ) )
1761                                         {
1762                                                 cm->type = CM_COLOR_DOT_PRODUCT_2 + alpha;
1763                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1764                                         }
1765                                         
1766                                         /* volume */
1767                                         else if( !Q_stricmp( token, "volume" ) )
1768                                         {
1769                                                 /* special stub mode for flagging volume brushes */
1770                                                 cm->type = CM_VOLUME;
1771                                         }
1772                                         
1773                                         /* unknown */
1774                                         else
1775                                                 Sys_Printf( "WARNING: Unknown colorMod method: %s\n", token );
1776                                 }
1777                                 
1778                                 /* ydnar: gs mods: q3map_tcMod <style> <parameters> */
1779                                 else if( !Q_stricmp( token, "q3map_tcMod" ) )
1780                                 {
1781                                         float   a, b;
1782                                         
1783                                         
1784                                         GetTokenAppend( shaderText, qfalse );
1785                                         
1786                                         /* q3map_tcMod [translate | shift | offset] <s> <t> */
1787                                         if( !Q_stricmp( token, "translate" ) || !Q_stricmp( token, "shift" ) || !Q_stricmp( token, "offset" ) )
1788                                         {
1789                                                 GetTokenAppend( shaderText, qfalse );
1790                                                 a = atof( token );
1791                                                 GetTokenAppend( shaderText, qfalse );
1792                                                 b = atof( token );
1793                                                 
1794                                                 TCModTranslate( si->mod, a, b );
1795                                         }
1796
1797                                         /* q3map_tcMod scale <s> <t> */
1798                                         else if( !Q_stricmp( token, "scale" ) )
1799                                         {
1800                                                 GetTokenAppend( shaderText, qfalse );
1801                                                 a = atof( token );
1802                                                 GetTokenAppend( shaderText, qfalse );
1803                                                 b = atof( token );
1804                                                 
1805                                                 TCModScale( si->mod, a, b );
1806                                         }
1807                                         
1808                                         /* q3map_tcMod rotate <s> <t> (fixme: make this communitive) */
1809                                         else if( !Q_stricmp( token, "rotate" ) )
1810                                         {
1811                                                 GetTokenAppend( shaderText, qfalse );
1812                                                 a = atof( token );
1813                                                 TCModRotate( si->mod, a );
1814                                         }
1815                                         else
1816                                                 Sys_Printf( "WARNING: Unknown q3map_tcMod method: %s\n", token );
1817                                 }
1818                                 
1819                                 /* q3map_fogDir (direction a fog shader fades from transparent to opaque) */
1820                                 else if( !Q_stricmp( token, "q3map_fogDir" ) )
1821                                 {
1822                                         Parse1DMatrixAppend( shaderText, 3, si->fogDir );
1823                                         VectorNormalize( si->fogDir, si->fogDir );
1824                                 }
1825                                 
1826                                 /* q3map_globaltexture */
1827                                 else if( !Q_stricmp( token, "q3map_globaltexture" )  )
1828                                         si->globalTexture = qtrue;
1829                                 
1830                                 /* ydnar: gs mods: q3map_nonplanar (make it a nonplanar merge candidate for meta surfaces) */
1831                                 else if( !Q_stricmp( token, "q3map_nonplanar" ) )
1832                                         si->nonplanar = qtrue;
1833                                 
1834                                 /* ydnar: gs mods: q3map_noclip (preserve original face winding, don't clip by bsp tree) */
1835                                 else if( !Q_stricmp( token, "q3map_noclip" ) )
1836                                         si->noClip = qtrue;
1837                                 
1838                                 /* q3map_notjunc */
1839                                 else if( !Q_stricmp( token, "q3map_notjunc" ) )
1840                                         si->notjunc = qtrue;
1841                                 
1842                                 /* q3map_nofog */
1843                                 else if( !Q_stricmp( token, "q3map_nofog" ) )
1844                                         si->noFog = qtrue;
1845                                 
1846                                 /* ydnar: gs mods: q3map_indexed (for explicit terrain-style indexed mapping) */
1847                                 else if( !Q_stricmp( token, "q3map_indexed" ) )
1848                                         si->indexed = qtrue;
1849                                 
1850                                 /* ydnar: q3map_invert (inverts a drawsurface's facing) */
1851                                 else if( !Q_stricmp( token, "q3map_invert" ) )
1852                                         si->invert = qtrue;
1853                                 
1854                                 /* ydnar: gs mods: q3map_lightmapMergable (ok to merge non-planar */
1855                                 else if( !Q_stricmp( token, "q3map_lightmapMergable" ) )
1856                                         si->lmMergable = qtrue;
1857                                 
1858                                 /* ydnar: q3map_nofast */
1859                                 else if( !Q_stricmp( token, "q3map_noFast" ) )
1860                                         si->noFast = qtrue;
1861                                 
1862                                 /* q3map_patchshadows */
1863                                 else if( !Q_stricmp( token, "q3map_patchShadows" ) )
1864                                         si->patchShadows = qtrue;
1865                                 
1866                                 /* q3map_vertexshadows */
1867                                 else if( !Q_stricmp( token, "q3map_vertexShadows" ) )
1868                                         si->vertexShadows = qtrue;      /* ydnar */
1869                                 
1870                                 /* q3map_novertexshadows */
1871                                 else if( !Q_stricmp( token, "q3map_noVertexShadows" ) )
1872                                         si->vertexShadows = qfalse;     /* ydnar */
1873                                 
1874                                 /* q3map_splotchfix (filter dark lightmap luxels on lightmapped models) */
1875                                 else if( !Q_stricmp( token, "q3map_splotchfix" ) )
1876                                         si->splotchFix = qtrue; /* ydnar */
1877                                 
1878                                 /* q3map_forcesunlight */
1879                                 else if( !Q_stricmp( token, "q3map_forceSunlight" ) )
1880                                         si->forceSunlight = qtrue;
1881                                 
1882                                 /* q3map_onlyvertexlighting (sof2) */
1883                                 else if( !Q_stricmp( token, "q3map_onlyVertexLighting" ) )
1884                                         ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1885                                 
1886                                 /* q3map_material (sof2) */
1887                                 else if( !Q_stricmp( token, "q3map_material" ) )
1888                                 {
1889                                         GetTokenAppend( shaderText, qfalse );
1890                                         sprintf( temp, "*mat_%s", token );
1891                                         if( ApplySurfaceParm( temp, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1892                                                 Sys_Printf( "WARNING: Unknown material \"%s\"\n", token );
1893                                 }
1894                                 
1895                                 /* ydnar: q3map_clipmodel (autogenerate clip brushes for model triangles using this shader) */
1896                                 else if( !Q_stricmp( token, "q3map_clipmodel" )  )
1897                                         si->clipModel = qtrue;
1898                                 
1899                                 /* ydnar: q3map_styleMarker[2] */
1900                                 else if( !Q_stricmp( token, "q3map_styleMarker" ) )
1901                                         si->styleMarker = 1;
1902                                 else if( !Q_stricmp( token, "q3map_styleMarker2" ) )    /* uses depthFunc equal */
1903                                         si->styleMarker = 2;
1904                                 
1905                                 /* ydnar: default to searching for q3map_<surfaceparm> */
1906                                 else
1907                                 {
1908                                         //%     Sys_FPrintf( SYS_VRB, "Attempting to match %s with a known surfaceparm\n", token );
1909                                         if( ApplySurfaceParm( &token[ 6 ], &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1910                                                 ;//%    Sys_Printf( "WARNING: Unknown q3map_* directive \"%s\"\n", token );
1911                                 }
1912                         }
1913                         
1914                         
1915                         /* -----------------------------------------------------------------
1916                            skip
1917                            ----------------------------------------------------------------- */
1918                         
1919                         /* ignore all other tokens on the line */
1920                         while( TokenAvailable() && GetTokenAppend( shaderText, qfalse ) );
1921                 }                       
1922         }
1923 }
1924
1925
1926
1927 /*
1928 ParseCustomInfoParms() - rr2do2
1929 loads custom info parms file for mods
1930 */
1931
1932 static void ParseCustomInfoParms( void )
1933 {
1934         qboolean parsedContent, parsedSurface;
1935         
1936         
1937         /* file exists? */
1938         if( vfsGetFileCount( "scripts/custinfoparms.txt" ) == 0 )
1939                 return;
1940         
1941         /* load it */
1942         LoadScriptFile( "scripts/custinfoparms.txt", 0 );
1943         
1944         /* clear the array */
1945         memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );
1946         numCustSurfaceParms = 0;
1947         parsedContent = parsedSurface = qfalse;
1948         
1949         /* parse custom contentflags */
1950         MatchToken( "{" );
1951         while ( 1 )
1952         {
1953                 if ( !GetToken( qtrue ) )
1954                         break;
1955
1956                 if ( !strcmp( token, "}" ) ) {
1957                         parsedContent = qtrue;
1958                         break;
1959                 }
1960
1961                 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
1962                 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
1963                 GetToken( qfalse );
1964                 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].contentFlags );
1965                 numCustSurfaceParms++;
1966         }
1967         
1968         /* any content? */
1969         if( !parsedContent )
1970         {
1971                 Sys_Printf( "WARNING: Couldn't find valid custom contentsflag section\n" );
1972                 return;
1973         }
1974         
1975         /* parse custom surfaceflags */
1976         MatchToken( "{" );
1977         while( 1 )
1978         {
1979                 if( !GetToken( qtrue ) )
1980                         break;
1981
1982                 if( !strcmp( token, "}" ) )
1983                 {
1984                         parsedSurface = qtrue;
1985                         break;
1986                 }
1987
1988                 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
1989                 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
1990                 GetToken( qfalse );
1991                 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );
1992                 numCustSurfaceParms++;
1993         }
1994         
1995         /* any content? */
1996         if( !parsedContent )
1997                 Sys_Printf( "WARNING: Couldn't find valid custom surfaceflag section\n" );
1998 }
1999
2000         
2001
2002 /*
2003 LoadShaderInfo()
2004 the shaders are parsed out of shaderlist.txt from a main directory
2005 that is, if using -fs_game we ignore the shader scripts that might be in baseq3/
2006 on linux there's an additional twist, we actually merge the stuff from ~/.q3a/ and from the base dir
2007 */
2008
2009 #define MAX_SHADER_FILES        1024
2010
2011 void LoadShaderInfo( void )
2012 {
2013         int                             i, j, numShaderFiles, count;
2014         char                    filename[ 1024 ];
2015         char                    *shaderFiles[ MAX_SHADER_FILES ];
2016         
2017         
2018         /* rr2do2: parse custom infoparms first */
2019         if( useCustomInfoParms )
2020                 ParseCustomInfoParms();
2021         
2022         /* start with zero */
2023         numShaderFiles = 0;
2024         
2025         /* we can pile up several shader files, the one in baseq3 and ones in the mod dir or other spots */
2026         sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2027         count = vfsGetFileCount( filename );
2028         
2029         /* load them all */
2030         for( i = 0; i < count; i++ )
2031         {
2032                 /* load shader list */
2033                 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2034                 LoadScriptFile( filename, i );
2035                 
2036                 /* parse it */
2037                 while( GetToken( qtrue ) )
2038                 {
2039                         /* check for duplicate entries */
2040                         for( j = 0; j < numShaderFiles; j++ )
2041                                 if( !strcmp( shaderFiles[ j ], token ) )
2042                                         break;
2043                         
2044                         /* test limit */
2045                         if( j >= MAX_SHADER_FILES )
2046                                 Error( "MAX_SHADER_FILES (%d) reached, trim your shaderlist.txt!", (int) MAX_SHADER_FILES );
2047                         
2048                         /* new shader file */
2049                         if( j == numShaderFiles )
2050                         {
2051                                 shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );
2052                                 strcpy( shaderFiles[ numShaderFiles ], token );
2053                                 numShaderFiles++;
2054                         }
2055                 }
2056         }
2057         
2058         /* parse the shader files */
2059         for( i = 0; i < numShaderFiles; i++ )
2060         {
2061                 sprintf( filename, "%s/%s.shader", game->shaderPath, shaderFiles[ i ] );
2062                 ParseShaderFile( filename );
2063                 free( shaderFiles[ i ] );
2064         }
2065         
2066         /* emit some statistics */
2067         Sys_FPrintf( SYS_VRB, "%9d shaderInfo\n", numShaderInfo );
2068 }