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.
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?