Skip to content

Instantly share code, notes, and snippets.

@dmoa
Created March 29, 2020 20:31
Show Gist options
  • Save dmoa/f7f970d22ad0bf7fca3d0704c7e74370 to your computer and use it in GitHub Desktop.
Save dmoa/f7f970d22ad0bf7fca3d0704c7e74370 to your computer and use it in GitHub Desktop.
Attempt at lighting shader
#define M_PI 3.1415926535897932384626433832795
#define NUM_LIGHTS 32
struct Light {
vec2 position;
// color of the light
vec3 diffuse;
// power of the light
float power;
};
uniform Light lights[NUM_LIGHTS];
uniform int num_lights;
uniform vec2 screen;
const float constant = 1.0;
const float linear = 0.09;
const float quadratic = 0.032;
uniform sampler2D texture;
void main()
{
vec2 norm_screen = vec2(gl_TexCoord[0].x, gl_TexCoord[0].y);
vec3 diffuse = vec3(0);
for (int i = 0; i < num_lights; i++) {
Light light = lights[i];
vec2 norm_pos = light.position / screen;
float distance = length(norm_pos - norm_screen) * light.power;
float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
diffuse += light.diffuse * attenuation;
}
diffuse = clamp(diffuse, 0.0, 1.0);
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
gl_FragColor = vec4(diffuse, 1.0);
}
@dmoa
Copy link
Author

dmoa commented Mar 29, 2020

c++: shader.setUniform("screen", sf::Glsl::Vec2(500.0f, 500.0f)); shader.setUniform("texture", shapeTexture); shader.setUniform("num_lights", 1); shader.setUniform("lights[0].position", sf::Glsl::Vec2(250.0f, 250.0f)); shader.setUniform("lights[0].diffuse", sf::Glsl::Vec3(1.0, 1.0, 1.0)); shader.setUniform("lights[0].power", 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment