//Note that this is linearized but NOT normalized, to normalize divide the result of this function by the far plane.
float linearizeDepth(float depth, float near, float far)
{
    float z = depth * 2.0 - 1.0;
    return (2.0 * near * far) / (far + near - z * (far - near));	
}
