//--------------------------------------------------------------------
// UI Effect
// Author: Ben Smith
// Date  : November 28th, 2005
//--------------------------------------------------------------------

#include "Effects/StdFunctions.fx"

//--------------------------------------------------------------------
// Global Variables
//--------------------------------------------------------------------
float4x4 World  : WORLD;
float    radius : RADIUS;

//--------------------------------------------------------------------
// External Texture for Outline
//--------------------------------------------------------------------
texture  texOutline : EXTTEXTURE
<
  int TextureID = 26;
>;

//--------------------------------------------------------------------
// Vertex Shader Output Structure
//--------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 position   : POSITION;  //Vertex position on screen
    float2 texCoords  : TEXCOORD0; //Vertex texture coordinates
};

//--------------------------------------------------------------------
// Vertex Shader Output Structure
//--------------------------------------------------------------------
struct VS_OUTPUT3D
{
    float4 position   : POSITION;  //Vertex position on screen
    float2 texCoords  : TEXCOORD0; //Vertex texture coordinates
    float4 lightColor : COLOR; //Vertex lit color
};

//--------------------------------------------------------------------
// Vertex Shader that computes screen position, texture coordinates,
// and lit vertex color.
//--------------------------------------------------------------------
VS_OUTPUT VSRenderScene( float4 worldPos : POSITION,
			 			 float2 texCoords: TEXCOORD0 )
{
    VS_OUTPUT Output;

     //Calculate screen position of vertex
     worldPos.xyz = mul(worldPos.xyz, (float3x3)World);
     worldPos.xy *= 2;
     worldPos.xy += float2(-1,1); //Place vert(0,0) at upper-left screen corner
     worldPos.x += 2*World[3][0];
     worldPos.y -= 2*World[3][1];
     Output.position = worldPos;

    //Set U,V coordinates
    Output.texCoords = texCoords;

    return Output;
}

//--------------------------------------------------------------------
// Vertex Shader that computes screen position, texture coordinates,
// and lit vertex color.
//--------------------------------------------------------------------
VS_OUTPUT3D VSRenderScene3D( float4 worldPos : POSITION,	
							 float3 normal : NORMAL,
			 			   float2 texCoords: TEXCOORD0 )
{
    VS_OUTPUT3D Output;

     //Calculate screen position of vertex
     worldPos.xyz = mul(worldPos.xyz, (float3x3)World);
     worldPos.z -= 3;
     worldPos = mul(worldPos,Projection);
     worldPos.xy *= 2;
     worldPos.xy += float2(-1,1); //Place vert(0,0) at upper-left screen corner
     worldPos.x += 2*World[3][0];
     worldPos.y -= 2*World[3][1];
     Output.position = worldPos;
     
     //Grab just a simple light value
     Output.lightColor.rgb = PhongVertexLighting(worldPos, normal, World, float3(0,0,0),
			float3(0,0,-1), mDiffuse, mSpecular, mEmissive, mPower);
     Output.lightColor.a = mAlpha;

    //Set U,V coordinates
    Output.texCoords = texCoords;

    return Output;
}

sampler Sampler = sampler_state
{
    Texture = (Tex);
    ADDRESSU  = CLAMP;
    ADDRESSV  = CLAMP;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
};

sampler OutlineSampler = sampler_state
{
    Texture = (texOutline);
	MinFilter = Linear;
	MagFilter = Linear;
};


//--------------------------------------------------------------------
// Pixel Shader Output Structure
//--------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 color : COLOR;     //Pixel's final color
};

//--------------------------------------------------------------------
// Pixel Shader that computes pixel color based on vertex color and
// texture.
//--------------------------------------------------------------------
PS_OUTPUT PSRenderScene( VS_OUTPUT Input )
{
    PS_OUTPUT Output;
    Output.color = tex2D(Sampler, Input.texCoords)*mAlpha;
    
    return Output;
}

//--------------------------------------------------------------------
// Pixel Shader that computes pixel color based on vertex color and
// texture.
//--------------------------------------------------------------------
PS_OUTPUT PSRenderScene3D( VS_OUTPUT3D Input, uniform bool hasTexture  )
{
	PS_OUTPUT Output;

	if (hasTexture)
		Output.color.rgb = tex2D(OutlineSampler, Input.texCoords)*tex2D(Sampler, Input.texCoords)*Input.lightColor.rgb;
    else
		Output.color.rgb = tex2D(OutlineSampler, Input.texCoords)*Input.lightColor.rgb;	
	Output.color.a = Input.lightColor.a;
    
    return Output;
}

//--------------------------------------------------------------------------------------
// Medium Requirement Scene Rendering
//-------------------------------------- ------------------------------------------------
technique RenderScene
{
    pass Scene
    {
		//Clockwise culling for RHW
		CULLMODE = CW;
		
		//No Z-Buffer
		ZENABLE = false;
		ZWRITEENABLE = false;

		//Alpha Blending
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;

        VertexShader = compile vs_1_1 VSRenderScene();
        PixelShader  = compile ps_2_0 PSRenderScene();
    }
}

//--------------------------------------------------------------------------------------
// Medium Requirement Scene Rendering
//-------------------------------------- ------------------------------------------------
technique RenderScene3D
{
    pass Scene
    {
		//Clockwise culling for RHW
		CULLMODE = CW;
		
		//No Z-Buffer
		ZENABLE = false;
		ZWRITEENABLE = false;

		//Alpha Blending
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;

        VertexShader = compile vs_1_1 VSRenderScene3D();
        PixelShader  = compile ps_2_0 PSRenderScene3D(true);
    }
}

//--------------------------------------------------------------------------------------
// Medium Requirement Scene Rendering
//-------------------------------------- ------------------------------------------------
technique RenderScene3D_NoTexture
{
    pass Scene
    {
		//Clockwise culling for RHW
		CULLMODE = CW;
		
		//No Z-Buffer
		ZENABLE = false;
		ZWRITEENABLE = false;

		//Alpha Blending
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;

        VertexShader = compile vs_1_1 VSRenderScene3D();
        PixelShader  = compile ps_2_0 PSRenderScene3D(false);
    }
}








