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_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 );
107 case CM_ALPHA_DOT_PRODUCT:
108 mult[ 3 ] = DotProduct( dv->normal, cm2->data );
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]);
117 case CM_COLOR_DOT_PRODUCT_2:
118 c = DotProduct( dv->normal, cm2->data );
120 VectorSet( mult, c, c, c );
123 case CM_COLOR_DOT_PRODUCT_2_SCALE:
124 c = DotProduct( dv->normal, cm2->data );
126 c = (c - cm2->data[3]) / (cm2->data[4] - cm2->data[3]);
127 VectorSet( mult, c, c, c );
130 case CM_ALPHA_DOT_PRODUCT_2:
131 mult[ 3 ] = DotProduct( dv->normal, cm2->data );
132 mult[ 3 ] *= mult[ 3 ];
135 case CM_ALPHA_DOT_PRODUCT_2_SCALE:
136 c = DotProduct( dv->normal, cm2->data );
138 c = (c - cm2->data[3]) / (cm2->data[4] - cm2->data[3]);
147 for( j = 0; j < MAX_LIGHTMAPS; j++ )
149 for( k = 0; k < 4; k++ )
151 c = (mult[ k ] * dv->color[ j ][ k ]) + add[ k ];
156 dv->color[ j ][ k ] = c;
167 routines for dealing with a 3x3 texture mod matrix
170 void TCMod( tcMod_t mod, float st[ 2 ] )
177 st[ 0 ] = (mod[ 0 ][ 0 ] * old[ 0 ]) + (mod[ 0 ][ 1 ] * old[ 1 ]) + mod[ 0 ][ 2 ];
178 st[ 1 ] = (mod[ 1 ][ 0 ] * old[ 0 ]) + (mod[ 1 ][ 1 ] * old[ 1 ]) + mod[ 1 ][ 2 ];
182 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 */
190 void TCModMultiply( tcMod_t a, tcMod_t b, tcMod_t out )
195 for( i = 0; i < 3; i++ )
197 out[ i ][ 0 ] = (a[ i ][ 0 ] * b[ 0 ][ 0 ]) + (a[ i ][ 1 ] * b[ 1 ][ 0 ]) + (a[ i ][ 2 ] * b[ 2 ][ 0 ]);
198 out[ i ][ 1 ] = (a[ i ][ 0 ] * b[ 0 ][ 1 ]) + (a[ i ][ 1 ] * b[ 1 ][ 1 ]) + (a[ i ][ 2 ] * b[ 2 ][ 1 ]);
199 out[ i ][ 2 ] = (a[ i ][ 0 ] * b[ 0 ][ 2 ]) + (a[ i ][ 1 ] * b[ 1 ][ 2 ]) + (a[ i ][ 2 ] * b[ 2 ][ 2 ]);
204 void TCModTranslate( tcMod_t mod, float s, float t )
211 void TCModScale( tcMod_t mod, float s, float t )
218 void TCModRotate( tcMod_t mod, float euler )
221 float radians, sinv, cosv;
224 memcpy( old, mod, sizeof( tcMod_t ) );
225 TCModIdentity( temp );
227 radians = euler / 180 * Q_PI;
228 sinv = sin( radians );
229 cosv = cos( radians );
231 temp[ 0 ][ 0 ] = cosv; temp[ 0 ][ 1 ] = -sinv;
232 temp[ 1 ][ 0 ] = sinv; temp[ 1 ][ 1 ] = cosv;
234 TCModMultiply( old, temp, mod );
240 ApplySurfaceParm() - ydnar
241 applies a named surfaceparm to the supplied flags
244 qboolean ApplySurfaceParm( char *name, int *contentFlags, int *surfaceFlags, int *compileFlags )
253 if( contentFlags == NULL )
254 contentFlags = &fake;
255 if( surfaceFlags == NULL )
256 surfaceFlags = &fake;
257 if( compileFlags == NULL )
258 compileFlags = &fake;
260 /* walk the current game's surfaceparms */
261 sp = game->surfaceParms;
262 while( sp->name != NULL )
265 if( !Q_stricmp( name, sp->name ) )
267 /* clear and set flags */
268 *contentFlags &= ~(sp->contentFlagsClear);
269 *contentFlags |= sp->contentFlags;
270 *surfaceFlags &= ~(sp->surfaceFlagsClear);
271 *surfaceFlags |= sp->surfaceFlags;
272 *compileFlags &= ~(sp->compileFlagsClear);
273 *compileFlags |= sp->compileFlags;
283 /* check custom info parms */
284 for( i = 0; i < numCustSurfaceParms; i++ )
286 /* get surfaceparm */
287 sp = &custSurfaceParms[ i ];
290 if( !Q_stricmp( name, sp->name ) )
292 /* clear and set flags */
293 *contentFlags &= ~(sp->contentFlagsClear);
294 *contentFlags |= sp->contentFlags;
295 *surfaceFlags &= ~(sp->surfaceFlagsClear);
296 *surfaceFlags |= sp->surfaceFlags;
297 *compileFlags &= ~(sp->compileFlagsClear);
298 *compileFlags |= sp->compileFlags;
305 /* no matching surfaceparm found */
312 BeginMapShaderFile() - ydnar
313 erases and starts a new map shader script
316 void BeginMapShaderFile( const char *mapFile )
324 mapShaderFile[ 0 ] = '\0';
325 if( mapFile == NULL || mapFile[ 0 ] == '\0' )
329 strcpy( base, mapFile );
330 StripExtension( base );
332 /* extract map name */
333 len = strlen( base ) - 1;
334 while( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )
336 strcpy( mapName, &base[ len + 1 ] );
341 /* append ../scripts/q3map2_<mapname>.shader */
342 sprintf( mapShaderFile, "%s/../%s/q3map2_%s.shader", base, game->shaderPath, mapName );
343 Sys_FPrintf( SYS_VRB, "Map has shader script %s\n", mapShaderFile );
346 remove( mapShaderFile );
348 /* stop making warnings about missing images */
355 WriteMapShaderFile() - ydnar
356 writes a shader to the map shader script
359 void WriteMapShaderFile( void )
367 if( mapShaderFile[ 0 ] == '\0' )
370 /* are there any custom shaders? */
371 for( i = 0, num = 0; i < numShaderInfo; i++ )
373 if( shaderInfo[ i ].custom )
376 if( i == numShaderInfo )
380 Sys_FPrintf( SYS_VRB, "--- WriteMapShaderFile ---\n");
381 Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile );
383 /* open shader file */
384 file = fopen( mapShaderFile, "w" );
387 Sys_Printf( "WARNING: Unable to open map shader file %s for writing\n", mapShaderFile );
393 "// Custom shader file for %s.bsp\n"
394 "// Generated by Q3Map2 (ydnar)\n"
395 "// Do not edit! This file is overwritten on recompiles.\n\n",
398 /* walk the shader list */
399 for( i = 0, num = 0; i < numShaderInfo; i++ )
401 /* get the shader and print it */
402 si = &shaderInfo[ i ];
403 if( si->custom == qfalse || si->shaderText == NULL || si->shaderText[ 0 ] == '\0' )
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 */
411 Sys_FPrintf( SYS_VRB, "." );
414 /* close the shader */
418 Sys_FPrintf( SYS_VRB, "\n" );
420 /* print some stats */
421 Sys_Printf( "%9d custom shaders emitted\n", num );
427 CustomShader() - ydnar
428 sets up a custom map shader
431 shaderInfo_t *CustomShader( shaderInfo_t *si, char *find, char *replace )
434 char shader[ MAX_QPATH ];
438 char *srcShaderText, temp[ 8192 ], shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */
443 return ShaderInfoForShader( "default" );
445 /* default shader text source */
446 srcShaderText = si->shaderText;
448 /* et: implicitMap */
449 if( si->implicitMap == IM_OPAQUE )
451 srcShaderText = temp;
453 "{ // Q3Map2 defaulted (implicitMap)\n"
455 "\t\tmap $lightmap\n"
456 "\t\trgbGen identity\n"
458 "\tq3map_styleMarker\n"
461 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
462 "\t\trgbGen identity\n"
465 si->implicitImagePath );
468 /* et: implicitMask */
469 else if( si->implicitMap == IM_MASKED )
471 srcShaderText = temp;
473 "{ // Q3Map2 defaulted (implicitMask)\n"
477 "\t\talphaFunc GE128\n"
481 "\t\tmap $lightmap\n"
482 "\t\trgbGen identity\n"
483 "\t\tdepthFunc equal\n"
485 "\tq3map_styleMarker\n"
488 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
489 "\t\tdepthFunc equal\n"
490 "\t\trgbGen identity\n"
493 si->implicitImagePath,
494 si->implicitImagePath );
497 /* et: implicitBlend */
498 else if( si->implicitMap == IM_BLEND )
500 srcShaderText = temp;
502 "{ // Q3Map2 defaulted (implicitBlend)\n"
506 "\t\tblendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA\n"
509 "\t\tmap $lightmap\n"
510 "\t\trgbGen identity\n"
511 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
513 "\tq3map_styleMarker\n"
515 si->implicitImagePath );
518 /* default shader text */
519 else if( srcShaderText == NULL )
521 srcShaderText = temp;
523 "{ // Q3Map2 defaulted\n"
525 "\t\tmap $lightmap\n"
526 "\t\trgbGen identity\n"
528 "\tq3map_styleMarker\n"
531 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
532 "\t\trgbGen identity\n"
539 if( (strlen( mapName ) + 1 + 32) > MAX_QPATH )
540 Error( "Custom shader name length (%d) exceeded. Shorten your map name.\n", MAX_QPATH );
542 /* do some bad find-replace */
543 s = strstr( srcShaderText, find );
545 //% strcpy( shaderText, srcShaderText );
546 return si; /* testing just using the existing shader if this fails */
549 /* substitute 'find' with 'replace' */
550 loc = s - srcShaderText;
551 strcpy( shaderText, srcShaderText );
552 shaderText[ loc ] = '\0';
553 strcat( shaderText, replace );
554 strcat( shaderText, &srcShaderText[ loc + strlen( find ) ] );
557 /* make md4 hash of the shader text */
558 Com_BlockFullChecksum(shaderText, strlen(shaderText), digest);
560 /* mangle hash into a shader name */
561 sprintf( shader, "%s/%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", mapName,
562 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ],
563 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ], digest[ 15 ] );
566 csi = ShaderInfoForShader( shader );
568 /* might be a preexisting shader */
572 /* clone the existing shader and rename */
573 memcpy( csi, si, sizeof( shaderInfo_t ) );
574 strcpy( csi->shader, shader );
577 /* store new shader text */
578 csi->shaderText = safe_malloc( strlen( shaderText ) + 1 );
579 strcpy( csi->shaderText, shaderText ); /* LEAK! */
588 EmitVertexRemapShader()
589 adds a vertexremapshader key/value pair to worldspawn
592 void EmitVertexRemapShader( char *from, char *to )
595 char key[ 64 ], value[ 256 ];
599 if( from == NULL || from[ 0 ] == '\0' ||
600 to == NULL || to[ 0 ] == '\0' )
604 sprintf( value, "%s;%s", from, to );
607 Com_BlockFullChecksum(value, strlen(value), digest);
609 /* make key (this is annoying, as vertexremapshader is precisely 17 characters,
610 which is one too long, so we leave off the last byte of the md5 digest) */
611 sprintf( key, "vertexremapshader%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
612 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ],
613 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ] ); /* no: digest[ 15 ] */
615 /* add key/value pair to worldspawn */
616 SetKeyValue( &entities[ 0 ], key, value );
623 allocates and initializes a new shader
626 static shaderInfo_t *AllocShaderInfo( void )
632 if( shaderInfo == NULL )
634 shaderInfo = safe_malloc( sizeof( shaderInfo_t ) * MAX_SHADER_INFO );
639 if( numShaderInfo == MAX_SHADER_INFO )
640 Error( "MAX_SHADER_INFO exceeded. Remove some PK3 files or shader scripts from shaderlist.txt and try again." );
641 si = &shaderInfo[ numShaderInfo ];
644 /* ydnar: clear to 0 first */
645 memset( si, 0, sizeof( shaderInfo_t ) );
648 ApplySurfaceParm( "default", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
650 si->backsplashFraction = DEF_BACKSPLASH_FRACTION;
651 si->backsplashDistance = DEF_BACKSPLASH_DISTANCE;
653 si->bounceScale = DEF_RADIOSITY_BOUNCE;
655 si->lightStyle = LS_NORMAL;
657 si->polygonOffset = qfalse;
659 si->shadeAngleDegrees = 0.0f;
660 si->lightmapSampleSize = 0;
661 si->lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;
662 si->patchShadows = qfalse;
663 si->vertexShadows = qtrue; /* ydnar: changed default behavior */
664 si->forceSunlight = qfalse;
665 si->vertexScale = 1.0;
666 si->notjunc = qfalse;
668 /* ydnar: set texture coordinate transform matrix to identity */
669 TCModIdentity( si->mod );
671 /* ydnar: lightmaps can now be > 128x128 in certain games or an externally generated tga */
672 si->lmCustomWidth = lmCustomSize;
673 si->lmCustomHeight = lmCustomSize;
675 /* return to sender */
682 FinishShader() - ydnar
683 sets a shader's width and height among other things
686 void FinishShader( shaderInfo_t *si )
689 float st[ 2 ], o[ 2 ], dist, bestDist;
693 /* don't double-dip */
697 /* if they're explicitly set, copy from image size */
698 if( si->shaderWidth == 0 && si->shaderHeight == 0 )
700 si->shaderWidth = si->shaderImage->width;
701 si->shaderHeight = si->shaderImage->height;
704 /* legacy terrain has explicit image-sized texture projection */
705 if( si->legacyTerrain && si->tcGen == qfalse )
707 /* set xy texture projection */
709 VectorSet( si->vecs[ 0 ], (1.0f / (si->shaderWidth * 0.5f)), 0, 0 );
710 VectorSet( si->vecs[ 1 ], 0, (1.0f / (si->shaderHeight * 0.5f)), 0 );
713 /* find pixel coordinates best matching the average color of the image */
715 o[ 0 ] = 1.0f / si->shaderImage->width;
716 o[ 1 ] = 1.0f / si->shaderImage->height;
717 for( y = 0, st[ 1 ] = 0.0f; y < si->shaderImage->height; y++, st[ 1 ] += o[ 1 ] )
719 for( x = 0, st[ 0 ] = 0.0f; x < si->shaderImage->width; x++, st[ 0 ] += o[ 0 ] )
721 /* sample the shader image */
722 RadSampleImage( si->shaderImage->pixels, si->shaderImage->width, si->shaderImage->height, st, color );
724 /* determine error squared */
725 VectorSubtract( color, si->averageColor, delta );
726 delta[ 3 ] = color[ 3 ] - si->averageColor[ 3 ];
727 dist = delta[ 0 ] * delta[ 0 ] + delta[ 1 ] * delta[ 1 ] + delta[ 2 ] * delta[ 2 ] + delta[ 3 ] * delta[ 3 ];
728 if( dist < bestDist )
730 si->stFlat[ 0 ] = st[ 0 ];
731 si->stFlat[ 1 ] = st[ 1 ];
736 /* set to finished */
737 si->finished = qtrue;
744 loads a shader's images
745 ydnar: image.c made this a bit simpler
748 static void LoadShaderImages( shaderInfo_t *si )
754 /* nodraw shaders don't need images */
755 if( si->compileFlags & C_NODRAW )
756 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
759 /* try to load editor image first */
760 si->shaderImage = ImageLoad( si->editorImagePath );
762 /* then try shadername */
763 if( si->shaderImage == NULL )
764 si->shaderImage = ImageLoad( si->shader );
766 /* then try implicit image path (note: new behavior!) */
767 if( si->shaderImage == NULL )
768 si->shaderImage = ImageLoad( si->implicitImagePath );
770 /* then try lightimage (note: new behavior!) */
771 if( si->shaderImage == NULL )
772 si->shaderImage = ImageLoad( si->lightImagePath );
774 /* otherwise, use default image */
775 if( si->shaderImage == NULL )
777 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
778 if( warnImage && strcmp( si->shader, "noshader" ) )
779 Sys_Printf( "WARNING: Couldn't find image for shader %s\n", si->shader );
782 /* load light image */
783 si->lightImage = ImageLoad( si->lightImagePath );
785 /* load normalmap image (ok if this is NULL) */
786 si->normalImage = ImageLoad( si->normalImagePath );
787 if( si->normalImage != NULL )
789 Sys_FPrintf( SYS_VRB, "Shader %s has\n"
790 " NM %s\n", si->shader, si->normalImagePath );
794 /* if no light image, use shader image */
795 if( si->lightImage == NULL )
796 si->lightImage = ImageLoad( si->shaderImage->name );
798 /* create default and average colors */
799 count = si->lightImage->width * si->lightImage->height;
800 VectorClear( color );
802 for( i = 0; i < count; i++ )
804 color[ 0 ] += si->lightImage->pixels[ i * 4 + 0 ];
805 color[ 1 ] += si->lightImage->pixels[ i * 4 + 1 ];
806 color[ 2 ] += si->lightImage->pixels[ i * 4 + 2 ];
807 color[ 3 ] += si->lightImage->pixels[ i * 4 + 3 ];
810 if( VectorLength( si->color ) <= 0.0f )
812 ColorNormalize( color, si->color );
813 VectorScale( color, (1.0f / count), si->averageColor );
817 VectorCopy( si->color, si->averageColor );
824 ShaderInfoForShader()
825 finds a shaderinfo for a named shader
828 #define MAX_SHADER_DEPRECATION_DEPTH 16
830 shaderInfo_t *ShaderInfoForShader( const char *shaderName )
833 int deprecationDepth;
835 char shader[ MAX_QPATH ];
838 if( shaderName == NULL || shaderName[ 0 ] == '\0' )
840 Sys_Printf( "WARNING: Null or empty shader name\n" );
841 shaderName = "missing";
844 /* strip off extension */
845 strcpy( shader, shaderName );
846 StripExtension( shader );
849 deprecationDepth = 0;
850 for( i = 0; i < numShaderInfo; i++ )
852 si = &shaderInfo[ i ];
853 if( !Q_stricmp( shader, si->shader ) )
855 /* check if shader is deprecated */
856 if (deprecationDepth < MAX_SHADER_DEPRECATION_DEPTH && si->deprecateShader && si->deprecateShader[ 0 ] )
859 strcpy( shader, si->deprecateShader );
860 StripExtension( shader );
861 /* increase deprecation depth */
863 if (deprecationDepth == MAX_SHADER_DEPRECATION_DEPTH)
864 Sys_Printf("WARNING: Max deprecation depth of %i is reached on shader '%s'\n", MAX_SHADER_DEPRECATION_DEPTH, shader);
865 /* search again from beginning */
870 /* load image if necessary */
871 if( si->finished == qfalse )
873 LoadShaderImages( si );
882 /* allocate a default shader */
883 si = AllocShaderInfo();
884 strcpy( si->shader, shader );
885 LoadShaderImages( si );
895 GetTokenAppend() - ydnar
896 gets a token and appends its text to the specified buffer
899 static int oldScriptLine = 0;
900 static int tabDepth = 0;
902 qboolean GetTokenAppend( char *buffer, qboolean crossline )
909 r = GetToken( crossline );
910 if( r == qfalse || buffer == NULL || token[ 0 ] == '\0' )
914 if( token[ 0 ] == '}' )
918 if( oldScriptLine != scriptline )
920 strcat( buffer, "\n" );
921 for( i = 0; i < tabDepth; i++ )
922 strcat( buffer, "\t" );
925 strcat( buffer, " " );
926 oldScriptLine = scriptline;
927 strcat( buffer, token );
930 if( token[ 0 ] == '{' )
938 void Parse1DMatrixAppend( char *buffer, int x, vec_t *m )
943 if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, "(" ) )
944 Error( "Parse1DMatrixAppend(): line %d: ( not found!", scriptline );
945 for( i = 0; i < x; i++ )
947 if( !GetTokenAppend( buffer, qfalse ) )
948 Error( "Parse1DMatrixAppend(): line %d: Number not found!", scriptline );
949 m[ i ] = atof( token );
951 if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, ")" ) )
952 Error( "Parse1DMatrixAppend(): line %d: ) not found!", scriptline );
960 parses a shader file into discrete shaderInfo_t
963 static void ParseShaderFile( const char *filename )
967 char *suffix, temp[ 1024 ];
968 char shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */
973 shaderText[ 0 ] = '\0';
975 /* load the shader */
976 LoadScriptFile( filename, 0 );
981 /* copy shader text to the shaderinfo */
982 if( si != NULL && shaderText[ 0 ] != '\0' )
984 strcat( shaderText, "\n" );
985 si->shaderText = safe_malloc( strlen( shaderText ) + 1 );
986 strcpy( si->shaderText, shaderText );
987 //% if( VectorLength( si->vecs[ 0 ] ) )
988 //% Sys_Printf( "%s\n", shaderText );
991 /* ydnar: clear shader text buffer */
992 shaderText[ 0 ] = '\0';
994 /* test for end of file */
995 if( !GetToken( qtrue ) )
998 /* shader name is initial token */
999 si = AllocShaderInfo();
1000 strcpy( si->shader, token );
1002 /* ignore ":q3map" suffix */
1003 suffix = strstr( si->shader, ":q3map" );
1004 if( suffix != NULL )
1007 /* handle { } section */
1008 if( !GetTokenAppend( shaderText, qtrue ) )
1010 if( strcmp( token, "{" ) )
1013 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s\nLast known shader: %s",
1014 filename, scriptline, token, si->shader );
1016 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s",
1017 filename, scriptline, token );
1022 /* get the next token */
1023 if( !GetTokenAppend( shaderText, qtrue ) )
1025 if( !strcmp( token, "}" ) )
1029 /* -----------------------------------------------------------------
1030 shader stages (passes)
1031 ----------------------------------------------------------------- */
1033 /* parse stage directives */
1034 if( !strcmp( token, "{" ) )
1036 si->hasPasses = qtrue;
1039 if( !GetTokenAppend( shaderText, qtrue ) )
1041 if( !strcmp( token, "}" ) )
1044 /* only care about images if we don't have a editor/light image */
1045 if( si->editorImagePath[ 0 ] == '\0' && si->lightImagePath[ 0 ] == '\0' && si->implicitImagePath[ 0 ] == '\0' )
1047 /* digest any images */
1048 if( !Q_stricmp( token, "map" ) ||
1049 !Q_stricmp( token, "clampMap" ) ||
1050 !Q_stricmp( token, "animMap" ) ||
1051 !Q_stricmp( token, "clampAnimMap" ) ||
1052 !Q_stricmp( token, "clampMap" ) ||
1053 !Q_stricmp( token, "mapComp" ) ||
1054 !Q_stricmp( token, "mapNoComp" ) )
1056 /* skip one token for animated stages */
1057 if( !Q_stricmp( token, "animMap" ) || !Q_stricmp( token, "clampAnimMap" ) )
1058 GetTokenAppend( shaderText, qfalse );
1061 GetTokenAppend( shaderText, qfalse );
1062 if( token[ 0 ] != '*' && token[ 0 ] != '$' )
1064 strcpy( si->lightImagePath, token );
1065 DefaultExtension( si->lightImagePath, ".tga" );
1068 //% Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath );
1076 /* -----------------------------------------------------------------
1077 surfaceparm * directives
1078 ----------------------------------------------------------------- */
1080 /* match surfaceparm */
1081 else if( !Q_stricmp( token, "surfaceparm" ) )
1083 GetTokenAppend( shaderText, qfalse );
1084 if( ApplySurfaceParm( token, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1085 Sys_Printf( "WARNING: Unknown surfaceparm: \"%s\"\n", token );
1089 /* -----------------------------------------------------------------
1090 game-related shader directives
1091 ----------------------------------------------------------------- */
1093 /* ydnar: fogparms (for determining fog volumes) */
1094 else if( !Q_stricmp( token, "fogparms" ) )
1095 si->fogParms = qtrue;
1097 /* ydnar: polygonoffset (for no culling) */
1098 else if( !Q_stricmp( token, "polygonoffset" ) )
1099 si->polygonOffset = qtrue;
1101 /* tesssize is used to force liquid surfaces to subdivide */
1102 else if( !Q_stricmp( token, "tessSize" ) || !Q_stricmp( token, "q3map_tessSize" ) /* sof2 */ )
1104 GetTokenAppend( shaderText, qfalse );
1105 si->subdivisions = atof( token );
1108 /* cull none will set twoSided (ydnar: added disable too) */
1109 else if ( !Q_stricmp( token, "cull" ) )
1111 GetTokenAppend( shaderText, qfalse );
1112 if( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "disable" ) || !Q_stricmp( token, "twosided" ) )
1113 si->twoSided = qtrue;
1116 /* deformVertexes autosprite[ 2 ]
1117 we catch this so autosprited surfaces become point
1118 lights instead of area lights */
1119 else if( !Q_stricmp( token, "deformVertexes" ) )
1121 GetTokenAppend( shaderText, qfalse );
1123 /* deformVertexes autosprite(2) */
1124 if( !Q_strncasecmp( token, "autosprite", 10 ) )
1126 /* set it as autosprite and detail */
1127 si->autosprite = qtrue;
1128 ApplySurfaceParm( "detail", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1130 /* ydnar: gs mods: added these useful things */
1132 si->notjunc = qtrue;
1135 /* deformVertexes move <x> <y> <z> <func> <base> <amplitude> <phase> <freq> (ydnar: for particle studio support) */
1136 if( !Q_stricmp( token, "move") )
1138 vec3_t amt, mins, maxs;
1142 /* get move amount */
1143 GetTokenAppend( shaderText, qfalse ); amt[ 0 ] = atof( token );
1144 GetTokenAppend( shaderText, qfalse ); amt[ 1 ] = atof( token );
1145 GetTokenAppend( shaderText, qfalse ); amt[ 2 ] = atof( token );
1148 GetTokenAppend( shaderText, qfalse );
1150 /* get base and amplitude */
1151 GetTokenAppend( shaderText, qfalse ); base = atof( token );
1152 GetTokenAppend( shaderText, qfalse ); amp = atof( token );
1155 VectorScale( amt, base, mins );
1156 VectorMA( mins, amp, amt, maxs );
1157 VectorAdd( si->mins, mins, si->mins );
1158 VectorAdd( si->maxs, maxs, si->maxs );
1162 /* light <value> (old-style flare specification) */
1163 else if( !Q_stricmp( token, "light" ) )
1165 GetTokenAppend( shaderText, qfalse );
1166 si->flareShader = game->flareShader;
1169 /* ydnar: damageShader <shader> <health> (sof2 mods) */
1170 else if( !Q_stricmp( token, "damageShader" ) )
1172 GetTokenAppend( shaderText, qfalse );
1173 if( token[ 0 ] != '\0' )
1175 si->damageShader = safe_malloc( strlen( token ) + 1 );
1176 strcpy( si->damageShader, token );
1178 GetTokenAppend( shaderText, qfalse ); /* don't do anything with health */
1181 /* ydnar: enemy territory implicit shaders */
1182 else if( !Q_stricmp( token, "implicitMap" ) )
1184 si->implicitMap = IM_OPAQUE;
1185 GetTokenAppend( shaderText, qfalse );
1186 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1187 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1189 strcpy( si->implicitImagePath, token );
1192 else if( !Q_stricmp( token, "implicitMask" ) )
1194 si->implicitMap = IM_MASKED;
1195 GetTokenAppend( shaderText, qfalse );
1196 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1197 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1199 strcpy( si->implicitImagePath, token );
1202 else if( !Q_stricmp( token, "implicitBlend" ) )
1204 si->implicitMap = IM_MASKED;
1205 GetTokenAppend( shaderText, qfalse );
1206 if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
1207 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1209 strcpy( si->implicitImagePath, token );
1213 /* -----------------------------------------------------------------
1215 ----------------------------------------------------------------- */
1217 /* qer_editorimage <image> */
1218 else if( !Q_stricmp( token, "qer_editorImage" ) )
1220 GetTokenAppend( shaderText, qfalse );
1221 strcpy( si->editorImagePath, token );
1222 DefaultExtension( si->editorImagePath, ".tga" );
1225 /* ydnar: q3map_normalimage <image> (bumpmapping normal map) */
1226 else if( !Q_stricmp( token, "q3map_normalImage" ) )
1228 GetTokenAppend( shaderText, qfalse );
1229 strcpy( si->normalImagePath, token );
1230 DefaultExtension( si->normalImagePath, ".tga" );
1233 /* q3map_lightimage <image> */
1234 else if( !Q_stricmp( token, "q3map_lightImage" ) )
1236 GetTokenAppend( shaderText, qfalse );
1237 strcpy( si->lightImagePath, token );
1238 DefaultExtension( si->lightImagePath, ".tga" );
1241 /* ydnar: skyparms <outer image> <cloud height> <inner image> */
1242 else if( !Q_stricmp( token, "skyParms" ) )
1244 /* get image base */
1245 GetTokenAppend( shaderText, qfalse );
1247 /* ignore bogus paths */
1248 if( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) )
1250 strcpy( si->skyParmsImageBase, token );
1252 /* use top image as sky light image */
1253 if( si->lightImagePath[ 0 ] == '\0' )
1254 sprintf( si->lightImagePath, "%s_up.tga", si->skyParmsImageBase );
1257 /* skip rest of line */
1258 GetTokenAppend( shaderText, qfalse );
1259 GetTokenAppend( shaderText, qfalse );
1262 /* -----------------------------------------------------------------
1264 ----------------------------------------------------------------- */
1266 /* q3map_sun <red> <green> <blue> <intensity> <degrees> <elevation>
1267 color will be normalized, so it doesn't matter what range you use
1268 intensity falls off with angle but not distance 100 is a fairly bright sun
1269 degree of 0 = from the east, 90 = north, etc. altitude of 0 = sunrise/set, 90 = noon
1270 ydnar: sof2map has bareword 'sun' token, so we support that as well */
1271 else if( !Q_stricmp( token, "sun" ) /* sof2 */ || !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) )
1278 /* ydnar: extended sun directive? */
1279 if( !Q_stricmp( token, "q3map_sunext" ) )
1283 sun = safe_malloc( sizeof( *sun ) );
1284 memset( sun, 0, sizeof( *sun ) );
1287 sun->style = si->lightStyle;
1290 GetTokenAppend( shaderText, qfalse );
1291 sun->color[ 0 ] = atof( token );
1292 GetTokenAppend( shaderText, qfalse );
1293 sun->color[ 1 ] = atof( token );
1294 GetTokenAppend( shaderText, qfalse );
1295 sun->color[ 2 ] = atof( token );
1298 VectorNormalize( sun->color, sun->color );
1300 /* scale color by brightness */
1301 GetTokenAppend( shaderText, qfalse );
1302 sun->photons = atof( token );
1304 /* get sun angle/elevation */
1305 GetTokenAppend( shaderText, qfalse );
1307 a = a / 180.0f * Q_PI;
1309 GetTokenAppend( shaderText, qfalse );
1311 b = b / 180.0f * Q_PI;
1313 sun->direction[ 0 ] = cos( a ) * cos( b );
1314 sun->direction[ 1 ] = sin( a ) * cos( b );
1315 sun->direction[ 2 ] = sin( b );
1317 /* get filter radius from shader */
1318 sun->filterRadius = si->lightFilterRadius;
1320 /* ydnar: get sun angular deviance/samples */
1321 if( ext && TokenAvailable() )
1323 GetTokenAppend( shaderText, qfalse );
1324 sun->deviance = atof( token );
1325 sun->deviance = sun->deviance / 180.0f * Q_PI;
1327 GetTokenAppend( shaderText, qfalse );
1328 sun->numSamples = atoi( token );
1332 sun->next = si->sun;
1335 /* apply sky surfaceparm */
1336 ApplySurfaceParm( "sky", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1338 /* don't process any more tokens on this line */
1343 else if( !Q_strncasecmp( token, "q3map_", 6 ) )
1345 /* ydnar: q3map_baseShader <shader> (inherit this shader's parameters) */
1346 if( !Q_stricmp( token, "q3map_baseShader" ) )
1349 qboolean oldWarnImage;
1353 GetTokenAppend( shaderText, qfalse );
1354 //% Sys_FPrintf( SYS_VRB, "Shader %s has base shader %s\n", si->shader, token );
1355 oldWarnImage = warnImage;
1357 si2 = ShaderInfoForShader( token );
1358 warnImage = oldWarnImage;
1364 strcpy( temp, si->shader );
1367 memcpy( si, si2, sizeof( *si ) );
1369 /* restore name and set to unfinished */
1370 strcpy( si->shader, temp );
1371 si->shaderWidth = 0;
1372 si->shaderHeight = 0;
1373 si->finished = qfalse;
1377 /* ydnar: q3map_surfacemodel <path to model> <density> <min scale> <max scale> <min angle> <max angle> <oriented (0 or 1)> */
1378 else if( !Q_stricmp( token, "q3map_surfacemodel" ) )
1380 surfaceModel_t *model;
1382 /* allocate new model and attach it */
1383 model = safe_malloc( sizeof( *model ) );
1384 memset( model, 0, sizeof( *model ) );
1385 model->next = si->surfaceModel;
1386 si->surfaceModel = model;
1388 /* get parameters */
1389 GetTokenAppend( shaderText, qfalse );
1390 strcpy( model->model, token );
1392 GetTokenAppend( shaderText, qfalse );
1393 model->density = atof( token );
1394 GetTokenAppend( shaderText, qfalse );
1395 model->odds = atof( token );
1397 GetTokenAppend( shaderText, qfalse );
1398 model->minScale = atof( token );
1399 GetTokenAppend( shaderText, qfalse );
1400 model->maxScale = atof( token );
1402 GetTokenAppend( shaderText, qfalse );
1403 model->minAngle = atof( token );
1404 GetTokenAppend( shaderText, qfalse );
1405 model->maxAngle = atof( token );
1407 GetTokenAppend( shaderText, qfalse );
1408 model->oriented = (token[ 0 ] == '1' ? qtrue : qfalse);
1411 /* ydnar/sd: q3map_foliage <path to model> <scale> <density> <odds> <invert alpha (1 or 0)> */
1412 else if( !Q_stricmp( token, "q3map_foliage" ) )
1417 /* allocate new foliage struct and attach it */
1418 foliage = safe_malloc( sizeof( *foliage ) );
1419 memset( foliage, 0, sizeof( *foliage ) );
1420 foliage->next = si->foliage;
1421 si->foliage = foliage;
1423 /* get parameters */
1424 GetTokenAppend( shaderText, qfalse );
1425 strcpy( foliage->model, token );
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 );
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" ) )
1440 GetTokenAppend( shaderText, qfalse );
1441 si->bounceScale = atof( token );
1444 /* ydnar/splashdamage: q3map_skyLight <value> <iterations> */
1445 else if( !Q_stricmp( token, "q3map_skyLight" ) )
1447 GetTokenAppend( shaderText, qfalse );
1448 si->skyLightValue = atof( token );
1449 GetTokenAppend( shaderText, qfalse );
1450 si->skyLightIterations = atoi( token );
1453 if( si->skyLightValue < 0.0f )
1454 si->skyLightValue = 0.0f;
1455 if( si->skyLightIterations < 2 )
1456 si->skyLightIterations = 2;
1459 /* q3map_surfacelight <value> */
1460 else if( !Q_stricmp( token, "q3map_surfacelight" ) )
1462 GetTokenAppend( shaderText, qfalse );
1463 si->value = atof( token );
1466 /* q3map_lightStyle (sof2/jk2 lightstyle) */
1467 else if( !Q_stricmp( token, "q3map_lightStyle" ) )
1469 GetTokenAppend( shaderText, qfalse );
1470 val = atoi( token );
1473 else if( val > LS_NONE )
1475 si->lightStyle = val;
1478 /* wolf: q3map_lightRGB <red> <green> <blue> */
1479 else if( !Q_stricmp( token, "q3map_lightRGB" ) )
1481 VectorClear( si->color );
1482 GetTokenAppend( shaderText, qfalse );
1483 si->color[ 0 ] = atof( token );
1484 GetTokenAppend( shaderText, qfalse );
1485 si->color[ 1 ] = atof( token );
1486 GetTokenAppend( shaderText, qfalse );
1487 si->color[ 2 ] = atof( token );
1488 ColorNormalize( si->color, si->color );
1491 /* q3map_lightSubdivide <value> */
1492 else if( !Q_stricmp( token, "q3map_lightSubdivide" ) )
1494 GetTokenAppend( shaderText, qfalse );
1495 si->lightSubdivide = atoi( token );
1498 /* q3map_backsplash <percent> <distance> */
1499 else if( !Q_stricmp( token, "q3map_backsplash" ) )
1501 GetTokenAppend( shaderText, qfalse );
1502 si->backsplashFraction = atof( token ) * 0.01f;
1503 GetTokenAppend( shaderText, qfalse );
1504 si->backsplashDistance = atof( token );
1507 /* q3map_floodLight <r> <g> <b> <diste> <intensity> <light_direction_power> */
1508 else if( !Q_stricmp( token, "q3map_floodLight" ) )
1511 GetTokenAppend( shaderText, qfalse );
1512 si->floodlightRGB[ 0 ] = atof( token );
1513 GetTokenAppend( shaderText, qfalse );
1514 si->floodlightRGB[ 1 ] = atof( token );
1515 GetTokenAppend( shaderText, qfalse );
1516 si->floodlightRGB[ 2 ] = atof( token );
1517 GetTokenAppend( shaderText, qfalse );
1518 si->floodlightDistance = atof( token );
1519 GetTokenAppend( shaderText, qfalse );
1520 si->floodlightIntensity = atof( token );
1521 GetTokenAppend( shaderText, qfalse );
1522 si->floodlightDirectionScale = atof( token );
1525 /* jal: q3map_nodirty : skip dirty */
1526 else if( !Q_stricmp( token, "q3map_nodirty" ) )
1528 si->noDirty = qtrue;
1531 /* q3map_lightmapSampleSize <value> */
1532 else if( !Q_stricmp( token, "q3map_lightmapSampleSize" ) )
1534 GetTokenAppend( shaderText, qfalse );
1535 si->lightmapSampleSize = atoi( token );
1538 /* q3map_lightmapSampleOffset <value> */
1539 else if( !Q_stricmp( token, "q3map_lightmapSampleOffset" ) )
1541 GetTokenAppend( shaderText, qfalse );
1542 si->lightmapSampleOffset = atof( token );
1545 /* ydnar: q3map_lightmapFilterRadius <self> <other> */
1546 else if( !Q_stricmp( token, "q3map_lightmapFilterRadius" ) )
1548 GetTokenAppend( shaderText, qfalse );
1549 si->lmFilterRadius = atof( token );
1550 GetTokenAppend( shaderText, qfalse );
1551 si->lightFilterRadius = atof( token );
1554 /* ydnar: q3map_lightmapAxis [xyz] */
1555 else if( !Q_stricmp( token, "q3map_lightmapAxis" ) )
1557 GetTokenAppend( shaderText, qfalse );
1558 if( !Q_stricmp( token, "x" ) )
1559 VectorSet( si->lightmapAxis, 1, 0, 0 );
1560 else if( !Q_stricmp( token, "y" ) )
1561 VectorSet( si->lightmapAxis, 0, 1, 0 );
1562 else if( !Q_stricmp( token, "z" ) )
1563 VectorSet( si->lightmapAxis, 0, 0, 1 );
1566 Sys_Printf( "WARNING: Unknown value for lightmap axis: %s\n", token );
1567 VectorClear( si->lightmapAxis );
1571 /* ydnar: q3map_lightmapSize <width> <height> (for autogenerated shaders + external tga lightmaps) */
1572 else if( !Q_stricmp( token, "q3map_lightmapSize" ) )
1574 GetTokenAppend( shaderText, qfalse );
1575 si->lmCustomWidth = atoi( token );
1576 GetTokenAppend( shaderText, qfalse );
1577 si->lmCustomHeight = atoi( token );
1579 /* must be a power of 2 */
1580 if( ((si->lmCustomWidth - 1) & si->lmCustomWidth) ||
1581 ((si->lmCustomHeight - 1) & si->lmCustomHeight) )
1583 Sys_Printf( "WARNING: Non power-of-two lightmap size specified (%d, %d)\n",
1584 si->lmCustomWidth, si->lmCustomHeight );
1585 si->lmCustomWidth = lmCustomSize;
1586 si->lmCustomHeight = lmCustomSize;
1590 /* ydnar: q3map_lightmapBrightness N (for autogenerated shaders + external tga lightmaps) */
1591 else if( !Q_stricmp( token, "q3map_lightmapBrightness" ) || !Q_stricmp( token, "q3map_lightmapGamma" ) )
1593 GetTokenAppend( shaderText, qfalse );
1594 si->lmBrightness = atof( token );
1595 if( si->lmBrightness < 0 )
1596 si->lmBrightness = 1.0;
1599 /* q3map_vertexScale (scale vertex lighting by this fraction) */
1600 else if( !Q_stricmp( token, "q3map_vertexScale" ) )
1602 GetTokenAppend( shaderText, qfalse );
1603 si->vertexScale = atof( token );
1606 /* q3map_noVertexLight */
1607 else if( !Q_stricmp( token, "q3map_noVertexLight" ) )
1609 si->noVertexLight = qtrue;
1612 /* q3map_flare[Shader] <shader> */
1613 else if( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) )
1615 GetTokenAppend( shaderText, qfalse );
1616 if( token[ 0 ] != '\0' )
1618 si->flareShader = safe_malloc( strlen( token ) + 1 );
1619 strcpy( si->flareShader, token );
1623 /* q3map_backShader <shader> */
1624 else if( !Q_stricmp( token, "q3map_backShader" ) )
1626 GetTokenAppend( shaderText, qfalse );
1627 if( token[ 0 ] != '\0' )
1629 si->backShader = safe_malloc( strlen( token ) + 1 );
1630 strcpy( si->backShader, token );
1634 /* ydnar: q3map_cloneShader <shader> */
1635 else if ( !Q_stricmp( token, "q3map_cloneShader" ) )
1637 GetTokenAppend( shaderText, qfalse );
1638 if( token[ 0 ] != '\0' )
1640 si->cloneShader = safe_malloc( strlen( token ) + 1 );
1641 strcpy( si->cloneShader, token );
1645 /* q3map_remapShader <shader> */
1646 else if( !Q_stricmp( token, "q3map_remapShader" ) )
1648 GetTokenAppend( shaderText, qfalse );
1649 if( token[ 0 ] != '\0' )
1651 si->remapShader = safe_malloc( strlen( token ) + 1 );
1652 strcpy( si->remapShader, token );
1656 /* q3map_deprecateShader <shader> */
1657 else if( !Q_stricmp( token, "q3map_deprecateShader" ) )
1659 GetTokenAppend( shaderText, qfalse );
1660 if( token[ 0 ] != '\0' )
1663 si->deprecateShader = safe_malloc( strlen( token ) + 1 );
1664 strcpy( si->deprecateShader, token );
1668 /* ydnar: q3map_offset <value> */
1669 else if( !Q_stricmp( token, "q3map_offset" ) )
1671 GetTokenAppend( shaderText, qfalse );
1672 si->offset = atof( token );
1675 /* ydnar: q3map_fur <numlayers> <offset> <fade> */
1676 else if( !Q_stricmp( token, "q3map_fur" ) )
1678 GetTokenAppend( shaderText, qfalse );
1679 si->furNumLayers = atoi( token );
1680 GetTokenAppend( shaderText, qfalse );
1681 si->furOffset = atof( token );
1682 GetTokenAppend( shaderText, qfalse );
1683 si->furFade = atof( token );
1686 /* ydnar: gs mods: legacy support for terrain/terrain2 shaders */
1687 else if( !Q_stricmp( token, "q3map_terrain" ) )
1689 /* team arena terrain is assumed to be nonplanar, with full normal averaging,
1690 passed through the metatriangle surface pipeline, with a lightmap axis on z */
1691 si->legacyTerrain = qtrue;
1693 si->notjunc = qtrue;
1694 si->indexed = qtrue;
1695 si->nonplanar = qtrue;
1696 si->forceMeta = qtrue;
1697 si->shadeAngleDegrees = 179.0f;
1698 //% VectorSet( si->lightmapAxis, 0, 0, 1 ); /* ydnar 2002-09-21: turning this off for better lightmapping of cliff faces */
1701 /* ydnar: picomodel: q3map_forceMeta (forces brush faces and/or triangle models to go through the metasurface pipeline) */
1702 else if( !Q_stricmp( token, "q3map_forceMeta" ) )
1704 si->forceMeta = qtrue;
1707 /* ydnar: gs mods: q3map_shadeAngle <degrees> */
1708 else if( !Q_stricmp( token, "q3map_shadeAngle" ) )
1710 GetTokenAppend( shaderText, qfalse );
1711 si->shadeAngleDegrees = atof( token );
1714 /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
1715 else if( !Q_stricmp( token, "q3map_textureSize" ) )
1717 GetTokenAppend( shaderText, qfalse );
1718 si->shaderWidth = atoi( token );
1719 GetTokenAppend( shaderText, qfalse );
1720 si->shaderHeight = atoi( token );
1723 /* ydnar: gs mods: q3map_tcGen <style> <parameters> */
1724 else if( !Q_stricmp( token, "q3map_tcGen" ) )
1727 GetTokenAppend( shaderText, qfalse );
1729 /* q3map_tcGen vector <s vector> <t vector> */
1730 if( !Q_stricmp( token, "vector" ) )
1732 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1733 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1736 /* q3map_tcGen ivector <1.0/s vector> <1.0/t vector> (inverse vector, easier for mappers to understand) */
1737 else if( !Q_stricmp( token, "ivector" ) )
1739 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1740 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1741 for( i = 0; i < 3; i++ )
1743 si->vecs[ 0 ][ i ] = si->vecs[ 0 ][ i ] ? 1.0 / si->vecs[ 0 ][ i ] : 0;
1744 si->vecs[ 1 ][ i ] = si->vecs[ 1 ][ i ] ? 1.0 / si->vecs[ 1 ][ i ] : 0;
1749 Sys_Printf( "WARNING: Unknown q3map_tcGen method: %s\n", token );
1750 VectorClear( si->vecs[ 0 ] );
1751 VectorClear( si->vecs[ 1 ] );
1755 /* ydnar: gs mods: q3map_[color|rgb|alpha][Gen|Mod] <style> <parameters> */
1756 else if( !Q_stricmp( token, "q3map_colorGen" ) || !Q_stricmp( token, "q3map_colorMod" ) ||
1757 !Q_stricmp( token, "q3map_rgbGen" ) || !Q_stricmp( token, "q3map_rgbMod" ) ||
1758 !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) )
1760 colorMod_t *cm, *cm2;
1764 /* alphamods are colormod + 1 */
1765 alpha = (!Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" )) ? 1 : 0;
1767 /* allocate new colormod */
1768 cm = safe_malloc( sizeof( *cm ) );
1769 memset( cm, 0, sizeof( *cm ) );
1771 /* attach to shader */
1772 if( si->colorMod == NULL )
1776 for( cm2 = si->colorMod; cm2 != NULL; cm2 = cm2->next )
1778 if( cm2->next == NULL )
1787 GetTokenAppend( shaderText, qfalse );
1789 /* alpha set|const A */
1790 if( alpha && (!Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" )) )
1792 cm->type = CM_ALPHA_SET;
1793 GetTokenAppend( shaderText, qfalse );
1794 cm->data[ 0 ] = atof( token );
1797 /* color|rgb set|const ( X Y Z ) */
1798 else if( !Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" ) )
1800 cm->type = CM_COLOR_SET;
1801 Parse1DMatrixAppend( shaderText, 3, cm->data );
1805 else if( alpha && !Q_stricmp( token, "scale" ) )
1807 cm->type = CM_ALPHA_SCALE;
1808 GetTokenAppend( shaderText, qfalse );
1809 cm->data[ 0 ] = atof( token );
1812 /* color|rgb scale ( X Y Z ) */
1813 else if( !Q_stricmp( token, "scale" ) )
1815 cm->type = CM_COLOR_SCALE;
1816 Parse1DMatrixAppend( shaderText, 3, cm->data );
1819 /* dotProduct ( X Y Z ) */
1820 else if( !Q_stricmp( token, "dotProduct" ) )
1822 cm->type = CM_COLOR_DOT_PRODUCT + alpha;
1823 Parse1DMatrixAppend( shaderText, 3, cm->data );
1826 /* dotProductScale ( X Y Z MIN MAX ) */
1827 else if( !Q_stricmp( token, "dotProductScale" ) )
1829 cm->type = CM_COLOR_DOT_PRODUCT_SCALE + alpha;
1830 Parse1DMatrixAppend( shaderText, 5, cm->data );
1833 /* dotProduct2 ( X Y Z ) */
1834 else if( !Q_stricmp( token, "dotProduct2" ) )
1836 cm->type = CM_COLOR_DOT_PRODUCT_2 + alpha;
1837 Parse1DMatrixAppend( shaderText, 3, cm->data );
1840 /* dotProduct2scale ( X Y Z MIN MAX ) */
1841 else if( !Q_stricmp( token, "dotProduct2scale" ) )
1843 cm->type = CM_COLOR_DOT_PRODUCT_2_SCALE + alpha;
1844 Parse1DMatrixAppend( shaderText, 5, cm->data );
1848 else if( !Q_stricmp( token, "volume" ) )
1850 /* special stub mode for flagging volume brushes */
1851 cm->type = CM_VOLUME;
1856 Sys_Printf( "WARNING: Unknown colorMod method: %s\n", token );
1859 /* ydnar: gs mods: q3map_tcMod <style> <parameters> */
1860 else if( !Q_stricmp( token, "q3map_tcMod" ) )
1865 GetTokenAppend( shaderText, qfalse );
1867 /* q3map_tcMod [translate | shift | offset] <s> <t> */
1868 if( !Q_stricmp( token, "translate" ) || !Q_stricmp( token, "shift" ) || !Q_stricmp( token, "offset" ) )
1870 GetTokenAppend( shaderText, qfalse );
1872 GetTokenAppend( shaderText, qfalse );
1875 TCModTranslate( si->mod, a, b );
1878 /* q3map_tcMod scale <s> <t> */
1879 else if( !Q_stricmp( token, "scale" ) )
1881 GetTokenAppend( shaderText, qfalse );
1883 GetTokenAppend( shaderText, qfalse );
1886 TCModScale( si->mod, a, b );
1889 /* q3map_tcMod rotate <s> <t> (fixme: make this communitive) */
1890 else if( !Q_stricmp( token, "rotate" ) )
1892 GetTokenAppend( shaderText, qfalse );
1894 TCModRotate( si->mod, a );
1897 Sys_Printf( "WARNING: Unknown q3map_tcMod method: %s\n", token );
1900 /* q3map_fogDir (direction a fog shader fades from transparent to opaque) */
1901 else if( !Q_stricmp( token, "q3map_fogDir" ) )
1903 Parse1DMatrixAppend( shaderText, 3, si->fogDir );
1904 VectorNormalize( si->fogDir, si->fogDir );
1907 /* q3map_globaltexture */
1908 else if( !Q_stricmp( token, "q3map_globaltexture" ) )
1909 si->globalTexture = qtrue;
1911 /* ydnar: gs mods: q3map_nonplanar (make it a nonplanar merge candidate for meta surfaces) */
1912 else if( !Q_stricmp( token, "q3map_nonplanar" ) )
1913 si->nonplanar = qtrue;
1915 /* ydnar: gs mods: q3map_noclip (preserve original face winding, don't clip by bsp tree) */
1916 else if( !Q_stricmp( token, "q3map_noclip" ) )
1920 else if( !Q_stricmp( token, "q3map_notjunc" ) )
1921 si->notjunc = qtrue;
1924 else if( !Q_stricmp( token, "q3map_nofog" ) )
1927 /* ydnar: gs mods: q3map_indexed (for explicit terrain-style indexed mapping) */
1928 else if( !Q_stricmp( token, "q3map_indexed" ) )
1929 si->indexed = qtrue;
1931 /* ydnar: q3map_invert (inverts a drawsurface's facing) */
1932 else if( !Q_stricmp( token, "q3map_invert" ) )
1935 /* ydnar: gs mods: q3map_lightmapMergable (ok to merge non-planar */
1936 else if( !Q_stricmp( token, "q3map_lightmapMergable" ) )
1937 si->lmMergable = qtrue;
1939 /* ydnar: q3map_nofast */
1940 else if( !Q_stricmp( token, "q3map_noFast" ) )
1943 /* q3map_patchshadows */
1944 else if( !Q_stricmp( token, "q3map_patchShadows" ) )
1945 si->patchShadows = qtrue;
1947 /* q3map_vertexshadows */
1948 else if( !Q_stricmp( token, "q3map_vertexShadows" ) )
1949 si->vertexShadows = qtrue; /* ydnar */
1951 /* q3map_novertexshadows */
1952 else if( !Q_stricmp( token, "q3map_noVertexShadows" ) )
1953 si->vertexShadows = qfalse; /* ydnar */
1955 /* q3map_splotchfix (filter dark lightmap luxels on lightmapped models) */
1956 else if( !Q_stricmp( token, "q3map_splotchfix" ) )
1957 si->splotchFix = qtrue; /* ydnar */
1959 /* q3map_forcesunlight */
1960 else if( !Q_stricmp( token, "q3map_forceSunlight" ) )
1961 si->forceSunlight = qtrue;
1963 /* q3map_onlyvertexlighting (sof2) */
1964 else if( !Q_stricmp( token, "q3map_onlyVertexLighting" ) )
1965 ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1967 /* q3map_material (sof2) */
1968 else if( !Q_stricmp( token, "q3map_material" ) )
1970 GetTokenAppend( shaderText, qfalse );
1971 sprintf( temp, "*mat_%s", token );
1972 if( ApplySurfaceParm( temp, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1973 Sys_Printf( "WARNING: Unknown material \"%s\"\n", token );
1976 /* ydnar: q3map_clipmodel (autogenerate clip brushes for model triangles using this shader) */
1977 else if( !Q_stricmp( token, "q3map_clipmodel" ) )
1978 si->clipModel = qtrue;
1980 /* ydnar: q3map_styleMarker[2] */
1981 else if( !Q_stricmp( token, "q3map_styleMarker" ) )
1982 si->styleMarker = 1;
1983 else if( !Q_stricmp( token, "q3map_styleMarker2" ) ) /* uses depthFunc equal */
1984 si->styleMarker = 2;
1986 /* ydnar: default to searching for q3map_<surfaceparm> */
1990 Sys_FPrintf( SYS_VRB, "Attempting to match %s with a known surfaceparm\n", token );
1991 if( ApplySurfaceParm( &token[ 6 ], &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
1992 Sys_Printf( "WARNING: Unknown q3map_* directive \"%s\"\n", token );
1998 /* -----------------------------------------------------------------
2000 ----------------------------------------------------------------- */
2002 /* ignore all other tokens on the line */
2003 while( TokenAvailable() && GetTokenAppend( shaderText, qfalse ) );
2011 ParseCustomInfoParms() - rr2do2
2012 loads custom info parms file for mods
2015 static void ParseCustomInfoParms( void )
2017 qboolean parsedContent, parsedSurface;
2021 if( vfsGetFileCount( "scripts/custinfoparms.txt" ) == 0 )
2025 LoadScriptFile( "scripts/custinfoparms.txt", 0 );
2027 /* clear the array */
2028 memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );
2029 numCustSurfaceParms = 0;
2030 parsedContent = parsedSurface = qfalse;
2032 /* parse custom contentflags */
2036 if ( !GetToken( qtrue ) )
2039 if ( !strcmp( token, "}" ) ) {
2040 parsedContent = qtrue;
2044 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
2045 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
2047 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].contentFlags );
2048 numCustSurfaceParms++;
2052 if( !parsedContent )
2054 Sys_Printf( "WARNING: Couldn't find valid custom contentsflag section\n" );
2058 /* parse custom surfaceflags */
2062 if( !GetToken( qtrue ) )
2065 if( !strcmp( token, "}" ) )
2067 parsedSurface = qtrue;
2071 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
2072 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
2074 sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );
2075 numCustSurfaceParms++;
2079 if( !parsedContent )
2080 Sys_Printf( "WARNING: Couldn't find valid custom surfaceflag section\n" );
2087 the shaders are parsed out of shaderlist.txt from a main directory
2088 that is, if using -fs_game we ignore the shader scripts that might be in baseq3/
2089 on linux there's an additional twist, we actually merge the stuff from ~/.q3a/ and from the base dir
2092 #define MAX_SHADER_FILES 1024
2094 void LoadShaderInfo( void )
2096 int i, j, numShaderFiles, count;
2097 char filename[ 1024 ];
2098 char *shaderFiles[ MAX_SHADER_FILES ];
2101 /* rr2do2: parse custom infoparms first */
2102 if( useCustomInfoParms )
2103 ParseCustomInfoParms();
2105 /* start with zero */
2108 /* we can pile up several shader files, the one in baseq3 and ones in the mod dir or other spots */
2109 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2110 count = vfsGetFileCount( filename );
2113 for( i = 0; i < count; i++ )
2115 /* load shader list */
2116 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2117 LoadScriptFile( filename, i );
2120 while( GetToken( qtrue ) )
2122 /* check for duplicate entries */
2123 for( j = 0; j < numShaderFiles; j++ )
2124 if( !strcmp( shaderFiles[ j ], token ) )
2128 if( j >= MAX_SHADER_FILES )
2129 Error( "MAX_SHADER_FILES (%d) reached, trim your shaderlist.txt!", (int) MAX_SHADER_FILES );
2131 /* new shader file */
2132 if( j == numShaderFiles )
2134 shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );
2135 strcpy( shaderFiles[ numShaderFiles ], token );
2141 /* parse the shader files */
2142 for( i = 0; i < numShaderFiles; i++ )
2144 sprintf( filename, "%s/%s.shader", game->shaderPath, shaderFiles[ i ] );
2145 ParseShaderFile( filename );
2146 free( shaderFiles[ i ] );
2149 /* emit some statistics */
2150 Sys_FPrintf( SYS_VRB, "%9d shaderInfo\n", numShaderInfo );