#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 }