/*
 * (C) Copyright Ian D. Romanick 2007
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

varying vec3 light_dir;
varying vec3 eye_pos;

uniform vec3 amb_color;

vec3 checker_floor(vec2 tc, vec3 color_on, vec3 color_off)
{
    vec2 parity = floor(mod(tc, 2.0));

#if 1
    if (parity.x == 1.0 && parity.y == 0.0) {
	parity = floor(mod(tc * 10.0, 2.0));
    }
#endif

    float parameter = abs((parity.x + parity.y) - 1.0);
    float lerp_param = smoothstep(0.48, 0.52, parameter);

    return mix(color_on, color_off, lerp_param);
}

#if defined(lit_shader)
	void main(void)
	{
		float dist = length(eye_pos);
#if 0
		float f_temp = gl_Fog.density * dist;
# if 1
		float f = exp(-(f_temp * f_temp));
# else
		float f = exp(-f_temp);
# endif
#else
		float f = (gl_Fog.end - dist) / (gl_Fog.end - gl_Fog.start);
#endif

		float fog_factor = clamp(f, 0.0, 1.0);
		vec3 C;

		if (fog_factor > 0.0) {
			vec3 Ct = checker_floor(gl_TexCoord[0].st, gl_Color.rgb, vec3(0.5));

			C = Ct * gl_LightModel.ambient.rgb;
			C += Ct * max(normalize(light_dir).z, 0.0);
		}

		C = mix(gl_Fog.color.rgb, C, fog_factor);
		gl_FragColor = vec4(C + amb_color, 1.0);
	}
#endif //lit_shader

#if defined(amb_shader)
	void main(void)
	{
		float dist = length(eye_pos);
#if 0
		float f_temp = gl_Fog.density * dist;
# if 1
		float f = exp(-(f_temp * f_temp));
# else
		float f = exp(-f_temp);
# endif
#else
		float f = (gl_Fog.end - dist) / (gl_Fog.end - gl_Fog.start);
#endif

		float fog_factor = clamp(f, 0.0, 1.0);
		vec3 C;

		if (fog_factor > 0.0) {
			vec3 Ct = checker_floor(gl_TexCoord[0].st, gl_Color.rgb, vec3(0.5));

			C = Ct * gl_LightModel.ambient.rgb;
			C += Ct * max(normalize(light_dir).z, 0.0);
		}

		C = mix(gl_Fog.color.rgb, C, fog_factor);
		gl_FragColor = vec4((C * 0.25) + amb_color, 1.0);
	}
#endif //amb_shader

#if defined(shv_shader)
	void main(void)
	{
	}
#endif //shv_shader