Skip to content
5 changes: 5 additions & 0 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ static std::string GenEngineConstants() {
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
35 changes: 22 additions & 13 deletions src/engine/renderer/glsl_source/computeLight_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ uniform vec2 u_SpecularExponent;

// lighting helper functions
void computeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
vec4 diffuseColor, vec4 specularColor,
vec4 diffuseColor, vec4 materialColor,
inout vec4 accumulator ) {
vec3 H = normalize( lightDir + viewDir );
float NdotH = clamp( dot( normal, H ), 0.0, 1.0 );

#if defined(USE_PHYSICAL_SHADING)
float metalness = specularColor.x;
float roughness = specularColor.y;
#if defined(r_physicalMapping) && defined(USE_PHYSICAL_SHADING)
// Daemon PBR packing defaults to ORM like glTF 2.0 defines
// https://www.khronos.org/blog/art-pipeline-for-gltf
// > ORM texture for Occlusion, Roughness, and Metallic
// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/material.pbrMetallicRoughness.schema.json
// > The metalness values are sampled from the B channel. The roughness values are sampled from the G channel.
// > These values are linear. If other channels are present (R or A), they are ignored for metallic-roughness calculations.
// https://docs.blender.org/manual/en/2.80/addons/io_scene_gltf2.html
// > glTF stores occlusion in the red (R) channel, allowing it to optionally share the same image
// > with the roughness and metallic channels.
float roughness = materialColor.g;
float metalness = materialColor.b;

float NdotV = clamp( dot( normal, viewDir ), 0.0, 1.0);
float VdotH = clamp( dot( viewDir, H ), 0.0, 1.0);
Expand All @@ -80,7 +89,7 @@ void computeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
accumulator.xyz += lightColor.xyz * (1.0 - metalness) * NdotL * diffuseColor.xyz;
accumulator.xyz += lightColor.xyz * vec3((D * F * G) / (4.0 * NdotV));
accumulator.a = mix(diffuseColor.a, 1.0, FexpNV);
#else // !USE_PHYSICAL_SHADING
#else // !r_physicalMapping || !USE_PHYSICAL_SHADING
float NdotL = dot( normal, lightDir );
#if defined(r_HalfLambertLighting)
// http://developer.valvesoftware.com/wiki/Half_Lambert
Expand All @@ -93,10 +102,10 @@ void computeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
#endif

accumulator.xyz += diffuseColor.xyz * lightColor.xyz * NdotL;
#if defined(r_specularMapping)
accumulator.xyz += specularColor.xyz * lightColor.xyz * pow( NdotH, u_SpecularExponent.x * specularColor.w + u_SpecularExponent.y) * r_SpecularScale;
#endif // r_specularMapping
#endif // USE_PHYSICAL_SHADING
#if defined(r_specularMapping) && !defined(USE_PHYSICAL_SHADING)
accumulator.xyz += materialColor.xyz * lightColor.xyz * pow( NdotH, u_SpecularExponent.x * materialColor.w + u_SpecularExponent.y) * r_SpecularScale;
#endif // r_specularMapping && !USE_PHYSICAL_SHADING&
#endif // !r_physicalMapping || !USE_PHYSICAL_SHADING
}

#if defined(TEXTURE_INTEGER)
Expand Down Expand Up @@ -129,7 +138,7 @@ int nextIdx( inout idxs_t idxs ) {
const int numLayers = MAX_REF_LIGHTS / 256;

void computeDLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
vec4 specular, inout vec4 color ) {
vec4 material, inout vec4 color ) {
vec4 center_radius = GetLight( idx, center_radius );
vec4 color_type = GetLight( idx, color_type );
vec3 L;
Expand Down Expand Up @@ -157,10 +166,10 @@ void computeDLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
}
computeLight( L, normal, viewDir,
attenuation * attenuation * color_type.xyz,
diffuse, specular, color );
diffuse, material, color );
}

void computeDLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 specular,
void computeDLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
inout vec4 color ) {
vec2 tile = floor( gl_FragCoord.xy * (1.0 / float( TILE_SIZE ) ) ) + 0.5;
vec3 tileScale = vec3( r_tileStep, 1.0/numLayers );
Expand All @@ -185,7 +194,7 @@ void computeDLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 specu
return;
}

computeDLight( idx, P, normal, viewDir, diffuse, specular, color );
computeDLight( idx, P, normal, viewDir, diffuse, material, color );

