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

#include "Effects/StdFunctions.fx"

float4x4 World : WORLD;

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

//--------------------------------------------------------------------
// Vertex Shader that computes screen position, texture coordinates,
// and lit vertex color.
//--------------------------------------------------------------------
VS_OUTPUT VSRenderScene( float4 worldPos : POSITION,
			 float4 color : COLOR,
			 float2 texCoords: TEXCOORD0,
			 float2 rot: TEXCOORD1,
			 float3 offset: TEXCOORD2,
			 float2 scale: TEXCOORD3 )
{
    VS_OUTPUT Output = (VS_OUTPUT)0;
    
    //Perform calculations on quad
    float4 pt = worldPos;
    worldPos.x = (pt.x*rot.x - pt.y*rot.y)*scale.x;
    worldPos.y = (pt.x*rot.y + pt.y*rot.x)*scale.y;
    worldPos.xyz = (CameraUp*worldPos.y + CameraSide*worldPos.x);
    worldPos.xyz += mul(offset, (float3x3)World) + float3(World[3][0], World[3][1], World[3][2]);

    //Calculate screen position of vertex
    Output.position = mul(worldPos,ViewProjection);

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

    return Output;
}

//--------------------------------------------------------------------
// Vertex Shader that computes screen position, texture coordinates,
// and lit vertex color. (Screen Space)
//--------------------------------------------------------------------
VS_OUTPUT VSRenderSceneScreen( float4 worldPos : POSITION,
			 float4 color : COLOR,
			 float2 texCoords: TEXCOORD0,
			 float2 rot: TEXCOORD1,
			 float3 offset: TEXCOORD2,
			 float2 scale: TEXCOORD3 )
{
    VS_OUTPUT Output = (VS_OUTPUT)0;
    
    worldPos.x = (worldPos.x*rot.x - worldPos.y*rot.y)*scale.x;
    worldPos.y = (worldPos.x*rot.y + worldPos.y*rot.x)*scale.y;
    worldPos.xy += mul(offset, (float3x3)World);
    worldPos.xy *= 2;
    worldPos.xy += float2(-1,1);
    worldPos.x += 2*World[3][0];
    worldPos.y -= 2*World[3][1];
    worldPos.z = 0;
    Output.position = worldPos;

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

    return Output;
}

sampler Sampler = sampler_state
{
    Texture = (Tex);
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Point;
};

//--------------------------------------------------------------------
// 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) * Input.color;
    
    return Output;
}

//--------------------------------------------------------------------------------------
// Addative Blending
//-------------------------------------- ------------------------------------------------
technique RenderScene_BlendAdd
{
    pass Scene
    {
		CULLMODE = None;
		ZWriteEnable = false;
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = One;
		
		StencilEnable = false;

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


//--------------------------------------------------------------------------------------
// Modulate Blending
//-------------------------------------- ------------------------------------------------
technique RenderScene_BlendModulate
{
    pass Scene
    {
		CULLMODE = None;
		ZWriteEnable = false;
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;
		
		StencilEnable = false;

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

//--------------------------------------------------------------------------------------
// Addative Blending (ScreenSpace)
//-------------------------------------- ------------------------------------------------
technique RenderScene_ScreenSpace_BlendAdd
{
    pass Scene
    {
		CULLMODE = None;
		ZWriteEnable = false;
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = One;
		StencilEnable = false;

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


//--------------------------------------------------------------------------------------
// Modulate Blending (Screen Space)
//--------------------------------------------------------------------------------------
technique RenderScene_ScreenSpace_BlendModulate
{
    pass Scene
    {
		CULLMODE = None;
		ZWriteEnable = false;
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;
		StencilEnable = false;

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












