0
votes

I have a simple GLSL fragment shader that I'm using for 2D lights, is there any way to modify this to implement setting a light's radius and/or intensity? Currently the only modifications available are to the constant/linear/quadratic light attenuation.

Here is the current shader code (not updated to modern OpenGL)

uniform vec2 lightpos;
uniform vec4 lightColor;
uniform float screenHeight;
uniform vec3 lightAttenuation;
uniform float radius; //Doesn't do anything
uniform float intensity; //Doesn't do anything

uniform sampler2D texture;

void main()
{       
    vec2 pixel=gl_FragCoord.xy;     
    pixel.y=screenHeight-pixel.y;   
    vec2 aux=lightpos-pixel;
    float distance=length(aux);
    float attenuation=1.0/(lightAttenuation.x+lightAttenuation.y*distance+lightAttenuation.z*distance*distance);
    vec4 color=vec4(attenuation,attenuation,attenuation,1.0)*lightColor;
    gl_FragColor = color;//*texture2D(texture,gl_TexCoord[0].st);
}
1
Lights in the real world do not have a radius. What exactly do you want to happen with them at this arbitrary radius?Nicol Bolas
Intensity could be computed by the distance a normal has to the light position. Im sorry i can't provide any code but that should be the theory behind it.apoiat

1 Answers

0
votes

Intensity - just multiply light color by intensity parameter. You can also pass premultiplied light color to shader.

Radius. With quadratic attenuation light's influence is essentially unbounded. For practical purposes we can define radius as a max distance where light's influence is bigger than some arbitrary constant. This means that radius is defined by light's attenuation and intensity. Now depending on what are you trying to achieve you should tweak either attenuation or intensity in order to set desired range. Which also should be done before shader - just pass appropriate light color and attenuation values.