Skip to content

Commit

Permalink
renderer: enable normalIntensity and normalScale for any glsl with no…
Browse files Browse the repository at this point in the history
…rmal map

no need to hack normalScale with a fourth channel to tell it's customized anymore
  • Loading branch information
illwieckz committed Dec 15, 2019
1 parent 5c4b1d8 commit c3ab496
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1526,15 +1526,15 @@ class u_NormalIntensity :
};

class u_NormalScale :
GLUniform4f
GLUniform3f
{
public:
u_NormalScale( GLShader *shader ) :
GLUniform4f( shader, "u_NormalScale" )
GLUniform3f( shader, "u_NormalScale" )
{
}

void SetUniform_NormalScale( const vec4_t value )
void SetUniform_NormalScale( const vec3_t value )
{
this->SetValue( value );
}
Expand Down
9 changes: 4 additions & 5 deletions src/engine/renderer/glsl_source/reliefMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ uniform int u_HeightMapInNormalMap;
#endif // r_normalMapping || USE_PARALLAX_MAPPING

#if defined(r_normalMapping)
// HACK: fourth component tells renderer if normal scale is customized or not
uniform vec4 u_NormalScale;
uniform vec3 u_NormalScale;
#endif // r_normalMapping

#if defined(USE_PARALLAX_MAPPING)
Expand Down Expand Up @@ -64,10 +63,10 @@ vec3 NormalInTangentSpace(vec2 texNormal)
normal = 2.0 * normal - 1.0;
}

// HACK: fourth component tells renderer if normal scale is customized or not
if (u_NormalScale.w != 0)
// HACK: 0 normal Z channel can't be good
if (u_NormalScale.z != 0)
{
normal *= u_NormalScale.xyz;
normal *= u_NormalScale;
}

#if defined(r_NormalScale)
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,8 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
expression_t fresnelBiasExp;

// normalMap channel scale, negative value flips channel
// HACK: fourth component tells renderer if normal scale is customized or not
vec4_t normalScale;
bool hasNormalScale;
vec3_t normalScale;

expression_t normalIntensityExp;

Expand Down
64 changes: 55 additions & 9 deletions src/engine/renderer/tr_shade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,28 @@ void Tess_Begin( void ( *stageIteratorFunc )(),
}
}

/*
==============
SetNormalScale
==============
*/
void SetNormalScale( shaderStage_t *pStage, vec3_t normalScale)
{
float normalIntensity;

normalIntensity = RB_EvalExpression( &pStage->normalIntensityExp, 1 );

normalScale[ 0 ] *= normalIntensity;
normalScale[ 1 ] *= normalIntensity;

if ( pStage->hasNormalScale )
{
normalScale[ 0 ] *= pStage->normalScale[ 0 ];
normalScale[ 1 ] *= pStage->normalScale[ 1 ];
normalScale[ 2 ] *= pStage->normalScale[ 2 ];
}
}

// *INDENT-ON*

static void Render_generic( int stage )
Expand Down Expand Up @@ -799,8 +821,11 @@ static void Render_vertexLighting_DBS_entity( int stage )
// bind u_NormalMap
GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] );

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( pStage, normalScale );

// bind u_NormalScale
gl_vertexLightingShader_DBS_entity->SetUniform_NormalScale( pStage->normalScale );
gl_vertexLightingShader_DBS_entity->SetUniform_NormalScale( normalScale );
}
else
{
Expand Down Expand Up @@ -1043,8 +1068,11 @@ static void Render_vertexLighting_DBS_world( int stage )
// bind u_NormalMap
GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] );

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( pStage, normalScale );

// bind u_NormalScale
gl_vertexLightingShader_DBS_world->SetUniform_NormalScale( pStage->normalScale );
gl_vertexLightingShader_DBS_world->SetUniform_NormalScale( normalScale );
}
else
{
Expand Down Expand Up @@ -1213,8 +1241,11 @@ static void Render_lightMapping( int stage )
{
GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] );

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( pStage, normalScale );

