1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
22 ----------------------------------------------------------------------------------
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."
27 ------------------------------------------------------------------------------- */
43 routines for dealing with vertex color/alpha modification
46 void ColorMod( colorMod_t *cm, int numVerts, bspDrawVert_t *drawVerts )
56 if( cm == NULL || numVerts < 1 || drawVerts == NULL )
60 /* walk vertex list */
61 for( i = 0; i < numVerts; i++ )
66 /* walk colorMod list */
67 for( cm2 = cm; cm2 != NULL; cm2 = cm2->next )
70 VectorSet( mult, 1.0f, 1.0f, 1.0f );
72 VectorSet( add, 0.0f, 0.0f, 0.0f );
80 VectorScale( cm2->data, 255.0f, add );
85 add[ 3 ] = cm2->data[ 0 ] * 255.0f;
89 VectorCopy( cm2->data, mult );
93 mult[ 3 ] = cm2->data[ 0 ];
96 case CM_COLOR_DOT_PRODUCT:
97 c = DotProduct( dv->normal, cm2->data );
98 VectorSet( mult, c, c, c );
101 case CM_ALPHA_DOT_PRODUCT:
102 mult[ 3 ] = DotProduct( dv->normal, cm2->data );
105 case CM_COLOR_DOT_PRODUCT_2:
106 c = DotProduct( dv->normal, cm2->data );
108 VectorSet( mult, c, c, c );
111 case CM_ALPHA_DOT_PRODUCT_2:
112 mult[ 3 ] = DotProduct( dv->normal, cm2->data );
113 mult[ 3 ] *= mult[ 3 ];
121 for( j = 0; j < MAX_LIGHTMAPS; j++ )
123 for( k = 0; k < 4; k++ )
125 c = (mult[ k ] * dv->color[ j ][ k ]) + add[ k ];
130 dv->color[ j ][ k ] = c;
141 routines for dealing with a 3x3 texture mod matrix
144 void TCMod( tcMod_t mod, float st[ 2 ] )
151 st[ 0 ] = (mod[ 0 ][ 0 ] * old[ 0 ]) + (mod[ 0 ][ 1 ] * old[ 1 ]) + mod[ 0 ][ 2 ];
152 st[ 1 ] = (mod[ 1 ][ 0 ] * old[ 0 ]) + (mod[ 1 ][ 1 ] * old[ 1 ]) + mod[ 1 ][ 2 ];
156 void TCModIdentity( tcMod_t mod )
158 mod[ 0 ][ 0 ] = 1.0f; mod[ 0 ][ 1 ] = 0.0f; mod[ 0 ][ 2 ] = 0.0f;
159 mod[ 1 ][ 0 ] = 0.0f; mod[ 1 ][ 1 ] = 1.0f; mod[ 1 ][ 2 ] = 0.0f;
160 mod[ 2 ][ 0 ] = 0.0f; mod[ 2 ][ 1 ] = 0.0f; mod[ 2 ][ 2 ] = 1.0f; /* this row is only used for multiples, not transformation */
164 void TCModMultiply( tcMod_t a, tcMod_t b, tcMod_t out )
169 for( i = 0; i < 3; i++ )
171 out[ i ][ 0 ] = (a[ i ][ 0 ] * b[ 0 ][ 0 ]) + (a[ i ][ 1 ] * b[ 1 ][ 0 ]) + (a[ i ][ 2 ] * b[ 2 ][ 0 ]);
172 out[ i ][ 1 ] = (a[ i ][ 0 ] * b[ 0 ][ 1 ]) + (a[ i ][ 1 ] * b[ 1 ][ 1 ]) + (a[ i ][ 2 ] * b[ 2 ][ 1 ]);
173 out[ i ][ 2 ] = (a[ i ][ 0 ] * b[ 0 ][ 2 ]) + (a[ i ][ 1 ] * b[ 1 ][ 2 ]) + (a[ i ][ 2 ] * b[ 2 ][ 2 ]);
178 void TCModTranslate( tcMod_t mod, float s, float t )
185 void TCModScale( tcMod_t mod, float s, float t )
192 void TCModRotate( tcMod_t mod, float euler )
195 float radians, sinv, cosv;
198 memcpy( old, mod, sizeof( tcMod_t ) );
199 TCModIdentity( temp );
201 radians = euler / 180 * Q_PI;
202 sinv = sin( radians );
203 cosv = cos( radians );
205 temp[ 0 ][ 0 ] = cosv; temp[ 0 ][ 1 ] = -sinv;
206 temp[ 1 ][ 0 ] = sinv; temp[ 1 ][ 1 ] = cosv;
208 TCModMultiply( old, temp, mod );
214 ApplySurfaceParm() - ydnar
215 applies a named surfaceparm to the supplied flags
218 qboolean ApplySurfaceParm( char *name, int *contentFlags, int *surfaceFlags, int *compileFlags )
227 if( contentFlags == NULL )
228 contentFlags = &fake;
229 if( surfaceFlags == NULL )
230 surfaceFlags = &fake;
231 if( compileFlags == NULL )
232 compileFlags = &fake;
234 /* walk the current game's surfaceparms */
235 sp = game->surfaceParms;
236 while( sp->name != NULL )
239 if( !Q_stricmp( name, sp->name ) )
241 /* clear and set flags */
242 *contentFlags &= ~(sp->contentFlagsClear);
243 *contentFlags |= sp->contentFlags;
244 *surfaceFlags &= ~(sp->surfaceFlagsClear);
245 *surfaceFlags |= sp->surfaceFlags;
246 *compileFlags &= ~(sp->compileFlagsClear);
247 *compileFlags |= sp->compileFlags;
257 /* check custom info parms */
258 for( i = 0; i < numCustSurfaceParms; i++ )
260 /* get surfaceparm */
261 sp = &custSurfaceParms[ i ];
264 if( !Q_stricmp( name, sp->name ) )
266 /* clear and set flags */
267 *contentFlags &= ~(sp->contentFlagsClear);
268 *contentFlags |= sp->contentFlags;
269 *surfaceFlags &= ~(sp->surfaceFlagsClear);
270 *surfaceFlags |= sp->surfaceFlags;
271 *compileFlags &= ~(sp->compileFlagsClear);
272 *compileFlags |= sp->compileFlags;
279 /* no matching surfaceparm found */
286 BeginMapShaderFile() - ydnar
287 erases and starts a new map shader script
290 void BeginMapShaderFile( const char *mapFile )
298 mapShaderFile[ 0 ] = '\0';
299 if( mapFile == NULL || mapFile[ 0 ] == '\0' )
303 strcpy( base, mapFile );
304 StripExtension( base );
306 /* extract map name */
307 len = strlen( base ) - 1;
308 while( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )
310 strcpy( mapName, &base[ len + 1 ] );
315 /* append ../scripts/q3map2_<mapname>.shader */
316 sprintf( mapShaderFile, "%s/../%s/q3map2_%s.shader", base, game->shaderPath, mapName );
317 Sys_FPrintf( SYS_VRB, "Map has shader script %s\n", mapShaderFile );
320 remove( mapShaderFile );
322 /* stop making warnings about missing images */
329 WriteMapShaderFile() - ydnar
330 writes a shader to the map shader script
333 void WriteMapShaderFile( void )
341 if( mapShaderFile[ 0 ] == '\0' )
344 /* are there any custom shaders? */
345 for( i = 0, num = 0; i < numShaderInfo; i++ )
347 if( shaderInfo[ i ].custom )
350 if( i == numShaderInfo )
354 Sys_FPrintf( SYS_VRB, "--- WriteMapShaderFile ---\n");
355 Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile );
357 /* open shader file */
358 file = fopen( mapShaderFile, "w" );
361 Sys_Printf( "WARNING: Unable to open map shader file %s for writing\n", mapShaderFile );
367 "// Custom shader file for %s.bsp\n"
368 "// Generated by Q3Map2 (ydnar)\n"
369 "// Do not edit! This file is overwritten on recompiles.\n\n",
372 /* walk the shader list */
373 for( i = 0, num = 0; i < numShaderInfo; i++ )
375 /* get the shader and print it */
376 si = &shaderInfo[ i ];
377 if( si->custom == qfalse || si->shaderText == NULL || si->shaderText[ 0 ] == '\0' )
381 /* print it to the file */
382 fprintf( file, "%s%s\n", si->shader, si->shaderText );
383 //Sys_Printf( "%s%s\n", si->shader, si->shaderText ); /* FIXME: remove debugging code */
385 Sys_FPrintf( SYS_VRB, "." );
388 /* close the shader */
392 Sys_FPrintf( SYS_VRB, "\n" );
394 /* print some stats */
395 Sys_Printf( "%9d custom shaders emitted\n", num );
401 CustomShader() - ydnar
402 sets up a custom map shader
405 shaderInfo_t *CustomShader( shaderInfo_t *si, char *find, char *replace )
408 char shader[ MAX_QPATH ];
413 char *srcShaderText, temp[ 8192 ], shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */
418 return ShaderInfoForShader( "default" );
420 /* default shader text source */
421 srcShaderText = si->shaderText;
423 /* et: implicitMap */
424 if( si->implicitMap == IM_OPAQUE )
426 srcShaderText = temp;
428 "{ // Q3Map2 defaulted (implicitMap)\n"
430 "\t\tmap $lightmap\n"
431 "\t\trgbGen identity\n"
433 "\tq3map_styleMarker\n"
436 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
437 "\t\trgbGen identity\n"
440 si->implicitImagePath );
443 /* et: implicitMask */
444 else if( si->implicitMap == IM_MASKED )
446 srcShaderText = temp;
448 "{ // Q3Map2 defaulted (implicitMask)\n"
452 "\t\talphaFunc GE128\n"
456 "\t\tmap $lightmap\n"
457 "\t\trgbGen identity\n"
458 "\t\tdepthFunc equal\n"
460 "\tq3map_styleMarker\n"
463 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
464 "\t\tdepthFunc equal\n"
465 "\t\trgbGen identity\n"
468 si->implicitImagePath,
469 si->implicitImagePath );
472 /* et: implicitBlend */
473 else if( si->implicitMap == IM_BLEND )
475 srcShaderText = temp;
477 "{ // Q3Map2 defaulted (implicitBlend)\n"
481 "\t\tblendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA\n"
484 "\t\tmap $lightmap\n"
485 "\t\trgbGen identity\n"
486 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
488 "\tq3map_styleMarker\n"
490 si->implicitImagePath );
493 /* default shader text */
494 else if( srcShaderText == NULL )
496 srcShaderText = temp;
498 "{ // Q3Map2 defaulted\n"
500 "\t\tmap $lightmap\n"
501 "\t\trgbGen identity\n"
503 "\tq3map_styleMarker\n"
506 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
507 "\t\trgbGen identity\n"
514 if( (strlen( mapName ) + 1 + 32) > MAX_QPATH )
515 Error( "Custom shader name length (%d) exceeded. Shorten your map name.\n", MAX_QPATH );
517 /* do some bad find-replace */
518 s = strstr( srcShaderText, find );
520 //% strcpy( shaderText, srcShaderText );
521 return si; /* testing just using the existing shader if this fails */
524 /* substitute 'find' with 'replace' */
525 loc = s - srcShaderText;
526 strcpy( shaderText, srcShaderText );
527 shaderText[ loc ] = '\0';
528 strcat( shaderText, replace );
529 strcat( shaderText, &srcShaderText[ loc + strlen( find ) ] );
532 /* make md5 hash of the shader text */
533 mh = mhash_init( MHASH_MD5 );
535 Error( "Unable to initialize MD5 hash context" );
536 mhash( mh, shaderText, strlen( shaderText ) );
537 mhash_deinit( mh, digest );
539 /* mangle hash into a shader name */
540 sprintf( shader, "%s/%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", mapName,
541 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ],
542 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ], digest[ 15 ] );
545 csi = ShaderInfoForShader( shader );
547 /* might be a preexisting shader */
551 /* clone the existing shader and rename */
552 memcpy( csi, si, sizeof( shaderInfo_t ) );
553 strcpy( csi->shader, shader );
556 /* store new shader text */
557 csi->shaderText = safe_malloc( strlen( shaderText ) + 1 );
558 strcpy( csi->shaderText, shaderText ); /* LEAK! */
567 EmitVertexRemapShader()
568 adds a vertexremapshader key/value pair to worldspawn
571 void EmitVertexRemapShader( char *from, char *to )
575 char key[ 64 ], value[ 256 ];
579 if( from == NULL || from[ 0 ] == '\0' ||
580 to == NULL || to[ 0 ] == '\0' )
584 sprintf( value, "%s;%s", from, to );
587 mh = mhash_init( MHASH_MD5 );
589 Error( "Unable to initialize MD5 hash context" );
590 mhash( mh, value, strlen( value ) );
591 mhash_deinit( mh, digest );
593 /* make key (this is annoying, as vertexremapshader is precisely 17 characters,
594 which is one too long, so we leave off the last byte of the md5 digest) */
595 sprintf( key, "vertexremapshader%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
596 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ],
597 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ] ); /* no: digest[ 15 ] */
599 /* add key/value pair to worldspawn */
600 SetKeyValue( &entities[ 0 ], key, value );
607 allocates and initializes a new shader
610 static shaderInfo_t *AllocShaderInfo( void )
616 if( shaderInfo == NULL )
618 shaderInfo = safe_malloc( sizeof( shaderInfo_t ) * MAX_SHADER_INFO );
623 if( numShaderInfo == MAX_SHADER_INFO )
624 Error( "MAX_SHADER_INFO exceeded. Remove some PK3 files or shader scripts from shaderlist.txt and try again." );
625 si = &shaderInfo[ numShaderInfo ];
628 /* ydnar: clear to 0 first */
629 memset( si, 0, sizeof( shaderInfo_t ) );
632 ApplySurfaceParm( "default", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
634 si->backsplashFraction = DEF_BACKSPLASH_FRACTION;
635 si->backsplashDistance = DEF_BACKSPLASH_DISTANCE;
637 si->bounceScale = DEF_RADIOSITY_BOUNCE;
639 si->lightStyle = LS_NORMAL;
641 si->polygonOffset = qfalse;
643 si->shadeAngleDegrees = 0.0f;
644 si->lightmapSampleSize = 0;
645 si->lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;
646 si->patchShadows = qfalse;
647 si->vertexShadows = qtrue; /* ydnar: changed default behavior */
648 si->forceSunlight = qfalse;
649 si->vertexScale = 1.0;
650 si->notjunc = qfalse;
652 /* ydnar: set texture coordinate transform matrix to identity */
653 TCModIdentity( si->mod );
655 /* ydnar: lightmaps can now be > 128x128 in certain games or an externally generated tga */
656 si->lmCustomWidth = lmCustomSize;
657 si->lmCustomHeight = lmCustomSize;
659 /* return to sender */
666 FinishShader() - ydnar
667 sets a shader's width and height among other things
670 void FinishShader( shaderInfo_t *si )
673 float st[ 2 ], o[ 2 ], dist, bestDist;
674 vec4_t color, bestColor, delta;
677 /* don't double-dip */
681 /* if they're explicitly set, copy from image size */
682 if( si->shaderWidth == 0 && si->shaderHeight == 0 )
684 si->shaderWidth = si->shaderImage->width;
685 si->shaderHeight = si->shaderImage->height;
688 /* legacy terrain has explicit image-sized texture projection */
689 if( si->legacyTerrain && si->tcGen == qfalse )
691 /* set xy texture projection */
693 VectorSet( si->vecs[ 0 ], (1.0f / (si->shaderWidth * 0.5f)), 0, 0 );
694 VectorSet( si->vecs[ 1 ], 0, (1.0f / (si->shaderHeight * 0.5f)), 0 );
697 /* find pixel coordinates best matching the average color of the image */
699 o[ 0 ] = 1.0f / si->shaderImage->width;
700 o[ 1 ] = 1.0f / si->shaderImage->height;
701 for( y = 0, st[ 1 ] = 0.0f; y < si->shaderImage->height; y++, st[ 1 ] += o[ 1 ] )
703 for( x = 0, st[ 0 ] = 0.0f; x < si->shaderImage->width; x++, st[ 0 ] += o[ 0 ] )
705 /* sample the shader image */
706 RadSampleImage( si->shaderImage->pixels, si->shaderImage->width, si->shaderImage->height, st, color );
708 /* determine error squared */
709 VectorSubtract( color, si->averageColor, delta );
710 delta[ 3 ] = color[ 3 ] - si->averageColor[ 3 ];
711 dist = delta[ 0 ] * delta[ 0 ] + delta[ 1 ] * delta[ 1 ] + delta[ 2 ] * delta[ 2 ] + delta[ 3 ] * delta[ 3 ];
712 if( dist < bestDist )
714 VectorCopy( color, bestColor );
715 bestColor[ 3 ] = color[ 3 ];
716 si->stFlat[ 0 ] = st[ 0 ];
717 si->stFlat[ 1 ] = st[ 1 ];
722 /* set to finished */
723 si->finished = qtrue;
730 loads a shader's images
731 ydnar: image.c made this a bit simpler
734 static void LoadShaderImages( shaderInfo_t *si )
740 /* nodraw shaders don't need images */
741 if( si->compileFlags & C_NODRAW )
742 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
745 /* try to load editor image first */
746 si->shaderImage = ImageLoad( si->editorImagePath );
748 /* then try shadername */
749 if( si->shaderImage == NULL )
750 si->shaderImage = ImageLoad( si->shader );
752 /* then try implicit image path (note: new behavior!) */
753 if( si->shaderImage == NULL )
754 si->shaderImage = ImageLoad( si->implicitImagePath );
756 /* then try lightimage (note: new behavior!) */
757 if( si->shaderImage == NULL )
758 si->shaderImage = ImageLoad( si->lightImagePath );
760 /* otherwise, use default image */
761 if( si->shaderImage == NULL )
763 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
764 if( warnImage && strcmp( si->shader, "noshader" ) )
765 Sys_Printf( "WARNING: Couldn't find image for shader %s\n", si->shader );
768 /* load light image */
769 si->lightImage = ImageLoad( si->lightImagePath );
771 /* load normalmap image (ok if this is NULL) */
772 si->normalImage = ImageLoad( si->normalImagePath );
773 if( si->normalImage != NULL )
775 Sys_FPrintf( SYS_VRB, "Shader %s has\n"
776 " NM %s\n", si->shader, si->normalImagePath );
780 /* if no light image, use shader image */
781 if( si->lightImage == NULL )
782 si->lightImage = ImageLoad( si->shaderImage->name );
784 /* create default and average colors */
785 count = si->lightImage->width * si->lightImage->height;
786 VectorClear( color );
788 for( i = 0; i < count; i++ )
790 color[ 0 ] += si->lightImage->pixels[ i * 4 + 0 ];
791 color[ 1 ] += si->lightImage->pixels[ i * 4 + 1 ];
792 color[ 2 ] += si->lightImage->pixels[ i * 4 + 2 ];
793 color[ 3 ] += si->lightImage->pixels[ i * 4 + 3 ];
796 if( VectorLength( si->color ) <= 0.0f )
797 ColorNormalize( color, si->color );
798 VectorScale( color, (1.0f / count), si->averageColor );
804 ShaderInfoForShader()
805 finds a shaderinfo for a named shader
808 shaderInfo_t *ShaderInfoForShader( const char *shaderName )
812 char shader[ MAX_QPATH ];
816 if( shaderName == NULL || shaderName[ 0 ] == '\0' )
818 Sys_Printf( "WARNING: Null or empty shader name\n" );
819 shaderName = "missing";
822 /* strip off extension */
823 strcpy( shader, shaderName );
824 StripExtension( shader );
827 for( i = 0; i < numShaderInfo; i++ )
829 si = &shaderInfo[ i ];
830 if( !Q_stricmp( shader, si->shader ) )
832 /* load image if necessary */
833 if( si->finished == qfalse )
835 LoadShaderImages( si );
844 /* allocate a default shader */
845 si = AllocShaderInfo();
846 strcpy( si->shader, shader );
847 LoadShaderImages( si );
857 GetTokenAppend() - ydnar
858 gets a token and appends its text to the specified buffer
861 static int oldScriptLine = 0;
862 static int tabDepth = 0;
864 qboolean GetTokenAppend( char *buffer, qboolean crossline )
871 r = GetToken( crossline );
872 if( r == qfalse || buffer == NULL || token[ 0 ] == '\0' )
876 if( token[ 0 ] == '}' )
880 if( oldScriptLine != scriptline )
882 strcat( buffer, "\n" );
883 for( i = 0; i < tabDepth; i++ )
884 strcat( buffer, "\t" );
887 strcat( buffer, " " );
888 oldScriptLine = scriptline;
889 strcat( buffer, token );
892 if( token[ 0 ] == '{' )
900 void Parse1DMatrixAppend( char *buffer, int x, vec_t *m )
905 if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, "(" ) )
906 Error( "Parse1DMatrixAppend(): line %d: ( not found!", scriptline );
907 for( i = 0; i < x; i++ )
909 if( !GetTokenAppend( buffer, qfalse ) )
910 Error( "Parse1DMatrixAppend(): line %d: Number not found!", scriptline );
911 m[ i ] = atof( token );
913 if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, ")" ) )
914 Error( "Parse1DMatrixAppend(): line %d: ) not found!", scriptline );
922 parses a shader file into discrete shaderInfo_t
925 static void ParseShaderFile( const char *filename )
929 char *suffix, temp[ 1024 ];
930 char shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */
935 shaderText[ 0 ] = '\0';
937 /* load the shader */
938 LoadScriptFile( filename, 0 );
943 /* copy shader text to the shaderinfo */
944 if( si != NULL && shaderText[ 0 ] != '\0' )
946 strcat( shaderText, "\n" );
947 si->shaderText = safe_malloc( strlen( shaderText ) + 1 );
948 strcpy( si->shaderText, shaderText );
949 //% if( VectorLength( si->vecs[ 0 ] ) )
950 //% Sys_Printf( "%s\n", shaderText );
953 /* ydnar: clear shader text buffer */
954 shaderText[ 0 ] = '\0';
956 /* test for end of file */
957 if( !GetToken( qtrue ) )
960 /* shader name is initial token */
961 si = AllocShaderInfo();
962 strcpy( si->shader, token );
964 /* ignore ":q3map" suffix */
965 suffix = strstr( si->shader, ":q3map" );
969 /* handle { } section */
970 if( !GetTokenAppend( shaderText, qtrue ) )
972 if( strcmp( token, "{" ) )
975 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s\nLast known shader: %s",
976 filename, scriptline, token, si->shader );
978 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s",
979 filename, scriptline, token );
984 /* get the next token */
985 if( !GetTokenAppend( shaderText, qtrue ) )
987 if( !strcmp( token, "}" ) )
991 /* -----------------------------------------------------------------
992 shader stages (passes)
993 ----------------------------------------------------------------- */
995 /* parse stage directives */
996 if( !strcmp( token, "{" ) )
998 si->hasPasses = qtrue;
1001 if( !GetTokenAppend( shaderText, qtrue ) )
1003 if( !strcmp( token, "}" ) )
1006 /* only care about images if we don't have a editor/light image */
1007 if( si->editorImagePath[ 0 ] == '\0' && si->lightImagePath[ 0 ] == '\0' && si->implicitImagePath[ 0 ] == '\0' )
1009 /* digest any images */
1010 if( !Q_stricmp( token, "map" ) ||
1011 !Q_stricmp( token, "clampMap" ) ||
1012 !Q_stricmp( token, "animMap" ) ||
1013 !Q_stricmp( token, "clampAnimMap" ) ||
1014 !Q_stricmp( token, "clampMap" ) ||
1015 !Q_stricmp( token, "mapComp" ) ||
1016 !Q_stricmp( token, "mapNoComp" ) )
1018 /* skip one token for animated stages */
1019 if( !Q_stricmp( token, "animMap" ) || !Q_stricmp( token, "clampAnimMap" ) )
1020 GetTokenAppend( shaderText, qfalse );
1023 GetTokenAppend( shaderText, qfalse );
1024 if( token[ 0 ] != '*' && token[ 0 ] != '$' )
1026 strcpy( si->lightImagePath, token );
1027 DefaultExtension( si->lightImagePath, ".tga" );
1030 //% Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath );
1038 /* -----------------------------------------------------------------
1039 surfaceparm * directives
1040 ----------------------------------------------------------------- */
1042 /* match surfaceparm */
1043 else if( !Q_stricmp( token, "surfaceparm" ) )
1045 GetTokenAppend( shaderText, qfalse );
1046 if( ApplySurfaceParm( token, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1047 Sys_Printf( "WARNING: Unknown surfaceparm: \"%s\"\n", token );
1051 /* -----------------------------------------------------------------
1052 game-related shader directives
1053 ----------------------------------------------------------------- */
1055 /* ydnar: fogparms (for determining fog volumes) */
1056 else if( !Q_stricmp( token, "fogparms" ) )
1057 si->fogParms = qtrue;
1059 /* ydnar: polygonoffset (for no culling) */
1060 else if( !Q_stricmp( token, "polygonoffset" ) )
1061 si->polygonOffset = qtrue;
1063 /* tesssize is used to force liquid surfaces to subdivide */
1064 else if( !Q_stricmp( token, "tessSize" ) || !Q_stricmp( token, "q3map_tessSize" ) /* sof2 */ )
1066 GetTokenAppend( shaderText, qfalse );
1067 si->subdivisions = atof( token );
1070 /* cull none will set twoSided (ydnar: added disable too) */
1071 else if ( !Q_stricmp( token, "cull" ) )
1073 GetTokenAppend( shaderText, qfalse );
1074 if( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "disable" ) || !Q_stricmp( token, "twosided" ) )
1075 si->twoSided = qtrue;
1078 /* deformVertexes autosprite[ 2 ]
1079 we catch this so autosprited surfaces become point
1080 lights instead of area lights */
1081 else if( !Q_stricmp( token, "deformVertexes" ) )
1083 GetTokenAppend( shaderText, qfalse );
1085 /* deformVertexes autosprite(2) */
1086 if( !Q_strncasecmp( token, "autosprite", 10 ) )
1088 /* set it as autosprite and detail */
1089 si->autosprite = qtrue;
1090 ApplySurfaceParm( "detail", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1092 /* ydnar: gs mods: added these useful things */
1094 si->notjunc = qtrue;
1097 /* deformVertexes move <x> <y> <z> <func> <base> <amplitude> <phase> <freq> (ydnar: for particle studio support) */
1098 if( !Q_stricmp( token, "move") )
1100 vec3_t amt, mins, maxs;
1104 /* get move amount */
1105 GetTokenAppend( shaderText, qfalse ); amt[ 0 ] = atof( token );
1106 GetTokenAppend( shaderText, qfalse ); amt[ 1 ] = atof( token );
1107 GetTokenAppend( shaderText, qfalse ); amt[ 2 ] = atof( token );
1110 GetTokenAppend( shaderText, qfalse );
1112 /* get base and amplitude */
1113 GetTokenAppend( shaderText, qfalse ); base = atof( token );
1114 GetTokenAppend( shaderText, qfalse ); amp = atof( token );
1117 VectorScale( amt, base, mins );
1118 VectorMA( mins, amp, amt, maxs );
1119 VectorAdd( si->mins, mins, si->mins );
1120 VectorAdd( si->maxs, maxs, si->maxs );
1124 /* light <value> (old-style flare specification) */
1125 else if( !Q_stricmp( token, "light" ) )
1127 GetTokenAppend( shaderText, qfalse );
1128 si->flareShader = game->flareShader;
1131 /* ydnar: damageShader <shader> <health> (sof2 mods) */
1132 else if( !Q_stricmp( token, "damageShader" ) )
1134 GetTokenAppend( shaderText, qfalse );
1135 if( token[ 0 ] != '\0' )
1137 si->damageShader = safe_malloc( strlen( token ) + 1 );
1138 strcpy( si->damageShader, token );
1140 GetTokenAppend( shaderText, qfalse ); /* don't do anything with health */
1143 /* ydnar: enemy territory implicit shaders */
1144 else if( !Q_stricmp( token, "implicitMap" ) )
1146 si->implicitMap = IM_OPAQUE;
1147 GetTokenAppend( shaderText, qfalse );
1148 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1149 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1151 strcpy( si->implicitImagePath, token );
1154 else if( !Q_stricmp( token, "implicitMask" ) )
1156 si->implicitMap = IM_MASKED;
1157 GetTokenAppend( shaderText, qfalse );
1158 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1159 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1161 strcpy( si->implicitImagePath, token );
1164 else if( !Q_stricmp( token, "implicitBlend" ) )
1166 si->implicitMap = IM_MASKED;
1167 GetTokenAppend( shaderText, qfalse );
1168 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1169 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1171 strcpy( si->implicitImagePath, token );
1175 /* -----------------------------------------------------------------
1177 ----------------------------------------------------------------- */
1179 /* qer_editorimage <image> */
1180 else if( !Q_stricmp( token, "qer_editorImage" ) )
1182 GetTokenAppend( shaderText, qfalse );
1183 strcpy( si->editorImagePath, token );
1184 DefaultExtension( si->editorImagePath, ".tga" );
1187 /* ydnar: q3map_normalimage <image> (bumpmapping normal map) */
1188 else if( !Q_stricmp( token, "q3map_normalImage" ) )
1190 GetTokenAppend( shaderText, qfalse );
1191 strcpy( si->normalImagePath, token );
1192 DefaultExtension( si->normalImagePath, ".tga" );
1195 /* q3map_lightimage <image> */
1196 else if( !Q_stricmp( token, "q3map_lightImage" ) )
1198 GetTokenAppend( shaderText, qfalse );
1199 strcpy( si->lightImagePath, token );
1200 DefaultExtension( si->lightImagePath, ".tga" );
1203 /* ydnar: skyparms <outer image> <cloud height> <inner image> */
1204 else if( !Q_stricmp( token, "skyParms" ) )
1206 /* get image base */
1207 GetTokenAppend( shaderText, qfalse );
1209 /* ignore bogus paths */
1210 if( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) )
1212 strcpy( si->skyParmsImageBase, token );
1214 /* use top image as sky light image */
1215 if( si->lightImagePath[ 0 ] == '\0' )
1216 sprintf( si->lightImagePath, "%s_up.tga", si->skyParmsImageBase );
1219 /* skip rest of line */
1220 GetTokenAppend( shaderText, qfalse );
1221 GetTokenAppend( shaderText, qfalse );
1224 /* -----------------------------------------------------------------
1226 ----------------------------------------------------------------- */
1228 /* q3map_sun <red> <green> <blue> <intensity> <degrees> <elevation>
1229 color will be normalized, so it doesn't matter what range you use
1230 intensity falls off with angle but not distance 100 is a fairly bright sun
1231 degree of 0 = from the east, 90 = north, etc. altitude of 0 = sunrise/set, 90 = noon
1232 ydnar: sof2map has bareword 'sun' token, so we support that as well */
1233 else if( !Q_stricmp( token, "sun" ) /* sof2 */ || !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) )
1240 /* ydnar: extended sun directive? */
1241 if( !Q_stricmp( token, "q3map_sunext" ) )
1245 sun = safe_malloc( sizeof( *sun ) );
1246 memset( sun, 0, sizeof( *sun ) );
1249 sun->style = si->lightStyle;
1252 GetTokenAppend( shaderText, qfalse );
1253 sun->color[ 0 ] = atof( token );
1254 GetTokenAppend( shaderText, qfalse );
1255 sun->color[ 1 ] = atof( token );
1256 GetTokenAppend( shaderText, qfalse );
1257 sun->color[ 2 ] = atof( token );
1260 VectorNormalize( sun->color, sun->color );
1262 /* scale color by brightness */
1263 GetTokenAppend( shaderText, qfalse );
1264 sun->photons = atof( token );
1266 /* get sun angle/elevation */
1267 GetTokenAppend( shaderText, qfalse );
1269 a = a / 180.0f * Q_PI;
1271 GetTokenAppend( shaderText, qfalse );
1273 b = b / 180.0f * Q_PI;
1275 sun->direction[ 0 ] = cos( a ) * cos( b );
1276 sun->direction[ 1 ] = sin( a ) * cos( b );
1277 sun->direction[ 2 ] = sin( b );
1279 /* get filter radius from shader */
1280 sun->filterRadius = si->lightFilterRadius;
1282 /* ydnar: get sun angular deviance/samples */
1283 if( ext && TokenAvailable() )
1285 GetTokenAppend( shaderText, qfalse );
1286 sun->deviance = atof( token );
1287 sun->deviance = sun->deviance / 180.0f * Q_PI;
1289 GetTokenAppend( shaderText, qfalse );
1290 sun->numSamples = atoi( token );
1294 sun->next = si->sun;
1297 /* apply sky surfaceparm */
1298 ApplySurfaceParm( "sky", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1300 /* don't process any more tokens on this line */
1305 else if( !Q_strncasecmp( token, "q3map_", 6 ) )
1307 /* ydnar: q3map_baseShader <shader> (inherit this shader's parameters) */
1308 if( !Q_stricmp( token, "q3map_baseShader" ) )
1311 qboolean oldWarnImage;
1315 GetTokenAppend( shaderText, qfalse );
1316 //% Sys_FPrintf( SYS_VRB, "Shader %s has base shader %s\n", si->shader, token );
1317 oldWarnImage = warnImage;
1319 si2 = ShaderInfoForShader( token );
1320 warnImage = oldWarnImage;
1326 strcpy( temp, si->shader );
1329 memcpy( si, si2, sizeof( *si ) );
1331 /* restore name and set to unfinished */
1332 strcpy( si->shader, temp );
1333 si->shaderWidth = 0;
1334 si->shaderHeight = 0;
1335 si->finished = qfalse;
1339 /* ydnar: q3map_surfacemodel <path to model> <density> <min scale> <max scale> <min angle> <max angle> <oriented (0 or 1)> */
1340 else if( !Q_stricmp( token, "q3map_surfacemodel" ) )
1342 surfaceModel_t *model;
1345 /* allocate new model and attach it */
1346 model = safe_malloc( sizeof( *model ) );
1347 memset( model, 0, sizeof( *model ) );
1348 model->next = si->surfaceModel;
1349 si->surfaceModel = model;
1351 /* get parameters */
1352 GetTokenAppend( shaderText, qfalse );
1353 strcpy( model->model, token );
1355 GetTokenAppend( shaderText, qfalse );
1356 model->density = atof( token );
1357 GetTokenAppend( shaderText, qfalse );
1358 model->odds = atof( token );
1360 GetTokenAppend( shaderText, qfalse );
1361 model->minScale = atof( token );
1362 GetTokenAppend( shaderText, qfalse );
1363 model->maxScale = atof( token );
1365 GetTokenAppend( shaderText, qfalse );
1366 model->minAngle = atof( token );
1367 GetTokenAppend( shaderText, qfalse );
1368 model->maxAngle = atof( token );
1370 GetTokenAppend( shaderText, qfalse );
1371 model->oriented = (token[ 0 ] == '1' ? qtrue : qfalse);
1374 /* ydnar/sd: q3map_foliage <path to model> <scale> <density> <odds> <invert alpha (1 or 0)> */
1375 else if( !Q_stricmp( token, "q3map_foliage" ) )
1380 /* allocate new foliage struct and attach it */
1381 foliage = safe_malloc( sizeof( *foliage ) );
1382 memset( foliage, 0, sizeof( *foliage ) );
1383 foliage->next = si->foliage;
1384 si->foliage = foliage;
1386 /* get parameters */
1387 GetTokenAppend( shaderText, qfalse );
1388 strcpy( foliage->model, token );
1390 GetTokenAppend( shaderText, qfalse );
1391 foliage->scale = atof( token );
1392 GetTokenAppend( shaderText, qfalse );
1393 foliage->density = atof( token );
1394 GetTokenAppend( shaderText, qfalse );
1395 foliage->odds = atof( token );
1396 GetTokenAppend( shaderText, qfalse );
1397 foliage->inverseAlpha = atoi( token );
1400 /* ydnar: q3map_bounce <value> (fraction of light to re-emit during radiosity passes) */
1401 else if( !Q_stricmp( token, "q3map_bounce" ) || !Q_stricmp( token, "q3map_bounceScale" ) )
1403 GetTokenAppend( shaderText, qfalse );
1404 si->bounceScale = atof( token );
1407 /* ydnar/splashdamage: q3map_skyLight <value> <iterations> */
1408 else if( !Q_stricmp( token, "q3map_skyLight" ) )
1410 GetTokenAppend( shaderText, qfalse );
1411 si->skyLightValue = atof( token );
1412 GetTokenAppend( shaderText, qfalse );
1413 si->skyLightIterations = atoi( token );
1416 if( si->skyLightValue < 0.0f )
1417 si->skyLightValue = 0.0f;
1418 if( si->skyLightIterations < 2 )
1419 si->skyLightIterations = 2;
1422 /* q3map_surfacelight <value> */
1423 else if( !Q_stricmp( token, "q3map_surfacelight" ) )
1425 GetTokenAppend( shaderText, qfalse );
1426 si->value = atof( token );
1429 /* q3map_lightStyle (sof2/jk2 lightstyle) */
1430 else if( !Q_stricmp( token, "q3map_lightStyle" ) )
1432 GetTokenAppend( shaderText, qfalse );
1433 val = atoi( token );
1436 else if( val > LS_NONE )
1438 si->lightStyle = val;
1441 /* wolf: q3map_lightRGB <red> <green> <blue> */
1442 else if( !Q_stricmp( token, "q3map_lightRGB" ) )
1444 VectorClear( si->color );
1445 GetTokenAppend( shaderText, qfalse );
1446 si->color[ 0 ] = atof( token );
1447 GetTokenAppend( shaderText, qfalse );
1448 si->color[ 1 ] = atof( token );
1449 GetTokenAppend( shaderText, qfalse );
1450 si->color[ 2 ] = atof( token );
1451 ColorNormalize( si->color, si->color );
1454 /* q3map_lightSubdivide <value> */
1455 else if( !Q_stricmp( token, "q3map_lightSubdivide" ) )
1457 GetTokenAppend( shaderText, qfalse );
1458 si->lightSubdivide = atoi( token );
1461 /* q3map_backsplash <percent> <distance> */
1462 else if( !Q_stricmp( token, "q3map_backsplash" ) )
1464 GetTokenAppend( shaderText, qfalse );
1465 si->backsplashFraction = atof( token ) * 0.01f;
1466 GetTokenAppend( shaderText, qfalse );
1467 si->backsplashDistance = atof( token );
1470 /* q3map_lightmapSampleSize <value> */
1471 else if( !Q_stricmp( token, "q3map_lightmapSampleSize" ) )
1473 GetTokenAppend( shaderText, qfalse );
1474 si->lightmapSampleSize = atoi( token );
1477 /* q3map_lightmapSampleSffset <value> */
1478 else if( !Q_stricmp( token, "q3map_lightmapSampleOffset" ) )
1480 GetTokenAppend( shaderText, qfalse );
1481 si->lightmapSampleOffset = atof( token );
1484 /* ydnar: q3map_lightmapFilterRadius <self> <other> */
1485 else if( !Q_stricmp( token, "q3map_lightmapFilterRadius" ) )
1487 GetTokenAppend( shaderText, qfalse );
1488 si->lmFilterRadius = atof( token );
1489 GetTokenAppend( shaderText, qfalse );
1490 si->lightFilterRadius = atof( token );
1493 /* ydnar: q3map_lightmapAxis [xyz] */
1494 else if( !Q_stricmp( token, "q3map_lightmapAxis" ) )
1496 GetTokenAppend( shaderText, qfalse );
1497 if( !Q_stricmp( token, "x" ) )
1498 VectorSet( si->lightmapAxis, 1, 0, 0 );
1499 else if( !Q_stricmp( token, "y" ) )
1500 VectorSet( si->lightmapAxis, 0, 1, 0 );
1501 else if( !Q_stricmp( token, "z" ) )
1502 VectorSet( si->lightmapAxis, 0, 0, 1 );
1505 Sys_Printf( "WARNING: Unknown value for lightmap axis: %s\n", token );
1506 VectorClear( si->lightmapAxis );
1510 /* ydnar: q3map_lightmapSize <width> <height> (for autogenerated shaders + external tga lightmaps) */
1511 else if( !Q_stricmp( token, "q3map_lightmapSize" ) )
1513 GetTokenAppend( shaderText, qfalse );
1514 si->lmCustomWidth = atoi( token );
1515 GetTokenAppend( shaderText, qfalse );
1516 si->lmCustomHeight = atoi( token );
1518 /* must be a power of 2 */
1519 if( ((si->lmCustomWidth - 1) & si->lmCustomWidth) ||
1520 ((si->lmCustomHeight - 1) & si->lmCustomHeight) )
1522 Sys_Printf( "WARNING: Non power-of-two lightmap size specified (%d, %d)\n",
1523 si->lmCustomWidth, si->lmCustomHeight );
1524 si->lmCustomWidth = lmCustomSize;
1525 si->lmCustomHeight = lmCustomSize;
1529 /* ydnar: q3map_lightmapBrightness N (for autogenerated shaders + external tga lightmaps) */
1530 else if( !Q_stricmp( token, "q3map_lightmapBrightness" ) || !Q_stricmp( token, "q3map_lightmapGamma" ) )
1532 GetTokenAppend( shaderText, qfalse );
1533 si->lmBrightness = atof( token );
1534 if( si->lmBrightness < 0 )
1535 si->lmBrightness = 1.0;
1538 /* q3map_vertexScale (scale vertex lighting by this fraction) */
1539 else if( !Q_stricmp( token, "q3map_vertexScale" ) )
1541 GetTokenAppend( shaderText, qfalse );
1542 si->vertexScale = atof( token );
1545 /* q3map_noVertexLight */
1546 else if( !Q_stricmp( token, "q3map_noVertexLight" ) )
1548 si->noVertexLight = qtrue;
1551 /* q3map_flare[Shader] <shader> */
1552 else if( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) )
1554 GetTokenAppend( shaderText, qfalse );
1555 if( token[ 0 ] != '\0' )
1557 si->flareShader = safe_malloc( strlen( token ) + 1 );
1558 strcpy( si->flareShader, token );
1562 /* q3map_backShader <shader> */
1563 else if( !Q_stricmp( token, "q3map_backShader" ) )
1565 GetTokenAppend( shaderText, qfalse );
1566 if( token[ 0 ] != '\0' )
1568 si->backShader = safe_malloc( strlen( token ) + 1 );
1569 strcpy( si->backShader, token );
1573 /* ydnar: q3map_cloneShader <shader> */
1574 else if ( !Q_stricmp( token, "q3map_cloneShader" ) )
1576 GetTokenAppend( shaderText, qfalse );
1577 if( token[ 0 ] != '\0' )
1579 si->cloneShader = safe_malloc( strlen( token ) + 1 );
1580 strcpy( si->cloneShader, token );
1584 /* q3map_remapShader <shader> */
1585 else if( !Q_stricmp( token, "q3map_remapShader" ) )
1587 GetTokenAppend( shaderText, qfalse );
1588 if( token[ 0 ] != '\0' )
1590 si->remapShader = safe_malloc( strlen( token ) + 1 );
1591 strcpy( si->remapShader, token );
1595 /* ydnar: q3map_offset <value> */
1596 else if( !Q_stricmp( token, "q3map_offset" ) )
1598 GetTokenAppend( shaderText, qfalse );
1599 si->offset = atof( token );
1602 /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
1603 else if( !Q_stricmp( token, "q3map_fur" ) )
1605 GetTokenAppend( shaderText, qfalse );
1606 si->furNumLayers = atoi( token );
1607 GetTokenAppend( shaderText, qfalse );
1608 si->furOffset = atof( token );
1609 GetTokenAppend( shaderText, qfalse );
1610 si->furFade = atof( token );
1613 /* ydnar: gs mods: legacy support for terrain/terrain2 shaders */
1614 else if( !Q_stricmp( token, "q3map_terrain" ) )
1616 /* team arena terrain is assumed to be nonplanar, with full normal averaging,
1617 passed through the metatriangle surface pipeline, with a lightmap axis on z */
1618 si->legacyTerrain = qtrue;
1620 si->notjunc = qtrue;
1621 si->indexed = qtrue;
1622 si->nonplanar = qtrue;
1623 si->forceMeta = qtrue;
1624 si->shadeAngleDegrees = 179.0f;
1625 //% VectorSet( si->lightmapAxis, 0, 0, 1 ); /* ydnar 2002-09-21: turning this off for better lightmapping of cliff faces */
1628 /* ydnar: picomodel: q3map_forceMeta (forces brush faces and/or triangle models to go through the metasurface pipeline) */
1629 else if( !Q_stricmp( token, "q3map_forceMeta" ) )
1631 si->forceMeta = qtrue;
1634 /* ydnar: gs mods: q3map_shadeAngle <degrees> */
1635 else if( !Q_stricmp( token, "q3map_shadeAngle" ) )
1637 GetTokenAppend( shaderText, qfalse );
1638 si->shadeAngleDegrees = atof( token );
1641 /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
1642 else if( !Q_stricmp( token, "q3map_textureSize" ) )
1644 GetTokenAppend( shaderText, qfalse );
1645 si->shaderWidth = atoi( token );
1646 GetTokenAppend( shaderText, qfalse );
1647 si->shaderHeight = atoi( token );
1650 /* ydnar: gs mods: q3map_tcGen <style> <parameters> */
1651 else if( !Q_stricmp( token, "q3map_tcGen" ) )
1654 GetTokenAppend( shaderText, qfalse );
1656 /* q3map_tcGen vector <s vector> <t vector> */
1657 if( !Q_stricmp( token, "vector" ) )
1659 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1660 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1663 /* q3map_tcGen ivector <1.0/s vector> <1.0/t vector> (inverse vector, easier for mappers to understand) */
1664 else if( !Q_stricmp( token, "ivector" ) )
1666 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1667 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1668 for( i = 0; i < 3; i++ )
1670 si->vecs[ 0 ][ i ] = si->vecs[ 0 ][ i ] ? 1.0 / si->vecs[ 0 ][ i ] : 0;
1671 si->vecs[ 1 ][ i ] = si->vecs[ 1 ][ i ] ? 1.0 / si->vecs[ 1 ][ i ] : 0;
1676 Sys_Printf( "WARNING: Unknown q3map_tcGen method: %s\n", token );
1677 VectorClear( si->vecs[ 0 ] );
1678 VectorClear( si->vecs[ 1 ] );
1682 /* ydnar: gs mods: q3map_[color|rgb|alpha][Gen|Mod] <style> <parameters> */
1683 else if( !Q_stricmp( token, "q3map_colorGen" ) || !Q_stricmp( token, "q3map_colorMod" ) ||
1684 !Q_stricmp( token, "q3map_rgbGen" ) || !Q_stricmp( token, "q3map_rgbMod" ) ||
1685 !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) )
1687 colorMod_t *cm, *cm2;
1691 /* alphamods are colormod + 1 */
1692 alpha = (!Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" )) ? 1 : 0;
1694 /* allocate new colormod */
1695 cm = safe_malloc( sizeof( *cm ) );
1696 memset( cm, 0, sizeof( *cm ) );
1698 /* attach to shader */
1699 if( si->colorMod == NULL )
1703 for( cm2 = si->colorMod; cm2 != NULL; cm2 = cm2->next )
1705 if( cm2->next == NULL )
1714 GetTokenAppend( shaderText, qfalse );
1716 /* alpha set|const A */
1717 if( alpha && (!Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" )) )
1719 cm->type = CM_ALPHA_SET;
1720 GetTokenAppend( shaderText, qfalse );
1721 cm->data[ 0 ] = atof( token );
1724 /* color|rgb set|const ( X Y Z ) */
1725 else if( !Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" ) )
1727 cm->type = CM_COLOR_SET;
1728 Parse1DMatrixAppend( shaderText, 3, cm->data );
1732 else if( alpha && !Q_stricmp( token, "scale" ) )
1734 cm->type = CM_ALPHA_SCALE;
1735 GetTokenAppend( shaderText, qfalse );
1736 cm->data[ 0 ] = atof( token );
1739 /* color|rgb scale ( X Y Z ) */
1740 else if( !Q_stricmp( token, "scale" ) )
1742 cm->type = CM_COLOR_SCALE;
1743 Parse1DMatrixAppend( shaderText, 3, cm->data );
1746 /* dotProduct ( X Y Z ) */
1747 else if( !Q_stricmp( token, "dotProduct" ) )
1749 cm->type = CM_COLOR_DOT_PRODUCT + alpha;
1750 Parse1DMatrixAppend( shaderText, 3, cm->data );
1753 /* dotProduct2 ( X Y Z ) */
1754 else if( !Q_stricmp( token, "dotProduct2" ) )
1756 cm->type = CM_COLOR_DOT_PRODUCT_2 + alpha;
1757 Parse1DMatrixAppend( shaderText, 3, cm->data );
1761 else if( !Q_stricmp( token, "volume" ) )
1763 /* special stub mode for flagging volume brushes */
1764 cm->type = CM_VOLUME;
1769 Sys_Printf( "WARNING: Unknown colorMod method: %s\n", token );
1772 /* ydnar: gs mods: q3map_tcMod <style> <parameters> */
1773 else if( !Q_stricmp( token, "q3map_tcMod" ) )
1778 GetTokenAppend( shaderText, qfalse );
1780 /* q3map_tcMod [translate | shift | offset] <s> <t> */
1781 if( !Q_stricmp( token, "translate" ) || !Q_stricmp( token, "shift" ) || !Q_stricmp( token, "offset" ) )
1783 GetTokenAppend( shaderText, qfalse );
1785 GetTokenAppend( shaderText, qfalse );
1788 TCModTranslate( si->mod, a, b );
1791 /* q3map_tcMod scale <s> <t> */
1792 else if( !Q_stricmp( token, "scale" ) )
1794 GetTokenAppend( shaderText, qfalse );
1796 GetTokenAppend( shaderText, qfalse );
1799 TCModScale( si->mod, a, b );
1802 /* q3map_tcMod rotate <s> <t> (fixme: make this communitive) */
1803 else if( !Q_stricmp( token, "rotate" ) )
1805 GetTokenAppend( shaderText, qfalse );
1807 TCModRotate( si->mod, a );
1810 Sys_Printf( "WARNING: Unknown q3map_tcMod method: %s\n", token );
1813 /* q3map_fogDir (direction a fog shader fades from transparent to opaque) */
1814 else if( !Q_stricmp( token, "q3map_fogDir" ) )
1816 Parse1DMatrixAppend( shaderText, 3, si->fogDir );
1817 VectorNormalize( si->fogDir, si->fogDir );
1820 /* q3map_globaltexture */
1821 else if( !Q_stricmp( token, "q3map_globaltexture" ) )
1822 si->globalTexture = qtrue;
1824 /* ydnar: gs mods: q3map_nonplanar (make it a nonplanar merge candidate for meta surfaces) */
1825 else if( !Q_stricmp( token, "q3map_nonplanar" ) )
1826 si->nonplanar = qtrue;
1828 /* ydnar: gs mods: q3map_noclip (preserve original face winding, don't clip by bsp tree) */
1829 else if( !Q_stricmp( token, "q3map_noclip" ) )
1833 else if( !Q_stricmp( token, "q3map_notjunc" ) )
1834 si->notjunc = qtrue;
1837 else if( !Q_stricmp( token, "q3map_nofog" ) )
1840 /* ydnar: gs mods: q3map_indexed (for explicit terrain-style indexed mapping) */
1841 else if( !Q_stricmp( token, "q3map_indexed" ) )
1842 si->indexed = qtrue;
1844 /* ydnar: q3map_invert (inverts a drawsurface's facing) */
1845 else if( !Q_stricmp( token, "q3map_invert" ) )
1848 /* ydnar: gs mods: q3map_lightmapMergable (ok to merge non-planar */
1849 else if( !Q_stricmp( token, "q3map_lightmapMergable" ) )
1850 si->lmMergable = qtrue;
1852 /* ydnar: q3map_nofast */
1853 else if( !Q_stricmp( token, "q3map_noFast" ) )
1856 /* q3map_patchshadows */
1857 else if( !Q_stricmp( token, "q3map_patchShadows" ) )
1858 si->patchShadows = qtrue;
1860 /* q3map_vertexshadows */
1861 else if( !Q_stricmp( token, "q3map_vertexShadows" ) )
1862 si->vertexShadows = qtrue; /* ydnar */
1864 /* q3map_novertexshadows */
1865 else if( !Q_stricmp( token, "q3map_noVertexShadows" ) )
1866 si->vertexShadows = qfalse; /* ydnar */
1868 /* q3map_splotchfix (filter dark lightmap luxels on lightmapped models) */
1869 else if( !Q_stricmp( token, "q3map_splotchfix" ) )
1870 si->splotchFix = qtrue; /* ydnar */
1872 /* q3map_forcesunlight */
1873 else if( !Q_stricmp( token, "q3map_forceSunlight" ) )
1874 si->forceSunlight = qtrue;
1876 /* q3map_onlyvertexlighting (sof2) */
1877 else if( !Q_stricmp( token, "q3map_onlyVertexLighting" ) )
1878 ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1880 /* q3map_material (sof2) */
1881 else if( !Q_stricmp( token, "q3map_material" ) )
1883 GetTokenAppend( shaderText, qfalse );
1884 sprintf( temp, "*mat_%s", token );
1885 if( ApplySurfaceParm( temp, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1886 Sys_Printf( "WARNING: Unknown material \"%s\"\n", token );
1889 /* ydnar: q3map_clipmodel (autogenerate clip brushes for model triangles using this shader) */
1890 else if( !Q_stricmp( token, "q3map_clipmodel" ) )
1891 si->clipModel = qtrue;
1893 /* ydnar: q3map_styleMarker[2] */
1894 else if( !Q_stricmp( token, "q3map_styleMarker" ) )
1895 si->styleMarker = 1;
1896 else if( !Q_stricmp( token, "q3map_styleMarker2" ) ) /* uses depthFunc equal */
1897 si->styleMarker = 2;
1899 /* ydnar: default to searching for q3map_<surfaceparm> */
1902 //% Sys_FPrintf( SYS_VRB, "Attempting to match %s with a known surfaceparm\n", token );
1903 if( ApplySurfaceParm( &token[ 6 ], &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1904 ;//% Sys_Printf( "WARNING: Unknown q3map_* directive \"%s\"\n", token );
1909 /* -----------------------------------------------------------------
1911 ----------------------------------------------------------------- */
1913 /* ignore all other tokens on the line */
1914 while( TokenAvailable() && GetTokenAppend( shaderText, qfalse ) );
1922 ParseCustomInfoParms() - rr2do2
1923 loads custom info parms file for mods
1926 static void ParseCustomInfoParms( void )
1928 qboolean parsedContent, parsedSurface;
1932 if( vfsGetFileCount( "scripts/custinfoparms.txt" ) == 0 )
1936 LoadScriptFile( "scripts/custinfoparms.txt", 0 );
1938 /* clear the array */
1939 memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );
1940 numCustSurfaceParms = 0;
1941 parsedContent = parsedSurface = qfalse;
1943 /* parse custom contentflags */
1947 if ( !GetToken( qtrue ) )
1950 if ( !strcmp( token, "}" ) ) {
1951 parsedContent = qtrue;
1955 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
1956 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
1958 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].contentFlags );
1959 numCustSurfaceParms++;
1963 if( !parsedContent )
1965 Sys_Printf( "WARNING: Couldn't find valid custom contentsflag section\n" );
1969 /* parse custom surfaceflags */
1973 if( !GetToken( qtrue ) )
1976 if( !strcmp( token, "}" ) )
1978 parsedSurface = qtrue;
1982 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
1983 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
1985 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );
1986 numCustSurfaceParms++;
1990 if( !parsedContent )
1991 Sys_Printf( "WARNING: Couldn't find valid custom surfaceflag section\n" );
1998 the shaders are parsed out of shaderlist.txt from a main directory
1999 that is, if using -fs_game we ignore the shader scripts that might be in baseq3/
2000 on linux there's an additional twist, we actually merge the stuff from ~/.q3a/ and from the base dir
2003 #define MAX_SHADER_FILES 1024
2005 void LoadShaderInfo( void )
2007 int i, j, numShaderFiles, count;
2008 char filename[ 1024 ];
2009 char *shaderFiles[ MAX_SHADER_FILES ];
2012 /* rr2do2: parse custom infoparms first */
2013 if( useCustomInfoParms )
2014 ParseCustomInfoParms();
2016 /* start with zero */
2019 /* we can pile up several shader files, the one in baseq3 and ones in the mod dir or other spots */
2020 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2021 count = vfsGetFileCount( filename );
2024 for( i = 0; i < count; i++ )
2026 /* load shader list */
2027 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2028 LoadScriptFile( filename, i );
2031 while( GetToken( qtrue ) )
2033 /* check for duplicate entries */
2034 for( j = 0; j < numShaderFiles; j++ )
2035 if( !strcmp( shaderFiles[ j ], token ) )
2039 if( j >= MAX_SHADER_FILES )
2040 Error( "MAX_SHADER_FILES (%d) reached, trim your shaderlist.txt!", (int) MAX_SHADER_FILES );
2042 /* new shader file */
2043 if( j == numShaderFiles )
2045 shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );
2046 strcpy( shaderFiles[ numShaderFiles ], token );
2052 /* parse the shader files */
2053 for( i = 0; i < numShaderFiles; i++ )
2055 sprintf( filename, "%s/%s.shader", game->shaderPath, shaderFiles[ i ] );
2056 ParseShaderFile( filename );
2057 free( shaderFiles[ i ] );
2060 /* emit some statistics */
2061 Sys_FPrintf( SYS_VRB, "%9d shaderInfo\n", numShaderInfo );