/******************************************************************************
 * The following code was generated by the mental mill(R)                     *
 *                                                                            *
 * Copyright 1986-2008 by mental images GmbH, Fasanenstr. 81, D-10623         *
 * Berlin, Germany. All rights reserved.                                      *
******************************************************************************/

//
// Values for the light_type parameter of light shaders
//
#define LIGHT_POINT    0
#define LIGHT_SPOT     1
#define LIGHT_INFINITE 2
#define LIGHT_PLANAR   3

//
// The light iterator structure holds the return values resulting from
// evaluating a light.
//
struct Light_iterator {
	float3 _point;
	float4 contribution;
	float4 raw_contribution;
	float  dot_nl;
	float3 direction;
	float  distance;
	float4 shadow;
	int    count;
};

struct Ray {
	float3 origin;
	float3 direction;
};

Ray __Ray_ctor(float3 o, float3 d)
{
	Ray r;
	r.origin = o;
	r.direction = d;
	return r;
}

float2 __float2_ctor(float v)
{
	return v.xx;
}

float3 __float3_ctor(float v)
{
	return v.xxx;
}

float4 __float4_ctor(float v)
{
	return v.xxxx;
}

float4 __tex2D_flip_v(sampler2D samp, float2 uv)
{
	return tex2D(samp, float2(uv.x, 1.0-uv.y));
}

float4 __tex2D_flip_v(sampler2D samp, float2 uv, float2 ddu, float2 ddv)
{
	return tex2D(samp, float2(uv.x, 1.0-uv.y), ddu, ddv);
}

void __make_basis(const float3 n, const float3 u, const float3 v,
        out float3 t, out float3 b)
{
    b = normalize(cross(u,n));
    t = cross(n,b);
    if (dot(b,v) < 0.0)  b = -b;
}

float3 __perspective_divide(float4 pt)
{
    return pt.xyz/pt.w;
}

#ifndef mi_phong_specular__Scalar__Vector3__Vector3__Vector3__Scalar
#define mi_phong_specular__Scalar__Vector3__Vector3__Vector3__Scalar 1
float mi_phong_specular(
	float3 lightDir,
	float3 eyeDir,
	float3 n,
	float specular_shininess)
{
	float3 refl = ((2.f * (-(dot(eyeDir, n)))) * n) + eyeDir;
	float h = dot(refl, lightDir);
	return ((pow((max(h, 0.f)), specular_shininess)) * (specular_shininess + 2.f)) / (2.0 * 3.14159265358979323846f);
}
#endif


//
// The state structure is used internally within the fragment shader to
// commonly used values.
//
struct State
{
	float4 tex_coord[4];
	float3 position;
	float3 normal;
	float3 direction;
	float3x3 tangent_space[1];
	float3 light_position;
	float3 light_to_surface;
	float light_distance;
	float light_dotnl;
};

//
// The following are free parameters of the shader that should be set by the
// application at runtime.
//
float4 __light0_Light_point_1_color
<
	string UIName = "Light0 Color";
	string UIWidget = "Color";
> = float4(1.000000, 1.000000, 1.000000, 1.000000);
float __light0_Light_point_1_intensity
<
	string UIName = "Light0 Intensity";
	float UIMin = 0.000000;
	float UIMax = 10.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 1.000000;
float __light0_Light_point_1_distance_falloff_exponent
<
	string UIName = "Light0 Distance Falloff Exponent";
	float UIMin = 0.000000;
	float UIMax = 4.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 1.000000;
float __light0_Light_point_1_distance_falloff_start
<
	string UIName = "Light0 Distance Falloff Start";
	float UIMin = 0.000000;
	float UIMax = 10.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 0.000000;
float __light0_Light_point_1_distance_falloff_limit
<
	string UIName = "Light0 Distance Falloff Limit";
	float UIMin = 0.000000;
	float UIMax = 1000.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 1000.000000;
float __light0_Light_point_1_distance_scale
<
	string UIName = "Light0 Distance Scale";
	float UIMin = 0.001000;
	float UIMax = 100.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 1.000000;
