# GLSL 2d Per Pixel Lighting

First time writing shaders, and having a bit of an issue getting per-pixel lighting to work. The problem is pretty simple, but I'm afraid am doing something wrong here.

#version 150

uniform mat4 proj;      // ortho projection matrix
uniform mat4 world;     // world transformation (usually only rotation)

in vec2 pos;            // position for this vertex

out vec2 ex_pos;        // pass position to frag shader

void main()
{
gl_Position = proj * world * vec4(pos, 0.0, 1.0);
ex_pos = pos;
}


#version 150

uniform vec2  light_pos;        // light position
uniform vec3  light_color;      // light color
uniform vec3  intensity;        // intensity value to apply to the light
uniform float attenuation_c;    // constant attenuation
uniform float attenuation_l;    // linear attenuation
uniform float attenuation_q;    // quadratic attenuation

in vec2 ex_pos;     // interpolated position

out vec4 frag;      // final fragment color value

void main()
{
// compute the current distance from light
float d = distance(light_pos, ex_pos);

// compute attenuation for this point
vec3 frag_intensity = intensity / (attenuation_c + attenuation_l*d + attenuation_q*pow(d,2) );

frag = vec4(frag_intensity * light_color, 1.0);
}


So, this seems pretty straightforward, but I am getting a hopelessly dark polygon (i.e., the additive fragment color always ends up 0,0,0). I have tested that simply setting d to any positive value causes the entire light polygon area to be uniformly lit, so I'm thinking that my distance computation is just wrong, but I'm not sure how it could be. I did have this working a bit earlier by doing vertex lighting instead of fragment, so I'm pretty sure my uniforms are being set correctly.

Is it the interpolated position that is causing a problem?

Thanks for any help.

• Just as some advice: Don't use linear attenuation. Instead use proper quadratic falloff and gamma correct rendering: See slides 4 and onward here slideshare.net/ozlael/hable-john-uncharted2-hdr-lighting . As for your question: Your code looks completely correct, so somethings wrong with the values that you're passing to it. – TravisG Oct 11 '13 at 3:37