summaryrefslogtreecommitdiff
path: root/src/phong.frag
blob: 0d53e853c9f5b6a9e91f309d8cc6f52924b8a0cd (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
varying vec3 light_ss;
varying vec3 eye_ss;
varying vec2 uv_coord;

const vec3 base_color = vec3(1.0, 0.0, 0.0);

/* Specular exponent.
 */
uniform float s;

/* Adapted from the Orange Book, 3rd edition, chapter 11. */
vec3 perturbed_normal()
{
	const float BumpDensity = 30.0;
	const float BumpSize = 0.15;

	/* The incoming texture coordinate (uv_coord) ranges from 0 to 1.
	 * Multiplying by BumpDensity gives coordinates between 0 and 30.
	 * Then, fract gives us a value from 0 to 1...and subtracting 0.5
	 * gives us a perturbation value between -0.5 and 0.5.
	 */
	vec2 c = BumpDensity * uv_coord;
	vec2 p = fract(c) - vec2(0.5);

	/* If not in a bump, return an unperturbed normal */
	if (dot(p,p) >= BumpSize)
		return vec3(0,0,1);

	/* Otherwise, return the perturbed normal */
	return normalize(vec3(p, 1));
}

void main(void)
{
	vec3 l = normalize(light_ss);
	vec3 v = normalize(eye_ss);
	vec3 h = normalize(l + v);
	vec3 n = perturbed_normal();
	vec3 diff = base_color * dot(n, l);
	float spec = pow(dot(n, h), s);

	gl_FragColor = step(0.0, dot(n, l)) * vec4(diff + vec3(spec), 1.0);
}