// courtesy of the ogre wiki and the defered shading sample
// http://www.ogre3d.org/wiki/index.php/Deferred_Shading
//------------------------------------------------------------------------------

void GBuffer_vp(
        float4 iPosition : POSITION,
        float3 iNormal   : NORMAL,

        out float4 oPosition : POSITION,
        out float3 oViewPos : TEXCOORD0,
        out float3 oNormal : TEXCOORD1,

        uniform float4x4 cWorldViewProj,
        uniform float4x4 cWorldView
        )
{
        oPosition = mul(cWorldViewProj, iPosition);         // transform the vertex position to the projection space
        oViewPos = mul(cWorldView, iPosition).xyz;          // transform the vertex position to the view space
        oNormal = mul(cWorldView, float4(iNormal,0)).xyz;   // transform the vertex normal to view space
}

void GBuffer_fp(
        float3 iViewPos : TEXCOORD0,
        float3 iNormal  : TEXCOORD1,

        out float4 oNormalDepth : COLOR0,	// normal + linear depth [0, 1]
        out float4 oViewPos : COLOR1,		// view space position
        
        uniform float cNearClipDistance,
        uniform float cFarClipDistance		// !!! might be 0 for infinite view projection.
        )
{
    	float clipDistance = cFarClipDistance - cNearClipDistance;
		oNormalDepth = float4(normalize(iNormal).xyz, (length(iViewPos) - cNearClipDistance) / clipDistance);
		oViewPos = float4(iViewPos, 0);
}

