//--------------------------------------------------------------------------------------
// File: Pick.fx
//
// The effect file for the Pick sample.  
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor;      // Material's ambient color
float4 g_MaterialDiffuseColor;      // Material's diffuse color
float3 g_LightDir;                  // Light's direction in world space
float4 g_LightDiffuse;              // Light's diffuse color

float    g_fTime;                   // App's time in seconds
float	 g_fDx;				// 
float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix

texture g_MeshTexture;              // Color texture for mesh

texture g_YTexture;              // Color texture for mesh
texture g_UTexture;              // Color texture for mesh
texture g_VTexture;              // Color texture for mesh

float4     g_fDrawColor;

float OffsetReflection=1.4f;
float edge = 1;

float2 g_TexelSize : TEXELSIZE;
//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler = 
sampler_state
{
    Texture = <g_MeshTexture>;
};

sampler YTextureSampler = 
sampler_state
{
    Texture = <g_YTexture>;
};

sampler UTextureSampler = 
sampler_state
{
    Texture = <g_UTexture>;
};

sampler VTextureSampler = 
sampler_state
{
    Texture = <g_VTexture>;
};


struct PS_IN
{
	float2 UV : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // vertex position 
    float4 Diffuse    : COLOR0;     // vertex diffuse color (note that COLOR0 is clamped from 0..1)
    float2 TextureUV  : TEXCOORD0;  // vertex texture coords 
};


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;	
	
	
	float4 Pos = vPos;

	/*
	float angle=(g_fTime%360)*2;

    Pos.z  = sin( Pos.x+angle);
    Pos.z += sin( Pos.y/2+angle);
    Pos.z *= Pos.x * 0.09f;
	*/
	Output.Diffuse    = 0;
	
	Pos.y*=OffsetReflection;
    Output.Position =  mul(Pos,g_mWorldViewProjection);
	
    Output.TextureUV = vTexCoord0; 
    Output.TextureUV.y*=OffsetReflection;;
    return Output;    
}


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderCdVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;	
	
	
	float4 Pos = vPos;

	/*
	float angle=(g_fTime%360)*2;

    Pos.z  = sin( Pos.x+angle);
    Pos.z += sin( Pos.y/2+angle);
    Pos.z *= Pos.x * 0.09f;
	*/
	Output.Diffuse    = 0;
	
	//Pos.y*=OffsetReflection;
    Output.Position =  mul(Pos,g_mWorldViewProjection);
	
    Output.TextureUV = vTexCoord0; 
    //Output.TextureUV.y*=OffsetReflection;;
    return Output;    
}

/*** BLUR **/
/*
PS_OUTPUT Output;
float2 samples[12] = {  
-0.326212, -0.405805,  
-0.840144, -0.073580,  
-0.695914, 0.457137,  
-0.203345, 0.620716,  
0.962340, -0.194983,  
0.473434, -0.480026,  
0.519456, 0.767022,  
0.185461, -0.893124,  
0.507431, 0.064425,  
0.896420, 0.412458,  
-0.321940, -0.932615,  
-0.791559, -0.597705,  
};  


{
Output.RGBColor = tex2D(MeshTextureSampler ,  In.tex);
Output.RGBColor.a =  1.0f; 
}

// Output.RGBColor = tex2D(MeshTextureSampler , In.tex );
float4 sum = tex2D(MeshTextureSampler , In.tex);  
for (int i = 0; i < 12; i++){  
sum += tex2D(MeshTextureSampler ,  In.tex+ 0.025 * samples[i]);  
}  
Output.RGBColor = sum / 13;  
return Output;
*/

//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT FullScreenVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;
	Output.Diffuse    = 0;
	
	vPos.xy = sign(vPos.xy);  

	Output.Position = float4(vPos.xy, 0, 1);  
	Output.Position.z = 1.0f;
	// Image-space  
	Output.TextureUV.x = 0.5 * (1 + vPos.x);  
	Output.TextureUV.y = 0.5 * (1 - vPos.y);  
	
    return Output;    
}

//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};

