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