working on the spot light shader

This commit is contained in:
Sam Gateau 2015-02-02 11:17:35 -08:00
parent 56925cee00
commit 0da38c103f
3 changed files with 67 additions and 24 deletions

View file

@ -52,6 +52,9 @@ float getLightSquareRadius(Light l) {
return l._attenuation.w * l._attenuation.w; return l._attenuation.w * l._attenuation.w;
} }
float getLightAttenuationCutoff(Light l) {
return l._attenuation.z;
}
<@if GLPROFILE == PC_GL@> <@if GLPROFILE == PC_GL@>
uniform lightBuffer { uniform lightBuffer {

View file

@ -18,46 +18,44 @@
// Everything about light // Everything about light
<@include Light.slh@> <@include Light.slh@>
// The view Matrix
uniform mat4 invViewMat; uniform mat4 invViewMat;
void main(void) { void main(void) {
// get the depth and exit early if it doesn't pass the test // get the depth and exit early if it doesn't pass the test
vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q;
DeferredFragment frag = unpackDeferredFragment(texCoord); DeferredFragment frag = unpackDeferredFragment(texCoord);
// Kill if in front of the light volume
float depth = frag.depthVal; float depth = frag.depthVal;
if (depth < gl_FragCoord.z) { if (depth < gl_FragCoord.z) {
discard; discard;
} }
vec4 wPos; // Need the light now
wPos = invViewMat * frag.position;
Light light = getLight(); Light light = getLight();
// Make the Light vector going from fragment to light center in world space
vec4 wPos;
wPos = invViewMat * frag.position;
vec3 lightVector = getLightPosition(light) - wPos.xyz; vec3 lightVector = getLightPosition(light) - wPos.xyz;
// Kill if too far from the light center
if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { if (dot(lightVector, lightVector) > getLightSquareRadius(light)) {
discard; discard;
} }
// Allright we re valid in the volume
float lightDistance = length(lightVector); float lightDistance = length(lightVector);
vec3 lightDir = lightVector / lightDistance;
vec4 wNor = invViewMat * vec4(frag.normal, 0.0);
gl_FragColor = vec4(wNor.xyz, 0.0);
/*
float lightAttenuation = evalLightAttenuation(light, lightDistance); float lightAttenuation = evalLightAttenuation(light, lightDistance);
vec3 lightDir = lightVector / lightDistance;
vec4 wNor = invViewMat * vec4(frag.normal, 0.0); vec4 wNor = invViewMat * vec4(frag.normal, 0.0);
vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0);
vec3 eyeDir = normalize(wEyeVector.xyz); vec3 eyeDir = normalize(wEyeVector.xyz);
vec3 wHalfDir = normalize(eyeDir + lightDir); vec3 wHalfDir = normalize(eyeDir + lightDir);
// Diffuse Lighting // Diffuse Lighting
float diffuseDot = dot(wNor.xyz, lightDir); float diffuseDot = dot(wNor.xyz, lightDir);
float facingLight = step(0.0, diffuseDot); float facingLight = step(0.0, diffuseDot);
@ -69,6 +67,10 @@ void main(void) {
vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular;
// add specular contribution // add specular contribution
gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); float ring = (lightAttenuation / (0.2 * getLightAttenuationCutoff(light))) - 1;
*/ if (ring < 1) {
gl_FragColor = vec4(ring * ring * getLightColor(light), 0.0);
} else {
gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0);
}
} }

View file

@ -18,6 +18,7 @@
// Everything about light // Everything about light
<@include Light.slh@> <@include Light.slh@>
// The view Matrix
uniform mat4 invViewMat; uniform mat4 invViewMat;
void main(void) { void main(void) {
@ -26,30 +27,67 @@ void main(void) {
DeferredFragment frag = unpackDeferredFragment(texCoord); DeferredFragment frag = unpackDeferredFragment(texCoord);
// Kill if in front of the light volume
float depth = frag.depthVal; float depth = frag.depthVal;
if (depth < gl_FragCoord.z) { if (depth < gl_FragCoord.z) {
discard; discard;
} }
vec4 wPos; // Need the light now
wPos = invViewMat * frag.position;
Light light = getLight(); Light light = getLight();
vec3 lightVector = getLightPosition(light) - wPos.xyz; // Make the Light vector going from fragment to light center in world space
vec4 wPos;
wPos = invViewMat * frag.position;
vec3 lightVector = wPos.xyz - getLightPosition(light);
// Kill if too far from the light center
if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { if (dot(lightVector, lightVector) > getLightSquareRadius(light)) {
discard; discard;
} }
// Allright we re valid in the volume
float lightDistance = length(lightVector); float lightDistance = length(lightVector);
float lightAttenuation = evalLightAttenuation(light, lightDistance);
vec3 lightDir = lightVector / lightDistance; vec3 lightDir = lightVector / lightDistance;
gl_FragColor = vec4(getLightColor(light), 0.0);
// Kill if not in the spot light (ah ah !)
vec3 lightSpotDir = getLightDirection(light); vec3 lightSpotDir = getLightDirection(light);
// compute attenuation based on spot angle, distance, etc.
float cosSpotAngle = max(-dot(lightDir, lightSpotDir), 0.0); float cosSpotAngle = max(-dot(lightDir, lightSpotDir), 0.0);
if (cosSpotAngle < getLightSpotAngleCos(light)) { if (cosSpotAngle < getLightSpotAngleCos(light)) {
discard; // discard;
gl_FragColor = vec4(vec3(1.0, 0.0, 0.0), 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
/* float ring = (lightAttenuation / (0.2 * getLightAttenuationCutoff(light))) - 1;
if (ring < 1) {
gl_FragColor = vec4(ring * ring * getLightColor(light), 0.0);
} else {
gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0);
}*/
}
/* /*
float attenuation = step(lightDistance, radius) * step(gl_LightSource[1].spotCosCutoff, cosSpotAngle) * float attenuation = step(lightDistance, radius) * step(gl_LightSource[1].spotCosCutoff, cosSpotAngle) *
pow(cosSpotAngle, gl_LightSource[1].spotExponent) / dot(vec3(gl_LightSource[1].constantAttenuation, pow(cosSpotAngle, gl_LightSource[1].spotExponent) / dot(vec3(gl_LightSource[1].constantAttenuation,
@ -77,9 +115,9 @@ void main(void) {
// add specular contribution // add specular contribution
gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0);
*/ */
gl_FragColor = vec4(frag.normal, 0.0); // gl_FragColor = vec4(frag.normal, 0.0);
} //}
/* /*
// the radius (hard cutoff) of the light effect // the radius (hard cutoff) of the light effect
uniform float radius; uniform float radius;
@ -130,4 +168,4 @@ void main(void) {
gl_FragColor = vec4(frag.normal, 0.0); gl_FragColor = vec4(frag.normal, 0.0);
} }
*/ */