From d8552f349f65d79e2fcfe644ffb54328010ee949 Mon Sep 17 00:00:00 2001 From: Ben Houston Date: Sat, 10 Oct 2015 22:14:08 -0400 Subject: [PATCH] introduce IncidentLight struct --- src/renderers/shaders/ShaderChunk/common.glsl | 48 +++++++++++-------- .../ShaderChunk/lights_lambert_vertex.glsl | 34 ++++++------- .../ShaderChunk/lights_phong_fragment.glsl | 30 ++++++------ 3 files changed, 59 insertions(+), 53 deletions(-) diff --git a/src/renderers/shaders/ShaderChunk/common.glsl b/src/renderers/shaders/ShaderChunk/common.glsl index b6b922d18c..8318e1529b 100644 --- a/src/renderers/shaders/ShaderChunk/common.glsl +++ b/src/renderers/shaders/ShaderChunk/common.glsl @@ -82,9 +82,15 @@ vec3 linearToOutput( in vec3 a ) { } -vec3 BRDF_Lambert( const in vec3 lightColor, const in vec3 lightDir, const in vec3 normal, const in vec3 diffuseColor ) { +struct IncidentLight { + vec3 color; + vec3 direction; +}; - return lightColor * diffuseColor * ( saturate( dot( normal, lightDir ) ) ); + +vec3 BRDF_Lambert( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 diffuseColor ) { + + return incidentLight.color * diffuseColor * ( saturate( dot( normal, incidentLight.direction ) ) ); // the above should be scaled by '' * RECIPROCAL_PI' } @@ -117,17 +123,17 @@ float D_BlinnPhong( const in float shininess, const in float dotNH ) { } -vec3 BRDF_BlinnPhong( const in vec3 lightColor, const in vec3 lightDir, const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float shininess ) { +vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float shininess ) { - vec3 halfDir = normalize( lightDir + viewDir ); + vec3 halfDir = normalize( incidentLight.direction + viewDir ); float dotNH = saturate( dot( normal, halfDir ) ); - float dotLH = saturate( dot( lightDir, halfDir ) ); + float dotLH = saturate( dot( incidentLight.direction, halfDir ) ); vec3 F = F_Schlick( specularColor, dotLH ); float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ ); float D = D_BlinnPhong( shininess, dotNH ); - return lightColor * F * ( G * D ); + return incidentLight.color * F * ( G * D ); } @@ -142,10 +148,10 @@ vec3 BRDF_BlinnPhong( const in vec3 lightColor, const in vec3 lightDir, const in uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ]; - void getDirLightDirect( const in DirectionalLight directionalLight, out vec3 lightColor, out vec3 lightDir ) { + void getDirLightDirect( const in DirectionalLight directionalLight, out IncidentLight incidentLight ) { - lightDir = directionalLight.direction; - lightColor = directionalLight.color; + incidentLight.color = directionalLight.color; + incidentLight.direction = directionalLight.direction; } @@ -162,15 +168,15 @@ vec3 BRDF_BlinnPhong( const in vec3 lightColor, const in vec3 lightDir, const in uniform PointLight pointLights[ MAX_POINT_LIGHTS ]; - void getPointLightDirect( const in PointLight pointLight, const in vec3 position, out vec3 lightColor, out vec3 lightDir ) { + void getPointLightDirect( const in PointLight pointLight, const in vec3 position, out IncidentLight incidentLight ) { vec3 lightPosition = pointLight.position; vec3 lVector = lightPosition - position; - lightDir = normalize( lVector ); + incidentLight.direction = normalize( lVector ); - lightColor = pointLight.color; - lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay ); + incidentLight.color = pointLight.color; + incidentLight.color *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay ); } @@ -190,18 +196,18 @@ vec3 BRDF_BlinnPhong( const in vec3 lightColor, const in vec3 lightDir, const in uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ]; - void getSpotLightDirect( const in SpotLight spotLight, const in vec3 position, out vec3 lightColor, out vec3 lightDir ) { + void getSpotLightDirect( const in SpotLight spotLight, const in vec3 position, out IncidentLight incidentLight ) { vec3 lightPosition = spotLight.position; vec3 lVector = lightPosition - position; - lightDir = normalize( lVector ); + incidentLight.direction = normalize( lVector ); - float spotEffect = dot( spotLight.direction, lightDir ); + float spotEffect = dot( spotLight.direction, incidentLight.direction ); spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) ); - lightColor = spotLight.color; - lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) ); + incidentLight.color = spotLight.color; + incidentLight.color *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) ); } @@ -218,15 +224,15 @@ vec3 BRDF_BlinnPhong( const in vec3 lightColor, const in vec3 lightDir, const in uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ]; - void getHemisphereLightIndirect( const in HemisphereLight hemiLight, const in vec3 normal, out vec3 lightColor, out vec3 lightDir ) { + void getHemisphereLightIndirect( const in HemisphereLight hemiLight, const in vec3 normal, out IncidentLight incidentLight ) { float dotNL = dot( normal, hemiLight.direction ); float hemiDiffuseWeight = 0.5 * dotNL + 0.5; - lightColor = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight ); + incidentLight.color = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight ); - lightDir = normal; + incidentLight.direction = normal; } diff --git a/src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl b/src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl index 53aeeeeadc..18d507af13 100644 --- a/src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl +++ b/src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl @@ -13,14 +13,14 @@ vec3 diffuse = vec3( 1.0 ); for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getPointLightDirect( pointLights[i], mvPosition.xyz, lightColor, lightDir ); + IncidentLight incidentLight; + getPointLightDirect( pointLights[ i ], mvPosition.xyz, incidentLight ); - vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + vLightFront += BRDF_Lambert( incidentLight, normal, diffuse ); #ifdef DOUBLE_SIDED - vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse ); + vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse ); #endif @@ -32,14 +32,14 @@ vec3 diffuse = vec3( 1.0 ); for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getSpotLightDirect( spotLights[i], mvPosition.xyz, lightColor, lightDir ); + IncidentLight incidentLight; + getSpotLightDirect( spotLights[ i ], mvPosition.xyz, incidentLight ); - vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + vLightFront += BRDF_Lambert( incidentLight, normal, diffuse ); #ifdef DOUBLE_SIDED - vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse ); + vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse ); #endif @@ -51,14 +51,14 @@ vec3 diffuse = vec3( 1.0 ); for ( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getDirLightDirect( directionalLights[i], lightColor, lightDir ); + IncidentLight incidentLight; + getDirLightDirect( directionalLights[ i ], incidentLight ); - vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + vLightFront += BRDF_Lambert( incidentLight, normal, diffuse ); #ifdef DOUBLE_SIDED - vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse ); + vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse ); #endif @@ -70,16 +70,16 @@ vec3 diffuse = vec3( 1.0 ); for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getHemisphereLightIndirect( hemisphereLights[ i ], normal, lightColor, lightDir ); + IncidentLight incidentLight; + getHemisphereLightIndirect( hemisphereLights[ i ], normal, incidentLight ); - vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + vLightFront += BRDF_Lambert( incidentLight, normal, diffuse ); #ifdef DOUBLE_SIDED - getHemisphereLightIndirect( hemisphereLights[ i ], -normal, lightColor, lightDir ); + incidentLight = getHemisphereLightIndirect( hemisphereLights[ i ], -normal ); - vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse ); + vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse ); #endif diff --git a/src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl b/src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl index e91f365248..e981fd67a2 100644 --- a/src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl +++ b/src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl @@ -17,14 +17,14 @@ vec3 diffuse = diffuseColor.rgb; for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getPointLightDirect( pointLights[i], -vViewPosition, lightColor, lightDir ); + IncidentLight incidentLight; + getPointLightDirect( pointLights[ i ], -vViewPosition, incidentLight ); totalDirectReflectedDiffuse += - BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + BRDF_Lambert( incidentLight, normal, diffuse ); totalDirectReflectedSpecular += - BRDF_BlinnPhong( lightColor, lightDir, normal, viewDir, specular, shininess ); + BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess ); } @@ -34,14 +34,14 @@ vec3 diffuse = diffuseColor.rgb; for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getSpotLightDirect( spotLights[i], -vViewPosition, lightColor, lightDir ); + IncidentLight incidentLight; + getSpotLightDirect( spotLights[ i ], -vViewPosition, incidentLight ); totalDirectReflectedDiffuse += - BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + BRDF_Lambert( incidentLight, normal, diffuse ); totalDirectReflectedSpecular += - BRDF_BlinnPhong( lightColor, lightDir, normal, viewDir, specular, shininess ); + BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess ); } @@ -51,14 +51,14 @@ vec3 diffuse = diffuseColor.rgb; for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getDirLightDirect( directionalLights[i], lightColor, lightDir ); + IncidentLight incidentLight; + getDirLightDirect( directionalLights[ i ], incidentLight ); totalDirectReflectedDiffuse += - BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + BRDF_Lambert( incidentLight, normal, diffuse ); totalDirectReflectedSpecular += - BRDF_BlinnPhong( lightColor, lightDir, normal, viewDir, specular, shininess ); + BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess ); } @@ -68,11 +68,11 @@ vec3 diffuse = diffuseColor.rgb; for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) { - vec3 lightColor, lightDir; - getHemisphereLightIndirect( hemisphereLights[ i ], normal, lightColor, lightDir ); + IncidentLight incidentLight; + getHemisphereLightIndirect( hemisphereLights[ i ], normal, incidentLight ); totalIndirectReflectedDiffuse += - BRDF_Lambert( lightColor, lightDir, normal, diffuse ); + BRDF_Lambert( incidentLight, normal, diffuse ); } -- GitLab