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