summaryrefslogtreecommitdiffstats
path: root/shader/default.frag
blob: b39b46d92de7fe8809f1844f0a87d812ff3ea12c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
uniform sampler2D tex;

varying vec4 diffuse, ambientGlobal, ambient;
varying vec3 normal, pos;
/*varying vec3 normal, lightVector, reflectVector, eyeVector;*/


void main() {
  vec3 n, l, refl, eye;
  float NdotL, RdotE;
  vec4 color, specularColor;
  float dist, att, specularFactor;
  
  color = ambientGlobal;
  
  n = normalize(normal);
  
  l = gl_LightSource[0].position.xyz - pos;
  
  dist = length(l);
  l /= dist;
  
  /* compute the dot product between normal and normalized lightdir */
  NdotL = max(dot(n, l), 0.0);

  if (NdotL > 0.0) {
    att = 1.0 / (gl_LightSource[0].constantAttenuation +
        gl_LightSource[0].linearAttenuation * dist +
        gl_LightSource[0].quadraticAttenuation * dist * dist);
    color += att * (diffuse * NdotL + ambient);
    
    refl = normalize(reflect(-l, n));
    eye = normalize(-pos);
    
    RdotE = max(dot(refl, eye), 0.0);
    specularFactor = att * pow(RdotE, gl_FrontMaterial.shininess);
    specularColor = specularFactor * gl_FrontMaterial.specular * gl_LightSource[0].specular;
  }
  else {
    specularColor = vec4(0, 0, 0, 1);
  }
  
  gl_FragColor = color * texture2D(tex, gl_TexCoord[0].st) + specularColor;
}