also support -sRGBcolor for floodlight
[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 = (c - cm2->data[3]) / (cm2->data[4] - cm2->data[3]);
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 = (c - cm2->data[3]) / (cm2->data[4] - cm2->data[3]);
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[3]) / (cm2->data[4] - cm2->data[3]);
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[3]) / (cm2->data[4] - cm2->data[3]);
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, 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                                 si->stFlat[ 0 ] = st[ 0 ];
731                                 si->stFlat[ 1 ] = st[ 1 ];
732                         }
733                 }
734         }
735         
736         /* set to finished */
737         si->finished = qtrue;
738 }
739
740
741
742 /*
743 LoadShaderImages()
744 loads a shader's images
745 ydnar: image.c made this a bit simpler
746 */
747
748 static void LoadShaderImages( shaderInfo_t *si )
749 {
750         int                     i, count;
751         float           color[ 4 ];
752         
753         
754         /* nodraw shaders don't need images */
755         if( si->compileFlags & C_NODRAW )
756                 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
757         else
758         {
759                 /* try to load editor image first */
760                 si->shaderImage = ImageLoad( si->editorImagePath );
761                 
762                 /* then try shadername */
763                 if( si->shaderImage == NULL )
764                         si->shaderImage = ImageLoad( si->shader );
765                 
766                 /* then try implicit image path (note: new behavior!) */
767                 if( si->shaderImage == NULL )
768                         si->shaderImage = ImageLoad( si->implicitImagePath );
769                 
770                 /* then try lightimage (note: new behavior!) */
771                 if( si->shaderImage == NULL )
772                         si->shaderImage = ImageLoad( si->lightImagePath );
773                 
774                 /* otherwise, use default image */
775                 if( si->shaderImage == NULL )
776                 {
777                         si->shaderImage = ImageLoad( DEFAULT_IMAGE );
778                         if( warnImage && strcmp( si->shader, "noshader" ) )
779                                 Sys_Printf( "WARNING: Couldn't find image for shader %s\n", si->shader );
780                 }
781                 
782                 /* load light image */
783                 si->lightImage = ImageLoad( si->lightImagePath );
784                 
785                 /* load normalmap image (ok if this is NULL) */
786                 si->normalImage = ImageLoad( si->normalImagePath );
787                 if( si->normalImage != NULL )
788                 {
789                         Sys_FPrintf( SYS_VRB, "Shader %s has\n"
790                                                                   "    NM %s\n", si->shader, si->normalImagePath );
791                 }
792         }
793         
794         /* if no light image, use shader image */
795         if( si->lightImage == NULL )
796                 si->lightImage = ImageLoad( si->shaderImage->name );
797         
798         /* create default and average colors */
799         count = si->lightImage->width * si->lightImage->height;
800         VectorClear( color );
801         color[ 3 ] = 0.0f;
802         for( i = 0; i < count; i++ )
803         {
804                 color[ 0 ] += si->lightImage->pixels[ i * 4 + 0 ];
805                 color[ 1 ] += si->lightImage->pixels[ i * 4 + 1 ];
806                 color[ 2 ] += si->lightImage->pixels[ i * 4 + 2 ];
807                 color[ 3 ] += si->lightImage->pixels[ i * 4 + 3 ];
808         }
809         
810         if( VectorLength( si->color ) <= 0.0f )
811         {
812                 ColorNormalize( color, si->color );
813                 VectorScale( color, (1.0f / count), si->averageColor );
814         }
815         else
816         {
817                 VectorCopy( si->color, si->averageColor );
818         }
819 }
820
821
822
823 /*
824 ShaderInfoForShader()
825 finds a shaderinfo for a named shader
826 */
827
828 #define MAX_SHADER_DEPRECATION_DEPTH 16
829
830 shaderInfo_t *ShaderInfoForShaderNull( const char *shaderName )
831 {
832         if(!strcmp(shaderName, "noshader"))
833                 return NULL;
834         return ShaderInfoForShader(shaderName);
835 }
836
837 shaderInfo_t *ShaderInfoForShader( const char *shaderName )
838 {
839         int                             i;
840         int                             deprecationDepth;
841         shaderInfo_t    *si;
842         char                    shader[ MAX_QPATH ];
843
844         /* dummy check */
845         if( shaderName == NULL || shaderName[ 0 ] == '\0' )
846         {
847                 Sys_Printf( "WARNING: Null or empty shader name\n" );
848                 shaderName = "missing";
849         }
850         
851         /* strip off extension */
852         strcpy( shader, shaderName );
853         StripExtension( shader );
854         
855         /* search for it */
856         deprecationDepth = 0;
857         for( i = 0; i < numShaderInfo; i++ )
858         {
859                 si = &shaderInfo[ i ];
860                 if( !Q_stricmp( shader, si->shader ) )
861                 {
862                         /* check if shader is deprecated */
863                         if (deprecationDepth < MAX_SHADER_DEPRECATION_DEPTH && si->deprecateShader && si->deprecateShader[ 0 ] )
864                         {
865                                 /* override name */
866                                 strcpy( shader, si->deprecateShader );
867                                 StripExtension( shader );
868                                 /* increase deprecation depth */
869                                 deprecationDepth++;
870                                 if (deprecationDepth == MAX_SHADER_DEPRECATION_DEPTH)
871                                         Sys_Printf("WARNING: Max deprecation depth of %i is reached on shader '%s'\n", MAX_SHADER_DEPRECATION_DEPTH, shader);
872                                 /* search again from beginning */
873                                 i = -1;
874                                 continue;
875                         }
876
877                         /* load image if necessary */
878                         if( si->finished == qfalse )
879                         {
880                                 LoadShaderImages( si );
881                                 FinishShader( si );
882                         }
883                         
884                         /* return it */
885                         return si;
886                 }
887         }
888         
889         /* allocate a default shader */
890         si = AllocShaderInfo();
891         strcpy( si->shader, shader );
892         LoadShaderImages( si );
893         FinishShader( si );
894         
895         /* return it */
896         return si;
897 }
898
899
900
901 /*
902 GetTokenAppend() - ydnar
903 gets a token and appends its text to the specified buffer
904 */
905
906 static int      oldScriptLine = 0;
907 static int      tabDepth = 0;
908
909 qboolean GetTokenAppend( char *buffer, qboolean crossline )
910 {
911         qboolean        r;
912         int                     i;
913         
914         
915         /* get the token */
916         r = GetToken( crossline );
917         if( r == qfalse || buffer == NULL || token[ 0 ] == '\0' )
918                 return r;
919         
920         /* pre-tabstops */
921         if( token[ 0 ] == '}' )
922                 tabDepth--;
923         
924         /* append? */
925         if( oldScriptLine != scriptline )
926         {
927                 strcat( buffer, "\n" );
928                 for( i = 0; i < tabDepth; i++ )
929                         strcat( buffer, "\t" );
930         }
931         else
932                 strcat( buffer, " " );
933         oldScriptLine = scriptline;
934         strcat( buffer, token );
935         
936         /* post-tabstops */
937         if( token[ 0 ] == '{' )
938                 tabDepth++;
939         
940         /* return */
941         return r;
942 }
943
944
945 void Parse1DMatrixAppend( char *buffer, int x, vec_t *m )
946 {
947         int             i;
948         
949         
950         if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, "(" ) )
951                 Error( "Parse1DMatrixAppend(): line %d: ( not found!", scriptline );
952         for( i = 0; i < x; i++ )
953         {
954                 if( !GetTokenAppend( buffer, qfalse ) )
955                         Error( "Parse1DMatrixAppend(): line %d: Number not found!", scriptline );
956                 m[ i ] = atof( token );
957         }
958         if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, ")" ) )
959                 Error( "Parse1DMatrixAppend(): line %d: ) not found!", scriptline );
960 }
961
962
963
964
965 /*
966 ParseShaderFile()
967 parses a shader file into discrete shaderInfo_t
968 */
969
970 static void ParseShaderFile( const char *filename )
971 {
972         int                             i, val;
973         shaderInfo_t    *si;
974         char                    *suffix, temp[ 1024 ];
975         char                    shaderText[ 8192 ];     /* ydnar: fixme (make this bigger?) */
976         
977         
978         /* init */
979         si = NULL;
980         shaderText[ 0 ] = '\0';
981         
982         /* load the shader */
983         LoadScriptFile( filename, 0 );
984         
985         /* tokenize it */
986         while( 1 )
987         {
988                 /* copy shader text to the shaderinfo */
989                 if( si != NULL && shaderText[ 0 ] != '\0' )
990                 {
991                         strcat( shaderText, "\n" );
992                         si->shaderText = safe_malloc( strlen( shaderText ) + 1 );
993                         strcpy( si->shaderText, shaderText );
994                         //%     if( VectorLength( si->vecs[ 0 ] ) )
995                         //%             Sys_Printf( "%s\n", shaderText );
996                 }
997                 
998                 /* ydnar: clear shader text buffer */
999                 shaderText[ 0 ] = '\0';
1000                 
1001                 /* test for end of file */
1002                 if( !GetToken( qtrue ) )
1003                         break;
1004                 
1005                 /* shader name is initial token */
1006                 si = AllocShaderInfo();
1007                 strcpy( si->shader, token );
1008                 
1009                 /* ignore ":q3map" suffix */
1010                 suffix = strstr( si->shader, ":q3map" );
1011                 if( suffix != NULL )
1012                         *suffix = '\0';
1013                 
1014                 /* handle { } section */
1015                 if( !GetTokenAppend( shaderText, qtrue ) )
1016                         break;
1017                 if( strcmp( token, "{" ) )
1018                 {
1019                         if( si != NULL )
1020                                 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s\nLast known shader: %s",
1021                                         filename, scriptline, token, si->shader );
1022                         else
1023                                 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s",
1024                                         filename, scriptline, token );
1025                 }
1026                 
1027                 while( 1 )
1028                 {
1029                         /* get the next token */
1030                         if( !GetTokenAppend( shaderText, qtrue ) )
1031                                 break;
1032                         if( !strcmp( token, "}" ) )
1033                                 break;
1034                         
1035                         
1036                         /* -----------------------------------------------------------------
1037                            shader stages (passes)
1038                            ----------------------------------------------------------------- */
1039                         
1040                         /* parse stage directives */
1041                         if( !strcmp( token, "{" ) )
1042                         {
1043                                 si->hasPasses = qtrue;
1044                                 while( 1 )
1045                                 {
1046                                         if( !GetTokenAppend( shaderText, qtrue ) )
1047                                                 break;
1048                                         if( !strcmp( token, "}" ) )
1049                                                 break;
1050                                         
1051                                         /* only care about images if we don't have a editor/light image */
1052                                         if( si->editorImagePath[ 0 ] == '\0' && si->lightImagePath[ 0 ] == '\0' && si->implicitImagePath[ 0 ] == '\0' )
1053                                         {
1054                                                 /* digest any images */
1055                                                 if( !Q_stricmp( token, "map" ) ||
1056                                                         !Q_stricmp( token, "clampMap" ) ||
1057                                                         !Q_stricmp( token, "animMap" ) ||
1058                                                         !Q_stricmp( token, "clampAnimMap" ) ||
1059                                                         !Q_stricmp( token, "clampMap" ) ||
1060                                                         !Q_stricmp( token, "mapComp" ) ||
1061                                                         !Q_stricmp( token, "mapNoComp" ) )
1062                                                 {
1063                                                         /* skip one token for animated stages */
1064                                                         if( !Q_stricmp( token, "animMap" ) || !Q_stricmp( token, "clampAnimMap" ) )
1065                                                                 GetTokenAppend( shaderText, qfalse );
1066                                                         
1067                                                         /* get an image */
1068                                                         GetTokenAppend( shaderText, qfalse );
1069                                                         if( token[ 0 ] != '*' && token[ 0 ] != '$' )
1070                                                         {
1071                                                                 strcpy( si->lightImagePath, token );
1072                                                                 DefaultExtension( si->lightImagePath, ".tga" );
1073                                                                 
1074                                                                 /* debug code */
1075                                                                 //%     Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath );
1076                                                         }
1077                                                 }
1078                                         }
1079                                 }
1080                         }
1081                         
1082                         
1083                         /* -----------------------------------------------------------------
1084                            surfaceparm * directives
1085                            ----------------------------------------------------------------- */
1086                         
1087                         /* match surfaceparm */
1088                         else if( !Q_stricmp( token, "surfaceparm" ) )
1089                         {
1090                                 GetTokenAppend( shaderText, qfalse );
1091                                 if( ApplySurfaceParm( token, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1092                                         Sys_Printf( "WARNING: Unknown surfaceparm: \"%s\"\n", token );
1093                         }
1094                         
1095                         
1096                         /* -----------------------------------------------------------------
1097                            game-related shader directives
1098                            ----------------------------------------------------------------- */
1099                         
1100                         /* ydnar: fogparms (for determining fog volumes) */
1101                         else if( !Q_stricmp( token, "fogparms" ) )
1102                                 si->fogParms = qtrue;
1103                         
1104                         /* ydnar: polygonoffset (for no culling) */
1105                         else if( !Q_stricmp( token, "polygonoffset" ) )
1106                                 si->polygonOffset = qtrue;
1107                         
1108                         /* tesssize is used to force liquid surfaces to subdivide */
1109                         else if( !Q_stricmp( token, "tessSize" ) || !Q_stricmp( token, "q3map_tessSize" ) /* sof2 */ )
1110                         {
1111                                 GetTokenAppend( shaderText, qfalse );
1112                                 si->subdivisions = atof( token );
1113                         }
1114                         
1115                         /* cull none will set twoSided (ydnar: added disable too) */
1116                         else if ( !Q_stricmp( token, "cull" ) )
1117                         {
1118                                 GetTokenAppend( shaderText, qfalse );
1119                                 if( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "disable" ) || !Q_stricmp( token, "twosided" ) )
1120                                         si->twoSided = qtrue;
1121                         }
1122                         
1123                         /* deformVertexes autosprite[ 2 ]
1124                            we catch this so autosprited surfaces become point
1125                            lights instead of area lights */
1126                         else if( !Q_stricmp( token, "deformVertexes" ) )
1127                         {
1128                                 GetTokenAppend( shaderText, qfalse );
1129                                 
1130                                 /* deformVertexes autosprite(2) */
1131                                 if( !Q_strncasecmp( token, "autosprite", 10 ) )
1132                                 {
1133                                         /* set it as autosprite and detail */
1134                                         si->autosprite = qtrue;
1135                                         ApplySurfaceParm( "detail", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1136                                         
1137                                         /* ydnar: gs mods: added these useful things */
1138                                         si->noClip = qtrue;
1139                                         si->notjunc = qtrue;
1140                                 }
1141                                 
1142                                 /* deformVertexes move <x> <y> <z> <func> <base> <amplitude> <phase> <freq> (ydnar: for particle studio support) */
1143                                 if( !Q_stricmp( token, "move") )
1144                                 {
1145                                         vec3_t  amt, mins, maxs;
1146                                         float   base, amp;
1147                                         
1148                                         
1149                                         /* get move amount */
1150                                         GetTokenAppend( shaderText, qfalse );   amt[ 0 ] = atof( token );
1151                                         GetTokenAppend( shaderText, qfalse );   amt[ 1 ] = atof( token );
1152                                         GetTokenAppend( shaderText, qfalse );   amt[ 2 ] = atof( token );
1153                                         
1154                                         /* skip func */
1155                                         GetTokenAppend( shaderText, qfalse );
1156                                         
1157                                         /* get base and amplitude */
1158                                         GetTokenAppend( shaderText, qfalse );   base = atof( token );
1159                                         GetTokenAppend( shaderText, qfalse );   amp = atof( token );
1160                                         
1161                                         /* calculate */
1162                                         VectorScale( amt, base, mins );
1163                                         VectorMA( mins, amp, amt, maxs );
1164                                         VectorAdd( si->mins, mins, si->mins );
1165                                         VectorAdd( si->maxs, maxs, si->maxs );
1166                                 } 
1167                         }
1168                         
1169                         /* light <value> (old-style flare specification) */
1170                         else if( !Q_stricmp( token, "light" ) )
1171                         {
1172                                 GetTokenAppend( shaderText, qfalse );
1173                                 si->flareShader = game->flareShader;
1174                         }
1175                         
1176                         /* ydnar: damageShader <shader> <health> (sof2 mods) */
1177                         else if( !Q_stricmp( token, "damageShader" ) )
1178                         {
1179                                 GetTokenAppend( shaderText, qfalse );
1180                                 if( token[ 0 ] != '\0' )
1181                                 {
1182                                         si->damageShader = safe_malloc( strlen( token ) + 1 );
1183                                         strcpy( si->damageShader, token );
1184                                 }
1185                                 GetTokenAppend( shaderText, qfalse );   /* don't do anything with health */
1186                         }
1187                         
1188                         /* ydnar: enemy territory implicit shaders */
1189                         else if( !Q_stricmp( token, "implicitMap" ) )
1190                         {
1191                                 si->implicitMap = IM_OPAQUE;
1192                                 GetTokenAppend( shaderText, qfalse );
1193                                 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1194                                         sprintf( si->implicitImagePath, "%s.tga", si->shader );
1195                                 else
1196                                         strcpy( si->implicitImagePath, token );
1197                         }
1198
1199                         else if( !Q_stricmp( token, "implicitMask" ) )
1200                         {
1201                                 si->implicitMap = IM_MASKED;
1202                                 GetTokenAppend( shaderText, qfalse );
1203                                 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1204                                         sprintf( si->implicitImagePath, "%s.tga", si->shader );
1205                                 else
1206                                         strcpy( si->implicitImagePath, token );
1207                         }
1208
1209                         else if( !Q_stricmp( token, "implicitBlend" ) )
1210                         {
1211                                 si->implicitMap = IM_MASKED;
1212                                 GetTokenAppend( shaderText, qfalse );
1213                                 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1214                                         sprintf( si->implicitImagePath, "%s.tga", si->shader );
1215                                 else
1216                                         strcpy( si->implicitImagePath, token );
1217                         }
1218                         
1219                         
1220                         /* -----------------------------------------------------------------
1221                            image directives
1222                            ----------------------------------------------------------------- */
1223                         
1224                         /* qer_editorimage <image> */
1225                         else if( !Q_stricmp( token, "qer_editorImage" ) )
1226                         {
1227                                 GetTokenAppend( shaderText, qfalse );
1228                                 strcpy( si->editorImagePath, token );
1229                                 DefaultExtension( si->editorImagePath, ".tga" );
1230                         }
1231                         
1232                         /* ydnar: q3map_normalimage <image> (bumpmapping normal map) */
1233                         else if( !Q_stricmp( token, "q3map_normalImage" ) )
1234                         {
1235                                 GetTokenAppend( shaderText, qfalse );
1236                                 strcpy( si->normalImagePath, token );
1237                                 DefaultExtension( si->normalImagePath, ".tga" );
1238                         }
1239                         
1240                         /* q3map_lightimage <image> */
1241                         else if( !Q_stricmp( token, "q3map_lightImage" ) )
1242                         {
1243                                 GetTokenAppend( shaderText, qfalse );
1244                                 strcpy( si->lightImagePath, token );
1245                                 DefaultExtension( si->lightImagePath, ".tga" );
1246                         }
1247                         
1248                         /* ydnar: skyparms <outer image> <cloud height> <inner image> */
1249                         else if( !Q_stricmp( token, "skyParms" ) )
1250                         {
1251                                 /* get image base */
1252                                 GetTokenAppend( shaderText, qfalse );
1253                                 
1254                                 /* ignore bogus paths */
1255                                 if( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) )
1256                                 {
1257                                         strcpy( si->skyParmsImageBase, token );
1258                                         
1259                                         /* use top image as sky light image */
1260                                         if( si->lightImagePath[ 0 ] == '\0' )
1261                                                 sprintf( si->lightImagePath, "%s_up.tga", si->skyParmsImageBase );
1262                                 }
1263                                 
1264                                 /* skip rest of line */
1265                                 GetTokenAppend( shaderText, qfalse );
1266                                 GetTokenAppend( shaderText, qfalse );
1267                         }
1268                         
1269                         /* -----------------------------------------------------------------
1270                            q3map_* directives
1271                            ----------------------------------------------------------------- */
1272                         
1273                         /* q3map_sun <red> <green> <blue> <intensity> <degrees> <elevation>
1274                            color will be normalized, so it doesn't matter what range you use
1275                            intensity falls off with angle but not distance 100 is a fairly bright sun
1276                            degree of 0 = from the east, 90 = north, etc.  altitude of 0 = sunrise/set, 90 = noon
1277                            ydnar: sof2map has bareword 'sun' token, so we support that as well */
1278                         else if( !Q_stricmp( token, "sun" ) /* sof2 */ || !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) )
1279                         {
1280                                 float           a, b;
1281                                 sun_t           *sun;
1282                                 qboolean        ext;
1283                                 
1284                                 
1285                                 /* ydnar: extended sun directive? */
1286                                 if( !Q_stricmp( token, "q3map_sunext" ) )
1287                                         ext = qtrue;
1288                                 
1289                                 /* allocate sun */
1290                                 sun = safe_malloc( sizeof( *sun ) );
1291                                 memset( sun, 0, sizeof( *sun ) );
1292                                 
1293                                 /* set style */
1294                                 sun->style = si->lightStyle;
1295                                 
1296                                 /* get color */
1297                                 GetTokenAppend( shaderText, qfalse );
1298                                 sun->color[ 0 ] = atof( token );
1299                                 GetTokenAppend( shaderText, qfalse );
1300                                 sun->color[ 1 ] = atof( token );
1301                                 GetTokenAppend( shaderText, qfalse );
1302                                 sun->color[ 2 ] = atof( token );
1303
1304                                 if(colorsRGB)
1305                                 {
1306                                         sun->color[0] = Image_LinearFloatFromsRGBFloat(sun->color[0]);
1307                                         sun->color[1] = Image_LinearFloatFromsRGBFloat(sun->color[1]);
1308                                         sun->color[2] = Image_LinearFloatFromsRGBFloat(sun->color[2]);
1309                                 }
1310                                 
1311                                 /* normalize it */
1312                                 VectorNormalize( sun->color, sun->color );
1313                                 
1314                                 /* scale color by brightness */
1315                                 GetTokenAppend( shaderText, qfalse );
1316                                 sun->photons = atof( token );
1317                                 
1318                                 /* get sun angle/elevation */
1319                                 GetTokenAppend( shaderText, qfalse );
1320                                 a = atof( token );
1321                                 a = a / 180.0f * Q_PI;
1322                                 
1323                                 GetTokenAppend( shaderText, qfalse );
1324                                 b = atof( token );
1325                                 b = b / 180.0f * Q_PI;
1326                                 
1327                                 sun->direction[ 0 ] = cos( a ) * cos( b );
1328                                 sun->direction[ 1 ] = sin( a ) * cos( b );
1329                                 sun->direction[ 2 ] = sin( b );
1330                                 
1331                                 /* get filter radius from shader */
1332                                 sun->filterRadius = si->lightFilterRadius;
1333                                 
1334                                 /* ydnar: get sun angular deviance/samples */
1335                                 if( ext && TokenAvailable() )
1336                                 {
1337                                         GetTokenAppend( shaderText, qfalse );
1338                                         sun->deviance = atof( token );
1339                                         sun->deviance = sun->deviance / 180.0f * Q_PI;
1340                                         
1341                                         GetTokenAppend( shaderText, qfalse );
1342                                         sun->numSamples = atoi( token );
1343                                 }
1344                                 
1345                                 /* store sun */
1346                                 sun->next = si->sun;
1347                                 si->sun = sun;
1348                                 
1349                                 /* apply sky surfaceparm */
1350                                 ApplySurfaceParm( "sky", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1351                                 
1352                                 /* don't process any more tokens on this line */
1353                                 continue;
1354                         }
1355
1356                         /* match q3map_ */
1357                         else if( !Q_strncasecmp( token, "q3map_", 6 ) )
1358                         {
1359                                 /* ydnar: q3map_baseShader <shader> (inherit this shader's parameters) */
1360                                 if( !Q_stricmp( token, "q3map_baseShader" ) )
1361                                 {
1362                                         shaderInfo_t    *si2;
1363                                         qboolean                oldWarnImage;
1364                                         
1365                                         
1366                                         /* get shader */
1367                                         GetTokenAppend( shaderText, qfalse );
1368                                         //%     Sys_FPrintf( SYS_VRB, "Shader %s has base shader %s\n", si->shader, token );
1369                                         oldWarnImage = warnImage;
1370                                         warnImage = qfalse;
1371                                         si2 = ShaderInfoForShader( token );
1372                                         warnImage = oldWarnImage;
1373                                         
1374                                         /* subclass it */
1375                                         if( si2 != NULL )
1376                                         {
1377                                                 /* preserve name */
1378                                                 strcpy( temp, si->shader );
1379                                                 
1380                                                 /* copy shader */
1381                                                 memcpy( si, si2, sizeof( *si ) );
1382                                                 
1383                                                 /* restore name and set to unfinished */
1384                                                 strcpy( si->shader, temp );
1385                                                 si->shaderWidth = 0;
1386                                                 si->shaderHeight = 0;
1387                                                 si->finished = qfalse;
1388                                         }
1389                                 }
1390                                 
1391                                 /* ydnar: q3map_surfacemodel <path to model> <density> <min scale> <max scale> <min angle> <max angle> <oriented (0 or 1)> */
1392                                 else if( !Q_stricmp( token, "q3map_surfacemodel" ) )
1393                                 {
1394                                         surfaceModel_t  *model;
1395                                         
1396                                         /* allocate new model and attach it */
1397                                         model = safe_malloc( sizeof( *model ) );
1398                                         memset( model, 0, sizeof( *model ) );
1399                                         model->next = si->surfaceModel;
1400                                         si->surfaceModel = model;
1401                                                 
1402                                         /* get parameters */
1403                                         GetTokenAppend( shaderText, qfalse );
1404                                         strcpy( model->model, token );
1405                                         
1406                                         GetTokenAppend( shaderText, qfalse );
1407                                         model->density = atof( token );
1408                                         GetTokenAppend( shaderText, qfalse );
1409                                         model->odds = atof( token );
1410                                         
1411                                         GetTokenAppend( shaderText, qfalse );
1412                                         model->minScale = atof( token );
1413                                         GetTokenAppend( shaderText, qfalse );
1414                                         model->maxScale = atof( token );
1415                                         
1416                                         GetTokenAppend( shaderText, qfalse );
1417                                         model->minAngle = atof( token );
1418                                         GetTokenAppend( shaderText, qfalse );
1419                                         model->maxAngle = atof( token );
1420                                         
1421                                         GetTokenAppend( shaderText, qfalse );
1422                                         model->oriented = (token[ 0 ] == '1' ? qtrue : qfalse);
1423                                 }
1424                                 
1425                                 /* ydnar/sd: q3map_foliage <path to model> <scale> <density> <odds> <invert alpha (1 or 0)> */
1426                                 else if( !Q_stricmp( token, "q3map_foliage" ) )
1427                                 {
1428                                         foliage_t       *foliage;
1429                                         
1430                                         
1431                                         /* allocate new foliage struct and attach it */
1432                                         foliage = safe_malloc( sizeof( *foliage ) );
1433                                         memset( foliage, 0, sizeof( *foliage ) );
1434                                         foliage->next = si->foliage;
1435                                         si->foliage = foliage;
1436                                         
1437                                         /* get parameters */
1438                                         GetTokenAppend( shaderText, qfalse );
1439                                         strcpy( foliage->model, token );
1440                                         
1441                                         GetTokenAppend( shaderText, qfalse );
1442                                         foliage->scale = atof( token );
1443                                         GetTokenAppend( shaderText, qfalse );
1444                                         foliage->density = atof( token );
1445                                         GetTokenAppend( shaderText, qfalse );
1446                                         foliage->odds = atof( token );
1447                                         GetTokenAppend( shaderText, qfalse );
1448                                         foliage->inverseAlpha = atoi( token );
1449                                 }
1450                                 
1451                                 /* ydnar: q3map_bounce <value> (fraction of light to re-emit during radiosity passes) */
1452                                 else if( !Q_stricmp( token, "q3map_bounce" ) || !Q_stricmp( token, "q3map_bounceScale" ) )
1453                                 {
1454                                         GetTokenAppend( shaderText, qfalse );
1455                                         si->bounceScale = atof( token );
1456                                 }
1457
1458                                 /* ydnar/splashdamage: q3map_skyLight <value> <iterations> */
1459                                 else if( !Q_stricmp( token, "q3map_skyLight" )  )
1460                                 {
1461                                         GetTokenAppend( shaderText, qfalse );
1462                                         si->skyLightValue = atof( token );
1463                                         GetTokenAppend( shaderText, qfalse );
1464                                         si->skyLightIterations = atoi( token );
1465                                         
1466                                         /* clamp */
1467                                         if( si->skyLightValue < 0.0f )
1468                                                 si->skyLightValue = 0.0f;
1469                                         if( si->skyLightIterations < 2 )
1470                                                 si->skyLightIterations = 2;
1471                                 }
1472                                 
1473                                 /* q3map_surfacelight <value> */
1474                                 else if( !Q_stricmp( token, "q3map_surfacelight" )  )
1475                                 {
1476                                         GetTokenAppend( shaderText, qfalse );
1477                                         si->value = atof( token );
1478                                 }
1479                                 
1480                                 /* q3map_lightStyle (sof2/jk2 lightstyle) */
1481                                 else if( !Q_stricmp( token, "q3map_lightStyle" ) )
1482                                 {
1483                                         GetTokenAppend( shaderText, qfalse );
1484                                         val = atoi( token );
1485                                         if( val < 0 )
1486                                                 val = 0;
1487                                         else if( val > LS_NONE )
1488                                                 val = LS_NONE;
1489                                         si->lightStyle = val;
1490                                 }
1491                                 
1492                                 /* wolf: q3map_lightRGB <red> <green> <blue> */
1493                                 else if( !Q_stricmp( token, "q3map_lightRGB" ) )
1494                                 {
1495                                         VectorClear( si->color );
1496                                         GetTokenAppend( shaderText, qfalse );
1497                                         si->color[ 0 ] = atof( token );
1498                                         GetTokenAppend( shaderText, qfalse );
1499                                         si->color[ 1 ] = atof( token );
1500                                         GetTokenAppend( shaderText, qfalse );
1501                                         si->color[ 2 ] = atof( token );
1502                                         if(colorsRGB)
1503                                         {
1504                                                 si->color[0] = Image_LinearFloatFromsRGBFloat(si->color[0]);
1505                                                 si->color[1] = Image_LinearFloatFromsRGBFloat(si->color[1]);
1506                                                 si->color[2] = Image_LinearFloatFromsRGBFloat(si->color[2]);
1507                                         }
1508                                         ColorNormalize( si->color, si->color );
1509                                 }
1510                                 
1511                                 /* q3map_lightSubdivide <value> */
1512                                 else if( !Q_stricmp( token, "q3map_lightSubdivide" )  )
1513                                 {
1514                                         GetTokenAppend( shaderText, qfalse );
1515                                         si->lightSubdivide = atoi( token );
1516                                 }
1517                                 
1518                                 /* q3map_backsplash <percent> <distance> */
1519                                 else if( !Q_stricmp( token, "q3map_backsplash" ) )
1520                                 {
1521                                         GetTokenAppend( shaderText, qfalse );
1522                                         si->backsplashFraction = atof( token ) * 0.01f;
1523                                         GetTokenAppend( shaderText, qfalse );
1524                                         si->backsplashDistance = atof( token );
1525                                 }
1526
1527                                 /* q3map_floodLight <r> <g> <b> <diste> <intensity> <light_direction_power> */
1528                                 else if( !Q_stricmp( token, "q3map_floodLight" ) )
1529                                 {
1530                                         /* get color */
1531                                         GetTokenAppend( shaderText, qfalse );
1532                                         si->floodlightRGB[ 0 ] = atof( token );
1533                                         GetTokenAppend( shaderText, qfalse );
1534                                         si->floodlightRGB[ 1 ] = atof( token );
1535                                         GetTokenAppend( shaderText, qfalse );
1536                                         si->floodlightRGB[ 2 ] = atof( token );
1537                                         GetTokenAppend( shaderText, qfalse );
1538                                         si->floodlightDistance = atof( token ); 
1539                                         GetTokenAppend( shaderText, qfalse );
1540                                         si->floodlightIntensity = atof( token ); 
1541                                         GetTokenAppend( shaderText, qfalse );
1542                                         si->floodlightDirectionScale = atof( token ); 
1543                                         if(colorsRGB)
1544                                         {
1545                                                 si->floodlightRGB[0] = Image_LinearFloatFromsRGBFloat(si->floodlightRGB[0]);
1546                                                 si->floodlightRGB[1] = Image_LinearFloatFromsRGBFloat(si->floodlightRGB[1]);
1547                                                 si->floodlightRGB[2] = Image_LinearFloatFromsRGBFloat(si->floodlightRGB[2]);
1548                                         }
1549                                         VectorNormalize(si->floodlightRGB, si->floodlightRGB);
1550                                 }
1551
1552                                 /* jal: q3map_nodirty : skip dirty */
1553                                 else if( !Q_stricmp( token, "q3map_nodirty" ) )
1554                                 {
1555                                         si->noDirty = qtrue;
1556                                 }
1557                                 
1558                                 /* q3map_lightmapSampleSize <value> */
1559                                 else if( !Q_stricmp( token, "q3map_lightmapSampleSize" ) )
1560                                 {
1561                                         GetTokenAppend( shaderText, qfalse );
1562                                         si->lightmapSampleSize = atoi( token );
1563                                 }
1564                                 
1565                                 /* q3map_lightmapSampleOffset <value> */
1566                                 else if( !Q_stricmp( token, "q3map_lightmapSampleOffset" ) )
1567                                 {
1568                                         GetTokenAppend( shaderText, qfalse );
1569                                         si->lightmapSampleOffset = atof( token );
1570                                 }
1571                                 
1572                                 /* ydnar: q3map_lightmapFilterRadius <self> <other> */
1573                                 else if( !Q_stricmp( token, "q3map_lightmapFilterRadius" ) )
1574                                 {
1575                                         GetTokenAppend( shaderText, qfalse );
1576                                         si->lmFilterRadius = atof( token );
1577                                         GetTokenAppend( shaderText, qfalse );
1578                                         si->lightFilterRadius = atof( token );
1579                                 }
1580                                 
1581                                 /* ydnar: q3map_lightmapAxis [xyz] */
1582                                 else if( !Q_stricmp( token, "q3map_lightmapAxis" ) )
1583                                 {
1584                                         GetTokenAppend( shaderText, qfalse );
1585                                         if( !Q_stricmp( token, "x" ) )
1586                                                 VectorSet( si->lightmapAxis, 1, 0, 0 );
1587                                         else if( !Q_stricmp( token, "y" ) )
1588                                                 VectorSet( si->lightmapAxis, 0, 1, 0 );
1589                                         else if( !Q_stricmp( token, "z" ) )
1590                                                 VectorSet( si->lightmapAxis, 0, 0, 1 );
1591                                         else
1592                                         {
1593                                                 Sys_Printf( "WARNING: Unknown value for lightmap axis: %s\n", token );
1594                                                 VectorClear( si->lightmapAxis );
1595                                         }
1596                                 }
1597                                 
1598                                 /* ydnar: q3map_lightmapSize <width> <height> (for autogenerated shaders + external tga lightmaps) */
1599                                 else if( !Q_stricmp( token, "q3map_lightmapSize" ) )
1600                                 {
1601                                         GetTokenAppend( shaderText, qfalse );
1602                                         si->lmCustomWidth = atoi( token );
1603                                         GetTokenAppend( shaderText, qfalse );
1604                                         si->lmCustomHeight = atoi( token );
1605                                         
1606                                         /* must be a power of 2 */
1607                                         if( ((si->lmCustomWidth - 1) & si->lmCustomWidth) ||
1608                                                 ((si->lmCustomHeight - 1) & si->lmCustomHeight) )
1609                                         {
1610                                                 Sys_Printf( "WARNING: Non power-of-two lightmap size specified (%d, %d)\n",
1611                                                          si->lmCustomWidth, si->lmCustomHeight );
1612                                                 si->lmCustomWidth = lmCustomSize;
1613                                                 si->lmCustomHeight = lmCustomSize;
1614                                         }
1615                                 }
1616
1617                                 /* ydnar: q3map_lightmapBrightness N (for autogenerated shaders + external tga lightmaps) */
1618                                 else if( !Q_stricmp( token, "q3map_lightmapBrightness" ) || !Q_stricmp( token, "q3map_lightmapGamma" ) )
1619                                 {
1620                                         GetTokenAppend( shaderText, qfalse );
1621                                         si->lmBrightness = atof( token );
1622                                         if( si->lmBrightness < 0 )
1623                                                 si->lmBrightness = 1.0;
1624                                 }
1625                                 
1626                                 /* q3map_vertexScale (scale vertex lighting by this fraction) */
1627                                 else if( !Q_stricmp( token, "q3map_vertexScale" ) )
1628                                 {
1629                                         GetTokenAppend( shaderText, qfalse );
1630                                         si->vertexScale = atof( token );
1631                                 }
1632                                 
1633                                 /* q3map_noVertexLight */
1634                                 else if( !Q_stricmp( token, "q3map_noVertexLight" )  )
1635                                 {
1636                                         si->noVertexLight = qtrue;
1637                                 }
1638                                 
1639                                 /* q3map_flare[Shader] <shader> */
1640                                 else if( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) )
1641                                 {
1642                                         GetTokenAppend( shaderText, qfalse );
1643                                         if( token[ 0 ] != '\0' )
1644                                         {
1645                                                 si->flareShader = safe_malloc( strlen( token ) + 1 );
1646                                                 strcpy( si->flareShader, token );
1647                                         }
1648                                 }
1649                                 
1650                                 /* q3map_backShader <shader> */
1651                                 else if( !Q_stricmp( token, "q3map_backShader" ) )
1652                                 {
1653                                         GetTokenAppend( shaderText, qfalse );
1654                                         if( token[ 0 ] != '\0' )
1655                                         {
1656                                                 si->backShader = safe_malloc( strlen( token ) + 1 );
1657                                                 strcpy( si->backShader, token );
1658                                         }
1659                                 }
1660                                 
1661                                 /* ydnar: q3map_cloneShader <shader> */
1662                                 else if ( !Q_stricmp( token, "q3map_cloneShader" ) )
1663                                 {
1664                                         GetTokenAppend( shaderText, qfalse );
1665                                         if( token[ 0 ] != '\0' )
1666                                         {
1667                                                 si->cloneShader = safe_malloc( strlen( token ) + 1 );
1668                                                 strcpy( si->cloneShader, token );
1669                                         }
1670                                 }
1671                                 
1672                                 /* q3map_remapShader <shader> */
1673                                 else if( !Q_stricmp( token, "q3map_remapShader" ) )
1674                                 {
1675                                         GetTokenAppend( shaderText, qfalse );
1676                                         if( token[ 0 ] != '\0' )
1677                                         {
1678                                                 si->remapShader = safe_malloc( strlen( token ) + 1 );
1679                                                 strcpy( si->remapShader, token );
1680                                         }
1681                                 }
1682
1683                                 /* q3map_deprecateShader <shader> */
1684                                 else if( !Q_stricmp( token, "q3map_deprecateShader" ) )
1685                                 {
1686                                         GetTokenAppend( shaderText, qfalse );
1687                                         if( token[ 0 ] != '\0' )
1688                                         {
1689
1690                                                 si->deprecateShader = safe_malloc( strlen( token ) + 1 );
1691                                                 strcpy( si->deprecateShader, token );
1692                                         }
1693                                 }
1694                                 
1695                                 /* ydnar: q3map_offset <value> */
1696                                 else if( !Q_stricmp( token, "q3map_offset" ) )
1697                                 {
1698                                         GetTokenAppend( shaderText, qfalse );
1699                                         si->offset = atof( token );
1700                                 }
1701                                 
1702                                 /* ydnar: q3map_fur <numlayers> <offset> <fade> */
1703                                 else if( !Q_stricmp( token, "q3map_fur" ) )
1704                                 {
1705                                         GetTokenAppend( shaderText, qfalse );
1706                                         si->furNumLayers = atoi( token );
1707                                         GetTokenAppend( shaderText, qfalse );
1708                                         si->furOffset = atof( token );
1709                                         GetTokenAppend( shaderText, qfalse );
1710                                         si->furFade = atof( token );
1711                                 }
1712                                 
1713                                 /* ydnar: gs mods: legacy support for terrain/terrain2 shaders */
1714                                 else if( !Q_stricmp( token, "q3map_terrain" ) )
1715                                 {
1716                                         /* team arena terrain is assumed to be nonplanar, with full normal averaging,
1717                                            passed through the metatriangle surface pipeline, with a lightmap axis on z */
1718                                         si->legacyTerrain = qtrue;
1719                                         si->noClip = qtrue;
1720                                         si->notjunc = qtrue;
1721                                         si->indexed = qtrue;
1722                                         si->nonplanar = qtrue;
1723                                         si->forceMeta = qtrue;
1724                                         si->shadeAngleDegrees = 179.0f;
1725                                         //%     VectorSet( si->lightmapAxis, 0, 0, 1 ); /* ydnar 2002-09-21: turning this off for better lightmapping of cliff faces */
1726                                 }
1727                                 
1728                                 /* ydnar: picomodel: q3map_forceMeta (forces brush faces and/or triangle models to go through the metasurface pipeline) */
1729                                 else if( !Q_stricmp( token, "q3map_forceMeta" ) )
1730                                 {
1731                                         si->forceMeta = qtrue;
1732                                 }
1733                                 
1734                                 /* ydnar: gs mods: q3map_shadeAngle <degrees> */
1735                                 else if( !Q_stricmp( token, "q3map_shadeAngle" ) )
1736                                 {
1737                                         GetTokenAppend( shaderText, qfalse );
1738                                         si->shadeAngleDegrees = atof( token );
1739                                 }
1740                                 
1741                                 /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
1742                                 else if( !Q_stricmp( token, "q3map_textureSize" ) )
1743                                 {
1744                                         GetTokenAppend( shaderText, qfalse );
1745                                         si->shaderWidth = atoi( token );
1746                                         GetTokenAppend( shaderText, qfalse );
1747                                         si->shaderHeight = atoi( token );
1748                                 }
1749                                 
1750                                 /* ydnar: gs mods: q3map_tcGen <style> <parameters> */
1751                                 else if( !Q_stricmp( token, "q3map_tcGen" ) )
1752                                 {
1753                                         si->tcGen = qtrue;
1754                                         GetTokenAppend( shaderText, qfalse );
1755                                         
1756                                         /* q3map_tcGen vector <s vector> <t vector> */
1757                                         if( !Q_stricmp( token, "vector" ) )
1758                                         {
1759                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1760                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1761                                         }
1762                                         
1763                                         /* q3map_tcGen ivector <1.0/s vector> <1.0/t vector> (inverse vector, easier for mappers to understand) */
1764                                         else if( !Q_stricmp( token, "ivector" ) )
1765                                         {
1766                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1767                                                 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1768                                                 for( i = 0; i < 3; i++ )
1769                                                 {
1770                                                         si->vecs[ 0 ][ i ] = si->vecs[ 0 ][ i ] ? 1.0 / si->vecs[ 0 ][ i ] : 0;
1771                                                         si->vecs[ 1 ][ i ] = si->vecs[ 1 ][ i ] ? 1.0 / si->vecs[ 1 ][ i ] : 0;
1772                                                 }
1773                                         }
1774                                         else
1775                                         {
1776                                                 Sys_Printf( "WARNING: Unknown q3map_tcGen method: %s\n", token );
1777                                                 VectorClear( si->vecs[ 0 ] );
1778                                                 VectorClear( si->vecs[ 1 ] );
1779                                         }
1780                                 }
1781                                 
1782                                 /* ydnar: gs mods: q3map_[color|rgb|alpha][Gen|Mod] <style> <parameters> */
1783                                 else if( !Q_stricmp( token, "q3map_colorGen" ) || !Q_stricmp( token, "q3map_colorMod" ) ||
1784                                         !Q_stricmp( token, "q3map_rgbGen" ) || !Q_stricmp( token, "q3map_rgbMod" ) ||
1785                                         !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) )
1786                                 {
1787                                         colorMod_t      *cm, *cm2;
1788                                         int                     alpha;
1789                                         
1790                                         
1791                                         /* alphamods are colormod + 1 */
1792                                         alpha = (!Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" )) ? 1 : 0;
1793                                         
1794                                         /* allocate new colormod */
1795                                         cm = safe_malloc( sizeof( *cm ) );
1796                                         memset( cm, 0, sizeof( *cm ) );
1797                                         
1798                                         /* attach to shader */
1799                                         if( si->colorMod == NULL )
1800                                                 si->colorMod = cm;
1801                                         else
1802                                         {
1803                                                 for( cm2 = si->colorMod; cm2 != NULL; cm2 = cm2->next )
1804                                                 {
1805                                                         if( cm2->next == NULL )
1806                                                         {
1807                                                                 cm2->next = cm;
1808                                                                 break;
1809                                                         }
1810                                                 }
1811                                         }
1812                                         
1813                                         /* get type */
1814                                         GetTokenAppend( shaderText, qfalse );
1815                                         
1816                                         /* alpha set|const A */
1817                                         if( alpha && (!Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" )) )
1818                                         {
1819                                                 cm->type = CM_ALPHA_SET;
1820                                                 GetTokenAppend( shaderText, qfalse );
1821                                                 cm->data[ 0 ] = atof( token );
1822                                         }
1823                                         
1824                                         /* color|rgb set|const ( X Y Z ) */
1825                                         else if( !Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" ) )
1826                                         {
1827                                                 cm->type = CM_COLOR_SET;
1828                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1829                                                 if(colorsRGB)
1830                                                 {
1831                                                         cm->data[0] = Image_LinearFloatFromsRGBFloat(cm->data[0]);
1832                                                         cm->data[1] = Image_LinearFloatFromsRGBFloat(cm->data[1]);
1833                                                         cm->data[2] = Image_LinearFloatFromsRGBFloat(cm->data[2]);
1834                                                 }
1835                                         }
1836                                         
1837                                         /* alpha scale A */
1838                                         else if( alpha && !Q_stricmp( token, "scale" ) )
1839                                         {
1840                                                 cm->type = CM_ALPHA_SCALE;
1841                                                 GetTokenAppend( shaderText, qfalse );
1842                                                 cm->data[ 0 ] = atof( token );
1843                                         }
1844                                         
1845                                         /* color|rgb scale ( X Y Z ) */
1846                                         else if( !Q_stricmp( token, "scale" ) )
1847                                         {
1848                                                 cm->type = CM_COLOR_SCALE;
1849                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1850                                         }
1851                                         
1852                                         /* dotProduct ( X Y Z ) */
1853                                         else if( !Q_stricmp( token, "dotProduct" ) )
1854                                         {
1855                                                 cm->type = CM_COLOR_DOT_PRODUCT + alpha;
1856                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1857                                         }
1858                                         
1859                                         /* dotProductScale ( X Y Z MIN MAX ) */
1860                                         else if( !Q_stricmp( token, "dotProductScale" ) )
1861                                         {
1862                                                 cm->type = CM_COLOR_DOT_PRODUCT_SCALE + alpha;
1863                                                 Parse1DMatrixAppend( shaderText, 5, cm->data );
1864                                         }
1865                                         
1866                                         /* dotProduct2 ( X Y Z ) */
1867                                         else if( !Q_stricmp( token, "dotProduct2" ) )
1868                                         {
1869                                                 cm->type = CM_COLOR_DOT_PRODUCT_2 + alpha;
1870                                                 Parse1DMatrixAppend( shaderText, 3, cm->data );
1871                                         }
1872                                         
1873                                         /* dotProduct2scale ( X Y Z MIN MAX ) */
1874                                         else if( !Q_stricmp( token, "dotProduct2scale" ) )
1875                                         {
1876                                                 cm->type = CM_COLOR_DOT_PRODUCT_2_SCALE + alpha;
1877                                                 Parse1DMatrixAppend( shaderText, 5, cm->data );
1878                                         }
1879                                         
1880                                         /* volume */
1881                                         else if( !Q_stricmp( token, "volume" ) )
1882                                         {
1883                                                 /* special stub mode for flagging volume brushes */
1884                                                 cm->type = CM_VOLUME;
1885                                         }
1886                                         
1887                                         /* unknown */
1888                                         else
1889                                                 Sys_Printf( "WARNING: Unknown colorMod method: %s\n", token );
1890                                 }
1891                                 
1892                                 /* ydnar: gs mods: q3map_tcMod <style> <parameters> */
1893                                 else if( !Q_stricmp( token, "q3map_tcMod" ) )
1894                                 {
1895                                         float   a, b;
1896                                         
1897                                         
1898                                         GetTokenAppend( shaderText, qfalse );
1899                                         
1900                                         /* q3map_tcMod [translate | shift | offset] <s> <t> */
1901                                         if( !Q_stricmp( token, "translate" ) || !Q_stricmp( token, "shift" ) || !Q_stricmp( token, "offset" ) )
1902                                         {
1903                                                 GetTokenAppend( shaderText, qfalse );
1904                                                 a = atof( token );
1905                                                 GetTokenAppend( shaderText, qfalse );
1906                                                 b = atof( token );
1907                                                 
1908                                                 TCModTranslate( si->mod, a, b );
1909                                         }
1910
1911                                         /* q3map_tcMod scale <s> <t> */
1912                                         else if( !Q_stricmp( token, "scale" ) )
1913                                         {
1914                                                 GetTokenAppend( shaderText, qfalse );
1915                                                 a = atof( token );
1916                                                 GetTokenAppend( shaderText, qfalse );
1917                                                 b = atof( token );
1918                                                 
1919                                                 TCModScale( si->mod, a, b );
1920                                         }
1921                                         
1922                                         /* q3map_tcMod rotate <s> <t> (fixme: make this communitive) */
1923                                         else if( !Q_stricmp( token, "rotate" ) )
1924                                         {
1925                                                 GetTokenAppend( shaderText, qfalse );
1926                                                 a = atof( token );
1927                                                 TCModRotate( si->mod, a );
1928                                         }
1929                                         else
1930                                                 Sys_Printf( "WARNING: Unknown q3map_tcMod method: %s\n", token );
1931                                 }
1932                                 
1933                                 /* q3map_fogDir (direction a fog shader fades from transparent to opaque) */
1934                                 else if( !Q_stricmp( token, "q3map_fogDir" ) )
1935                                 {
1936                                         Parse1DMatrixAppend( shaderText, 3, si->fogDir );
1937                                         VectorNormalize( si->fogDir, si->fogDir );
1938                                 }
1939                                 
1940                                 /* q3map_globaltexture */
1941                                 else if( !Q_stricmp( token, "q3map_globaltexture" )  )
1942                                         si->globalTexture = qtrue;
1943                                 
1944                                 /* ydnar: gs mods: q3map_nonplanar (make it a nonplanar merge candidate for meta surfaces) */
1945                                 else if( !Q_stricmp( token, "q3map_nonplanar" ) )
1946                                         si->nonplanar = qtrue;
1947                                 
1948                                 /* ydnar: gs mods: q3map_noclip (preserve original face winding, don't clip by bsp tree) */
1949                                 else if( !Q_stricmp( token, "q3map_noclip" ) )
1950                                         si->noClip = qtrue;
1951                                 
1952                                 /* q3map_notjunc */
1953                                 else if( !Q_stricmp( token, "q3map_notjunc" ) )
1954                                         si->notjunc = qtrue;
1955                                 
1956                                 /* q3map_nofog */
1957                                 else if( !Q_stricmp( token, "q3map_nofog" ) )
1958                                         si->noFog = qtrue;
1959                                 
1960                                 /* ydnar: gs mods: q3map_indexed (for explicit terrain-style indexed mapping) */
1961                                 else if( !Q_stricmp( token, "q3map_indexed" ) )
1962                                         si->indexed = qtrue;
1963                                 
1964                                 /* ydnar: q3map_invert (inverts a drawsurface's facing) */
1965                                 else if( !Q_stricmp( token, "q3map_invert" ) )
1966                                         si->invert = qtrue;
1967                                 
1968                                 /* ydnar: gs mods: q3map_lightmapMergable (ok to merge non-planar */
1969                                 else if( !Q_stricmp( token, "q3map_lightmapMergable" ) )
1970                                         si->lmMergable = qtrue;
1971                                 
1972                                 /* ydnar: q3map_nofast */
1973                                 else if( !Q_stricmp( token, "q3map_noFast" ) )
1974                                         si->noFast = qtrue;
1975                                 
1976                                 /* q3map_patchshadows */
1977                                 else if( !Q_stricmp( token, "q3map_patchShadows" ) )
1978                                         si->patchShadows = qtrue;
1979                                 
1980                                 /* q3map_vertexshadows */
1981                                 else if( !Q_stricmp( token, "q3map_vertexShadows" ) )
1982                                         si->vertexShadows = qtrue;      /* ydnar */
1983                                 
1984                                 /* q3map_novertexshadows */
1985                                 else if( !Q_stricmp( token, "q3map_noVertexShadows" ) )
1986                                         si->vertexShadows = qfalse;     /* ydnar */
1987                                 
1988                                 /* q3map_splotchfix (filter dark lightmap luxels on lightmapped models) */
1989                                 else if( !Q_stricmp( token, "q3map_splotchfix" ) )
1990                                         si->splotchFix = qtrue; /* ydnar */
1991                                 
1992                                 /* q3map_forcesunlight */
1993                                 else if( !Q_stricmp( token, "q3map_forceSunlight" ) )
1994                                         si->forceSunlight = qtrue;
1995                                 
1996                                 /* q3map_onlyvertexlighting (sof2) */
1997                                 else if( !Q_stricmp( token, "q3map_onlyVertexLighting" ) )
1998                                         ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1999                                 
2000                                 /* q3map_material (sof2) */
2001                                 else if( !Q_stricmp( token, "q3map_material" ) )
2002                                 {
2003                                         GetTokenAppend( shaderText, qfalse );
2004                                         sprintf( temp, "*mat_%s", token );
2005                                         if( ApplySurfaceParm( temp, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
2006                                                 Sys_Printf( "WARNING: Unknown material \"%s\"\n", token );
2007                                 }
2008                                 
2009                                 /* ydnar: q3map_clipmodel (autogenerate clip brushes for model triangles using this shader) */
2010                                 else if( !Q_stricmp( token, "q3map_clipmodel" )  )
2011                                         si->clipModel = qtrue;
2012                                 
2013                                 /* ydnar: q3map_styleMarker[2] */
2014                                 else if( !Q_stricmp( token, "q3map_styleMarker" ) )
2015                                         si->styleMarker = 1;
2016                                 else if( !Q_stricmp( token, "q3map_styleMarker2" ) )    /* uses depthFunc equal */
2017                                         si->styleMarker = 2;
2018                                 
2019                                 /* ydnar: default to searching for q3map_<surfaceparm> */
2020 #if 0
2021                                 else
2022                                 {
2023                                         Sys_FPrintf( SYS_VRB, "Attempting to match %s with a known surfaceparm\n", token );
2024                                         if( ApplySurfaceParm( &token[ 6 ], &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
2025                                                 Sys_Printf( "WARNING: Unknown q3map_* directive \"%s\"\n", token );
2026                                 }
2027 #endif
2028                         }
2029                         
2030                         
2031                         /* -----------------------------------------------------------------
2032                            skip
2033                            ----------------------------------------------------------------- */
2034                         
2035                         /* ignore all other tokens on the line */
2036                         while( TokenAvailable() && GetTokenAppend( shaderText, qfalse ) );
2037                 }                       
2038         }
2039 }
2040
2041
2042
2043 /*
2044 ParseCustomInfoParms() - rr2do2
2045 loads custom info parms file for mods
2046 */
2047
2048 static void ParseCustomInfoParms( void )
2049 {
2050         qboolean parsedContent, parsedSurface;
2051         
2052         
2053         /* file exists? */
2054         if( vfsGetFileCount( "scripts/custinfoparms.txt" ) == 0 )
2055                 return;
2056         
2057         /* load it */
2058         LoadScriptFile( "scripts/custinfoparms.txt", 0 );
2059         
2060         /* clear the array */
2061         memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );
2062         numCustSurfaceParms = 0;
2063         parsedContent = parsedSurface = qfalse;
2064         
2065         /* parse custom contentflags */
2066         MatchToken( "{" );
2067         while ( 1 )
2068         {
2069                 if ( !GetToken( qtrue ) )
2070                         break;
2071
2072                 if ( !strcmp( token, "}" ) ) {
2073                         parsedContent = qtrue;
2074                         break;
2075                 }
2076
2077                 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
2078                 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
2079                 GetToken( qfalse );
2080                 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].contentFlags );
2081                 numCustSurfaceParms++;
2082         }
2083         
2084         /* any content? */
2085         if( !parsedContent )
2086         {
2087                 Sys_Printf( "WARNING: Couldn't find valid custom contentsflag section\n" );
2088                 return;
2089         }
2090         
2091         /* parse custom surfaceflags */
2092         MatchToken( "{" );
2093         while( 1 )
2094         {
2095                 if( !GetToken( qtrue ) )
2096                         break;
2097
2098                 if( !strcmp( token, "}" ) )
2099                 {
2100                         parsedSurface = qtrue;
2101                         break;
2102                 }
2103
2104                 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
2105                 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
2106                 GetToken( qfalse );
2107                 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );
2108                 numCustSurfaceParms++;
2109         }
2110         
2111         /* any content? */
2112         if( !parsedContent )
2113                 Sys_Printf( "WARNING: Couldn't find valid custom surfaceflag section\n" );
2114 }
2115
2116         
2117
2118 /*
2119 LoadShaderInfo()
2120 the shaders are parsed out of shaderlist.txt from a main directory
2121 that is, if using -fs_game we ignore the shader scripts that might be in baseq3/
2122 on linux there's an additional twist, we actually merge the stuff from ~/.q3a/ and from the base dir
2123 */
2124
2125 #define MAX_SHADER_FILES        1024
2126
2127 void LoadShaderInfo( void )
2128 {
2129         int                             i, j, numShaderFiles, count;
2130         char                    filename[ 1024 ];
2131         char                    *shaderFiles[ MAX_SHADER_FILES ];
2132         
2133         
2134         /* rr2do2: parse custom infoparms first */
2135         if( useCustomInfoParms )
2136                 ParseCustomInfoParms();
2137         
2138         /* start with zero */
2139         numShaderFiles = 0;
2140         
2141         /* we can pile up several shader files, the one in baseq3 and ones in the mod dir or other spots */
2142         sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2143         count = vfsGetFileCount( filename );
2144         
2145         /* load them all */
2146         for( i = 0; i < count; i++ )
2147         {
2148                 /* load shader list */
2149                 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2150                 LoadScriptFile( filename, i );
2151                 
2152                 /* parse it */
2153                 while( GetToken( qtrue ) )
2154                 {
2155                         /* check for duplicate entries */
2156                         for( j = 0; j < numShaderFiles; j++ )
2157                                 if( !strcmp( shaderFiles[ j ], token ) )
2158                                         break;
2159                         
2160                         /* test limit */
2161                         if( j >= MAX_SHADER_FILES )
2162                                 Error( "MAX_SHADER_FILES (%d) reached, trim your shaderlist.txt!", (int) MAX_SHADER_FILES );
2163                         
2164                         /* new shader file */
2165                         if( j == numShaderFiles )
2166                         {
2167                                 shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );
2168                                 strcpy( shaderFiles[ numShaderFiles ], token );
2169                                 numShaderFiles++;
2170                         }
2171                 }
2172         }
2173         
2174         /* parse the shader files */
2175         for( i = 0; i < numShaderFiles; i++ )
2176         {
2177                 sprintf( filename, "%s/%s.shader", game->shaderPath, shaderFiles[ i ] );
2178                 ParseShaderFile( filename );
2179                 free( shaderFiles[ i ] );
2180         }
2181         
2182         /* emit some statistics */
2183         Sys_FPrintf( SYS_VRB, "%9d shaderInfo\n", numShaderInfo );
2184 }