float3 __light0_position : Position 
<
	string Object = "PointLight0";
	string Space = "World";
	string UIName = "Light0 Light_position";
> = float3(0.000000, 0.000000, 0.000000);
texture __Texture_lookup_2d_1_texture <
	string UIName = "Texture_lookup_2d_1_texture";
	string ResourceName = "C:\\Documents and Settings\\Administrator\\Desktop\\lowNormalsMapOOS.dds";
	string ResourceType = "2D";
>;

sampler2D Texture_lookup_2d_1_texture = sampler_state {
	Texture = <__Texture_lookup_2d_1_texture>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
};

float Normals_make_normal_1_amount
<
	string UIName = "Amount";
	float UIMin = -1.000000;
	float UIMax = 1.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 1.000000;
float4 Illumination_phong_1_diffuse_color
<
	string UIName = "Diffuse Color";
	float4 UIMin = float4(0.000000, 0.000000, 0.000000, 0.000000);
	float4 UIMax = float4(1.000000, 1.000000, 1.000000, 1.000000);
	float UIStep = 0.01;
	string UIWidget = "slider";
> = float4(1.000000, 1.000000, 1.000000, 1.000000);
float Illumination_phong_1_diffuse_scalar
<
	string UIName = "Diffuse Scalar";
	float UIMin = 0.000000;
	float UIMax = 1.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 0.700000;
float4 Illumination_phong_1_specular_color
<
	string UIName = "Specular Color";
	float4 UIMin = float4(0.000000, 0.000000, 0.000000, 0.000000);
	float4 UIMax = float4(1.000000, 1.000000, 1.000000, 1.000000);
	float UIStep = 0.01;
	string UIWidget = "slider";
> = float4(1.000000, 1.000000, 1.000000, 1.000000);
float Illumination_phong_1_specular_scalar
<
	string UIName = "Specular Scalar";
	float UIMin = 0.000000;
	float UIMax = 0.300000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 0.060000;
float Illumination_phong_1_specular_shininess
<
	string UIName = "Specular Shininess";
	float UIMin = 0.000000;
	float UIMax = 100.000000;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 15.000000;


//
// The following are parameters representing non-varying state variables 
// referenced by the shader. These should be set by the application at runtime.
// Note that vector parameters should be provided in camera space.
//
float4x4 __object_to_ndc : WorldViewProjection
<
	string UIWidget = "none";
>;
float4x4 __world_to_object : WorldInverse
<
	string UIWidget = "none";
>;
float4x4 __object_to_world : World
<
	string UIWidget = "none";
>;
float4x4 __camera_to_world : ViewInverse
<
	string UIWidget = "none";
>;
float4 __scene_ambient : Ambient
<
	string UIName = "Scene Ambient";
	string UIWidget = "color";
> = float4(0.00,0.00,0.00,0.00);

//
// The App2vert structure defines the vertex attributes used by the vertex
// shader. The application should supply a vertex stream containing these 
// elements.
//
struct App2vert
{
	float3 position : POSITION;
	float3 normal : NORMAL;
	float4 texcoord0 : TEXCOORD0;
	float4 texcoord1 : TEXCOORD1;
	float4 texcoord2 : TEXCOORD2;
	float4 texcoord3 : TEXCOORD3;
	float3 tex_du : TANGENT;
	float3 tex_dv : BINORMAL;
};

//
// The Vert2frag_out structure defines values output by the vertex shader.
//
struct Vert2frag_out
{
	float4 hpos : POSITION;
	float4 tex_coord[4] : TEXCOORD0;
	float3 tex_du : TEXCOORD4;
	float3 tex_dv : TEXCOORD5;
	float3 position : TEXCOORD6;
	float3 normal : TEXCOORD7;
};

//
// The Vert2frag_in structure defines values used by the fragment shader.
//
struct Vert2frag_in
{
	float4 hpos : POSITION;
	float4 tex_coord[4] : TEXCOORD0;
	float3 tex_du : TEXCOORD4;
	float3 tex_dv : TEXCOORD5;
	float3 position : TEXCOORD6;
	float3 localnormal : TEXCOORD7;
    float3 localTangent  	: TEXCOORD8;//надо-ли?  <<<<<<<<< 
    float3 localBinormal 	: TEXCOORD9;//надо-ли?  <<<<<<<<<
};

