From 471da3935d0ea3d37b86b13257b6a66070ce101c Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 29 Jan 2015 17:11:50 -0800 Subject: [PATCH] working on the lighting --- .../render-utils/src/DeferredLighting.slh | 1 - libraries/render-utils/src/point_light.slf | 42 +++++++++---------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index cd65fd1053..62d2e1e00f 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -92,5 +92,4 @@ vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, ve return diffuse * (ambientLight + diffuseLight); } - <@endif@> diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 599d10fbdb..e91a559eba 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -23,6 +23,8 @@ uniform float radius; uniform mat4 invViewMat; + + void main(void) { // get the depth and exit early if it doesn't pass the test vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; @@ -37,7 +39,6 @@ void main(void) { vec4 wPos; wPos = invViewMat * frag.position; Light light = getLight(); - gl_FragColor = vec4(frag.normal, 0.0); vec3 lightVector = getLightPosition(light) - wPos.xyz; if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { @@ -45,29 +46,24 @@ void main(void) { } float lightDistance = length(lightVector); - lightVector = lightVector / lightDistance; + vec3 lightDir = lightVector / lightDistance; float lightAttenuation = evalLightAttenuation(light, lightDistance); - gl_FragColor.xyz *= lightAttenuation; -/* - float diffuse = dot(frag.normal, lightVector); - - float diffuse = dot(normalizedNormal, lightVector); - float facingLight = step(0.0, diffuse); - vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + - gl_FrontLightProduct[1].diffuse * (diffuse * facingLight)); - - // compute attenuation based on distance, etc. - float attenuation = step(lightDistance, radius) / dot(vec3(gl_LightSource[1].constantAttenuation, - gl_LightSource[1].linearAttenuation, gl_LightSource[1].quadraticAttenuation), - vec3(1.0, lightDistance, lightDistance * lightDistance)); - - // add base to specular, modulate by attenuation - float specular = facingLight * max(0.0, dot(normalize(lightVector - normalize(vec4(position.xyz, 0.0))), - normalizedNormal)); - vec4 specularColor = texture2D(specularMap, texCoord); - gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); - */ - // gl_FragColor = vec4(frag.normal, 0.0); + vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); + vec3 eyeDir = normalize(wEyeVector.xyz); + vec3 wHalfDir = normalize(eyeDir + lightDir); + // Diffuse Lighting + float diffuseDot = dot(wNor.xyz, lightDir); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(eyeDir, wHalfDir)); + vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; + + // add specular contribution + gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); }