Skip to content

Instantly share code, notes, and snippets.

@Shaptic
Last active January 3, 2019 20:29
Show Gist options
  • Save Shaptic/3809040 to your computer and use it in GitHub Desktop.
Save Shaptic/3809040 to your computer and use it in GitHub Desktop.
Point-lighting shader
#define MAX_LIGHTS 10 // Re-written on the fly
/**
* @file
* Fragment shader for lighting.
* Per-pixel point light lighting is done in this shader.
*
* @author me
* @addtogroup Shaders
* @{
*/
uniform sampler2D tex; // Active texture
uniform int scr_height; // Screen height
uniform vec2 light_pos[MAX_LIGHTS]; // Light position
uniform vec3 light_col[MAX_LIGHTS]; // Light color
uniform vec3 light_att[MAX_LIGHTS]; // Light attenuation
uniform float light_brt[MAX_LIGHTS]; // Light brightness
void main()
{
vec2 pixel = gl_FragCoord.xy;
pixel.y = scr_height - pixel.y;
vec3 lights;
for(int i = 0; i < MAX_LIGHTS; ++i)
{
vec2 light_vec = light_pos[i] - pixel;
float dist = length(light_vec);
float att = 1.0 / ( light_att[i].x +
(light_att[i].y * dist) +
(light_att[i].z * dist * dist));
lights += light_col[i] * att * light_brt[i];
}
vec4 texel = texture2D(tex, gl_TexCoord[0].st) * gl_Color;
gl_FragColor = texel * vec4(lights, 1.0);
}
/** @} */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment