//-----------------------------------------------------------------------------
// Desc: Effect file for image post-processing sample.
// Simply samples from one texture with no fuzz.
//-----------------------------------------------------------------------------


float4x4 TotalMatrix;
float4x4 WorldMatrix;
float4 VectorFromObject; // y=pixel_height (1 / viewport_height), z=blur factor 
float4 TextureFactor; // object rgba
float4 VectorFromShader;

float4 LightPos0;
float4 LightDir0;
float4 LightColor0;

float4 LightPos1;
float4 LightDir1;
float4 LightColor1;



texture g_Texture0;
texture g_Texture1;

sampler2D g_Sampler0 =
sampler_state {
    Texture = <g_Texture0>;
    AddressU = Wrap;
    AddressV = Wrap;
    MinFilter = Point;
    MagFilter = Linear;
    MipFilter = Linear;
};


sampler2D g_Sampler1 =
sampler_state {
    Texture = <g_Texture1>;
    AddressU = Wrap;
    AddressV = Wrap;
    MinFilter = Point;
    MagFilter = Linear;
    MipFilter = Linear;
};


struct VS_OUTPUT
{
    float4 Position   : POSITION;   // vertex position 
    float4 Diffuse    : COLOR0;     // vertex diffuse color (note that COLOR0 is clamped from 0..1)
    float2 TexUV0     : TEXCOORD0;  // vertex texture coords 
    float2 TexUV1     : TEXCOORD1;  // vertex texture coords 
};


VS_OUTPUT PostProcessVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0,
                         float2 vTexCoord1 : TEXCOORD1,
                         uniform int nNumLights)
{
    VS_OUTPUT Output;
  
    // Transform the position and normal:
    Output.Position = mul(vPos, TotalMatrix);
    float4 world_pos = mul(vPos, WorldMatrix);
    float3 world_normal = normalize(mul(vNormal, (float3x3)WorldMatrix)); // normal (world space)
    
    float intensity, cos_angle;
    float3 diff;

    // Compute light 0:
    diff = LightPos0 - world_pos;
    intensity = 1.0f / (0.1f + dot(diff, diff));
    cos_angle = max(0, dot(world_normal, normalize(diff)));
    float3 light0_rgb = LightColor0 * cos_angle * intensity;

    // Compute light 1:
    diff = LightPos1 - world_pos;
    intensity = 1.0f / (0.1f + dot(diff, diff));
    cos_angle = max(0, dot(world_normal, normalize(diff)));
    float3 light1_rgb = LightColor1 * cos_angle * intensity;

    Output.Diffuse.rgb = light0_rgb + light1_rgb;
    Output.Diffuse.a = 1.0f; 
    
    // Just copy the texture coordinates through
    Output.TexUV0 = vTexCoord0;
    Output.TexUV1 = vTexCoord1;

    return Output;    
}


float4 PostProcessPS(float4 diffuse : COLOR0, float2 tex0 : TEXCOORD0, float2 tex1 : TEXCOORD1) : COLOR0 {
    float4 c = tex2D(g_Sampler0, tex0);
    c *= tex2D(g_Sampler1, tex1);
    float4 c2 = (c + diffuse);// * VectorFromShader.x;
    
    //float3 vTotalLightDiffuse = float3(0,0,0);
    //vTotalLightDiffuse += g_LightDiffuse[0] * max(0, dot(vNormalWorldSpace, g_LightDir[0]));

    return float4(c2.r, c2.g, c2.b, 1.0); // RGBA
}


technique PostProcess {
    pass p0 {
		AlphaBlendEnable = false;
        VertexShader = compile vs_2_0 PostProcessVS(0);
        PixelShader = compile ps_2_0 PostProcessPS();
    }
}




















