string description = "LS3D Diffuse Shader";

//------------------------------------
float4x4	worldViewProj			: WorldViewProjection;
float4x4	world					: World;
float4x4	worldInverseTranspose	: WorldInverseTranspose;

shared float4x4 Model;
shared float4x4 InvModel;
shared float4x4 ModelViewProjection;
shared float4x4 InvModelView;
shared float3   ModelEyePos;
shared float4x4 TextureTransform0;
shared float4x4 TextureTransform1;



texture DiffMap0;

float4 lightDir : Direction
<
	string Object = "DirectionalLight";
    string Space = "World";
> = {1.0f, -1.0f, 1.0f, 0.0f};

float4 lightColor : Diffuse
<
    string UIName = "Diffuse Light Color";
    string Object = "DirectionalLight";
    string Space = "material";    
> = { 1.0f, 1.0f, 1.0f, 1.0f };

float4 lightAmbient : Ambient
<
    string UIName = "Ambient Light Color";
    string Object = "DirectionalLight";    
    string Space = "material";
> = {0.0f, 0.0f, 0.0f, 1.0f};

//------------------------------------
struct vertexInput {
    float3 position	: POSITION;
    float3 normal	: NORMAL;
    float2 uv0		: TEXCOORD0;
};

struct vertexOutput {
    float4 position	: POSITION;
    float2 uv0		: TEXCOORD0;
    float3 lightVec	: TEXCOORD1;
    float3 normal	: TEXCOORD2;
};

//------------------------------------
vertexOutput VS_TransformAndTexture( vertexInput IN ) 
{
    vertexOutput OUT;
  
	//calculate our vectors N, E, L, and H (world space)
	float3 worldVertPos = mul(IN.position, ModelViewProjection).xyz;

    OUT.lightVec = normalize( -lightDir.xyz ); //light vector;
    OUT.normal = mul( IN.normal, InvModel ); //normal vector;

    OUT.position = mul( float4( IN.position.xyz , 1.0 ) , ModelViewProjection );
    OUT.uv0 = IN.uv0;

    return OUT;
}

//------------------------------------
sampler diffMapSampler = sampler_state 
{
    texture = <DiffMap0>;
    AddressU  = Clamp;        
    AddressV  = Clamp;
    AddressW  = Clamp;
    MIPFILTER = Linear;
    MINFILTER = Linear;
    MAGFILTER = Linear;
};

//-----------------------------------
float4 PS_Textured( vertexOutput IN ): COLOR
{
	float4 diffVal = tex2D( diffMapSampler, IN.uv0 );

	// compute light intensities
	float dotLight = dot( IN.lightVec, IN.normal );
	float primDiffIntensity = saturate( dotLight );
	
	// compute light colors
//	float4 diffColor = diffVal * primDiffIntensity * lightColor + float4( 1.0, 0.0, 0.0, 0.0 );
//	float4 diffColor = diffVal + float4( 1.0, 0.0, 0.0, 0.0 );
	float4 diffColor = diffVal * primDiffIntensity * lightColor ;
	diffColor.a = diffVal.a;
	return diffColor;
}

//-----------------------------------
technique textured
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformAndTexture();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}