Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically generate a specular map when there is no specular map #1243

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,8 @@ static void BindShaderLightMapping( Material* material ) {

gl_lightMappingShaderMaterial->SetReflectiveSpecular( material->enableNormalMapping && tr.cubeHashTable != nullptr );

gl_lightMappingShaderMaterial->SetAutomaticSpecularMap( material->hasAutomaticSpecularMap );

gl_lightMappingShaderMaterial->SetPhysicalShading( material->enablePhysicalMapping );

gl_lightMappingShaderMaterial->BindProgram( material->deformIndex );
Expand Down Expand Up @@ -1318,6 +1320,7 @@ static void ProcessMaterialLightMapping( Material* material, shaderStage_t* pSta
material->hasHeightMapInNormalMap = pStage->hasHeightMapInNormalMap;
material->enableReliefMapping = pStage->enableReliefMapping;
material->enableNormalMapping = pStage->enableNormalMapping && tr.cubeHashTable != nullptr;
material->hasAutomaticSpecularMap = pStage->hasAutomaticSpecularMap;
material->enablePhysicalMapping = pStage->enablePhysicalMapping;
material->deformIndex = pStage->deformIndex;

Expand All @@ -1333,6 +1336,8 @@ static void ProcessMaterialLightMapping( Material* material, shaderStage_t* pSta

gl_lightMappingShaderMaterial->SetReflectiveSpecular( pStage->enableNormalMapping && tr.cubeHashTable != nullptr );

gl_lightMappingShaderMaterial->SetAutomaticSpecularMap( pStage->hasAutomaticSpecularMap );

gl_lightMappingShaderMaterial->SetPhysicalShading( pStage->enablePhysicalMapping );

material->program = gl_lightMappingShaderMaterial->GetProgram( pStage->deformIndex );
Expand Down
1 change: 1 addition & 0 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ struct Material {
bool hasHeightMapInNormalMap;
bool enableReliefMapping;
bool enableNormalMapping;
bool hasAutomaticSpecularMap;
bool enablePhysicalMapping;

cullType_t cullType;
Expand Down
33 changes: 26 additions & 7 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ static std::string GenEngineConstants() {
AddDefine( str, "r_showLightTiles", 1 );
}

if ( r_normalMapping->integer )
if ( glConfig2.normalMapping )
{
AddDefine( str, "r_normalMapping", 1 );
}
Expand All @@ -739,16 +739,11 @@ static std::string GenEngineConstants() {
AddDefine( str, "r_liquidMapping", 1 );
}

if ( r_specularMapping->integer )
if ( glConfig2.specularMapping )
{
AddDefine( str, "r_specularMapping", 1 );
}

if ( r_physicalMapping->integer )
{
AddDefine( str, "r_physicalMapping", 1 );
}

if ( r_glowMapping->integer )
{
AddDefine( str, "r_glowMapping", 1 );
Expand Down Expand Up @@ -890,6 +885,7 @@ static bool IsUnusedPermutation( const char *compileMacros )
if ( strcmp( token, "USE_NORMAL_MAPPING" ) == 0 )
{
if ( !glConfig2.normalMapping ) return true;
if ( !glConfig2.deluxeMapping ) return true;
}
else if ( strcmp( token, "USE_DELUXE_MAPPING" ) == 0 )
{
Expand All @@ -902,6 +898,7 @@ static bool IsUnusedPermutation( const char *compileMacros )
else if ( strcmp( token, "USE_PHYSICAL_MAPPING" ) == 0 )
{
if ( !glConfig2.physicalMapping ) return true;
if ( !glConfig2.deluxeMapping ) return true;
}
else if ( strcmp( token, "USE_REFLECTIVE_SPECULAR" ) == 0 )
{
Expand All @@ -910,13 +907,19 @@ static bool IsUnusedPermutation( const char *compileMacros )
see https://github.com/DaemonEngine/Daemon/issues/355 */
if ( !glConfig2.specularMapping ) return true;
}
else if ( strcmp( token, "USE_AUTOMATIC_SPECULARMAP" ) == 0 )
{
if ( !glConfig2.automaticSpecularMap ) return true;
if ( !glConfig2.deluxeMapping ) return true;
}
else if ( strcmp( token, "USE_RELIEF_MAPPING" ) == 0 )
{
if ( !glConfig2.reliefMapping ) return true;
}
else if ( strcmp( token, "USE_HEIGHTMAP_IN_NORMALMAP" ) == 0 )
{
if ( !glConfig2.reliefMapping && !glConfig2.normalMapping ) return true;
if ( !glConfig2.deluxeMapping ) return true;
}
}

Expand Down Expand Up @@ -1584,6 +1587,20 @@ bool GLCompileMacro_USE_REFLECTIVE_SPECULAR::HasConflictingMacros( size_t permut
return false;
}

bool GLCompileMacro_USE_AUTOMATIC_SPECULARMAP::HasConflictingMacros( size_t permutation, const std::vector< GLCompileMacro * > &macros ) const
{
for (const GLCompileMacro* macro : macros)
{
if ( ( permutation & macro->GetBit() ) != 0 && (macro->GetType() == USE_PHYSICAL_MAPPING) )
{
//Log::Notice("conflicting macro! canceling '%s' vs. '%s'", GetName(), macro->GetName());
return true;
}
}

return false;
}

bool GLCompileMacro_USE_VERTEX_SKINNING::HasConflictingMacros( size_t permutation, const std::vector< GLCompileMacro * > &macros ) const
{
for (const GLCompileMacro* macro : macros)
Expand Down Expand Up @@ -2111,6 +2128,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP( this ),
GLCompileMacro_USE_RELIEF_MAPPING( this ),
GLCompileMacro_USE_REFLECTIVE_SPECULAR( this ),
GLCompileMacro_USE_AUTOMATIC_SPECULARMAP( this ),
GLCompileMacro_USE_PHYSICAL_MAPPING( this )
{
}
Expand Down Expand Up @@ -2180,6 +2198,7 @@ GLShader_lightMappingMaterial::GLShader_lightMappingMaterial( GLShaderManager* m
GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP( this ),
GLCompileMacro_USE_RELIEF_MAPPING( this ),
GLCompileMacro_USE_REFLECTIVE_SPECULAR( this ),
GLCompileMacro_USE_AUTOMATIC_SPECULARMAP( this ),
GLCompileMacro_USE_PHYSICAL_MAPPING( this ) {
}

Expand Down
32 changes: 31 additions & 1 deletion src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#define USE_UNIFORM_FIREWALL 1

// *INDENT-OFF*
static const unsigned int MAX_SHADER_MACROS = 10;
static const unsigned int MAX_SHADER_MACROS = 11;
static const unsigned int GL_SHADER_VERSION = 5;

class ShaderException : public std::runtime_error
Expand Down Expand Up @@ -1675,6 +1675,7 @@ class GLCompileMacro
USE_HEIGHTMAP_IN_NORMALMAP,
USE_RELIEF_MAPPING,
USE_REFLECTIVE_SPECULAR,
USE_AUTOMATIC_SPECULARMAP,
USE_SHADOWING,
LIGHT_DIRECTIONAL,
USE_DEPTH_FADE,
Expand Down Expand Up @@ -2058,6 +2059,33 @@ class GLCompileMacro_USE_REFLECTIVE_SPECULAR :
}
};

class GLCompileMacro_USE_AUTOMATIC_SPECULARMAP :
GLCompileMacro
{
public:
GLCompileMacro_USE_AUTOMATIC_SPECULARMAP( GLShader *shader ) :
GLCompileMacro( shader )
{
}

const char *GetName() const override
{
return "USE_AUTOMATIC_SPECULARMAP";
}

bool HasConflictingMacros(size_t permutation, const std::vector< GLCompileMacro * > &macros) const override;

EGLCompileMacro GetType() const override
{
return EGLCompileMacro::USE_AUTOMATIC_SPECULARMAP;
}

void SetAutomaticSpecularMap( bool enable )
{
SetMacro( enable );
}
};

class GLCompileMacro_LIGHT_DIRECTIONAL :
GLCompileMacro
{
Expand Down Expand Up @@ -4040,6 +4068,7 @@ class GLShader_lightMapping :
public GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP,
public GLCompileMacro_USE_RELIEF_MAPPING,
public GLCompileMacro_USE_REFLECTIVE_SPECULAR,
public GLCompileMacro_USE_AUTOMATIC_SPECULARMAP,
public GLCompileMacro_USE_PHYSICAL_MAPPING
{
public:
Expand Down Expand Up @@ -4091,6 +4120,7 @@ class GLShader_lightMappingMaterial :
public GLCompileMacro_USE_HEIGHTMAP_IN_NORMALMAP,
public GLCompileMacro_USE_RELIEF_MAPPING,
public GLCompileMacro_USE_REFLECTIVE_SPECULAR,
public GLCompileMacro_USE_AUTOMATIC_SPECULARMAP,
public GLCompileMacro_USE_PHYSICAL_MAPPING {
public:
GLShader_lightMappingMaterial( GLShaderManager* manager );
Expand Down
7 changes: 6 additions & 1 deletion src/engine/renderer/glsl_source/lightMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ void main()
#endif // !r_normalMapping

// Compute the material term.
vec4 material = texture2D(u_MaterialMap, texCoords);
#if defined(USE_AUTOMATIC_SPECULARMAP)
float bright = max(max(diffuse.r, diffuse.g), diffuse.b);
vec4 material = vec4(vec3(max(0.0, (bright * 0.3) - 0.05)), 1.0);
#else
vec4 material = texture2D(u_MaterialMap, texCoords);
#endif

// Compute final color.
vec4 color;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/renderer/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cvar_t *r_specularExponentMax;
cvar_t *r_specularScale;
cvar_t *r_specularMapping;
Cvar::Cvar<bool> r_automaticSpecularMap( "r_automaticSpecularMap", "Generate a specular map from diffuse map if missing", Cvar::NONE, true );
cvar_t *r_deluxeMapping;
cvar_t *r_normalScale;
cvar_t *r_normalMapping;
Expand Down Expand Up @@ -1193,6 +1194,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
r_specularExponentMax = Cvar_Get( "r_specularExponentMax", "16", CVAR_CHEAT );
r_specularScale = Cvar_Get( "r_specularScale", "1.0", CVAR_CHEAT | CVAR_LATCH );
r_specularMapping = Cvar_Get( "r_specularMapping", "1", CVAR_LATCH | CVAR_ARCHIVE );
Cvar::Latch( r_automaticSpecularMap );
r_deluxeMapping = Cvar_Get( "r_deluxeMapping", "1", CVAR_LATCH | CVAR_ARCHIVE );
r_normalScale = Cvar_Get( "r_normalScale", "1.0", CVAR_ARCHIVE );
r_normalMapping = Cvar_Get( "r_normalMapping", "1", CVAR_LATCH | CVAR_ARCHIVE );
Expand Down
2 changes: 2 additions & 0 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ enum class dynamicLightRenderer_t { LEGACY, TILED };

// Texture storage variants.
bool hasHeightMapInNormalMap;
bool hasAutomaticSpecularMap;

// Available features.
bool enableNormalMapping;
Expand Down Expand Up @@ -2922,6 +2923,7 @@ enum class dynamicLightRenderer_t { LEGACY, TILED };
extern cvar_t *r_specularExponentMax;
extern cvar_t *r_specularScale;
extern cvar_t *r_specularMapping;
extern Cvar::Cvar<bool> r_automaticSpecularMap;
extern cvar_t *r_deluxeMapping;
extern cvar_t *r_normalScale;
extern cvar_t *r_normalMapping;
Expand Down
1 change: 1 addition & 0 deletions src/engine/renderer/tr_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct glconfig2_t
bool deluxeMapping;
bool normalMapping;
bool specularMapping;
bool automaticSpecularMap;
bool physicalMapping;
bool reliefMapping;
bool bloom;
Expand Down
5 changes: 5 additions & 0 deletions src/engine/renderer/tr_shade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ static void EnableAvailableFeatures()
}
}

glConfig2.automaticSpecularMap = glConfig2.specularMapping && r_automaticSpecularMap.Get();

glConfig2.bloom = r_bloom->integer;

/* Motion blur is enabled by cg_motionblur which is a client cvar so we have to build it in all cases,
Expand Down Expand Up @@ -1159,6 +1161,8 @@ void Render_lightMapping( shaderStage_t *pStage )

gl_lightMappingShader->SetReflectiveSpecular( enableReflectiveSpecular );

gl_lightMappingShader->SetAutomaticSpecularMap( pStage->hasAutomaticSpecularMap );

gl_lightMappingShader->SetPhysicalShading( pStage->enablePhysicalMapping );

gl_lightMappingShader->BindProgram( pStage->deformIndex );
Expand Down Expand Up @@ -1301,6 +1305,7 @@ void Render_lightMapping( shaderStage_t *pStage )
gl_lightMappingShader->SetUniform_SpecularExponent( specExpMin, specExpMax );
}

// specular reflection
if ( enableReflectiveSpecular )
{
cubemapProbe_t *cubeProbeNearest;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/renderer/tr_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5241,7 +5241,8 @@ static void FinishStages()

// Available features.
stage->enableNormalMapping = glConfig2.normalMapping && hasNormalMap;
stage->enableDeluxeMapping = glConfig2.deluxeMapping && ( hasNormalMap || hasMaterialMap );
stage->hasAutomaticSpecularMap = glConfig2.automaticSpecularMap && !hasMaterialMap;
stage->enableDeluxeMapping = glConfig2.deluxeMapping && ( hasNormalMap || hasMaterialMap || stage->hasAutomaticSpecularMap );

stage->enableReliefMapping = glConfig2.reliefMapping && !shader.disableReliefMapping
&& ( hasHeightMap || stage->hasHeightMapInNormalMap );
Expand Down