

/*###############################################
# Shader Configuration
################################################*/

#pragma Config

	#define BW_VERTEX_POSITION 	1
	#define BW_VERTEX_NORMAL 	2
	#define BW_VERTEX_UV 		4
	#define BW_VERTEX_COLOR 	8

	// Vertex structure 
	VertexProfileCreate(3);
	VertexProfileSet(0,0,3,BW_VERTEX_POSITION,0);
	VertexProfileSet(1,12,3,BW_VERTEX_NORMAL,0);
	VertexProfileSet(2,24,3,BW_VERTEX_UV,0);

	VertexShaderVersion(2);
	PixelShaderVersion(2);

/*###############################################
# Vertex Shader
################################################*/
#pragma HLSL_VS

// Matrix
float4x4 bw_ModelViewMatrix;	
float4x4 bw_ModelViewProjectionMatrix;
float4x4 bw_NormalMatrix;
float4x4 bw_ObjectMatrix;

// Ilumination data, lights for pixelshader
float4	bw_pslPosition[4];
float4	bw_pslColor[4];

// Material Color
float4	bwMaterialDiffuse;

// Diffuse lighting expression
float3 bwDiffuseLighting(float3 vPosition,float3 vNormal,float3 lPosition, float lDistance, float3 lColor,float3 lEnergy)
{
	float3 Direction = lPosition-vPosition;
	float Distancia = length(Direction);
	float Att = lDistance/(Distancia*0.5 + pow(Distancia,2) *0.02); 

	return lColor * Att * lEnergy* clamp(dot(normalize(Direction),vNormal),0,1) ;
}


struct VS_INPUT
{
	float3 position  : POSITION;
	float3 normal    : NORMAL;
	float2 texcoord0 : TEXCOORD0;
};


struct VS_OUTPUT
{
	float4 hposition	:POSITION;
	float4 Color		:COLOR0;
	float2 texcoord0	:TEXCOORD0;
	float3 R			:TEXCOORD1;
	float  z			:TEXCOORD2;
};


VS_OUTPUT main( VS_INPUT IN )
{
	VS_OUTPUT OUT;
	OUT.texcoord0 = IN.texcoord0;
	OUT.hposition = mul(float4(IN.position.xyz,1.0),bw_ModelViewProjectionMatrix);
	OUT.Color = 1.0;

	float4 v_Pos = mul(float4(IN.position.xyz,1.0),bw_ObjectMatrix);
	float4 v_Nor = normalize(mul(float4(IN.normal.xyz,0.0),bw_ObjectMatrix));

	float3 diffuse_col=0.0; 

	for (int i = 0;i<2;i++)
	{
		diffuse_col+=bwDiffuseLighting(	v_Pos,v_Nor,
							bw_pslPosition[i].xyz,bw_pslPosition[i].w,
							bw_pslColor[i].xyz,bw_pslColor[i].w
							);
	}

	OUT.Color= float4(diffuse_col,1);

	return OUT;
}



#end