I have progressed to the lighting portion of my little framework and I managed to solve a few initial problems so now I'm left with the subtle weirdnesses.
At this stage I just have 2 lights - the ambient daylight and a hard-coded example light in my fragment shader (ultimately, I want to have multiple lights that I loop through adding together, and shadows would be nice too, but 1 step at a time!):
uniform sampler2D u_image;
varying vec2 v_texCoords;
varying vec4 v_position;
uniform vec3 ambient_light; // set as (0.3, 0.3, 0.3), night!
vec2 point_light_pos = vec2(-0.4, 0.3);
vec3 point_light_col = vec3(0.999, 0.999, 0.999);
float point_light_intensity = 0.4;
void main()
{
vec4 frag_color = texture2D(u_image, v_texCoords);
if(frag_color.a < 1.0)
discard;
float distance = distance(point_light_pos, v_position.xy);
float diffuse = 0.0;
if (distance <= point_light_intensity)
diffuse = 1.0 - abs(distance / point_light_intensity);
gl_FragColor = vec4(min(frag_color.rgb * ((point_light_col * diffuse) + ambient_light), frag_color.rgb), 1.0);
}
Which almost does what I want, but there are 2 problems:
The light has a bright ring in it and the light is squashed on the y-axis. I actually like how it looks squashed at this ratio, but I'd much rather do it deliberately.
I the y-axis squashing is down to the window aspect ratio - if the window is square, the light is circular, but I don't know how to fix this in the fragment shader.
I don't know where the bright ring is coming from.