diff options
author | SmileTheory <SmileTheory@gmail.com> | 2013-09-16 23:41:04 -0700 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2014-06-17 17:43:34 +0100 |
commit | b46c14676aca4fd8d013be565001c0a2f1fe4815 (patch) | |
tree | 7bcdfd18ef49dcfdb82b5dc85e1390b13543b208 /src/renderergl2/glsl | |
parent | a204b2fbd9b9efc8ae5a65c1fc3abdf1a3042849 (diff) |
OpenGL2: Some shader cleanup
Diffstat (limited to 'src/renderergl2/glsl')
-rw-r--r-- | src/renderergl2/glsl/lightall_fp.glsl | 56 | ||||
-rw-r--r-- | src/renderergl2/glsl/lightall_vp.glsl | 42 |
2 files changed, 56 insertions, 42 deletions
diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index 6aec2b4a..6dbfe963 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -261,6 +261,30 @@ vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float s return vec3(0.0); } + +float CalcLightAttenuation(vec3 dir, float sqrRadius) +{ + // point light at >0 radius, directional otherwise + float point = float(sqrRadius > 0.0); + + // inverse square light + float attenuation = sqrRadius / dot(dir, dir); + + // zero light at radius, approximating q3 style + // also don't attenuate directional light + attenuation = (0.5 * attenuation - 1.5) * point + 1.0; + + // clamp attenuation + #if defined(NO_LIGHT_CLAMP) + attenuation *= float(attenuation > 0.0); + #else + attenuation = clamp(attenuation, 0.0, 1.0); + #endif + + return attenuation; +} + + void main() { vec3 L, N, E, H; @@ -290,30 +314,12 @@ void main() #endif vec3 lightColor = lightSample.rgb; #elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT) - // inverse square light - float attenuation = u_LightRadius * u_LightRadius / dot(L, L); - - // zero light at radius, approximating q3 style - attenuation = 0.5 * attenuation - 0.5; - //attenuation = 0.0697168 * attenuation; - //attenuation *= step(0.294117, attenuation); - - // clamp attenuation - #if defined(NO_LIGHT_CLAMP) - attenuation *= step(0.0, attenuation); - #else - attenuation = clamp(attenuation, 0.0, 1.0); - #endif - - // don't attenuate directional light - attenuation = (attenuation - 1.0) * var_LightDir.w + 1.0; - - vec3 lightColor = u_DirectedLight * attenuation; + vec3 lightColor = u_DirectedLight * CalcLightAttenuation(L, u_LightRadius * u_LightRadius); vec3 ambientColor = u_AmbientLight; #elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT) vec3 lightColor = var_lightColor; #endif - + vec2 texCoords = var_DiffuseTex; #if defined(USE_PARALLAXMAP) @@ -360,9 +366,9 @@ void main() // surfaces not facing the light are always shadowed #if defined(USE_TANGENT_SPACE_LIGHT) - shadowValue *= step(0.0, var_PrimaryLightDir.z); + shadowValue *= float(var_PrimaryLightDir.z > 0.0); #else - shadowValue *= step(0.0, dot(var_Normal, var_PrimaryLightDir)); + shadowValue *= float(dot(var_Normal, var_PrimaryLightDir) > 0.0); #endif #if defined(SHADOWMAP_MODULATE) @@ -488,11 +494,13 @@ void main() reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess); reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, shininess); + lightColor = u_PrimaryLightColor; // * CalcLightAttenuation(L, u_PrimaryLightRadius * u_PrimaryLightRadius); + #if defined(USE_SHADOWMAP) - reflectance *= shadowValue; + lightColor *= shadowValue; #endif - gl_FragColor.rgb += u_PrimaryLightColor * reflectance * NL; + gl_FragColor.rgb += lightColor * reflectance * NL; #endif #if defined(USE_LINEAR_LIGHT) diff --git a/src/renderergl2/glsl/lightall_vp.glsl b/src/renderergl2/glsl/lightall_vp.glsl index d2bfb395..3ea822a4 100644 --- a/src/renderergl2/glsl/lightall_vp.glsl +++ b/src/renderergl2/glsl/lightall_vp.glsl @@ -130,6 +130,29 @@ vec2 ModTexCoords(vec2 st, vec3 position, vec4 texMatrix, vec4 offTurb) #endif +float CalcLightAttenuation(vec3 dir, float sqrRadius) +{ + // point light at >0 radius, directional otherwise + float point = float(sqrRadius > 0.0); + + // inverse square light + float attenuation = sqrRadius / dot(dir, dir); + + // zero light at radius, approximating q3 style + // also don't attenuate directional light + attenuation = (0.5 * attenuation - 1.5) * point + 1.0; + + // clamp attenuation + #if defined(NO_LIGHT_CLAMP) + attenuation *= float(attenuation > 0.0); + #else + attenuation = clamp(attenuation, 0.0, 1.0); + #endif + + return attenuation; +} + + void main() { #if defined(USE_VERTEX_ANIMATION) @@ -187,24 +210,7 @@ void main() #endif #if defined(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT) - // inverse square light - float attenuation = u_LightRadius * u_LightRadius / dot(L, L); - - // zero light at radius, approximating q3 style - attenuation = 0.5 * attenuation - 0.5; - //attenuation = 0.0697168 * attenuation; - //attenuation *= step(0.294117, attenuation); - - // clamp attenuation - #if defined(NO_LIGHT_CLAMP) - attenuation *= step(0.0, attenuation); - #else - attenuation = clamp(attenuation, 0.0, 1.0); - #endif - - // don't attenuate directional light - attenuation = (attenuation - 1.0) * u_LightOrigin.w + 1.0; - + float attenuation = CalcLightAttenuation(L, u_LightRadius * u_LightRadius); float NL = clamp(dot(normal, normalize(L)), 0.0, 1.0); var_Color.rgb *= u_DirectedLight * attenuation * NL + u_AmbientLight; |