diff options
author | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-12-07 14:20:01 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-12-09 13:53:23 +0000 |
commit | 94f3df1aacab5853d09e79596eb3bdae3aecae0a (patch) | |
tree | 4db06240d22462becc9ad5b499926aa11316b88d /src/render/shaders/gl3 | |
parent | 3655c334b0f4a28a1487c918384da50498ef9418 (diff) |
Add support for spotlights
Change-Id: I61a4e072c1a2e00cdbcee917aa557e56fb8cb7c0
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/render/shaders/gl3')
-rw-r--r-- | src/render/shaders/gl3/light.inc.frag | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/src/render/shaders/gl3/light.inc.frag b/src/render/shaders/gl3/light.inc.frag index e9fcd127a..d3cce2edc 100644 --- a/src/render/shaders/gl3/light.inc.frag +++ b/src/render/shaders/gl3/light.inc.frag @@ -9,7 +9,7 @@ struct Light { float intensity; vec3 direction; vec3 attenuation; -// float cutOffAngle; + float cutOffAngle; }; uniform Light lights[MAX_LIGHTS]; uniform int lightCount; @@ -28,20 +28,24 @@ void adsModelNormalMapped(const in vec3 vpos, const in vec3 vnormal, const in ve for (i = 0; i < lightCount; ++i) { float att = 1.0; if ( lights[i].type != TYPE_DIRECTIONAL ) { - s = lights[i].position - vpos; + s = tangentMatrix * ( lights[i].position - vpos ); if (length( lights[i].attenuation ) != 0.0) { float dist = length(s); att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist); } + s = normalize( s ); + if ( lights[i].type == TYPE_SPOT ) { + if ( degrees(acos(dot(-s, normalize(lights[i].direction))) ) > lights[i].cutOffAngle) + att = 0.0; + } } else { - s = -lights[i].direction; + s = normalize( tangentMatrix * -lights[i].direction ); } - s = normalize( tangentMatrix * s ); float diffuse = max( dot( s, n ), 0.0 ); float specular = 0.0; - if (diffuse > 0.0 && shininess > 0.0) { + if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) { vec3 r = reflect( -s, n ); vec3 v = normalize( tangentMatrix * ( eye - vpos ) ); float normFactor = ( shininess + 2.0 ) / 2.0; @@ -49,7 +53,7 @@ void adsModelNormalMapped(const in vec3 vpos, const in vec3 vnormal, const in ve } diffuseColor += att * lights[i].intensity * diffuse * lights[i].color; - specularColor += specular; + specularColor += att * specular; } } @@ -71,15 +75,19 @@ void adsModel(const in vec3 vpos, const in vec3 vnormal, const in vec3 eye, cons float dist = length(s); att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist); } + s = normalize( s ); + if ( lights[i].type == TYPE_SPOT ) { + if ( degrees(acos(dot(-s, normalize(lights[i].direction))) ) > lights[i].cutOffAngle) + att = 0.0; + } } else { - s = -lights[i].direction; + s = normalize( -lights[i].direction ); } - s = normalize( s ); float diffuse = max( dot( s, n ), 0.0 ); float specular = 0.0; - if (diffuse > 0.0 && shininess > 0.0) { + if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) { vec3 r = reflect( -s, n ); vec3 v = normalize( eye - vpos ); float normFactor = ( shininess + 2.0 ) / 2.0; @@ -87,7 +95,7 @@ void adsModel(const in vec3 vpos, const in vec3 vnormal, const in vec3 eye, cons } diffuseColor += att * lights[i].intensity * diffuse * lights[i].color; - specularColor += specular; + specularColor += att * specular; } } @@ -107,11 +115,15 @@ void adModel(const in vec3 vpos, const in vec3 vnormal, out vec3 diffuseColor) float dist = length(s); att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist); } + s = normalize( s ); + if ( lights[i].type == TYPE_SPOT ) { + if ( degrees(acos(dot(-s, normalize(lights[i].direction))) ) > lights[i].cutOffAngle) + att = 0.0; + } } else { - s = -lights[i].direction; + s = normalize( -lights[i].direction ); } - s = normalize( s ); float diffuse = max( dot( s, n ), 0.0 ); diffuseColor += att * lights[i].intensity * diffuse * lights[i].color; |