1
General / Lighting shader creates a bright point at the source origin.
« on: August 02, 2021, 07:03:46 pm »
I was attempting to create a lighting shader, but am having issues where there is a bright point at the center of the source (I think its because there isn't enough of a gradient). I could be missing a setting or my shader could be poorly implemented, but I'm not sure how to fix this.
Any help would be greatly appreciated.
Here is my shader
------------------------
And some example implementation code
------------------------------------------------------
//libs: -lsfml-graphics -lsfml-system -lsfml-window
Any help would be greatly appreciated.
Here is my shader
------------------------
uniform vec2 size;
uniform vec2 origin;
void main() {
float light_strength = 1000.0;
vec2 frag_location = gl_FragCoord.xy;
// Flip over the x axis
frag_location.y = size.y - frag_location.y;
// Get the distance from the fragment to the source
float abs_distance = length(origin - frag_location);
// Calculate the attenuation based on the distance
// 1.0 / (1.0 + 1.0*d + 1.0*d^2)
float attenuation = 1.0 / (1.0 + (1.0 * abs_distance) + (1.0 * pow(abs_distance, 2.0))) * light_strength;
// Create a base color
vec4 light_color = vec4(1.0, 1.0, 1.0, 1.0);
// Change the color based on the attenuation
vec4 color = vec4(attenuation, attenuation, attenuation, 1.0) * light_color;
gl_FragColor = color;
}
uniform vec2 origin;
void main() {
float light_strength = 1000.0;
vec2 frag_location = gl_FragCoord.xy;
// Flip over the x axis
frag_location.y = size.y - frag_location.y;
// Get the distance from the fragment to the source
float abs_distance = length(origin - frag_location);
// Calculate the attenuation based on the distance
// 1.0 / (1.0 + 1.0*d + 1.0*d^2)
float attenuation = 1.0 / (1.0 + (1.0 * abs_distance) + (1.0 * pow(abs_distance, 2.0))) * light_strength;
// Create a base color
vec4 light_color = vec4(1.0, 1.0, 1.0, 1.0);
// Change the color based on the attenuation
vec4 color = vec4(attenuation, attenuation, attenuation, 1.0) * light_color;
gl_FragColor = color;
}
And some example implementation code
------------------------------------------------------
//libs: -lsfml-graphics -lsfml-system -lsfml-window
#include <SFML/Graphics.hpp>
#define width 600
#define height 600
int main() {
sf::RenderWindow window;
sf::Shader shader;
sf::RenderStates states;
window.create(sf::VideoMode(width, height), "");
// Create a shape to fill the screen
sf::RectangleShape fill_color(sf::Vector2f(0, 0));
fill_color.setSize(sf::Vector2f(width, height));
fill_color.setFillColor(sf::Color::White);
// Shader gets compiled by whatever SFML uses for it
shader.loadFromFile("shaders/lighting.frag", sf::Shader::Fragment);
// Set some origin point
shader.setUniform("size", sf::Vector2f(width, height));
shader.setUniform("origin", sf::Vector2f(300, 300));
states = &shader;
while (window.isOpen()) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
// Draws
window.clear();
window.draw(fill_color, states);
window.display();
}
}
#define width 600
#define height 600
int main() {
sf::RenderWindow window;
sf::Shader shader;
sf::RenderStates states;
window.create(sf::VideoMode(width, height), "");
// Create a shape to fill the screen
sf::RectangleShape fill_color(sf::Vector2f(0, 0));
fill_color.setSize(sf::Vector2f(width, height));
fill_color.setFillColor(sf::Color::White);
// Shader gets compiled by whatever SFML uses for it
shader.loadFromFile("shaders/lighting.frag", sf::Shader::Fragment);
// Set some origin point
shader.setUniform("size", sf::Vector2f(width, height));
shader.setUniform("origin", sf::Vector2f(300, 300));
states = &shader;
while (window.isOpen()) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
// Draws
window.clear();
window.draw(fill_color, states);
window.display();
}
}