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