diff options
author | Matt Turner <mattst88@gmail.com> | 2012-12-11 12:34:57 -0800 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2012-12-11 12:34:57 -0800 |
commit | 18b7caedb71a56381fafaa724ad3c9fa2dc8e465 (patch) | |
tree | 3565888e7a076a5097add2aa935dba9e8937d0a8 /data/simple.frag |
Initial import
Diffstat (limited to 'data/simple.frag')
-rw-r--r-- | data/simple.frag | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/data/simple.frag b/data/simple.frag new file mode 100644 index 0000000..89480f7 --- /dev/null +++ b/data/simple.frag @@ -0,0 +1,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 +} |