//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In ) 
{ 
    PS_OUTPUT Output;

	if (In.TextureUV.y > edge)
	{
		In.TextureUV.y = edge - (In.TextureUV.y - edge);
		Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.TextureUV.y;
		Output.RGBColor.a = 0.8f-acos(In.TextureUV.y)*1.4;
	}
	else
	{
		Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV);
		Output.RGBColor.a = 1.f;
	}
	
	
	//Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV);



    return Output;
}

PS_OUTPUT RenderFullScreenPS( VS_OUTPUT In ) 
{ 
    PS_OUTPUT Output;


	Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV);
	Output.RGBColor.a = 1.f;

	
	//Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV);



    return Output;
}

//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderVideoPS( VS_OUTPUT In ) 
{ 
    PS_OUTPUT Output = (PS_OUTPUT)0;

	float4 RGBColor = float4(0,0,0,0);
	
	if (In.TextureUV.y > edge)
	{
		In.TextureUV.y = edge - (In.TextureUV.y - edge);
	
		float4 Y_4D = tex2D( YTextureSampler, In.TextureUV)* In.TextureUV.y;
		float4 U_4D = tex2D( UTextureSampler, In.TextureUV)* In.TextureUV.y;
		float4 V_4D = tex2D( VTextureSampler, In.TextureUV)* In.TextureUV.y;
		
		float R = 1.164 * ( Y_4D.r - 0.0625 ) + 1.596 * ( V_4D.r - 0.5 );
		float G = 1.164 * ( Y_4D.r - 0.0625 ) - 0.391 * ( U_4D.r - 0.5 ) - 0.813 * ( V_4D.r - 0.5 );
		float B = 1.164 * ( Y_4D.r - 0.0625 ) + 2.018 * ( U_4D.r - 0.5 );
		
		RGBColor.r=R;
		RGBColor.g=G;
		RGBColor.b=B;
		
		RGBColor.a = 0.8f-acos(In.TextureUV.y)*1.4;
	}
	else
	{
		float4 Y_4D = tex2D( YTextureSampler, In.TextureUV);
		float4 U_4D = tex2D( UTextureSampler, In.TextureUV);
		float4 V_4D = tex2D( VTextureSampler, In.TextureUV);
		
		float R = 1.164 * ( Y_4D.r - 0.0625 ) + 1.596 * ( V_4D.r - 0.5 );
		float G = 1.164 * ( Y_4D.r - 0.0625 ) - 0.391 * ( U_4D.r - 0.5 ) - 0.813 * ( V_4D.r - 0.5 );
		float B = 1.164 * ( Y_4D.r - 0.0625 ) + 2.018 * ( U_4D.r - 0.5 );
		
		RGBColor.r=R;
		RGBColor.g=G;
		RGBColor.b=B;
		RGBColor.a = 1.f;
	}
	
	Output.RGBColor = RGBColor;
	
    return Output;
}



//--------------------------------------------------------------------------------------
// Render Background 
//--------------------------------------------------------------------------------------
technique RenderFullScreen
{
    pass P0
    {          
        VertexShader = compile vs_2_0 FullScreenVS();
        PixelShader  = compile ps_2_0 RenderScenePS(); 
    }
}

//--------------------------------------------------------------------------------------
// Renders scene 
//--------------------------------------------------------------------------------------
technique RenderModel
{
    pass P0
    {          
        VertexShader = compile vs_2_0 RenderSceneVS();
        PixelShader  = compile ps_2_0 RenderScenePS(); 
    }
}

//--------------------------------------------------------------------------------------
// Renders scene 
//--------------------------------------------------------------------------------------
technique RenderCdModel
{
    pass P0
    {          
        VertexShader = compile vs_2_0 RenderCdVS();
        PixelShader  = compile ps_2_0 RenderScenePS(); 
    }
}

//--------------------------------------------------------------------------------------
// Render video 
//--------------------------------------------------------------------------------------
technique RenderVideo
{
    pass P0
    {          
        VertexShader = compile vs_2_0 RenderSceneVS();
        PixelShader  = compile ps_2_0 RenderVideoPS(); 
    }
}