/*
 * (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.
 */

#if defined(lit_shader)
	varying vec3 light_dir;
	varying vec3 eye_dir;

	attribute vec3 tangent;

	void main(void)
	{
		/* Transform the vertex into eye-space.
		 */
		vec3 vert_pos = vec3(gl_ModelViewMatrix * gl_Vertex);

	    
		/* gl_LightSource[0].position is the eye-space position of the light.  By
		 * subtracting the eye-space position of the vertex, we obtain a vector
		 * from the vertex to the light.
		 */
		vec3 light = gl_LightSource[0].position.xyz - vert_pos;


		/* vec3(0.0, 0.0, 0.0) is the eye-space position of the eye.  By
		 * subtracting the eye-space position of the vertex, we obtain a vector
		 * from the vertex to the eye.
		 */
		vec3 ep = -vert_pos;


		/* Calculate an orthonormal basis for the vertex in surface-space.
		 */
		vec3 t = normalize(gl_NormalMatrix * tangent);
		vec3 n = normalize(gl_NormalMatrix * gl_Normal);
		vec3 b = cross(n, t);

		/* Transform the vertex-light vector to surface-space.
		 */
		vec3 lv = vec3(dot(light, t), dot(light, b), dot(light, n));
		light_dir = normalize(lv);


		/* Transform the vertex-eye vector to surface-space.
		 */
		vec3 ev = vec3(dot(ep, t), dot(ep, b), dot(ep, n));
		eye_dir = normalize(ev);

		gl_Position = gl_ProjectionMatrix * vec4(vert_pos, 1.0);
		gl_FrontColor = gl_Color;
	}
#endif //lit_shader

#if defined(amb_shader)
	varying vec3 light_dir;
	varying vec3 eye_dir;

	attribute vec3 tangent;

	void main(void)
	{
		/* Transform the vertex into eye-space.
		 */
		vec3 vert_pos = vec3(gl_ModelViewMatrix * gl_Vertex);

	    
		/* gl_LightSource[0].position is the eye-space position of the light.  By
		 * subtracting the eye-space position of the vertex, we obtain a vector
		 * from the vertex to the light.
		 */
		vec3 light = gl_LightSource[0].position.xyz - vert_pos;


		/* vec3(0.0, 0.0, 0.0) is the eye-space position of the eye.  By
		 * subtracting the eye-space position of the vertex, we obtain a vector
		 * from the vertex to the eye.
		 */
		vec3 ep = -vert_pos;


		/* Calculate an orthonormal basis for the vertex in surface-space.
		 */
		vec3 t = normalize(gl_NormalMatrix * tangent);
		vec3 n = normalize(gl_NormalMatrix * gl_Normal);
		vec3 b = cross(n, t);

		/* Transform the vertex-light vector to surface-space.
		 */
		vec3 lv = vec3(dot(light, t), dot(light, b), dot(light, n));
		light_dir = normalize(lv);


		/* Transform the vertex-eye vector to surface-space.
		 */
		vec3 ev = vec3(dot(ep, t), dot(ep, b), dot(ep, n));
		eye_dir = normalize(ev);

		gl_Position = gl_ProjectionMatrix * vec4(vert_pos, 1.0);
		gl_FrontColor = gl_Color;
	}
#endif //amb_shader

#if defined(shv_shader)
	attribute vec3 tangent;

	void main(void)
	{
		vec3 light_dir;
		vec3 eye_dir;
	
		/* Transform the vertex into eye-space.
		 */
		vec3 vert_pos = vec3(gl_ModelViewMatrix * gl_Vertex);

		/* gl_LightSource[0].position is the eye-space position of the light.  By
		 * subtracting the eye-space position of the vertex, we obtain a vector
		 * from the vertex to the light.
		 */
		vec3 light = gl_LightSource[0].position.xyz - vert_pos;
		/* Calculate an orthonormal basis for the vertex in surface-space.
		 */

		/* vec3(0.0, 0.0, 0.0) is the eye-space position of the eye.  By
		 * subtracting the eye-space position of the vertex, we obtain a vector
		 * from the vertex to the eye.
		 */
		vec3 ep = -vert_pos;

		vec3 t = normalize(gl_NormalMatrix * tangent);
		vec3 n = normalize(gl_NormalMatrix * gl_Normal);
		vec3 b = cross(n, t);

		/* Transform the vertex-light vector to surface-space.
		 */
		vec3 lv = vec3(dot(light, t), dot(light, b), dot(light, n));
		light_dir = normalize(lv);


		/* Transform the vertex-eye vector to surface-space.
		 */
		vec3 ev = vec3(dot(ep, t), dot(ep, b), dot(ep, n));
		eye_dir = normalize(ev);

		//Project Vertex Away from Light if not lit
		if (light_dir.z < 0.0) {
			vert_pos -= 100.0 * light;
		}

		gl_Position = gl_ProjectionMatrix * vec4(vert_pos, 1.0);
		gl_FrontColor = gl_Color;
	}
#endif //shv_shader