diff options
author | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-11-30 14:04:26 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-11-30 14:29:01 +0000 |
commit | e56be70aed73a849aeb7cfbf76b594c5e8dd8941 (patch) | |
tree | 951d22e45a72f2daa129cd5fbb8cd47b57b7b72e /src/render/shaders | |
parent | 07fcd52d9588eaa14e46cef743fd4c8e14599139 (diff) |
Add shader fragment for basic lighting
Simple diffuse lighting for point and direction lights for now.
Not used from anywhere yet.
Change-Id: I20f7ddad9ad5c78f120d47efe8599a2dbe015e65
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/render/shaders')
-rw-r--r-- | src/render/shaders/light.inc.frag | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/render/shaders/light.inc.frag b/src/render/shaders/light.inc.frag new file mode 100644 index 000000000..4db64100d --- /dev/null +++ b/src/render/shaders/light.inc.frag @@ -0,0 +1,33 @@ +const int MAX_LIGHTS = 8; +struct Light { + vec3 position; + vec3 color; + float intensity; + vec3 direction; + vec3 attenuation; +// float cutOffAngle; +}; +uniform Light lights[MAX_LIGHTS]; +uniform int lightCount; + +vec3 lightColor(vec3 vpos, vec3 vnormal) +{ + vec3 n = normalize( vnormal ); + vec3 c = vec3( 0.0 ); + int i; + for (i = 0; i < lightCount; ++i) { + vec3 lightDir = lights[i].direction; + float att = 1.0; + if (length( lightDir ) == 0.0) { + lightDir = lights[i].position - vpos; + if (length( lights[i].attenuation ) != 0.0) { + float dist = length(lightDir); + att = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * dist + lights[i].attenuation.z * dist * dist); + } + } + vec3 s = normalize( lightDir ); + float diffuse = max( dot( s, n ), 0.0 ); + c += att * (diffuse * lights[i].color) * lights[i].intensity; + } + return c; +} |