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

implement per-stage normalscale #231

Merged
merged 4 commits into from
Dec 25, 2019
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
17 changes: 7 additions & 10 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,6 @@ std::string GLShaderManager::BuildGPUShaderText( Str::StringRef mainShaderNa

AddDefine( env, "r_AmbientScale", r_ambientScale->value );
AddDefine( env, "r_SpecularScale", r_specularScale->value );
AddDefine( env, "r_NormalScale", r_normalScale->value );
AddDefine( env, "r_zNear", r_znear->value );

AddDefine( env, "M_PI", static_cast<float>( M_PI ) );
Expand Down Expand Up @@ -1468,7 +1467,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_NormalScale( this ),
u_numLights( this ),
u_Lights( this ),
GLDeformStage( this ),
Expand Down Expand Up @@ -1519,7 +1518,7 @@ GLShader_vertexLighting_DBS_entity::GLShader_vertexLighting_DBS_entity( GLShader
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_NormalScale( this ),
u_EnvironmentInterpolation( this ),
u_LightGridOrigin( this ),
u_LightGridScale( this ),
Expand Down Expand Up @@ -1580,7 +1579,7 @@ GLShader_vertexLighting_DBS_world::GLShader_vertexLighting_DBS_world( GLShaderMa
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_NormalScale( this ),
u_LightWrapAround( this ),
u_LightGridOrigin( this ),
u_LightGridScale( this ),
Expand Down Expand Up @@ -1642,7 +1641,7 @@ GLShader_forwardLighting_omniXYZ::GLShader_forwardLighting_omniXYZ( GLShaderMana
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_NormalScale( this ),
GLDeformStage( this ),
GLCompileMacro_USE_VERTEX_SKINNING( this ),
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
Expand Down Expand Up @@ -1701,7 +1700,7 @@ GLShader_forwardLighting_projXYZ::GLShader_forwardLighting_projXYZ( GLShaderMana
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_NormalScale( this ),
GLDeformStage( this ),
GLCompileMacro_USE_VERTEX_SKINNING( this ),
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
Expand Down Expand Up @@ -1763,7 +1762,7 @@ GLShader_forwardLighting_directionalSun::GLShader_forwardLighting_directionalSun
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_NormalScale( this ),
GLDeformStage( this ),
GLCompileMacro_USE_VERTEX_SKINNING( this ),
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
Expand Down Expand Up @@ -1846,7 +1845,6 @@ GLShader_reflection::GLShader_reflection( GLShaderManager *manager ):
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalScale( this ),
u_NormalFormat( this ),
u_VertexInterpolation( this ),
GLDeformStage( this ),
GLCompileMacro_USE_VERTEX_SKINNING( this ),
Expand Down Expand Up @@ -1948,7 +1946,7 @@ GLShader_heatHaze::GLShader_heatHaze( GLShaderManager *manager ) :
u_ColorModulate( this ),
u_Color( this ),
u_Bones( this ),
u_NormalFormat( this ),
u_NormalScale( this ),
u_VertexInterpolation( this ),
GLDeformStage( this ),
GLCompileMacro_USE_VERTEX_SKINNING( this ),
Expand Down Expand Up @@ -2112,7 +2110,6 @@ GLShader_liquid::GLShader_liquid( GLShaderManager *manager ) :
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalScale( this ),
u_NormalFormat( this ),
u_FogDensity( this ),
u_FogColor( this ),
u_SpecularExponent( this ),
Expand Down
37 changes: 10 additions & 27 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1511,30 +1511,15 @@ class u_FresnelBias :
};

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

void SetUniform_NormalScale( float value )
{
this->SetValue( value );
}
};

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

void SetUniform_NormalFormat( const vec3_t value )
void SetUniform_NormalScale( const vec3_t value )
{
this->SetValue( value );
}
Expand Down Expand Up @@ -2162,7 +2147,7 @@ class GLShader_lightMapping :
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_NormalScale,
public u_numLights,
public u_Lights,
public GLDeformStage,
Expand Down Expand Up @@ -2191,7 +2176,7 @@ class GLShader_vertexLighting_DBS_entity :
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_NormalScale,
public u_EnvironmentInterpolation,
public u_LightGridOrigin,
public u_LightGridScale,
Expand Down Expand Up @@ -2225,7 +2210,7 @@ class GLShader_vertexLighting_DBS_world :
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_NormalScale,
public u_LightWrapAround,
public u_LightGridOrigin,
public u_LightGridScale,
Expand Down Expand Up @@ -2266,7 +2251,7 @@ class GLShader_forwardLighting_omniXYZ :
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_NormalScale,
public GLDeformStage,
public GLCompileMacro_USE_VERTEX_SKINNING,
public GLCompileMacro_USE_VERTEX_ANIMATION,
Expand Down Expand Up @@ -2305,7 +2290,7 @@ class GLShader_forwardLighting_projXYZ :
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_NormalScale,
public GLDeformStage,
public GLCompileMacro_USE_VERTEX_SKINNING,
public GLCompileMacro_USE_VERTEX_ANIMATION,
Expand Down Expand Up @@ -2346,7 +2331,7 @@ class GLShader_forwardLighting_directionalSun :
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_NormalScale,
public GLDeformStage,
public GLCompileMacro_USE_VERTEX_SKINNING,
public GLCompileMacro_USE_VERTEX_ANIMATION,
Expand Down Expand Up @@ -2395,7 +2380,6 @@ class GLShader_reflection :
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalScale,
public u_NormalFormat,
public u_VertexInterpolation,
public GLDeformStage,
public GLCompileMacro_USE_VERTEX_SKINNING,
Expand Down Expand Up @@ -2472,7 +2456,7 @@ class GLShader_heatHaze :
public u_ColorModulate,
public u_Color,
public u_Bones,
public u_NormalFormat,
public u_NormalScale,
public u_VertexInterpolation,
public GLDeformStage,
public GLCompileMacro_USE_VERTEX_SKINNING,
Expand Down Expand Up @@ -2602,7 +2586,6 @@ class GLShader_liquid :
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalScale,
public u_NormalFormat,
public u_FogDensity,
public u_FogColor,
public u_SpecularExponent,
Expand Down
6 changes: 5 additions & 1 deletion src/engine/renderer/glsl_source/liquid_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ void main()
float fresnel = clamp(u_FresnelBias + pow(1.0 - dot(viewDir, normal), u_FresnelPower) *
u_FresnelScale, 0.0, 1.0);

texScreen += u_NormalScale * normal.xy;
// HACK: 0 normal Z channel can't be good
if (u_NormalScale.z != 0)
{
texScreen *= u_NormalScale;
}

vec3 refractColor = texture2D(u_CurrentMap, texScreen).rgb;
vec3 reflectColor = texture2D(u_PortalMap, texScreen).rgb;
Expand Down
40 changes: 9 additions & 31 deletions src/engine/renderer/glsl_source/reliefMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,15 @@ uniform sampler2D u_NormalMap;
uniform int u_HeightMapInNormalMap;
#endif // r_normalMapping || USE_PARALLAX_MAPPING

#if defined(r_normalMapping)
uniform vec3 u_NormalScale;
#endif // r_normalMapping

#if defined(USE_PARALLAX_MAPPING)
uniform float u_ParallaxDepthScale;
uniform float u_ParallaxOffsetBias;
#endif // USE_PARALLAX_MAPPING

#if defined(r_normalMapping)
uniform vec3 u_NormalFormat;

vec3 normalFlip(vec3 normal)
{
// undefined (zero) means default means 1.0 means do nothing

if (u_NormalFormat.x < 0.0)
{
normal.x *= -1.0;
}

if (u_NormalFormat.y < 0.0)
{
normal.y *= -1.0;
}

if (u_NormalFormat.z < 0.0)
{
normal.z *= -1.0;
}

return normal;
}
#endif // r_normalMapping

// compute normal in tangent space
vec3 NormalInTangentSpace(vec2 texNormal)
{
Expand Down Expand Up @@ -85,11 +63,11 @@ vec3 NormalInTangentSpace(vec2 texNormal)
normal = 2.0 * normal - 1.0;
}

normal = normalFlip(normal);

#if defined(r_NormalScale)
normal.z *= r_NormalScale;
#endif
// HACK: 0 normal Z channel can't be good
if (u_NormalScale.z != 0)
{
normal *= u_NormalScale;
}
#else // !r_normalMapping
normal = vec3(0.5, 0.5, 1.0);
#endif // !r_normalMapping
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
r_specularScale = ri.Cvar_Get( "r_specularScale", "1.0", CVAR_CHEAT | CVAR_LATCH );
r_specularMapping = ri.Cvar_Get( "r_specularMapping", "1", CVAR_LATCH );
r_deluxeMapping = ri.Cvar_Get( "r_deluxeMapping", "1", CVAR_ARCHIVE );
r_normalScale = ri.Cvar_Get( "r_normalScale", "1.0", CVAR_LATCH );
r_normalScale = ri.Cvar_Get( "r_normalScale", "1.0", CVAR_ARCHIVE );
r_normalMapping = ri.Cvar_Get( "r_normalMapping", "1", CVAR_ARCHIVE );
r_highQualityNormalMapping = ri.Cvar_Get( "r_highQualityNormalMapping", "0", CVAR_LATCH );
r_parallaxDepthScale = ri.Cvar_Get( "r_parallaxDepthScale", "0.03", CVAR_CHEAT );
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 @@ -1121,7 +1121,11 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
expression_t fresnelScaleExp;
expression_t fresnelBiasExp;

expression_t normalScaleExp;
// normalMap channel scale, negative value flips channel
bool hasNormalScale;
vec3_t normalScale;

expression_t normalIntensityExp;

expression_t etaExp;
expression_t etaDeltaExp;
Expand Down Expand Up @@ -1210,8 +1214,6 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
float parallaxOffsetBias; // offset the heightmap top relatively to the floor
float parallaxDepthScale; // per-shader parallax depth scale

vec3_t normalFormat; // normalmap format (channel flip)

bool noShadows;
bool fogLight;
bool blendLight;
Expand Down
Loading