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 ){
55 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 ];
155 else if ( c > 255 ) {
158 dv->color[ j ][ k ] = c;
169 routines for dealing with a 3x3 texture mod matrix
172 void TCMod( tcMod_t mod, float st[ 2 ] ){
178 st[ 0 ] = ( mod[ 0 ][ 0 ] * old[ 0 ] ) + ( mod[ 0 ][ 1 ] * old[ 1 ] ) + mod[ 0 ][ 2 ];
179 st[ 1 ] = ( mod[ 1 ][ 0 ] * old[ 0 ] ) + ( mod[ 1 ][ 1 ] * old[ 1 ] ) + mod[ 1 ][ 2 ];
183 void TCModIdentity( tcMod_t mod ){
184 mod[ 0 ][ 0 ] = 1.0f; mod[ 0 ][ 1 ] = 0.0f; mod[ 0 ][ 2 ] = 0.0f;
185 mod[ 1 ][ 0 ] = 0.0f; mod[ 1 ][ 1 ] = 1.0f; mod[ 1 ][ 2 ] = 0.0f;
186 mod[ 2 ][ 0 ] = 0.0f; mod[ 2 ][ 1 ] = 0.0f; mod[ 2 ][ 2 ] = 1.0f; /* this row is only used for multiples, not transformation */
190 void TCModMultiply( tcMod_t a, tcMod_t b, tcMod_t out ){
194 for ( i = 0; i < 3; i++ )
196 out[ i ][ 0 ] = ( a[ i ][ 0 ] * b[ 0 ][ 0 ] ) + ( a[ i ][ 1 ] * b[ 1 ][ 0 ] ) + ( a[ i ][ 2 ] * b[ 2 ][ 0 ] );
197 out[ i ][ 1 ] = ( a[ i ][ 0 ] * b[ 0 ][ 1 ] ) + ( a[ i ][ 1 ] * b[ 1 ][ 1 ] ) + ( a[ i ][ 2 ] * b[ 2 ][ 1 ] );
198 out[ i ][ 2 ] = ( a[ i ][ 0 ] * b[ 0 ][ 2 ] ) + ( a[ i ][ 1 ] * b[ 1 ][ 2 ] ) + ( a[ i ][ 2 ] * b[ 2 ][ 2 ] );
203 void TCModTranslate( tcMod_t mod, float s, float t ){
209 void TCModScale( tcMod_t mod, float s, float t ){
215 void TCModRotate( tcMod_t mod, float euler ){
217 float radians, sinv, cosv;
220 memcpy( old, mod, sizeof( tcMod_t ) );
221 TCModIdentity( temp );
223 radians = euler / 180 * Q_PI;
224 sinv = sin( radians );
225 cosv = cos( radians );
227 temp[ 0 ][ 0 ] = cosv; temp[ 0 ][ 1 ] = -sinv;
228 temp[ 1 ][ 0 ] = sinv; temp[ 1 ][ 1 ] = cosv;
230 TCModMultiply( old, temp, mod );
236 ApplySurfaceParm() - ydnar
237 applies a named surfaceparm to the supplied flags
240 qboolean ApplySurfaceParm( char *name, int *contentFlags, int *surfaceFlags, int *compileFlags ){
246 if ( name == NULL ) {
249 if ( contentFlags == NULL ) {
250 contentFlags = &fake;
252 if ( surfaceFlags == NULL ) {
253 surfaceFlags = &fake;
255 if ( compileFlags == NULL ) {
256 compileFlags = &fake;
259 /* walk the current game's surfaceparms */
260 sp = game->surfaceParms;
261 while ( sp->name != NULL )
264 if ( !Q_stricmp( name, sp->name ) ) {
265 /* clear and set flags */
266 *contentFlags &= ~( sp->contentFlagsClear );
267 *contentFlags |= sp->contentFlags;
268 *surfaceFlags &= ~( sp->surfaceFlagsClear );
269 *surfaceFlags |= sp->surfaceFlags;
270 *compileFlags &= ~( sp->compileFlagsClear );
271 *compileFlags |= sp->compileFlags;
281 /* check custom info parms */
282 for ( i = 0; i < numCustSurfaceParms; i++ )
284 /* get surfaceparm */
285 sp = &custSurfaceParms[ i ];
288 if ( !Q_stricmp( name, sp->name ) ) {
289 /* clear and set flags */
290 *contentFlags &= ~( sp->contentFlagsClear );
291 *contentFlags |= sp->contentFlags;
292 *surfaceFlags &= ~( sp->surfaceFlagsClear );
293 *surfaceFlags |= sp->surfaceFlags;
294 *compileFlags &= ~( sp->compileFlagsClear );
295 *compileFlags |= sp->compileFlags;
302 /* no matching surfaceparm found */
309 BeginMapShaderFile() - ydnar
310 erases and starts a new map shader script
313 void BeginMapShaderFile( const char *mapFile ){
320 mapShaderFile[ 0 ] = '\0';
321 if ( mapFile == NULL || mapFile[ 0 ] == '\0' ) {
326 strcpy( base, mapFile );
327 StripExtension( base );
329 /* extract map name */
330 len = strlen( base ) - 1;
331 while ( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )
333 strcpy( mapName, &base[ len + 1 ] );
339 /* append ../scripts/q3map2_<mapname>.shader */
340 sprintf( mapShaderFile, "%s/../%s/q3map2_%s.shader", base, game->shaderPath, mapName );
341 Sys_FPrintf( SYS_VRB, "Map has shader script %s\n", mapShaderFile );
344 remove( mapShaderFile );
346 /* stop making warnings about missing images */
353 WriteMapShaderFile() - ydnar
354 writes a shader to the map shader script
357 void WriteMapShaderFile( void ){
364 if ( mapShaderFile[ 0 ] == '\0' ) {
368 /* are there any custom shaders? */
369 for ( i = 0, num = 0; i < numShaderInfo; i++ )
371 if ( shaderInfo[ i ].custom ) {
375 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" );
385 if ( file == NULL ) {
386 Sys_FPrintf( SYS_WRN, "WARNING: Unable to open map shader file %s for writing\n", mapShaderFile );
392 "// Custom shader file for %s.bsp\n"
393 "// Generated by Q3Map2 (ydnar)\n"
394 "// Do not edit! This file is overwritten on recompiles.\n\n",
397 /* walk the shader list */
398 for ( i = 0, num = 0; i < numShaderInfo; i++ )
400 /* get the shader and print it */
401 si = &shaderInfo[ i ];
402 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 ){
433 char shader[ MAX_QPATH ];
437 char *srcShaderText, temp[ 8192 ], shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */
442 return ShaderInfoForShader( "default" );
445 /* default shader text source */
446 srcShaderText = si->shaderText;
448 /* et: implicitMap */
449 if ( si->implicitMap == IM_OPAQUE ) {
450 srcShaderText = temp;
452 "{ // Q3Map2 defaulted (implicitMap)\n"
454 "\t\tmap $lightmap\n"
455 "\t\trgbGen identity\n"
457 "\tq3map_styleMarker\n"
460 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
461 "\t\trgbGen identity\n"
464 si->implicitImagePath );
467 /* et: implicitMask */
468 else if ( si->implicitMap == IM_MASKED ) {
469 srcShaderText = temp;
471 "{ // Q3Map2 defaulted (implicitMask)\n"
475 "\t\talphaFunc GE128\n"
479 "\t\tmap $lightmap\n"
480 "\t\trgbGen identity\n"
481 "\t\tdepthFunc equal\n"
483 "\tq3map_styleMarker\n"
486 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
487 "\t\tdepthFunc equal\n"
488 "\t\trgbGen identity\n"
491 si->implicitImagePath,
492 si->implicitImagePath );
495 /* et: implicitBlend */
496 else if ( si->implicitMap == IM_BLEND ) {
497 srcShaderText = temp;
499 "{ // Q3Map2 defaulted (implicitBlend)\n"
503 "\t\tblendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA\n"
506 "\t\tmap $lightmap\n"
507 "\t\trgbGen identity\n"
508 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
510 "\tq3map_styleMarker\n"
512 si->implicitImagePath );
515 /* default shader text */
516 else if ( srcShaderText == NULL ) {
517 srcShaderText = temp;
519 "{ // Q3Map2 defaulted\n"
521 "\t\tmap $lightmap\n"
522 "\t\trgbGen identity\n"
524 "\tq3map_styleMarker\n"
527 "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
528 "\t\trgbGen identity\n"
535 if ( ( strlen( mapName ) + 1 + 32 ) > MAX_QPATH ) {
536 Error( "Custom shader name length (%d) exceeded. Shorten your map name.\n", MAX_QPATH );
539 /* do some bad find-replace */
540 s = strstr( srcShaderText, find );
542 //% strcpy( shaderText, srcShaderText );
543 return si; /* testing just using the existing shader if this fails */
547 /* substitute 'find' with 'replace' */
548 loc = s - srcShaderText;
549 strcpy( shaderText, srcShaderText );
550 shaderText[ loc ] = '\0';
551 strcat( shaderText, replace );
552 strcat( shaderText, &srcShaderText[ loc + strlen( find ) ] );
555 /* make md4 hash of the shader text */
556 Com_BlockFullChecksum( shaderText, strlen( shaderText ), digest );
558 /* mangle hash into a shader name */
559 sprintf( shader, "%s/%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", mapName,
560 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ],
561 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ], digest[ 15 ] );
564 csi = ShaderInfoForShader( shader );
566 /* might be a preexisting shader */
571 /* clone the existing shader and rename */
572 memcpy( csi, si, sizeof( shaderInfo_t ) );
573 strcpy( csi->shader, shader );
576 /* store new shader text */
577 csi->shaderText = safe_malloc( strlen( shaderText ) + 1 );
578 strcpy( csi->shaderText, shaderText ); /* LEAK! */
587 EmitVertexRemapShader()
588 adds a vertexremapshader key/value pair to worldspawn
591 void EmitVertexRemapShader( char *from, char *to ){
593 char key[ 64 ], value[ 256 ];
597 if ( from == NULL || from[ 0 ] == '\0' ||
598 to == NULL || to[ 0 ] == '\0' ) {
603 sprintf( value, "%s;%s", from, to );
606 Com_BlockFullChecksum( value, strlen( value ), digest );
608 /* make key (this is annoying, as vertexremapshader is precisely 17 characters,
609 which is one too long, so we leave off the last byte of the md5 digest) */
610 sprintf( key, "vertexremapshader%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
611 digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ],
612 digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ] ); /* no: digest[ 15 ] */
614 /* add key/value pair to worldspawn */
615 SetKeyValue( &entities[ 0 ], key, value );
622 allocates and initializes a new shader
625 static shaderInfo_t *AllocShaderInfo( void ){
630 if ( shaderInfo == NULL ) {
631 shaderInfo = safe_malloc( sizeof( shaderInfo_t ) * MAX_SHADER_INFO );
636 if ( numShaderInfo == MAX_SHADER_INFO ) {
637 Error( "MAX_SHADER_INFO exceeded. Remove some PK3 files or shader scripts from shaderlist.txt and try again." );
639 si = &shaderInfo[ numShaderInfo ];
642 /* ydnar: clear to 0 first */
643 memset( si, 0, sizeof( shaderInfo_t ) );
646 ApplySurfaceParm( "default", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
648 si->backsplashFraction = DEF_BACKSPLASH_FRACTION;
649 si->backsplashDistance = DEF_BACKSPLASH_DISTANCE;
651 si->bounceScale = DEF_RADIOSITY_BOUNCE;
653 si->lightStyle = LS_NORMAL;
655 si->polygonOffset = qfalse;
657 si->shadeAngleDegrees = 0.0f;
658 si->lightmapSampleSize = 0;
659 si->lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;
660 si->patchShadows = qfalse;
661 si->vertexShadows = qtrue; /* ydnar: changed default behavior */
662 si->forceSunlight = qfalse;
663 si->vertexScale = 1.0;
664 si->notjunc = qfalse;
666 /* ydnar: set texture coordinate transform matrix to identity */
667 TCModIdentity( si->mod );
669 /* ydnar: lightmaps can now be > 128x128 in certain games or an externally generated tga */
670 si->lmCustomWidth = lmCustomSize;
671 si->lmCustomHeight = lmCustomSize;
673 /* return to sender */
680 FinishShader() - ydnar
681 sets a shader's width and height among other things
684 void FinishShader( shaderInfo_t *si ){
686 float st[ 2 ], o[ 2 ], dist, bestDist;
690 /* don't double-dip */
691 if ( si->finished ) {
695 /* if they're explicitly set, copy from image size */
696 if ( si->shaderWidth == 0 && si->shaderHeight == 0 ) {
697 si->shaderWidth = si->shaderImage->width;
698 si->shaderHeight = si->shaderImage->height;
701 /* legacy terrain has explicit image-sized texture projection */
702 if ( si->legacyTerrain && si->tcGen == qfalse ) {
703 /* set xy texture projection */
705 VectorSet( si->vecs[ 0 ], ( 1.0f / ( si->shaderWidth * 0.5f ) ), 0, 0 );
706 VectorSet( si->vecs[ 1 ], 0, ( 1.0f / ( si->shaderHeight * 0.5f ) ), 0 );
709 /* find pixel coordinates best matching the average color of the image */
711 o[ 0 ] = 1.0f / si->shaderImage->width;
712 o[ 1 ] = 1.0f / si->shaderImage->height;
713 for ( y = 0, st[ 1 ] = 0.0f; y < si->shaderImage->height; y++, st[ 1 ] += o[ 1 ] )
715 for ( x = 0, st[ 0 ] = 0.0f; x < si->shaderImage->width; x++, st[ 0 ] += o[ 0 ] )
717 /* sample the shader image */
718 RadSampleImage( si->shaderImage->pixels, si->shaderImage->width, si->shaderImage->height, st, color );
720 /* determine error squared */
721 VectorSubtract( color, si->averageColor, delta );
722 delta[ 3 ] = color[ 3 ] - si->averageColor[ 3 ];
723 dist = delta[ 0 ] * delta[ 0 ] + delta[ 1 ] * delta[ 1 ] + delta[ 2 ] * delta[ 2 ] + delta[ 3 ] * delta[ 3 ];
724 if ( dist < bestDist ) {
725 si->stFlat[ 0 ] = st[ 0 ];
726 si->stFlat[ 1 ] = st[ 1 ];
731 /* set to finished */
732 si->finished = qtrue;
739 loads a shader's images
740 ydnar: image.c made this a bit simpler
743 static void LoadShaderImages( shaderInfo_t *si ){
748 /* nodraw shaders don't need images */
749 if ( si->compileFlags & C_NODRAW ) {
750 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
754 /* try to load editor image first */
755 si->shaderImage = ImageLoad( si->editorImagePath );
757 /* then try shadername */
758 if ( si->shaderImage == NULL ) {
759 si->shaderImage = ImageLoad( si->shader );
762 /* then try implicit image path (note: new behavior!) */
763 if ( si->shaderImage == NULL ) {
764 si->shaderImage = ImageLoad( si->implicitImagePath );
767 /* then try lightimage (note: new behavior!) */
768 if ( si->shaderImage == NULL ) {
769 si->shaderImage = ImageLoad( si->lightImagePath );
772 /* otherwise, use default image */
773 if ( si->shaderImage == NULL ) {
774 si->shaderImage = ImageLoad( DEFAULT_IMAGE );
775 if ( warnImage && strcmp( si->shader, "noshader" ) ) {
776 Sys_FPrintf( SYS_WRN, "WARNING: Couldn't find image for shader %s\n", si->shader );
780 /* load light image */
781 si->lightImage = ImageLoad( si->lightImagePath );
783 /* load normalmap image (ok if this is NULL) */
784 si->normalImage = ImageLoad( si->normalImagePath );
785 if ( si->normalImage != NULL ) {
786 Sys_FPrintf( SYS_VRB, "Shader %s has\n"
787 " NM %s\n", si->shader, si->normalImagePath );
791 /* if no light image, use shader image */
792 if ( si->lightImage == NULL ) {
793 si->lightImage = ImageLoad( si->shaderImage->name );
796 /* create default and average colors */
797 count = si->lightImage->width * si->lightImage->height;
798 VectorClear( color );
800 for ( i = 0; i < count; i++ )
802 color[ 0 ] += si->lightImage->pixels[ i * 4 + 0 ];
803 color[ 1 ] += si->lightImage->pixels[ i * 4 + 1 ];
804 color[ 2 ] += si->lightImage->pixels[ i * 4 + 2 ];
805 color[ 3 ] += si->lightImage->pixels[ i * 4 + 3 ];
808 if ( VectorLength( si->color ) <= 0.0f ) {
809 ColorNormalize( color, si->color );
810 VectorScale( color, ( 1.0f / count ), si->averageColor );
814 VectorCopy( si->color, si->averageColor );
821 ShaderInfoForShader()
822 finds a shaderinfo for a named shader
825 #define MAX_SHADER_DEPRECATION_DEPTH 16
827 shaderInfo_t *ShaderInfoForShaderNull( const char *shaderName ){
828 if ( !strcmp( shaderName, "noshader" ) ) {
831 return ShaderInfoForShader( shaderName );
834 shaderInfo_t *ShaderInfoForShader( const char *shaderName ){
836 int deprecationDepth;
838 char shader[ MAX_QPATH ];
841 if ( shaderName == NULL || shaderName[ 0 ] == '\0' ) {
842 Sys_FPrintf( SYS_WRN, "WARNING: Null or empty shader name\n" );
843 shaderName = "missing";
846 /* strip off extension */
847 strcpy( shader, shaderName );
848 StripExtension( shader );
851 deprecationDepth = 0;
852 for ( i = 0; i < numShaderInfo; i++ )
854 si = &shaderInfo[ i ];
855 if ( !Q_stricmp( shader, si->shader ) ) {
856 /* check if shader is deprecated */
857 if ( deprecationDepth < MAX_SHADER_DEPRECATION_DEPTH && si->deprecateShader && si->deprecateShader[ 0 ] ) {
859 strcpy( shader, si->deprecateShader );
860 StripExtension( shader );
861 /* increase deprecation depth */
863 if ( deprecationDepth == MAX_SHADER_DEPRECATION_DEPTH ) {
864 Sys_FPrintf( SYS_WRN, "WARNING: Max deprecation depth of %i is reached on shader '%s'\n", MAX_SHADER_DEPRECATION_DEPTH, shader );
866 /* search again from beginning */
871 /* load image if necessary */
872 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 ){
908 r = GetToken( crossline );
909 if ( r == qfalse || buffer == NULL || token[ 0 ] == '\0' ) {
914 if ( token[ 0 ] == '}' ) {
919 if ( oldScriptLine != scriptline ) {
920 strcat( buffer, "\n" );
921 for ( i = 0; i < tabDepth; i++ )
922 strcat( buffer, "\t" );
925 strcat( buffer, " " );
927 oldScriptLine = scriptline;
928 strcat( buffer, token );
931 if ( token[ 0 ] == '{' ) {
940 void Parse1DMatrixAppend( char *buffer, int x, vec_t *m ){
944 if ( !GetTokenAppend( buffer, qtrue ) || strcmp( token, "(" ) ) {
945 Error( "Parse1DMatrixAppend(): line %d: ( not found!", scriptline );
947 for ( i = 0; i < x; i++ )
949 if ( !GetTokenAppend( buffer, qfalse ) ) {
950 Error( "Parse1DMatrixAppend(): line %d: Number not found!", scriptline );
952 m[ i ] = atof( token );
954 if ( !GetTokenAppend( buffer, qtrue ) || strcmp( token, ")" ) ) {
955 Error( "Parse1DMatrixAppend(): line %d: ) not found!", scriptline );
964 parses a shader file into discrete shaderInfo_t
967 static void ParseShaderFile( const char *filename ){
970 char *suffix, temp[ 1024 ];
971 char shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */
976 shaderText[ 0 ] = '\0';
978 /* load the shader */
979 LoadScriptFile( filename, 0 );
984 /* copy shader text to the shaderinfo */
985 if ( si != NULL && shaderText[ 0 ] != '\0' ) {
986 strcat( shaderText, "\n" );
987 si->shaderText = safe_malloc( strlen( shaderText ) + 1 );
988 strcpy( si->shaderText, shaderText );
989 //% if( VectorLength( si->vecs[ 0 ] ) )
990 //% Sys_Printf( "%s\n", shaderText );
993 /* ydnar: clear shader text buffer */
994 shaderText[ 0 ] = '\0';
996 /* test for end of file */
997 if ( !GetToken( qtrue ) ) {
1001 /* shader name is initial token */
1002 si = AllocShaderInfo();
1003 strcpy( si->shader, token );
1005 /* ignore ":q3map" suffix */
1006 suffix = strstr( si->shader, ":q3map" );
1007 if ( suffix != NULL ) {
1011 /* handle { } section */
1012 if ( !GetTokenAppend( shaderText, qtrue ) ) {
1015 if ( strcmp( token, "{" ) ) {
1017 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s\nLast known shader: %s",
1018 filename, scriptline, token, si->shader );
1021 Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s",
1022 filename, scriptline, token );
1028 /* get the next token */
1029 if ( !GetTokenAppend( shaderText, qtrue ) ) {
1032 if ( !strcmp( token, "}" ) ) {
1037 /* -----------------------------------------------------------------
1038 shader stages (passes)
1039 ----------------------------------------------------------------- */
1041 /* parse stage directives */
1042 if ( !strcmp( token, "{" ) ) {
1043 si->hasPasses = qtrue;
1046 if ( !GetTokenAppend( shaderText, qtrue ) ) {
1049 if ( !strcmp( token, "}" ) ) {
1053 /* only care about images if we don't have a editor/light image */
1054 if ( si->editorImagePath[ 0 ] == '\0' && si->lightImagePath[ 0 ] == '\0' && si->implicitImagePath[ 0 ] == '\0' ) {
1055 /* digest any images */
1056 if ( !Q_stricmp( token, "map" ) ||
1057 !Q_stricmp( token, "clampMap" ) ||
1058 !Q_stricmp( token, "animMap" ) ||
1059 !Q_stricmp( token, "clampAnimMap" ) ||
1060 !Q_stricmp( token, "clampMap" ) ||
1061 !Q_stricmp( token, "mapComp" ) ||
1062 !Q_stricmp( token, "mapNoComp" ) ) {
1063 /* skip one token for animated stages */
1064 if ( !Q_stricmp( token, "animMap" ) || !Q_stricmp( token, "clampAnimMap" ) ) {
1065 GetTokenAppend( shaderText, qfalse );
1069 GetTokenAppend( shaderText, qfalse );
1070 if ( token[ 0 ] != '*' && token[ 0 ] != '$' ) {
1071 strcpy( si->lightImagePath, token );
1072 DefaultExtension( si->lightImagePath, ".tga" );
1075 //% Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath );
1083 /* -----------------------------------------------------------------
1084 surfaceparm * directives
1085 ----------------------------------------------------------------- */
1087 /* match surfaceparm */
1088 else if ( !Q_stricmp( token, "surfaceparm" ) ) {
1089 GetTokenAppend( shaderText, qfalse );
1090 if ( ApplySurfaceParm( token, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse ) {
1091 Sys_FPrintf( SYS_WRN, "WARNING: Unknown surfaceparm: \"%s\"\n", token );
1096 /* -----------------------------------------------------------------
1097 game-related shader directives
1098 ----------------------------------------------------------------- */
1100 /* ydnar: fogparms (for determining fog volumes) */
1101 else if ( !Q_stricmp( token, "fogparms" ) ) {
1102 si->fogParms = qtrue;
1105 /* ydnar: polygonoffset (for no culling) */
1106 else if ( !Q_stricmp( token, "polygonoffset" ) ) {
1107 si->polygonOffset = qtrue;
1110 /* tesssize is used to force liquid surfaces to subdivide */
1111 else if ( !Q_stricmp( token, "tessSize" ) || !Q_stricmp( token, "q3map_tessSize" ) /* sof2 */ ) {
1112 GetTokenAppend( shaderText, qfalse );
1113 si->subdivisions = atof( token );
1116 /* cull none will set twoSided (ydnar: added disable too) */
1117 else if ( !Q_stricmp( token, "cull" ) ) {
1118 GetTokenAppend( shaderText, qfalse );
1119 if ( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "disable" ) || !Q_stricmp( token, "twosided" ) ) {
1120 si->twoSided = qtrue;
1124 /* deformVertexes autosprite[ 2 ]
1125 we catch this so autosprited surfaces become point
1126 lights instead of area lights */
1127 else if ( !Q_stricmp( token, "deformVertexes" ) ) {
1128 GetTokenAppend( shaderText, qfalse );
1130 /* deformVertexes autosprite(2) */
1131 if ( !Q_strncasecmp( token, "autosprite", 10 ) ) {
1132 /* set it as autosprite and detail */
1133 si->autosprite = qtrue;
1134 ApplySurfaceParm( "detail", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1136 /* ydnar: gs mods: added these useful things */
1138 si->notjunc = qtrue;
1141 /* deformVertexes move <x> <y> <z> <func> <base> <amplitude> <phase> <freq> (ydnar: for particle studio support) */
1142 if ( !Q_stricmp( token, "move" ) ) {
1143 vec3_t amt, mins, maxs;
1147 /* get move amount */
1148 GetTokenAppend( shaderText, qfalse ); amt[ 0 ] = atof( token );
1149 GetTokenAppend( shaderText, qfalse ); amt[ 1 ] = atof( token );
1150 GetTokenAppend( shaderText, qfalse ); amt[ 2 ] = atof( token );
1153 GetTokenAppend( shaderText, qfalse );
1155 /* get base and amplitude */
1156 GetTokenAppend( shaderText, qfalse ); base = atof( token );
1157 GetTokenAppend( shaderText, qfalse ); amp = atof( token );
1160 VectorScale( amt, base, mins );
1161 VectorMA( mins, amp, amt, maxs );
1162 VectorAdd( si->mins, mins, si->mins );
1163 VectorAdd( si->maxs, maxs, si->maxs );
1167 /* light <value> (old-style flare specification) */
1168 else if ( !Q_stricmp( token, "light" ) ) {
1169 GetTokenAppend( shaderText, qfalse );
1170 si->flareShader = game->flareShader;
1173 /* ydnar: damageShader <shader> <health> (sof2 mods) */
1174 else if ( !Q_stricmp( token, "damageShader" ) ) {
1175 GetTokenAppend( shaderText, qfalse );
1176 if ( token[ 0 ] != '\0' ) {
1177 si->damageShader = safe_malloc( strlen( token ) + 1 );
1178 strcpy( si->damageShader, token );
1180 GetTokenAppend( shaderText, qfalse ); /* don't do anything with health */
1183 /* ydnar: enemy territory implicit shaders */
1184 else if ( !Q_stricmp( token, "implicitMap" ) ) {
1185 si->implicitMap = IM_OPAQUE;
1186 GetTokenAppend( shaderText, qfalse );
1187 if ( token[ 0 ] == '-' && token[ 1 ] == '\0' ) {
1188 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1191 strcpy( si->implicitImagePath, token );
1195 else if ( !Q_stricmp( token, "implicitMask" ) ) {
1196 si->implicitMap = IM_MASKED;
1197 GetTokenAppend( shaderText, qfalse );
1198 if ( token[ 0 ] == '-' && token[ 1 ] == '\0' ) {
1199 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1202 strcpy( si->implicitImagePath, token );
1206 else if ( !Q_stricmp( token, "implicitBlend" ) ) {
1207 si->implicitMap = IM_MASKED;
1208 GetTokenAppend( shaderText, qfalse );
1209 if ( token[ 0 ] == '-' && token[ 1 ] == '\0' ) {
1210 sprintf( si->implicitImagePath, "%s.tga", si->shader );
1213 strcpy( si->implicitImagePath, token );
1218 /* -----------------------------------------------------------------
1220 ----------------------------------------------------------------- */
1222 /* qer_editorimage <image> */
1223 else if ( !Q_stricmp( token, "qer_editorImage" ) ) {
1224 GetTokenAppend( shaderText, qfalse );
1225 strcpy( si->editorImagePath, token );
1226 DefaultExtension( si->editorImagePath, ".tga" );
1229 /* ydnar: q3map_normalimage <image> (bumpmapping normal map) */
1230 else if ( !Q_stricmp( token, "q3map_normalImage" ) ) {
1231 GetTokenAppend( shaderText, qfalse );
1232 strcpy( si->normalImagePath, token );
1233 DefaultExtension( si->normalImagePath, ".tga" );
1236 /* q3map_lightimage <image> */
1237 else if ( !Q_stricmp( token, "q3map_lightImage" ) ) {
1238 GetTokenAppend( shaderText, qfalse );
1239 strcpy( si->lightImagePath, token );
1240 DefaultExtension( si->lightImagePath, ".tga" );
1243 /* ydnar: skyparms <outer image> <cloud height> <inner image> */
1244 else if ( !Q_stricmp( token, "skyParms" ) ) {
1245 /* get image base */
1246 GetTokenAppend( shaderText, qfalse );
1248 /* ignore bogus paths */
1249 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 );
1258 /* skip rest of line */
1259 GetTokenAppend( shaderText, qfalse );
1260 GetTokenAppend( shaderText, qfalse );
1263 /* -----------------------------------------------------------------
1265 ----------------------------------------------------------------- */
1267 /* q3map_sun <red> <green> <blue> <intensity> <degrees> <elevation>
1268 color will be normalized, so it doesn't matter what range you use
1269 intensity falls off with angle but not distance 100 is a fairly bright sun
1270 degree of 0 = from the east, 90 = north, etc. altitude of 0 = sunrise/set, 90 = noon
1271 ydnar: sof2map has bareword 'sun' token, so we support that as well */
1272 else if ( !Q_stricmp( token, "sun" ) /* sof2 */ || !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) ) {
1275 qboolean ext = qfalse;
1277 /* ydnar: extended sun directive? */
1278 if ( !Q_stricmp( token, "q3map_sunext" ) ) {
1283 sun = safe_malloc0( sizeof( *sun ) );
1286 sun->style = si->lightStyle;
1289 GetTokenAppend( shaderText, qfalse );
1290 sun->color[ 0 ] = atof( token );
1291 GetTokenAppend( shaderText, qfalse );
1292 sun->color[ 1 ] = atof( token );
1293 GetTokenAppend( shaderText, qfalse );
1294 sun->color[ 2 ] = atof( token );
1297 sun->color[0] = Image_LinearFloatFromsRGBFloat( sun->color[0] );
1298 sun->color[1] = Image_LinearFloatFromsRGBFloat( sun->color[1] );
1299 sun->color[2] = Image_LinearFloatFromsRGBFloat( sun->color[2] );
1303 ColorNormalize( sun->color, sun->color );
1305 /* scale color by brightness */
1306 GetTokenAppend( shaderText, qfalse );
1307 sun->photons = atof( token );
1309 /* get sun angle/elevation */
1310 GetTokenAppend( shaderText, qfalse );
1312 a = a / 180.0f * Q_PI;
1314 GetTokenAppend( shaderText, qfalse );
1316 b = b / 180.0f * Q_PI;
1318 sun->direction[ 0 ] = cos( a ) * cos( b );
1319 sun->direction[ 1 ] = sin( a ) * cos( b );
1320 sun->direction[ 2 ] = sin( b );
1322 /* get filter radius from shader */
1323 sun->filterRadius = si->lightFilterRadius;
1325 /* ydnar: get sun angular deviance/samples */
1326 if ( ext && TokenAvailable() ) {
1327 GetTokenAppend( shaderText, qfalse );
1328 sun->deviance = atof( token );
1329 sun->deviance = sun->deviance / 180.0f * Q_PI;
1331 GetTokenAppend( shaderText, qfalse );
1332 sun->numSamples = atoi( token );
1336 sun->next = si->sun;
1339 /* apply sky surfaceparm */
1340 ApplySurfaceParm( "sky", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1342 /* don't process any more tokens on this line */
1347 else if ( !Q_strncasecmp( token, "q3map_", 6 ) ) {
1348 /* ydnar: q3map_baseShader <shader> (inherit this shader's parameters) */
1349 if ( !Q_stricmp( token, "q3map_baseShader" ) ) {
1351 qboolean oldWarnImage;
1355 GetTokenAppend( shaderText, qfalse );
1356 //% Sys_FPrintf( SYS_VRB, "Shader %s has base shader %s\n", si->shader, token );
1357 oldWarnImage = warnImage;
1359 si2 = ShaderInfoForShader( token );
1360 warnImage = oldWarnImage;
1363 if ( si2 != NULL ) {
1365 strcpy( temp, si->shader );
1368 memcpy( si, si2, sizeof( *si ) );
1370 /* restore name and set to unfinished */
1371 strcpy( si->shader, temp );
1372 si->shaderWidth = 0;
1373 si->shaderHeight = 0;
1374 si->finished = qfalse;
1378 /* ydnar: q3map_surfacemodel <path to model> <density> <min scale> <max scale> <min angle> <max angle> <oriented (0 or 1)> */
1379 else if ( !Q_stricmp( token, "q3map_surfacemodel" ) ) {
1380 surfaceModel_t *model;
1382 /* allocate new model and attach it */
1383 model = safe_malloc0( sizeof( *model ) );
1384 model->next = si->surfaceModel;
1385 si->surfaceModel = model;
1387 /* get parameters */
1388 GetTokenAppend( shaderText, qfalse );
1389 strcpy( model->model, token );
1391 GetTokenAppend( shaderText, qfalse );
1392 model->density = atof( token );
1393 GetTokenAppend( shaderText, qfalse );
1394 model->odds = atof( token );
1396 GetTokenAppend( shaderText, qfalse );
1397 model->minScale = atof( token );
1398 GetTokenAppend( shaderText, qfalse );
1399 model->maxScale = atof( token );
1401 GetTokenAppend( shaderText, qfalse );
1402 model->minAngle = atof( token );
1403 GetTokenAppend( shaderText, qfalse );
1404 model->maxAngle = atof( token );
1406 GetTokenAppend( shaderText, qfalse );
1407 model->oriented = ( token[ 0 ] == '1' ? qtrue : qfalse );
1410 /* ydnar/sd: q3map_foliage <path to model> <scale> <density> <odds> <invert alpha (1 or 0)> */
1411 else if ( !Q_stricmp( token, "q3map_foliage" ) ) {
1415 /* allocate new foliage struct and attach it */
1416 foliage = safe_malloc0( sizeof( *foliage ) );
1417 foliage->next = si->foliage;
1418 si->foliage = foliage;
1420 /* get parameters */
1421 GetTokenAppend( shaderText, qfalse );
1422 strcpy( foliage->model, token );
1424 GetTokenAppend( shaderText, qfalse );
1425 foliage->scale = atof( token );
1426 GetTokenAppend( shaderText, qfalse );
1427 foliage->density = atof( token );
1428 GetTokenAppend( shaderText, qfalse );
1429 foliage->odds = atof( token );
1430 GetTokenAppend( shaderText, qfalse );
1431 foliage->inverseAlpha = atoi( token );
1434 /* ydnar: q3map_bounce <value> (fraction of light to re-emit during radiosity passes) */
1435 else if ( !Q_stricmp( token, "q3map_bounce" ) || !Q_stricmp( token, "q3map_bounceScale" ) ) {
1436 GetTokenAppend( shaderText, qfalse );
1437 si->bounceScale = atof( token );
1440 /* ydnar/splashdamage: q3map_skyLight <value> <iterations> */
1441 else if ( !Q_stricmp( token, "q3map_skyLight" ) ) {
1442 GetTokenAppend( shaderText, qfalse );
1443 si->skyLightValue = atof( token );
1444 GetTokenAppend( shaderText, qfalse );
1445 si->skyLightIterations = atoi( token );
1448 if ( si->skyLightValue < 0.0f ) {
1449 si->skyLightValue = 0.0f;
1451 if ( si->skyLightIterations < 2 ) {
1452 si->skyLightIterations = 2;
1456 /* q3map_surfacelight <value> */
1457 else if ( !Q_stricmp( token, "q3map_surfacelight" ) ) {
1458 GetTokenAppend( shaderText, qfalse );
1459 si->value = atof( token );
1462 /* q3map_lightStyle (sof2/jk2 lightstyle) */
1463 else if ( !Q_stricmp( token, "q3map_lightStyle" ) ) {
1464 GetTokenAppend( shaderText, qfalse );
1465 val = atoi( token );
1469 else if ( val > LS_NONE ) {
1472 si->lightStyle = val;
1475 /* wolf: q3map_lightRGB <red> <green> <blue> */
1476 else if ( !Q_stricmp( token, "q3map_lightRGB" ) ) {
1477 VectorClear( si->color );
1478 GetTokenAppend( shaderText, qfalse );
1479 si->color[ 0 ] = atof( token );
1480 GetTokenAppend( shaderText, qfalse );
1481 si->color[ 1 ] = atof( token );
1482 GetTokenAppend( shaderText, qfalse );
1483 si->color[ 2 ] = atof( token );
1485 si->color[0] = Image_LinearFloatFromsRGBFloat( si->color[0] );
1486 si->color[1] = Image_LinearFloatFromsRGBFloat( si->color[1] );
1487 si->color[2] = Image_LinearFloatFromsRGBFloat( si->color[2] );
1489 ColorNormalize( si->color, si->color );
1492 /* q3map_lightSubdivide <value> */
1493 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" ) ) {
1500 GetTokenAppend( shaderText, qfalse );
1501 si->backsplashFraction = atof( token ) * 0.01f;
1502 GetTokenAppend( shaderText, qfalse );
1503 si->backsplashDistance = atof( token );
1506 /* q3map_floodLight <r> <g> <b> <diste> <intensity> <light_direction_power> */
1507 else if ( !Q_stricmp( token, "q3map_floodLight" ) ) {
1509 GetTokenAppend( shaderText, qfalse );
1510 si->floodlightRGB[ 0 ] = atof( token );
1511 GetTokenAppend( shaderText, qfalse );
1512 si->floodlightRGB[ 1 ] = atof( token );
1513 GetTokenAppend( shaderText, qfalse );
1514 si->floodlightRGB[ 2 ] = atof( token );
1515 GetTokenAppend( shaderText, qfalse );
1516 si->floodlightDistance = atof( token );
1517 GetTokenAppend( shaderText, qfalse );
1518 si->floodlightIntensity = atof( token );
1519 GetTokenAppend( shaderText, qfalse );
1520 si->floodlightDirectionScale = atof( token );
1522 si->floodlightRGB[0] = Image_LinearFloatFromsRGBFloat( si->floodlightRGB[0] );
1523 si->floodlightRGB[1] = Image_LinearFloatFromsRGBFloat( si->floodlightRGB[1] );
1524 si->floodlightRGB[2] = Image_LinearFloatFromsRGBFloat( si->floodlightRGB[2] );
1526 ColorNormalize( si->floodlightRGB, si->floodlightRGB );
1529 /* jal: q3map_nodirty : skip dirty */
1530 else if ( !Q_stricmp( token, "q3map_nodirty" ) ) {
1531 si->noDirty = qtrue;
1534 /* q3map_lightmapSampleSize <value> */
1535 else if ( !Q_stricmp( token, "q3map_lightmapSampleSize" ) ) {
1536 GetTokenAppend( shaderText, qfalse );
1537 si->lightmapSampleSize = atoi( token );
1540 /* q3map_lightmapSampleOffset <value> */
1541 else if ( !Q_stricmp( token, "q3map_lightmapSampleOffset" ) ) {
1542 GetTokenAppend( shaderText, qfalse );
1543 si->lightmapSampleOffset = atof( token );
1546 /* ydnar: q3map_lightmapFilterRadius <self> <other> */
1547 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" ) ) {
1556 GetTokenAppend( shaderText, qfalse );
1557 if ( !Q_stricmp( token, "x" ) ) {
1558 VectorSet( si->lightmapAxis, 1, 0, 0 );
1560 else if ( !Q_stricmp( token, "y" ) ) {
1561 VectorSet( si->lightmapAxis, 0, 1, 0 );
1563 else if ( !Q_stricmp( token, "z" ) ) {
1564 VectorSet( si->lightmapAxis, 0, 0, 1 );
1568 Sys_FPrintf( SYS_WRN, "WARNING: Unknown value for lightmap axis: %s\n", token );
1569 VectorClear( si->lightmapAxis );
1573 /* ydnar: q3map_lightmapSize <width> <height> (for autogenerated shaders + external tga lightmaps) */
1574 else if ( !Q_stricmp( token, "q3map_lightmapSize" ) ) {
1575 GetTokenAppend( shaderText, qfalse );
1576 si->lmCustomWidth = atoi( token );
1577 GetTokenAppend( shaderText, qfalse );
1578 si->lmCustomHeight = atoi( token );
1580 /* must be a power of 2 */
1581 if ( ( ( si->lmCustomWidth - 1 ) & si->lmCustomWidth ) ||
1582 ( ( si->lmCustomHeight - 1 ) & si->lmCustomHeight ) ) {
1583 Sys_FPrintf( SYS_WRN, "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" ) ) {
1592 GetTokenAppend( shaderText, qfalse );
1593 si->lmBrightness = atof( token );
1594 if ( si->lmBrightness < 0 ) {
1595 si->lmBrightness = 1.0;
1599 /* q3map_vertexScale (scale vertex lighting by this fraction) */
1600 else if ( !Q_stricmp( token, "q3map_vertexScale" ) ) {
1601 GetTokenAppend( shaderText, qfalse );
1602 si->vertexScale = atof( token );
1605 /* q3map_noVertexLight */
1606 else if ( !Q_stricmp( token, "q3map_noVertexLight" ) ) {
1607 si->noVertexLight = qtrue;
1610 /* q3map_flare[Shader] <shader> */
1611 else if ( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) ) {
1612 GetTokenAppend( shaderText, qfalse );
1613 if ( token[ 0 ] != '\0' ) {
1614 si->flareShader = safe_malloc( strlen( token ) + 1 );
1615 strcpy( si->flareShader, token );
1619 /* q3map_backShader <shader> */
1620 else if ( !Q_stricmp( token, "q3map_backShader" ) ) {
1621 GetTokenAppend( shaderText, qfalse );
1622 if ( token[ 0 ] != '\0' ) {
1623 si->backShader = safe_malloc( strlen( token ) + 1 );
1624 strcpy( si->backShader, token );
1628 /* ydnar: q3map_cloneShader <shader> */
1629 else if ( !Q_stricmp( token, "q3map_cloneShader" ) ) {
1630 GetTokenAppend( shaderText, qfalse );
1631 if ( token[ 0 ] != '\0' ) {
1632 si->cloneShader = safe_malloc( strlen( token ) + 1 );
1633 strcpy( si->cloneShader, token );
1637 /* q3map_remapShader <shader> */
1638 else if ( !Q_stricmp( token, "q3map_remapShader" ) ) {
1639 GetTokenAppend( shaderText, qfalse );
1640 if ( token[ 0 ] != '\0' ) {
1641 si->remapShader = safe_malloc( strlen( token ) + 1 );
1642 strcpy( si->remapShader, token );
1646 /* q3map_deprecateShader <shader> */
1647 else if ( !Q_stricmp( token, "q3map_deprecateShader" ) ) {
1648 GetTokenAppend( shaderText, qfalse );
1649 if ( token[ 0 ] != '\0' ) {
1651 si->deprecateShader = safe_malloc( strlen( token ) + 1 );
1652 strcpy( si->deprecateShader, token );
1656 /* ydnar: q3map_offset <value> */
1657 else if ( !Q_stricmp( token, "q3map_offset" ) ) {
1658 GetTokenAppend( shaderText, qfalse );
1659 si->offset = atof( token );
1662 /* ydnar: q3map_fur <numlayers> <offset> <fade> */
1663 else if ( !Q_stricmp( token, "q3map_fur" ) ) {
1664 GetTokenAppend( shaderText, qfalse );
1665 si->furNumLayers = atoi( token );
1666 GetTokenAppend( shaderText, qfalse );
1667 si->furOffset = atof( token );
1668 GetTokenAppend( shaderText, qfalse );
1669 si->furFade = atof( token );
1672 /* ydnar: gs mods: legacy support for terrain/terrain2 shaders */
1673 else if ( !Q_stricmp( token, "q3map_terrain" ) ) {
1674 /* team arena terrain is assumed to be nonplanar, with full normal averaging,
1675 passed through the metatriangle surface pipeline, with a lightmap axis on z */
1676 si->legacyTerrain = qtrue;
1678 si->notjunc = qtrue;
1679 si->indexed = qtrue;
1680 si->nonplanar = qtrue;
1681 si->forceMeta = qtrue;
1682 si->shadeAngleDegrees = 179.0f;
1683 //% VectorSet( si->lightmapAxis, 0, 0, 1 ); /* ydnar 2002-09-21: turning this off for better lightmapping of cliff faces */
1686 /* ydnar: picomodel: q3map_forceMeta (forces brush faces and/or triangle models to go through the metasurface pipeline) */
1687 else if ( !Q_stricmp( token, "q3map_forceMeta" ) ) {
1688 si->forceMeta = qtrue;
1691 /* ydnar: gs mods: q3map_shadeAngle <degrees> */
1692 else if ( !Q_stricmp( token, "q3map_shadeAngle" ) ) {
1693 GetTokenAppend( shaderText, qfalse );
1694 si->shadeAngleDegrees = atof( token );
1697 /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
1698 else if ( !Q_stricmp( token, "q3map_textureSize" ) ) {
1699 GetTokenAppend( shaderText, qfalse );
1700 si->shaderWidth = atoi( token );
1701 GetTokenAppend( shaderText, qfalse );
1702 si->shaderHeight = atoi( token );
1705 /* ydnar: gs mods: q3map_tcGen <style> <parameters> */
1706 else if ( !Q_stricmp( token, "q3map_tcGen" ) ) {
1708 GetTokenAppend( shaderText, qfalse );
1710 /* q3map_tcGen vector <s vector> <t vector> */
1711 if ( !Q_stricmp( token, "vector" ) ) {
1712 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1713 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1716 /* q3map_tcGen ivector <1.0/s vector> <1.0/t vector> (inverse vector, easier for mappers to understand) */
1717 else if ( !Q_stricmp( token, "ivector" ) ) {
1718 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
1719 Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
1720 for ( i = 0; i < 3; i++ )
1722 si->vecs[ 0 ][ i ] = si->vecs[ 0 ][ i ] ? 1.0 / si->vecs[ 0 ][ i ] : 0;
1723 si->vecs[ 1 ][ i ] = si->vecs[ 1 ][ i ] ? 1.0 / si->vecs[ 1 ][ i ] : 0;
1728 Sys_FPrintf( SYS_WRN, "WARNING: Unknown q3map_tcGen method: %s\n", token );
1729 VectorClear( si->vecs[ 0 ] );
1730 VectorClear( si->vecs[ 1 ] );
1734 /* ydnar: gs mods: q3map_[color|rgb|alpha][Gen|Mod] <style> <parameters> */
1735 else if ( !Q_stricmp( token, "q3map_colorGen" ) || !Q_stricmp( token, "q3map_colorMod" ) ||
1736 !Q_stricmp( token, "q3map_rgbGen" ) || !Q_stricmp( token, "q3map_rgbMod" ) ||
1737 !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) ) {
1738 colorMod_t *cm, *cm2;
1742 /* alphamods are colormod + 1 */
1743 alpha = ( !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) ) ? 1 : 0;
1745 /* allocate new colormod */
1746 cm = safe_malloc0( sizeof( *cm ) );
1748 /* attach to shader */
1749 if ( si->colorMod == NULL ) {
1754 for ( cm2 = si->colorMod; cm2 != NULL; cm2 = cm2->next )
1756 if ( cm2->next == NULL ) {
1764 GetTokenAppend( shaderText, qfalse );
1766 /* alpha set|const A */
1767 if ( alpha && ( !Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" ) ) ) {
1768 cm->type = CM_ALPHA_SET;
1769 GetTokenAppend( shaderText, qfalse );
1770 cm->data[ 0 ] = atof( token );
1773 /* color|rgb set|const ( X Y Z ) */
1774 else if ( !Q_stricmp( token, "set" ) || !Q_stricmp( token, "const" ) ) {
1775 cm->type = CM_COLOR_SET;
1776 Parse1DMatrixAppend( shaderText, 3, cm->data );
1778 cm->data[0] = Image_LinearFloatFromsRGBFloat( cm->data[0] );
1779 cm->data[1] = Image_LinearFloatFromsRGBFloat( cm->data[1] );
1780 cm->data[2] = Image_LinearFloatFromsRGBFloat( cm->data[2] );
1785 else if ( alpha && !Q_stricmp( token, "scale" ) ) {
1786 cm->type = CM_ALPHA_SCALE;
1787 GetTokenAppend( shaderText, qfalse );
1788 cm->data[ 0 ] = atof( token );
1791 /* color|rgb scale ( X Y Z ) */
1792 else if ( !Q_stricmp( token, "scale" ) ) {
1793 cm->type = CM_COLOR_SCALE;
1794 Parse1DMatrixAppend( shaderText, 3, cm->data );
1797 /* dotProduct ( X Y Z ) */
1798 else if ( !Q_stricmp( token, "dotProduct" ) ) {
1799 cm->type = CM_COLOR_DOT_PRODUCT + alpha;
1800 Parse1DMatrixAppend( shaderText, 3, cm->data );
1803 /* dotProductScale ( X Y Z MIN MAX ) */
1804 else if ( !Q_stricmp( token, "dotProductScale" ) ) {
1805 cm->type = CM_COLOR_DOT_PRODUCT_SCALE + alpha;
1806 Parse1DMatrixAppend( shaderText, 5, cm->data );
1809 /* dotProduct2 ( X Y Z ) */
1810 else if ( !Q_stricmp( token, "dotProduct2" ) ) {
1811 cm->type = CM_COLOR_DOT_PRODUCT_2 + alpha;
1812 Parse1DMatrixAppend( shaderText, 3, cm->data );
1815 /* dotProduct2scale ( X Y Z MIN MAX ) */
1816 else if ( !Q_stricmp( token, "dotProduct2scale" ) ) {
1817 cm->type = CM_COLOR_DOT_PRODUCT_2_SCALE + alpha;
1818 Parse1DMatrixAppend( shaderText, 5, cm->data );
1822 else if ( !Q_stricmp( token, "volume" ) ) {
1823 /* special stub mode for flagging volume brushes */
1824 cm->type = CM_VOLUME;
1829 Sys_FPrintf( SYS_WRN, "WARNING: Unknown colorMod method: %s\n", token );
1833 /* ydnar: gs mods: q3map_tcMod <style> <parameters> */
1834 else if ( !Q_stricmp( token, "q3map_tcMod" ) ) {
1838 GetTokenAppend( shaderText, qfalse );
1840 /* q3map_tcMod [translate | shift | offset] <s> <t> */
1841 if ( !Q_stricmp( token, "translate" ) || !Q_stricmp( token, "shift" ) || !Q_stricmp( token, "offset" ) ) {
1842 GetTokenAppend( shaderText, qfalse );
1844 GetTokenAppend( shaderText, qfalse );
1847 TCModTranslate( si->mod, a, b );
1850 /* q3map_tcMod scale <s> <t> */
1851 else if ( !Q_stricmp( token, "scale" ) ) {
1852 GetTokenAppend( shaderText, qfalse );
1854 GetTokenAppend( shaderText, qfalse );
1857 TCModScale( si->mod, a, b );
1860 /* q3map_tcMod rotate <s> <t> (fixme: make this communitive) */
1861 else if ( !Q_stricmp( token, "rotate" ) ) {
1862 GetTokenAppend( shaderText, qfalse );
1864 TCModRotate( si->mod, a );
1867 Sys_FPrintf( SYS_WRN, "WARNING: Unknown q3map_tcMod method: %s\n", token );
1871 /* q3map_fogDir (direction a fog shader fades from transparent to opaque) */
1872 else if ( !Q_stricmp( token, "q3map_fogDir" ) ) {
1873 Parse1DMatrixAppend( shaderText, 3, si->fogDir );
1874 VectorNormalize( si->fogDir, si->fogDir );
1877 /* q3map_globaltexture */
1878 else if ( !Q_stricmp( token, "q3map_globaltexture" ) ) {
1879 si->globalTexture = qtrue;
1882 /* ydnar: gs mods: q3map_nonplanar (make it a nonplanar merge candidate for meta surfaces) */
1883 else if ( !Q_stricmp( token, "q3map_nonplanar" ) ) {
1884 si->nonplanar = qtrue;
1887 /* ydnar: gs mods: q3map_noclip (preserve original face winding, don't clip by bsp tree) */
1888 else if ( !Q_stricmp( token, "q3map_noclip" ) ) {
1893 else if ( !Q_stricmp( token, "q3map_notjunc" ) ) {
1894 si->notjunc = qtrue;
1898 else if ( !Q_stricmp( token, "q3map_nofog" ) ) {
1902 /* ydnar: gs mods: q3map_indexed (for explicit terrain-style indexed mapping) */
1903 else if ( !Q_stricmp( token, "q3map_indexed" ) ) {
1904 si->indexed = qtrue;
1907 /* ydnar: q3map_invert (inverts a drawsurface's facing) */
1908 else if ( !Q_stricmp( token, "q3map_invert" ) ) {
1912 /* ydnar: gs mods: q3map_lightmapMergable (ok to merge non-planar */
1913 else if ( !Q_stricmp( token, "q3map_lightmapMergable" ) ) {
1914 si->lmMergable = qtrue;
1917 /* ydnar: q3map_nofast */
1918 else if ( !Q_stricmp( token, "q3map_noFast" ) ) {
1922 /* q3map_patchshadows */
1923 else if ( !Q_stricmp( token, "q3map_patchShadows" ) ) {
1924 si->patchShadows = qtrue;
1927 /* q3map_vertexshadows */
1928 else if ( !Q_stricmp( token, "q3map_vertexShadows" ) ) {
1929 si->vertexShadows = qtrue; /* ydnar */
1932 /* q3map_novertexshadows */
1933 else if ( !Q_stricmp( token, "q3map_noVertexShadows" ) ) {
1934 si->vertexShadows = qfalse; /* ydnar */
1937 /* q3map_splotchfix (filter dark lightmap luxels on lightmapped models) */
1938 else if ( !Q_stricmp( token, "q3map_splotchfix" ) ) {
1939 si->splotchFix = qtrue; /* ydnar */
1942 /* q3map_forcesunlight */
1943 else if ( !Q_stricmp( token, "q3map_forceSunlight" ) ) {
1944 si->forceSunlight = qtrue;
1947 /* q3map_onlyvertexlighting (sof2) */
1948 else if ( !Q_stricmp( token, "q3map_onlyVertexLighting" ) ) {
1949 ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
1952 /* q3map_material (sof2) */
1953 else if ( !Q_stricmp( token, "q3map_material" ) ) {
1954 GetTokenAppend( shaderText, qfalse );
1955 sprintf( temp, "*mat_%s", token );
1956 if ( ApplySurfaceParm( temp, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse ) {
1957 Sys_FPrintf( SYS_WRN, "WARNING: Unknown material \"%s\"\n", token );
1961 /* ydnar: q3map_clipmodel (autogenerate clip brushes for model triangles using this shader) */
1962 else if ( !Q_stricmp( token, "q3map_clipmodel" ) ) {
1963 si->clipModel = qtrue;
1966 /* ydnar: q3map_styleMarker[2] */
1967 else if ( !Q_stricmp( token, "q3map_styleMarker" ) ) {
1968 si->styleMarker = 1;
1970 else if ( !Q_stricmp( token, "q3map_styleMarker2" ) ) { /* uses depthFunc equal */
1971 si->styleMarker = 2;
1974 /* ydnar: default to searching for q3map_<surfaceparm> */
1978 Sys_FPrintf( SYS_VRB, "Attempting to match %s with a known surfaceparm\n", token );
1979 if ( ApplySurfaceParm( &token[ 6 ], &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse ) {
1980 Sys_FPrintf( SYS_WRN, "WARNING: Unknown q3map_* directive \"%s\"\n", token );
1987 /* -----------------------------------------------------------------
1989 ----------------------------------------------------------------- */
1991 /* ignore all other tokens on the line */
1992 while ( TokenAvailable() && GetTokenAppend( shaderText, qfalse ) ) ;
2000 ParseCustomInfoParms() - rr2do2
2001 loads custom info parms file for mods
2004 static void ParseCustomInfoParms( void ){
2005 qboolean parsedContent, parsedSurface;
2009 if ( vfsGetFileCount( "scripts/custinfoparms.txt" ) == 0 ) {
2014 LoadScriptFile( "scripts/custinfoparms.txt", 0 );
2016 /* clear the array */
2017 memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );
2018 numCustSurfaceParms = 0;
2019 parsedContent = parsedSurface = qfalse;
2021 /* parse custom contentflags */
2025 if ( !GetToken( qtrue ) ) {
2029 if ( !strcmp( token, "}" ) ) {
2030 parsedContent = qtrue;
2034 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
2035 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
2037 sscanf( token, "%x", (unsigned int *) &custSurfaceParms[ numCustSurfaceParms ].contentFlags );
2038 numCustSurfaceParms++;
2042 if ( !parsedContent ) {
2043 Sys_FPrintf( SYS_WRN, "WARNING: Couldn't find valid custom contentsflag section\n" );
2047 /* parse custom surfaceflags */
2051 if ( !GetToken( qtrue ) ) {
2055 if ( !strcmp( token, "}" ) ) {
2056 parsedSurface = qtrue;
2060 custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
2061 strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
2063 sscanf( token, "%x", (unsigned int *) &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );
2064 numCustSurfaceParms++;
2068 if ( !parsedContent ) {
2069 Sys_FPrintf( SYS_WRN, "WARNING: Couldn't find valid custom surfaceflag section\n" );
2077 the shaders are parsed out of shaderlist.txt from a main directory
2078 that is, if using -fs_game we ignore the shader scripts that might be in baseq3/
2079 on linux there's an additional twist, we actually merge the stuff from ~/.q3a/ and from the base dir
2082 #define MAX_SHADER_FILES 1024
2084 void LoadShaderInfo( void ){
2085 int i, j, numShaderFiles, count;
2086 char filename[ 1024 ];
2087 char *shaderFiles[ MAX_SHADER_FILES ];
2090 /* rr2do2: parse custom infoparms first */
2091 if ( useCustomInfoParms ) {
2092 ParseCustomInfoParms();
2095 /* start with zero */
2098 /* we can pile up several shader files, the one in baseq3 and ones in the mod dir or other spots */
2099 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2100 count = vfsGetFileCount( filename );
2103 for ( i = 0; i < count; i++ )
2105 /* load shader list */
2106 sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
2107 LoadScriptFile( filename, i );
2110 while ( GetToken( qtrue ) )
2112 /* check for duplicate entries */
2113 for ( j = 0; j < numShaderFiles; j++ )
2114 if ( !strcmp( shaderFiles[ j ], token ) ) {
2119 if ( j >= MAX_SHADER_FILES ) {
2120 Error( "MAX_SHADER_FILES (%d) reached, trim your shaderlist.txt!", (int) MAX_SHADER_FILES );
2123 /* new shader file */
2124 if ( j == numShaderFiles ) {
2125 shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );
2126 strcpy( shaderFiles[ numShaderFiles ], token );
2132 /* parse the shader files */
2133 for ( i = 0; i < numShaderFiles; i++ )
2135 sprintf( filename, "%s/%s.shader", game->shaderPath, shaderFiles[ i ] );
2136 ParseShaderFile( filename );
2137 free( shaderFiles[ i ] );
2140 /* emit some statistics */
2141 Sys_FPrintf( SYS_VRB, "%9d shaderInfo\n", numShaderInfo );