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