summaryrefslogtreecommitdiff
path: root/data/simple.frag
blob: 89480f75b7449d9429b01f4bf6041075b32b9303 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
#version 130
#extension GL_ARB_explicit_attrib_location: require

#define DO_LIGHTING 0

/* Debug mode.  This will cause the value supplied as 'normal_cs' to be
 * written as the output color.  The normal will be remapped from the usual
 * [-1,1] range to [0,1].
 */
#define SHOW_NORMALS 0

/* Approximate (u,v) at the current fragment.  Passed from the vertex shader,
 * this is used to determine the base color of the fragment.
 */
in vec2 frag_uv;

// Direction of the normal in camera-space.
in vec3 normal_cs;

// Direction to the light in camera-space.
uniform vec3 light_dir_cs = normalize(vec3(0., 1., 0.));

layout(location = 0) out vec4 color;

void main(void)
{
	/* Partition the surface into a checkerboard of 10x10 tiles that
	 * alternate colors.
	 */
	ivec2 tile = ivec2(floor(frag_uv * 10.));

	vec3 base_color = bool((tile.x ^ tile.y) & 1)
		? vec3(0.00784313679, 0.470588207, 0.996078372)
		: vec3(0.00784313679, 0.992156804, 0.00784313679);

	/* Renormalize the normal to account for interpolation errors.
	 */
	vec3 n_cs = normalize(normal_cs);

	/* If the polygon is back-facing, change the color and invert the
	 * normal.  This causes back-facing polygons to have different colors
	 * and be correctly lit.
	 */
	if (! gl_FrontFacing) {
		n_cs = -n_cs;
		base_color = base_color.zxy;
	}

#if DO_LIGHTING
	float diff = min(max(.2, dot(n_cs, light_dir_cs)), 1.);
	color = vec4(base_color * diff, 1.);
#elif SHOW_NORMALS
	color = vec4(normal_cs * .5 + .5, 1.);
#else
	color = vec4(base_color, 1.);
#endif
}