stk-code_catmod/data/shaders/pointlight.frag

50 lines
1.2 KiB
GLSL
Raw Normal View History

uniform sampler2D ntex;
2014-01-27 13:10:34 -05:00
uniform sampler2D dtex;
uniform float spec;
uniform mat4 invproj;
2014-03-10 18:29:37 -04:00
uniform mat4 ViewMatrix;
uniform vec2 screen;
flat in vec3 center;
flat in float energy;
flat in vec3 col;
out vec4 Diffuse;
out vec4 Specular;
vec3 DecodeNormal(vec2 n);
void main() {
2014-03-10 18:29:37 -04:00
vec2 texc = gl_FragCoord.xy / screen;
2014-01-27 13:10:34 -05:00
float z = texture(dtex, texc).x;
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, texc).xy - 1.));
vec4 xpos = 2.0 * vec4(texc, z, 1.0) - 1.0f;
xpos = invproj * xpos;
xpos /= xpos.w;
2014-01-22 16:22:54 -05:00
vec3 eyedir = normalize(xpos.xyz);
vec3 diffuse = vec3(0.), specular = vec3(0.);
2014-03-10 18:29:37 -04:00
vec4 pseudocenter = ViewMatrix * vec4(center.xyz, 1.0);
pseudocenter /= pseudocenter.w;
vec3 light_pos = pseudocenter.xyz;
vec3 light_col = col.xyz;
float d = distance(light_pos, xpos.xyz);
float att = energy * 200. / (4. * 3.14 * d * d);
float spec_att = (energy + 10.) * 200. / (4. * 3.14 * d * d);
// Light Direction
vec3 L = normalize(xpos.xyz - light_pos);
float NdotL = max(0.0, dot(norm, -L));
diffuse += NdotL * light_col * att;
// Reflected light dir
vec3 R = reflect(-L, norm);
float RdotE = max(0.0, dot(R, eyedir));
specular += pow(RdotE, spec) * light_col * spec_att;
Diffuse = vec4(diffuse, 1.);
Specular = vec4(specular , 1.);
}