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