#if defined(r_showLightTiles)
numLights++;
Expand Down
8 changes: 6 additions & 2 deletions src/engine/renderer/glsl_source/forwardLighting_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#endif

uniform sampler2D u_DiffuseMap;
uniform sampler2D u_SpecularMap;
uniform sampler2D u_MaterialMap;
uniform sampler2D u_AttenuationMapXY;
uniform sampler2D u_AttenuationMapZ;

Expand Down Expand Up @@ -975,11 +975,13 @@ void main()
}
diffuse.rgb *= u_LightColor * NL;

#if !defined(USE_PHYSICAL_SHADING)
Comment thread
illwieckz marked this conversation as resolved.
#if defined(r_specularMapping)
// compute the specular term
vec4 spec = texture2D(u_SpecularMap, texCoords).rgba;
vec4 spec = texture2D(u_MaterialMap, texCoords).rgba;
vec3 specular = spec.rgb * u_LightColor * pow(clamp(dot(normal, H), 0.0, 1.0), u_SpecularExponent.x * spec.a + u_SpecularExponent.y) * r_SpecularScale;
#endif // r_specularMapping
#endif // !USE_PHYSICAL_SHADING

// compute light attenuation
#if defined(LIGHT_PROJ)
Expand All @@ -998,9 +1000,11 @@ void main()
// compute final color
vec4 color = diffuse;

#if !defined(USE_PHYSICAL_SHADING)
#if defined(r_specularMapping)
color.rgb += specular;
#endif // r_specularMapping
#endif // !USE_PHYSICAL_SHADING

#if !defined(LIGHT_DIRECTIONAL)
color.rgb *= attenuationXY;
Expand Down
10 changes: 5 additions & 5 deletions src/engine/renderer/glsl_source/lightMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

/* lightMapping_fp.glsl */
uniform sampler2D u_DiffuseMap;
uniform sampler2D u_SpecularMap;
uniform sampler2D u_MaterialMap;
uniform sampler2D u_GlowMap;
uniform sampler2D u_LightMap;
uniform sampler2D u_DeluxeMap;
Expand Down Expand Up @@ -66,8 +66,8 @@ void main()
return;
}

// compute the specular term
vec4 specular = texture2D(u_SpecularMap, texCoords);
// compute the material term
vec4 material = texture2D(u_MaterialMap, texCoords);

// compute normal in world space from normalmap
vec3 normal = NormalInWorldSpace(texCoords, tangentToWorldMatrix);
Expand All @@ -88,13 +88,13 @@ void main()
lightColor /= clamp(dot(normalize(var_Normal), L), 0.004, 1.0);

// compute final color
computeLight( L, normal, viewDir, lightColor, diffuse, specular, color );
computeLight( L, normal, viewDir, lightColor, diffuse, material, color );
#else // !USE_DELUXE_MAPPING
// normal/deluxe mapping is disabled
color.xyz += lightColor.xyz * diffuse.xyz;
#endif // USE_DELUXE_MAPPING

computeDLights( var_Position, normal, viewDir, diffuse, specular, color );
computeDLights( var_Position, normal, viewDir, diffuse, material, color );

#if defined(r_glowMapping)
color.rgb += texture2D(u_GlowMap, texCoords).rgb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/* vertexLighting_DBS_entity_fp.glsl */

uniform sampler2D u_DiffuseMap;
uniform sampler2D u_SpecularMap;
uniform sampler2D u_MaterialMap;
uniform sampler2D u_GlowMap;

uniform samplerCube u_EnvironmentMap0;
Expand Down Expand Up @@ -97,7 +97,7 @@ void main()
#if defined(USE_REFLECTIVE_SPECULAR)
// not implemented for PBR yet

vec4 specBase = texture2D(u_SpecularMap, texCoords).rgba;
vec4 specBase = texture2D(u_MaterialMap, texCoords).rgba;

vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect(-viewDir, normal)).rgba;
vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect(-viewDir, normal)).rgba;
Expand All @@ -106,7 +106,7 @@ void main()

#else // USE_REFLECTIVE_SPECULAR
// simple Blinn-Phong
vec4 specBase = texture2D(u_SpecularMap, texCoords).rgba;
vec4 specBase = texture2D(u_MaterialMap, texCoords).rgba;

