Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

Vertex Shader:

#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;

Fragment Shader:

#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) );

    // blend mode is additive
    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.

share|improve this question
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

1 Answer 1

up vote 0 down vote accepted

So, I just messed around with the attenuation values I was providing and managed to get the lighting to work. It turns out the vertex lighting version was working because the light intensity at the point of the light was always near base value of the light, and the triangle fan far vertices were always zero, causing a linear light blend from the center out, which made it look mostly correct.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.