/*****************************************
* Texture.fx - handles a single texture
******************************************/

// globals
float4x4	gWVP;
texture		tex1;
texture		tex2;
texture		tex3;
float		brightness;
float 		gamma;
float           screenWidth;
float           screenHeight;

// texture sampler
sampler		SceneSampler
{
	Texture = <tex1>;
	// texture filtering
	MinFilter = NONE;
	MagFilter = NONE;
	MipFilter = NONE;
	// texture address modes (available options are WRAP, MIRROR, BORDER, and CLAMP)
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};
sampler		NormalSampler
{
	Texture = <tex2>;
	// texture filtering
	MinFilter = NONE;
	MagFilter = NONE;
	MipFilter = NONE;
	// texture address modes (available options are WRAP, MIRROR, BORDER, and CLAMP)
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};
sampler		DepthSampler
{
	Texture = <tex3>;
	// texture filtering
	MinFilter = NONE;
	MagFilter = NONE;
	MipFilter = NONE;
	// texture address modes (available options are WRAP, MIRROR, BORDER, and CLAMP)
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};
// available filter options are NONE, POINT (Nearest Neighbor), LINEAR (average 2x2), and ANISOTROPIC. 
	
// Define a vertex shader output structure;
struct OutputVS
{
    float4 posH:POSITION0;
    float4 color:COLOR0;
    float2 texCoord0:TEXCOORD0;
};

// Vertex shader -------------------------------------------------------------------------------------
OutputVS TransformVS(float3 position:POSITION0, float4 col:COLOR0, float2 uv0:TEXCOORD0)
{
	// output structure
	OutputVS outVS;
	
	// create the transformed vertex position
	outVS.posH = mul(float4(position, 1.0f), gWVP);	
	
	// pass through the color
	outVS.color = col;
	
	// pass through the texture coordinates
	outVS.texCoord0 = uv0;
	
    return outVS;
}

float getGray(float4 c)
{
    return(dot(c.rgb,((0.33333).xxx)));
}

// Pixel Shader ---------------------------------------------------------------------------
float4 TransformPS(float4 c:COLOR0, float2 uv0:TEXCOORD0):COLOR
{
	// determine the pixel to sampler from
    float2 ox = float2(1.0f/screenWidth,0.0);
    float2 oy = float2(0.0,1.0f/screenHeight);
    float2 uv = uv0.xy;
    float2 PP = uv - oy;
    float4 CC = tex2D(SceneSampler,PP-ox); float g00 = getGray(CC);
    CC = tex2D(SceneSampler,PP);    float g01 = getGray(CC);
    CC = tex2D(SceneSampler,PP+ox); float g02 = getGray(CC);
    PP = uv;
    CC = tex2D(SceneSampler,PP-ox); float g10 = getGray(CC);
    CC = tex2D(SceneSampler,PP);    float g11 = getGray(CC);
    CC = tex2D(SceneSampler,PP+ox); float g12 = getGray(CC);
    PP = uv + oy;
    CC = tex2D(SceneSampler,PP-ox); float g20 = getGray(CC);
    CC = tex2D(SceneSampler,PP);    float g21 = getGray(CC);
    CC = tex2D(SceneSampler,PP+ox); float g22 = getGray(CC);
    float K00 = -1;
    float K01 = -2;
    float K02 = -1;
    float K10 = 0;
    float K11 = 0;
    float K12 = 0;
    float K20 = 1;
    float K21 = 2;
    float K22 = 1;
    float sx = 0;
    float sy = 0;
    sx += g00 * K00;
    sx += g01 * K01;
    sx += g02 * K02;
    sx += g10 * K10;
    sx += g11 * K11;
    sx += g12 * K12;
    sx += g20 * K20;
    sx += g21 * K21;
    sx += g22 * K22; 
    sy += g00 * K00;
    sy += g01 * K10;
    sy += g02 * K20;
    sy += g10 * K01;
    sy += g11 * K11;
    sy += g12 * K21;
    sy += g20 * K02;
    sy += g21 * K12;
    sy += g22 * K22; 
    float dist = sqrt(sx*sx+sy*sy);
    float4 result = float4(0.0f, 0.0f, 0.0f, 0.0f);
    if (dist>0.3f) { result = float4(0.0f, 0.0f, 0.0f, 1.0f); }
    return result;
}

