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