forked from mckennapsean/code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture-p.frag
51 lines (38 loc) · 1.34 KB
/
texture-p.frag
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
45
46
47
48
49
50
51
// texture fragment shader with Phong shading
// grab lighting vars
varying vec3 ldir;
varying vec3 lhalf;
varying float ldist;
// grab the vertex normal
varying vec3 n;
// grab ambient, diffuse lighting terms
varying vec4 diff;
varying vec4 amb;
// from main program, grab the input texture
uniform sampler2D tex;
// from main program, get specular highlight value
uniform float s;
void main(){
// set the fragment color based on texture
vec4 col = texture2D(tex, gl_TexCoord[0].st);
// final color to output
vec4 c;
// set color to the global ambient
c = amb;
// renormalize for fragment
vec3 norm = normalize(n);
vec3 l = normalize(ldir);
vec3 h = lhalf;
// sets the diffuse darkness (dot product betwee normal and light)
float dotProd = max(dot(norm, l), 0.0);
// add diffuse & specular highlights if bright
if(dotProd > 0.0){
// calculate attenuation
float att = 1.0 / (gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * ldist + gl_LightSource[0].quadraticAttenuation * ldist * ldist);
// calculate diffuse and specular brightness
c += att * (diff * dotProd);
c += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(max(dot(norm, h), 0.0), gl_FrontMaterial.shininess * (2.50 - s));
}
// set the output color
gl_FragColor = c * c * col;
}