float4 TransformPSDepth(float4 c:COLOR0, float2 uv0:TEXCOORD0):COLOR
{

	// determine the pixel to sampler from
    float2 ox = float2(0.5f/screenWidth,0.0);
    float2 oy = float2(0.0,0.5f/screenHeight);
    float2 uv = uv0.xy;
    float2 PP = uv - oy;
    //return tex2D(DepthSampler, PP);
    float4 CC = tex2D(DepthSampler,PP-ox); float g00 = getGray(CC);
    CC = tex2D(DepthSampler,PP);    float g01 = getGray(CC);
    CC = tex2D(DepthSampler,PP+ox); float g02 = getGray(CC);
    PP = uv;
    CC = tex2D(DepthSampler,PP-ox); float g10 = getGray(CC);
    CC = tex2D(DepthSampler,PP);    float g11 = getGray(CC);
    CC = tex2D(DepthSampler,PP+ox); float g12 = getGray(CC);
    PP = uv + oy;
    CC = tex2D(DepthSampler,PP-ox); float g20 = getGray(CC);
    CC = tex2D(DepthSampler,PP);    float g21 = getGray(CC);
    CC = tex2D(DepthSampler,PP+ox); float g22 = getGray(CC);
    float K00 = -1;
    float K01 = -2;
    float K02 = -1;
    float K10 = 0;
    float K11 = 0;
    float K12 = 0;
    float K20 = 1;
    float K21 = 2;
    float K22 = 1;
    float sx = 0;
    float sy = 0;
    sx += g00 * K00;
    sx += g01 * K01;
    sx += g02 * K02;
    sx += g10 * K10;
    sx += g11 * K11;
    sx += g12 * K12;
    sx += g20 * K20;
    sx += g21 * K21;
    sx += g22 * K22; 
    sy += g00 * K00;
    sy += g01 * K10;
    sy += g02 * K20;
    sy += g10 * K01;
    sy += g11 * K11;
    sy += g12 * K21;
    sy += g20 * K02;
    sy += g21 * K12;
    sy += g22 * K22; 
    float dist = sqrt(sx*sx+sy*sy);
    float4 result = float4(0.0f, 0.0f, 0.0f, 0.0f);
    if (dist>0.001f) { result = float4(0.8f, 0.60f, 0.0f, 1.0f); }
    return result;
}

float4 TransformPSNormals(float4 c:COLOR0, float2 uv0:TEXCOORD0):COLOR
{
    // determine the pixel to sampler from
    float2 ox = float2(1.5f/screenWidth,0.0);
    float2 oy = float2(0.0,1.5f/screenHeight);
    float2 uv = uv0.xy;
    float2 PP = uv - oy;
    float4 CC0= tex2D(NormalSampler,PP+ox) * (tex2D(DepthSampler, PP+ox) * 100);
    float4 CC1= tex2D(NormalSampler,PP-ox) * (tex2D(DepthSampler, PP-ox) * 100);

    float dist = distance(CC0, CC1);
    float4 result = float4(0.0f, 0.0f, 0.0f, 0.0f);
    if (dist>0.5f) { result = float4(0.0f, 0.0f, 0.0f, 1.0f); }
    return result;
}

// Pixel Shader ---------------------------------------------------------------------------
float4 TransformPSNormal(float4 c:COLOR0, float2 uv0:TEXCOORD0):COLOR
{
	// determine the pixel to sampler from
    float4 texCol = tex2D(SceneSampler, uv0) * brightness;
    
    return texCol * float4(0.5f + gamma, 0.5f + gamma, 0.5f + gamma, 1.0f);
}
// ----------------------------------------------------------------------------------------
technique Basic
{

    //pass P0
    //{
        //vertexShader = compile vs_2_0 TransformVS();
        //pixelShader  = compile ps_2_0 TransformPSNormal();

	//ShadeMode = Gouraud;
        //FillMode = Wireframe;
        //ZEnable = false;
        //CullMode = NONE;
	//AlphaBlendEnable = false;
    //}


    //pass P1
    //{
        //vertexShader = compile vs_2_0 TransformVS();
        //pixelShader  = compile ps_2_0 TransformPS();

	//ShadeMode = Gouraud;
        //FillMode = Wireframe;
        //ZEnable = false;
        //CullMode = NONE;
	//AlphaBlendEnable = false;
    //}

    //pass P2
    //{
        //vertexShader = compile vs_2_0 TransformVS();
        //pixelShader  = compile ps_2_0 TransformPSNormals();

	//ShadeMode = Gouraud;
        //FillMode = Wireframe;
	//ZEnable = false;
        //CullMode = NONE;
	//AlphaBlendEnable = false;
    //}

    pass P3
    {
        vertexShader = compile vs_2_0 TransformVS();
        pixelShader  = compile ps_2_0 TransformPSDepth();

	//ShadeMode = Gouraud;
        //FillMode = Wireframe;
	ZEnable = false;
        CullMode = NONE;
	//AlphaBlendEnable = true;
    }

}