Skip to content

Commit ce34661

Browse files
committed
ParseShader: attempt to load extra textures the DarkPlaces way
1 parent b52cdfb commit ce34661

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

src/engine/renderer/tr_local.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,11 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
10761076

10771077
bool active;
10781078

1079+
// DarkPlaces compatibility
1080+
// only used to backup color map texture name
1081+
// to guess extra map name
1082+
char textureName[ MAX_QPATH ];
1083+
10791084
textureBundle_t bundle[ MAX_TEXTURE_BUNDLES ];
10801085

10811086
expression_t ifExp;

src/engine/renderer/tr_shader.cpp

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,13 @@ static bool LoadMap( shaderStage_t *stage, const char *buffer )
14421442
return false;
14431443
}
14441444

1445+
// DarkPlaces compatibility
1446+
// a diffuse maps loaded without stage keyword is a color map
1447+
// backup texture name somewhere, its basename will be needed for extra map lookup
1448+
if ( stage->type == stageType_t::ST_COLORMAP ) {
1449+
strcpy( stage->textureName, buffer );
1450+
}
1451+
14451452
return true;
14461453
}
14471454

@@ -3100,6 +3107,23 @@ static void ParseLightFalloffImage( shaderStage_t *stage, const char **text )
31003107
}
31013108
}
31023109

3110+
struct extraMapParser_t
3111+
{
3112+
const char *suffix;
3113+
const char *description;
3114+
void ( *parser ) ( shaderStage_t*, const char** );
3115+
};
3116+
3117+
static const extraMapParser_t extraMapParsers[] =
3118+
{
3119+
{ "_glow", "glow map", ParseGlowMap },
3120+
{ "_norm", "normal map", ParseNormalMap },
3121+
{ "_gloss", "specular map", ParseSpecularMap },
3122+
{ "_reflect", "reflection map", ParseReflectionMap },
3123+
};
3124+
3125+
static int numExtraMapParsers = ARRAY_LEN( extraMapParsers );
3126+
31033127
/*
31043128
=================
31053129
ParseShader
@@ -3111,13 +3135,12 @@ will optimize it.
31113135
*/
31123136
static bool ParseShader( const char *_text )
31133137
{
3138+
int s;
31143139
const char **text;
31153140
char *token;
3116-
int s;
31173141

31183142
s = 0;
31193143
text = &_text;
3120-
31213144
token = COM_ParseExt2( text, true );
31223145

31233146
if ( token[ 0 ] != '{' )
@@ -3678,6 +3701,48 @@ static bool ParseShader( const char *_text )
36783701
}
36793702
}
36803703

3704+
// DarkPlaces compatibility
3705+
// look for extra maps based on their suffixes
3706+
int c;
3707+
shaderStage_t colorMapStage;
3708+
3709+
// pick the first color map
3710+
for ( c = 0; c < s; c++ ) {
3711+
if ( stages[ c ].type == stageType_t::ST_COLORMAP && stages[ c ].textureName[ 0 ] != '\0' ) {
3712+
colorMapStage = stages[ c ];
3713+
break;
3714+
}
3715+
}
3716+
3717+
// guess extra maps for this color map
3718+
if ( c < s )
3719+
{
3720+
const char *prefix; // not used but required by R_FindImageLoader
3721+
const char *mapName;
3722+
char baseName[ MAX_QPATH ];
3723+
3724+
Log::Debug( "looking for extra maps for color map: '%s'", colorMapStage.textureName );
3725+
COM_StripExtension3( colorMapStage.textureName, baseName, MAX_QPATH );
3726+
3727+
int i;
3728+
for ( i = 0; i < numExtraMapParsers; i++ )
3729+
{
3730+
if ( s >= ( MAX_SHADER_STAGES - 1 ) )
3731+
{
3732+
Log::Warn( "too many extra stages in shader %s", shader.name );
3733+
return false;
3734+
}
3735+
3736+
mapName = va( "%s%s", baseName, extraMapParsers[ i ].suffix );
3737+
if( R_FindImageLoader( (char*) mapName, &prefix ) >= 0 ) {
3738+
Log::Debug( "found extra %s '%s'", extraMapParsers[ i ].description, mapName );
3739+
// colorMapStage.type = stageType_t::ST_DIFFUSEMAP;
3740+
extraMapParsers[ i ].parser( &stages[ s ], &mapName );
3741+
s++;
3742+
}
3743+
}
3744+
}
3745+
36813746
// ignore shaders that don't have any stages, unless it is a sky or fog
36823747
if ( s == 0 && !shader.forceOpaque && !shader.isSky && !( shader.contentFlags & CONTENTS_FOG ) && implicitMap[ 0 ] == '\0' )
36833748
{

0 commit comments

Comments
 (0)