//
// This function is the main method of the vertex shader.
//
Vert2frag_out vertex_main(
	App2vert vs_in)
{
	Vert2frag_out vs_out;
	float4 position = float4(vs_in.position, 1);
	vs_out.hpos = mul(position, __object_to_ndc);
	vs_out.position = mul(position, __object_to_world).xyz;
	vs_out.normal = mul((float3x3)__world_to_object, vs_in.normal);
	vs_out.tex_coord[0] = vs_in.texcoord0;
	vs_out.tex_coord[1] = vs_in.texcoord1;
	vs_out.tex_coord[2] = vs_in.texcoord2;
	vs_out.tex_coord[3] = vs_in.texcoord3;
	vs_out.tex_du = mul((float3x3)__world_to_object, vs_in.tex_du);
	vs_out.tex_dv = mul((float3x3)__world_to_object, vs_in.tex_dv);
	return vs_out;
}

//
// The following functions are generated from the MetaSL implementation of
// the shaders that are part of the compiled shader graph.
//

void Light_point_main(
	float4 color,
	float intensity,
	float distance_falloff_exponent,
	float distance_falloff_start,
	float distance_falloff_limit,
	float distance_scale,
	State state,
	out float4 result,
	out float4 light_shadow)
{
	{
		float d = saturate((((state.light_distance) / (distance_scale - distance_falloff_start)) / (distance_falloff_limit - distance_falloff_start)));
		float f = 1.0 / (1.0 + (pow(d, distance_falloff_exponent)));
		result = ((color * intensity) * f);
		light_shadow = (float4(1,1,1,1));
		result *= 3.14159265358979323846f;
	}
}

void Light_point_1_main(
	float4 Light_point_1_color,
	float Light_point_1_intensity,
	float Light_point_1_distance_falloff_exponent,
	float Light_point_1_distance_falloff_start,
	float Light_point_1_distance_falloff_limit,
	float Light_point_1_distance_scale,
	float3 __light_position,
	State state,
	out Light_iterator __light)
{
	{
		float4 light_shadow = float4(0.000000, 0.000000, 0.000000, 0.000000);
		float4 result = float4(0.000000, 0.000000, 0.000000, 0.000000);
		__light.direction = (__light_position - (state.position));
		__light.distance = (length(__light.direction));
		__light.direction /= __light.distance;
		__light.dot_nl = (dot(__light.direction, (state.normal)));
		__light.contribution = 0.0;
		__light.raw_contribution = 0.0;
		__light.count = 0.0;
		__light.shadow = 0.0;
		__light._point = 0.0;
		(state.light_to_surface) = (-__light.direction);
		(state.light_distance) = __light.distance;
		(state.light_dotnl) = __light.dot_nl;
		(state.light_position) = __light_position;
		Light_point_main(Light_point_1_color, Light_point_1_intensity, Light_point_1_distance_falloff_exponent, Light_point_1_distance_falloff_start, Light_point_1_distance_falloff_limit, Light_point_1_distance_scale, (state), result, light_shadow);
		__light._point = __light_position;
		__light.shadow = light_shadow;
		__light.count = 0;
		__light.raw_contribution = result;
		__light.contribution = (result * light_shadow);
	}
}

void Texture_lookup_2d_main(
	sampler2D _texture,
	int texture_space,
	float2 texture_uv,
	State state,
	out float4 result)
{
	{
		result = (__tex2D_flip_v(_texture, texture_uv));
	}
}

void Normals_make_normal_main(
	float4 color,
	float amount,
	State state,
	out float3 result)
{
	{
		float3 n = (color.xyz - 0.5) * 2.0;
		n.xy *= amount;
		state.tangent_space[0] = transpose(float3x3(   // не работает почему-то :(((((
	    t_tex_tangent,
	    t_tex_binormal,
	    state.normal));

		Out.localNormal = In.state.normal.xyz;	
        Out.localBinormal = -In.t_tex_binormal.xyz;	
        Out.localTangent = In.t_tex_tangent.xyz;
		result = (normalize((mul((state.tangent_space)[0], n))));
			
		}
}

