Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3162)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3163)
@@ -1,8 +1,9 @@
 #include "../shaderenv.h"
-
-
-/******************************************/
-/* Filter for combining ssao with image   */
-/******************************************/
+#include "common.h"
+
+
+/*************************************************/
+/*     Filter for combining ssao with image      */
+/*************************************************/
 
 
@@ -21,5 +22,5 @@
 
 /** Filter taking into account depth and normal differences, 
-   and convergence of a sample  
+    and convergence of a sample  
 */
 float DiscontinuityFilter(float2 texCoord,
@@ -31,5 +32,6 @@
 						  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
 						  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
-						  float scale)
+						  float scale
+						  )
 {
 	float average = .0f;
@@ -84,4 +86,71 @@
 
 
+
+/** Filter taking into account depth and normal differences, 
+   and convergence of a sample  
+*/
+float DiscontinuityFilter2(float2 texCoord,
+						   float4 ao,
+						   float4 color,
+						   uniform sampler2D ssaoTex,
+						   uniform sampler2D normalsTex,
+						   uniform sampler2D colorsTex,
+						   uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
+						   uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
+						   float scale,
+						   float3 bl,
+						   float3 br,
+						   float3 tl,
+						   float3 tr)
+{
+	float average = .0f;
+	float total_w = .0f;
+
+	const float3 centerPos = ReconstructSamplePos(ssaoTex, texCoord, bl, br, tl, tr);
+	const float3 centerNormal = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
+	//const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
+
+	float4 aoSample;
+	float3 sampleNorm;
+	float3 samplePos;
+	float w;
+	float4 sampleTexCoord;
+	float spatialFactor;
+	float normalFactor;
+	float convergenceFactor;
+	float sampleDepth;
+
+	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
+	{
+		sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
+
+		aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
+		sampleNorm = normalize(tex2Dlod(normalsTex, sampleTexCoord).xyz);
+		//sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
+
+		// check spatial discontinuity
+		samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
+		float len = SqrLen(centerPos - samplePos);
+
+		spatialFactor = 1.0f / max(len, 1e-3f);
+
+		//normalFactor = max(step(0.6f, dot(sampleNorm, centerNormal)), 1e-3f);
+		normalFactor = max(dot(sampleNorm, samplePos), 1e-3f);
+		convergenceFactor = min(100.0f, aoSample.y);
+		//convergenceFactor = aoSample.y;
+
+		// combine the weights
+		w = convergenceFactor * spatialFactor * normalFactor;
+
+		average += aoSample.x * w;
+		total_w += w;
+	}
+
+	average /= max(total_w, 1e-6f);
+
+	return saturate(average);
+}
+
+
 /** Function combining image and indirect illumination buffer using a 
 	depth and normal aware discontinuity filter.
@@ -93,5 +162,9 @@
 			  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
 			  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
-			  uniform float4x4 modelViewProj
+			  uniform float4x4 modelViewProj,
+			  uniform float3 bl,
+			  uniform float3 br,
+			  uniform float3 tl,
+			  uniform float3 tr
 			  )
 {
@@ -122,5 +195,5 @@
 	//if (col.w < 1e10f)
 	{
-		ao.x = DiscontinuityFilter(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, filterWeights, convergenceScale * distanceScale);
+		ao.x = DiscontinuityFilter2(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, filterWeights, convergenceScale * distanceScale, bl, br, tl, tr);
 	}
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/normalMapping.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/normalMapping.cg	(revision 3154)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/normalMapping.cg	(revision 3163)
@@ -96,4 +96,5 @@
 /******************************************************************/
 
+
 pixel frag(fragin IN,
 		   uniform float4x4 viewMatrix,
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3162)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3163)
@@ -414,6 +414,6 @@
 	string combineSsaoParams[] = 
 		{"colorsTex", "normalsTex", "ssaoTex", "filterOffs", 
-		 "filterWeights", "modelViewProj"};
-	sCgCombineSsaoProgram->AddParameters(combineSsaoParams, 0, 6);
+		 "filterWeights", "modelViewProj", "bl", "br", "tl", "tr"};
+	sCgCombineSsaoProgram->AddParameters(combineSsaoParams, 0, 10);
 
 	//////////////
@@ -826,4 +826,6 @@
 	sCgCombineSsaoProgram->SetMatrix(i++, mProjViewMatrix);
 
+	for (int j = 0; j < 4; ++ j, ++ i)
+		sCgCombineSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
 
 	DrawQuad(sCgCombineSsaoProgram);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 3126)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 3163)
@@ -752,4 +752,8 @@
 			</File>
 			<File
+				RelativePath=".\src\shaders\common.h"
+				>
+			</File>
+			<File
 				RelativePath=".\src\shaders\deferred.cg"
 				>
