2 uniform samplerCube envmap;
3 uniform sampler2DShadow shadowmap;
5 varying vec3 vdir, ldir[3], normal;
6 varying vec4 shadow_tc;
9 #define KD gl_FrontMaterial.diffuse.rgb
10 #define KS gl_FrontMaterial.specular.rgb
11 #define SPOW gl_FrontMaterial.shininess
13 #define LD(i) gl_LightSource[i].diffuse.rgb
14 #define LS(i) gl_LightSource[i].specular.rgb
16 vec3 calc_diffuse(in vec3 n, in vec3 l, in vec3 lcol)
18 float ndotl = max(dot(n, l), 0.0);
19 return KD * lcol * ndotl;
22 vec3 calc_specular(in vec3 n, in vec3 l, in vec3 v, in vec3 lcol)
24 vec3 h = normalize(l + v);
25 float ndoth = max(dot(n, h), 0.0);
26 return KS * lcol * pow(ndoth, SPOW);
31 float shadow = shadow2DProj(shadowmap, shadow_tc).x;
33 vec3 n = normalize(normal);
34 vec3 v = normalize(vdir);
36 vec3 l = normalize(ldir[0]);
37 vec3 diffuse = calc_diffuse(n, l, LD(0)) * shadow;
38 vec3 specular = calc_specular(n, l, v, LS(0)) * shadow;
40 l = normalize(ldir[1]);
41 diffuse += calc_diffuse(n, l, LD(1));
42 specular += calc_specular(n, l, v, LS(1));
44 l = normalize(ldir[2]);
45 diffuse += calc_diffuse(n, l, LD(2));
46 specular += calc_specular(n, l, v, LS(2));
49 vec3 rdir = -reflect(wdir, n);
50 vec3 env_texel = textureCube(envmap, rdir).xyz;
51 specular += KS * env_texel;
53 vec3 ambient = gl_LightModel.ambient.rgb * KD;
54 gl_FragColor.rgb = ambient + diffuse + specular;
55 gl_FragColor.a = gl_FrontMaterial.diffuse.a;