1// $Id$
2//
3// Fragment shader for point lights
4//
5// Author: Dan Gudmundsson
6//
7
8#version 120
9
10#include "lib_base.glsl"
11#include "lib_normal.glsl"
12#include "lib_material.glsl"
13
14varying vec3 ws_position;
15uniform vec3 ws_eyepoint;
16uniform vec4 ws_lightpos;
17uniform vec4 light_diffuse;
18uniform vec4 light_specular;
19uniform vec3 light_att;
20uniform vec3 light_dir;
21uniform float light_angle;
22uniform float light_exp;
23uniform float Exposure;
24
25void main(void)
26{
27
28  float dist;
29  float att;
30  vec3 n = get_normal();
31  vec3 v = normalize(ws_eyepoint-ws_position);  // point to camera
32  vec3 l = ws_lightpos.xyz-ws_position;
33  dist = length(l);
34  l = normalize(l);
35  PBRInfo pbr = calc_views(n, v, l);
36  pbr = calc_material(pbr);
37
38  att = 1.0/(light_att.x+light_att.y*dist+light_att.z*dist*dist);
39  float spotDot = dot(-l, normalize(light_dir));
40  if(spotDot < light_angle)
41    att = 0.0;
42  else
43    att *= pow(spotDot, light_exp);
44
45  // Calculate the shading terms for the microfacet specular shading model
46  vec3  F = specularReflection(pbr);
47  float G = geometricOcclusion(pbr);
48  float D = microfacetDistribution(pbr);
49
50  // Calculation of analytical lighting contribution
51  vec3 diffuseContrib = (1.0 - max(max(F.r, F.g), F.b)) * diffuse(pbr);
52  diffuseContrib *= light_diffuse.xyz;
53  vec3 specContrib = F * G * D / (4.0 * pbr.NdotL * pbr.NdotV);
54  specContrib *= light_specular.xyz;
55  // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
56  vec3 frag_color = att * pbr.NdotL * (diffuseContrib + specContrib);
57  frag_color *= Exposure;
58  gl_FragColor = vec4(pow(frag_color, vec3(1.0/2.2)), pbr.opaque);
59}
60
61
62