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