void Illumination_phong_main(
	float4 diffuse_color,
	float diffuse_scalar,
	float4 specular_color,
	float specular_scalar,
	float specular_shininess,
	State state,
	out float4 sum,
	out float4 diffuse,
	out float4 specular)
{
	{
		diffuse = (float4(0, 0, 0, 0));
		specular = (float4(0, 0, 0, 0));
		float4 Rd = diffuse_color * diffuse_scalar;
		float4 Rs = specular_color * specular_scalar;
		float3 vdir = state.direction;
		Light_iterator light;
		{
			{
				Light_point_1_main(__light0_Light_point_1_color, __light0_Light_point_1_intensity, __light0_Light_point_1_distance_falloff_exponent, __light0_Light_point_1_distance_falloff_start, __light0_Light_point_1_distance_falloff_limit, __light0_Light_point_1_distance_scale, __light0_position, (state), light);
				float cos = saturate(light.dot_nl);
				if (cos > 0.0)
				{
					diffuse += ((cos / 3.14159265358979323846f) * light.contribution);
					float s = mi_phong_specular(light.direction, vdir, (state.normal), specular_shininess);
					specular += ((s * cos) * light.contribution);
				}
			}
		}
		diffuse *= Rd;
		specular *= Rs;
		
		;
		diffuse += (Rd * (__scene_ambient));
		diffuse.a = 1.0;
		specular.a = 1.0;
		sum = (diffuse + specular);
	}
}

//
// The following method is the root function of the shader graph
//
float4 Illumination_phong_1_eval(State state)
{
	float4 sum;
	float4 diffuse;
	float4 specular;
	float4 Texture_lookup_2d_1_result = float4(0.000000, 0.000000, 0.000000, 0.000000);
	Texture_lookup_2d_main(Texture_lookup_2d_1_texture, 0, (state.tex_coord)[0].xy, (state), Texture_lookup_2d_1_result);
	float3 Normals_make_normal_1_result = float3(0.000000, 0.000000, 0.000000);
	Normals_make_normal_main(Texture_lookup_2d_1_result, Normals_make_normal_1_amount, (state), Normals_make_normal_1_result);
	{
		float3 temp0 = state.normal;
		(state.normal) = Normals_make_normal_1_result;
		Illumination_phong_main(Illumination_phong_1_diffuse_color, Illumination_phong_1_diffuse_scalar, Illumination_phong_1_specular_color, Illumination_phong_1_specular_scalar, Illumination_phong_1_specular_shininess, (state), sum, diffuse, specular);
		(state.normal) = temp0;
	}
	return sum;
}

//


// This function is the main method of the fragment shader. It initializes the
// values in the state structure that are used by nodes in the shader graph
// and produces the final result of the shader.
//
float4 fragment_main(
	Vert2frag_in fs_in) : COLOR
{
	State state;
	state.position = fs_in.position;
	state.normal = normalize(fs_in.localnormal);
	state.tex_coord = fs_in.tex_coord;
	float3 eye_to_pos = state.position - __camera_to_world[3];
	state.direction = normalize(eye_to_pos);
	float3 t_tex_tangent, t_tex_binormal;
	__make_basis(state.normal, fs_in.tex_du, fs_in.tex_dv, t_tex_tangent, t_tex_binormal);
	state.tangent_space[0] = transpose(float3x3(
	    t_tex_tangent,
	    t_tex_binormal,
	    state.normal));
	state.light_position = 0;
	state.light_to_surface = 0;
	state.light_distance = 0;
	state.light_dotnl = 0;
	return Illumination_phong_1_eval(state);
}

//
// The following define the default technique and pass of the effect.
//
technique T0
{
	pass P0
	{
		ZEnable      = true;
		ZWriteEnable = true;
		CullMode     = none;
		VertexShader = compile vs_3_0 vertex_main();
		PixelShader  = compile ps_3_0 fragment_main();
	}
}

