//--------------------------------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------------------------------
float4x4 g_mCurrentWorldView;
float4x4 g_mPreviousWorldView;
float4x4 g_mProj;

texture g_SkyBoxTex;
samplerCUBE g_samSkyBox = sampler_state
{
	Texture = <g_SkyBoxTex>;
	AddressU = Wrap;
	AddressV = Wrap;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
};

//--------------------------------------------------------------------------------------


void RenderSkyBoxVS( float4 Pos				: POSITION,
                     float3 Tex				: TEXCOORD0,
                     out float4 oPos		: POSITION,
                     out float3 oTex		: TEXCOORD0,
                     out float3 oViewPos	: TEXCOORD1,
                     out float3 oVelocity	: TEXCOORD2 )
{
	// Transform the Pos for the current frame
	oViewPos = mul(Pos, g_mCurrentWorldView);
	oPos = mul(float4( oViewPos, 1.0f ), g_mProj);
	
	// Find the previous frame Pos
	float3 tempPos = mul(Pos, g_mPreviousWorldView);
	float4 previousFramePos = mul(float4(tempPos, 1.0f), g_mProj);
	
	// Tex coords are passing through
	oTex = Tex;
	
	// Compute the vertex velocity
    oVelocity = ( oPos/(2.0*oPos.w) ) - ( previousFramePos/(2.0*previousFramePos.w) );
}


void RenderSkyBoxPS( float3 Tex				: TEXCOORD0,
                     float3 ViewPos			: TEXCOORD1,
                     float3 Velocity		: TEXCOORD2,
                     out float4 oColor		: COLOR0,
                     out float4 oNormal		: COLOR1,
                     out float4 oPos		: COLOR2,
                     out float4 oVelocity	: COLOR3 )
{
	// Output pixel color
	oColor = texCUBE(g_samSkyBox, Tex);

	// Output normal has (0, 0, -1) always
	oNormal = float4(0.0f, 0.0f, -1.0f, 1.0f);

	// Output view position
	oPos = float4(ViewPos, 1.0f);
	
	// Output the skybox velocity
    oVelocity = float4(Velocity, 1.0);
}


//-----------------------------------------------------------------------------
// Technique: RenderSkyBox
// Desc: Renders using 3D texture coordinates to sample from a cube texture.
//-----------------------------------------------------------------------------
technique RenderSkyBox
{
	pass p0
	{
		ZEnable = true;
		ZWriteEnable = false;
		
		VertexShader = compile vs_1_1 RenderSkyBoxVS();
		PixelShader = compile ps_2_0 RenderSkyBoxPS();
	}
}