0

I found a shader on the Internet which creates 2D lights.

What I'm curious about is that "How can I make the centre of the light less dense to be able to see other objects while still illuminating them?"

Here is the shader:

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform float screenHeight;

void main() {
    float distance = length(lightLocation - gl_FragCoord.xy);
    float attenuation = 1.0 / distance;
    vec4 color = vec4(attenuation, attenuation, attenuation, attenuation) * vec4(lightColor, 1);

    gl_FragColor = color;
}

This is how the light is rendered:

    glUseProgram(lightShaderProgram);
    glUniform2f(glGetUniformLocation(lightShaderProgram, "lightLocation"), location.getX(), location.getY());
    glUniform3f(glGetUniformLocation(lightShaderProgram, "lightColor"), red, green, blue);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);

    glBegin(GL_QUADS); {
        glVertex2f(0, 0);
        glVertex2f(0, Engine.getDisplayHeight());
        glVertex2f(Engine.getDisplayWidth(), Engine.getDisplayHeight());
        glVertex2f(Engine.getDisplayWidth(), 0);
    } glEnd();

This is an image of a light created with this shader and a red rectangle being illuminated by the light.

The centre is very dense.

From what I was able to understand from my Google searches, I guess there should be other variables in the shader but I couldn't figure out which one I need and how to implement them. Any help?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Sierox
  • 459
  • 3
  • 18

1 Answers1

1

You can tinker with float attenuation = 1.0 / distance; If you want more rapid drop in brightness with distance you can, for example, square it or if you want to make it dimmer in general then you can subtract some constant from the attenuation. For example, http://glsl.heroku.com/e#18242.1

JAre
  • 4,666
  • 3
  • 27
  • 45
  • How would I have **less** rapid drop in brightness? Squareroot? What I actually want is that the centre is so small that it doesn't create that HUGE circle. – Sierox Jul 09 '14 at 19:31
  • `float attenuation = pow(1.0/distance, 2);` makes everything dimmer. I want to make the CENTRE of the light INVISIBLE and still have BRIGHT light. Or, making very small centres emmit BRIGHTER lights would work since I could make the centre very small and still have light. – Sierox Jul 09 '14 at 19:53
  • Yep I am working on that, trying different functions :/ – Sierox Jul 09 '14 at 21:01
  • I have been searching for the right function for half an hour or so If anyone can find it, It would be very good :) – Sierox Jul 09 '14 at 21:48
  • @Sierox I added demo to my answer and deleted some comments. – JAre Jul 10 '14 at 05:54