Deferred Shading

Nibbie
Posts: 4
Joined: 2011.05
Post: #1
Hi,

I'm trying to get a deferred renderer working, and I'm able to store everything in the G-Buffer, but I can't get the lighting working. I found some sample code from WWDC10, but the lights it renders appear to have no attenuation (the light shines on an object no matter how far away). How could I get the lights to have a certain radius of affect?

Here's the lighting code:
Code:
uniform sampler2D normalTex, posTex, colorTex;
uniform vec3 light1Pos;
uniform vec3 light1Color;
uniform vec3 light2Pos;
uniform vec3 light2Color;
uniform vec3 light3Pos;
uniform vec3 light3Color;
uniform vec3 light4Pos;
uniform vec3 light4Color;

void main()
{
    vec4 color = texture2D(colorTex, gl_TexCoord[0].xy);
    
    if (color.a < 0.00001)
    {
        discard;
    }
    
    vec2 norm = texture2D(normalTex, gl_TexCoord[0].xy).xy;
    vec3 normal = vec3(norm.xy, 0.0);
    normal.z = sqrt(1.0 - norm.x * norm.x - norm.y * norm.y);
    
    vec3 position = texture2D(posTex, gl_TexCoord[0].xy).xyz;
    vec3 view = normalize(-position);
    
    vec3 l1 = normalize(light1Pos - position);
    float atten1 = max(0.0, dot(l1, normal));
    vec3 reflectv1 = normalize(reflect(-l1, normal));
    float spec1 = max(dot(reflectv1, view), 0.0);
    
    vec3 l2 = normalize(light2Pos - position);
    float atten2 = max(0.0, dot(l2, normal));
    vec3 reflectv2 = normalize(reflect(-l2, normal));
    float spec2 = max(dot(reflectv2, view), 0.0);
    
    vec3 l3 = normalize(light3Pos - position);
    float atten3 = max(0.0, dot(l3, normal));
    vec3 reflectv3 = normalize(reflect(-l3, normal));
    float spec3 = max(dot(reflectv3, view), 0.0);
    
    vec3 l4 = normalize(light4Pos - position);
    float atten4 = max(0.0, dot(l4, normal));
    vec3 reflectv4 = normalize(reflect(-l4, normal));
    float spec4 = max(dot(reflectv4, view), 0.0);
    
    gl_FragColor = color +
                    (vec4(min(pow(spec1, 32.0) + atten1 * light1Color, 1.0), 1) +
                    vec4(min(pow(spec2, 32.0) + atten2 * light2Color, 1.0), 1) +
                    vec4(min(pow(spec3, 32.0) + atten3 * light3Color, 1.0), 1) +
                    vec4(min(pow(spec4, 32.0) + atten4 * light4Color, 1.0), 1));
}

Thanks Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenCL deferred lighting demo Holmes 9 11,782 Feb 22, 2011 07:53 PM
Last Post: Holmes
  Irrlicht Gouraud/Smooth Shading merrill541 4 5,652 Feb 3, 2010 01:13 PM
Last Post: merrill541
  GLUT Shading merrill541 1 2,929 Oct 15, 2008 02:53 PM
Last Post: arekkusu
  open gl shading language brtnrdr 8 5,249 Dec 30, 2004 02:15 AM
Last Post: OneSadCookie
  Anyone playing with the OpenGL Shading Laguage? NYGhost 7 4,909 Aug 4, 2004 01:23 PM
Last Post: OneSadCookie