Skip to content
Merged
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
13 changes: 12 additions & 1 deletion src/engine/renderer/glsl_source/reliefMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,23 @@ vec3 NormalInTangentSpace(vec2 texNormal)
// take the square root of a negative number here.
normal.z = sqrt(max(0, 1.0 - dot(normal.xy, normal.xy)));
#endif // !USE_HEIGHTMAP_IN_NORMALMAP
// HACK: 0 normal Z channel can't be good
/* Disable normal map scaling when normal Z scale is set to zero.

This happens when r_normalScale is set to zero because
u_NormalScale.z is premultiplied with r_normalScale. User can
disable normal map scaling by setting r_normalScale to zero.

Normal Z component equal to zero would be wrong anyway.
*/
if (u_NormalScale.z != 0)
{
normal *= u_NormalScale;
}

// HACK: the GLSL code is currently assuming
// DirectX normal map format (+X -Y +Z)
// but engine is assuming the OpenGL way (+X +Y +Z)
normal.y *= -1;
#else // !r_normalMapping
// Flat normal map is {0.5, 0.5, 1.0} in [ 0.0, 1.0]
// which is stored as {0.0, 0.0, 1.0} in [-1.0, 1.0].
Expand Down
8 changes: 5 additions & 3 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1140,9 +1140,11 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
bool enableSpecularMapping;
bool enableGlowMapping;

// normalMap channel scale, negative value flips channel
bool hasNormalScale;
vec3_t normalScale;
// Normal map scale and format.
bool hasNormalFormat;
bool hasNormalScale;
vec3_t normalFormat;
vec3_t normalScale;

expression_t normalIntensityExp;

Expand Down
47 changes: 22 additions & 25 deletions src/engine/renderer/tr_shade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,37 +579,34 @@ void Tess_Begin( void ( *stageIteratorFunc )(),
}
}

/*
==============
SetNormalScale
==============
*/
void SetNormalScale( shaderStage_t *pStage, vec3_t normalScale)
void SetNormalScale( shaderStage_t *pStage, vec3_t normalScale )
{
float normalIntensity = RB_EvalExpression( &pStage->normalIntensityExp, 1 );

normalScale[ 0 ] = 1;
normalScale[ 1 ] = 1;
normalScale[ 2 ] = 1;
float normalIntensity = RB_EvalExpression( &pStage->normalIntensityExp, 1.0 );

normalScale[ 0 ] *= normalIntensity;
normalScale[ 1 ] *= normalIntensity;
for ( int i = 0; i < 3; i++ )
{
normalScale[ i ] = pStage->normalScale[ i ];

if ( pStage->hasNormalScale )
// Normal intensity is only applied on X and Y.
// This behaviour is inherited.
if ( i < 2 )
{
normalScale[ 0 ] *= pStage->normalScale[ 0 ];
normalScale[ 1 ] *= pStage->normalScale[ 1 ];
normalScale[ 2 ] *= pStage->normalScale[ 2 ];
normalScale[ i ] *= normalIntensity;
}
}

/* Note: the GLSL code disables normal map scaling when normal Z scale is
equal to zero.

It means normal map scaling is disabled when r_normalScale is set to zero.
This is cool enough to be kept as a feature.

Normal Z component equal to zero would be wrong anyway.

// NOTE: normalmap with zero Z component is wrong
//
// since glsl disables normalScale when Z is zero
// setting this to zero has side effect to reset
// per-stage normalScale
//
// there is no intention to keep this undefined feature
normalScale[ 2 ] *= r_normalScale->value;
r_normalScale is only applied on Z.
This behaviour is inherited.
*/
normalScale[ 2 ] *= r_normalScale->value;
}

// *INDENT-ON*
Expand Down
Loading