I’m trying to implement specular maps into my shader code, but I have little to no good tutorials on how to calculate it. So far, I’ve gone on a vague quest to the four corners of the internet, copying the specular component of the tutorials and tried to make some kind of sense from all of it. But so far, this is all I’ve got. Most of it is incorrect, I should warn you.
pointLight.fs:
void main(void) {
float a = 1.0 / length(light_position.xy - gl_FragCoord.xy);
vec4 attenuation = vec4(a, a, a, pow(a, 3.0));
vec3 n = normalize((texture2D(s_normal, pass_TextureUV) * 2.0 - 1.0).xyz);
vec3 l = normalize(vec3((light_position.xy - gl_FragCoord.xy) / resolution.xy, light_position.z));
vec4 c_specular = texture2D(s_specular, pass_TextureUV);
vec3 reflectionVector = reflect(-l, n);
vec3 surfaceToCamera = normalize(vec3(pass_Camera_Position) - vec3(pass_Position));
float cosAngle = max(0.0, dot(surfaceToCamera, reflectionVector));
float specularComponent = pow(cosAngle, 1);
out_Color = (light_color * max(dot(n, l), 0.0) * attenuation) + specularComponent;
}
pointLight.vs:
void main(void) {
gl_Position = projMatrix * viewMatrix * modelMatrix * in_Position;
pass_Position = modelMatrix * in_Position;
pass_Color = in_Color;
pass_TextureUV = in_TextureUV;
pass_Camera_Position = viewMatrix * vec4(0.0, 0.0, 0.0, 0.0) - modelMatrix * in_Position;
}
Final result with diffuse map (Which is done by a separate shader pass)
MP4 version: http://i.gyazo.com/c41238766703772b7dc634e99b5d3c32.mp4