#endif // USE_REFLECTIVE_SPECULAR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/* vertexLighting_DBS_world_fp.glsl */

uniform sampler2D u_DiffuseMap;
uniform sampler2D u_SpecularMap;
uniform sampler2D u_MaterialMap;
uniform sampler2D u_GlowMap;

uniform float u_AlphaThreshold;
Expand Down Expand Up @@ -96,16 +96,16 @@ void main()
return;
}

vec4 specular = texture2D(u_SpecularMap, texCoords);
vec4 material = texture2D(u_MaterialMap, texCoords);

// compute normal in world space from normalmap
vec3 normal = NormalInWorldSpace(texCoords, tangentToWorldMatrix);

// compute final color
vec4 color = vec4( ambCol * diffuse.xyz, diffuse.a );
computeLight( L, normal, viewDir, dirCol, diffuse, specular, color );
computeLight( L, normal, viewDir, dirCol, diffuse, material, color );

computeDLights( var_Position, normal, viewDir, diffuse, specular, color );
computeDLights( var_Position, normal, viewDir, diffuse, material, color );

#if defined(r_glowMapping)
color.rgb += texture2D(u_GlowMap, texCoords).rgb;
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 @@ -158,6 +158,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cvar_t *r_offsetFactor;
cvar_t *r_offsetUnits;

cvar_t *r_physicalMapping;
cvar_t *r_specularExponentMin;
cvar_t *r_specularExponentMax;
cvar_t *r_specularScale;
Expand Down Expand Up @@ -1189,6 +1190,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
r_offsetFactor = ri.Cvar_Get( "r_offsetFactor", "-1", CVAR_CHEAT );
r_offsetUnits = ri.Cvar_Get( "r_offsetUnits", "-2", CVAR_CHEAT );

r_physicalMapping = ri.Cvar_Get( "r_physicalMapping", "0", CVAR_LATCH );
r_specularExponentMin = ri.Cvar_Get( "r_specularExponentMin", "0", CVAR_CHEAT );
r_specularExponentMax = ri.Cvar_Get( "r_specularExponentMax", "16", CVAR_CHEAT );
r_specularScale = ri.Cvar_Get( "r_specularScale", "1.0", CVAR_CHEAT | CVAR_LATCH );
Expand Down
12 changes: 7 additions & 5 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,9 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
TB_DIFFUSEMAP = TB_COLORMAP,
TB_REFLECTIONMAP = TB_COLORMAP,
TB_NORMALMAP,
TB_SPECULARMAP,
TB_MATERIALMAP = TB_SPECULARMAP,
TB_MATERIALMAP,
TB_PHYSICALMAP = TB_MATERIALMAP,
TB_SPECULARMAP = TB_MATERIALMAP,
TB_GLOWMAP,
MAX_TEXTURE_BUNDLES
};
Expand All @@ -1032,8 +1033,8 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
ST_GLOWMAP,
ST_DIFFUSEMAP,
ST_NORMALMAP,
ST_PHYSICALMAP,
ST_SPECULARMAP,
ST_MATERIALMAP,
ST_REFLECTIONMAP, // cubeMap based reflection
ST_REFRACTIONMAP,
ST_DISPERSIONMAP,
Expand All @@ -1043,8 +1044,8 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
ST_HEATHAZEMAP, // heatHaze post process effect
ST_LIQUIDMAP,
ST_LIGHTMAP,
ST_COLLAPSE_lighting_PHONG, // diffusemap + opt:normalmap + opt:glowmap + opt:specularmap
ST_COLLAPSE_lighting_PBR, // diffusemap + opt:normalmap + opt:glowmap + materialmap
ST_COLLAPSE_lighting_PBR, // map|diffusemap + opt:normalmap + opt:glowmap + opt:physicalmap
ST_COLLAPSE_lighting_PHONG, // map|diffusemap + opt:normalmap + opt:glowmap + specularmap
ST_COLLAPSE_reflection_CB, // color cubemap + normalmap

// light shader stage types
Expand Down Expand Up @@ -2844,6 +2845,7 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
extern cvar_t *r_offsetFactor;
extern cvar_t *r_offsetUnits;

extern cvar_t *r_physicalMapping;
extern cvar_t *r_specularExponentMin;
extern cvar_t *r_specularExponentMax;
extern cvar_t *r_specularScale;
Expand Down
Loading