// bind u_NormalScale
gl_lightMappingShader->SetUniform_NormalScale( pStage->normalScale );
gl_lightMappingShader->SetUniform_NormalScale( normalScale );
}
else
{
Expand Down Expand Up @@ -1552,8 +1583,11 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *diffuseStage,
// bind u_NormalMap
GL_BindToTMU( 1, diffuseStage->bundle[ TB_NORMALMAP ].image[ 0 ] );

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( diffuseStage, normalScale );

// bind u_NormalScale
gl_forwardLightingShader_omniXYZ->SetUniform_NormalScale( diffuseStage->normalScale );
gl_forwardLightingShader_omniXYZ->SetUniform_NormalScale( normalScale );
}
else
{
Expand Down Expand Up @@ -1739,8 +1773,11 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *diffuseStage,
// bind u_NormalMap
GL_BindToTMU( 1, diffuseStage->bundle[ TB_NORMALMAP ].image[ 0 ] );

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( diffuseStage, normalScale );

// bind u_NormalScale
gl_forwardLightingShader_projXYZ->SetUniform_NormalScale( diffuseStage->normalScale );
gl_forwardLightingShader_projXYZ->SetUniform_NormalScale( normalScale );
}
else
{
Expand Down Expand Up @@ -1928,8 +1965,11 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *diffuseStage,
// bind u_NormalMap
GL_BindToTMU( 1, diffuseStage->bundle[ TB_NORMALMAP ].image[ 0 ] );

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( diffuseStage, normalScale );

// bind u_NormalScale
gl_forwardLightingShader_directionalSun->SetUniform_NormalScale( diffuseStage->normalScale );
gl_forwardLightingShader_directionalSun->SetUniform_NormalScale( normalScale );
}
else
{
Expand Down Expand Up @@ -2064,8 +2104,11 @@ static void Render_reflection_CB( int stage )
gl_reflectionShader->SetUniform_HeightMapInNormalMap( pStage->heightMapInNormalMap );
}

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( pStage, normalScale );

// bind u_NormalScale
gl_reflectionShader->SetUniform_NormalScale( pStage->normalScale );
gl_reflectionShader->SetUniform_NormalScale( normalScale );

gl_reflectionShader->SetRequiredVertexPointers();

Expand Down Expand Up @@ -2231,8 +2274,11 @@ static void Render_heatHaze( int stage )

gl_heatHazeShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_COLORMAP ] );

vec3_t normalScale = { 1, 1, 1 };
SetNormalScale( pStage, normalScale );

// bind u_NormalScale
gl_heatHazeShader->SetUniform_NormalScale( pStage->normalScale );
gl_heatHazeShader->SetUniform_NormalScale( normalScale );
}

GL_BindToTMU( 1, tr.currentRenderImage[ backEnd.currentMainFBO ] );
Expand Down Expand Up @@ -2318,7 +2364,7 @@ static void Render_liquid( int stage )
SetNormalScale( pStage, normalScale );

// bind u_NormalScale
gl_liquidShader->SetUniform_NormalScale( pStage->normalScale );
gl_liquidShader->SetUniform_NormalScale( normalScale );
}

gl_liquidShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_COLORMAP ] );
Expand Down
12 changes: 5 additions & 7 deletions src/engine/renderer/tr_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2832,7 +2832,8 @@ static bool ParseStage( shaderStage_t *stage, const char **text )
// normalScale X Y
// having a fallback for missing Z component keeps compatibility
stage->normalScale[ 2 ] = 1;
// no need to hack the fourth component since previous loop did it
// no need to set hasNormalScale since previous loop did it
// stage->hasNormalScale = true;
}
else
{
Expand All @@ -2842,8 +2843,7 @@ static bool ParseStage( shaderStage_t *stage, const char **text )
}

stage->normalScale[ i ] = atof( token );
// HACK: fourth component tells renderer if normal scale is customized or not
stage->normalScale[ 3 ] = 1;
stage->hasNormalScale = true;
}
SkipRestOfLine( text );
continue;
Expand Down Expand Up @@ -3594,8 +3594,7 @@ static bool ParseShader( const char *_text )
stages[ s ].normalScale[ 0 ] = 1;
stages[ s ].normalScale[ 1 ] = -1;
stages[ s ].normalScale[ 2 ] = 1;
// HACK: fourth component tells renderer if normal scale is customized or not
stages[ s ].normalScale[ 3 ] = 1;
stages[ s ].hasNormalScale = true;
}

s++;
Expand Down Expand Up @@ -4522,8 +4521,7 @@ static void CollapseStages()
stages[ diffuseStage ].normalScale[ 0 ] = stages[ normalStage ].normalScale[ 0 ];
stages[ diffuseStage ].normalScale[ 1 ] = stages[ normalStage ].normalScale[ 1 ];
stages[ diffuseStage ].normalScale[ 2 ] = stages[ normalStage ].normalScale[ 2 ];
// HACK: fourth component tells renderer if normal scale is customized or not
stages[ diffuseStage ].normalScale[ 3 ] = stages[ normalStage ].normalScale[ 3 ];
stages[ diffuseStage ].hasNormalScale = stages[ normalStage ].hasNormalScale;
stages[ diffuseStage ].heightMapInNormalMap = stages[ normalStage ].heightMapInNormalMap;
// disable since it's merged
stages[ normalStage ].active = false;
Expand Down

0 comments on commit c3ab496

Please sign in to comment.