Source

ogre-ssao / media / ssao / diffuse_gbuffer.cg

Full commit
struct AppData
{
    float4 position : POSITION;
    float3 normal : NORMAL;
};

struct VertexOut
{
    float4 position : POSITION;
    float3 position_in_view_space : TEXCOORD0;
    float3 normal_in_view_space   : TEXCOORD1;    
};


VertexOut geom_vs(
    AppData IN    
    ,uniform float4x4 worldViewProjMatrix
    ,uniform float4x4 worldViewMatrix)
{
    VertexOut OUT;

    OUT.position = mul(worldViewProjMatrix, IN.position);
    OUT.position_in_view_space = mul(worldViewMatrix, IN.position).xyz;
    OUT.normal_in_view_space = mul(worldViewMatrix, float4(IN.normal, 0)).xyz;

    return OUT;
}
//------------------------------------------------------------------------------
struct FragmentInput
{
    float3 position_in_view_space : TEXCOORD0;
    float3 normal_in_view_space   : TEXCOORD1;    
};

struct FragmentOut
{
    float4 color : COLOR0;
};
									  

FragmentOut geom_ps(
	FragmentInput IN        
    ,uniform float cNearClipDistance
    ,uniform float cFarClipDistance // !!! might be 0 for infinite view projection.
)
{
    FragmentOut OUT;

    
    float3 p = IN.position_in_view_space.xyz;
    float3 n = IN.normal_in_view_space.xyz;
  
    OUT.color.gba = normalize(n);
		        
    float clipDistance = cFarClipDistance - cNearClipDistance;
    OUT.color.r = (length(p.z) - cNearClipDistance) / clipDistance; // norm to [0, 1]        

    return OUT;
}