Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2903)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2904)
@@ -8,5 +8,5 @@
 // for linear falloff
 //#define SAMPLE_INTENSITY 500.0f
-//#define SAMPLE_INTENSITY 100.0f
+//#define SAMPLE_INTENSITY 40.0f
 
 // for quadradtc falloff
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2903)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2904)
@@ -35,11 +35,11 @@
 /** The ssao shader returning the an intensity value between 0 and 1
 */
-float ssao(fragment IN,
+float2 ssao(fragment IN,
 		   uniform sampler2D positions,
 		   uniform sampler2D noiseTexture,
 		   uniform float2 samples[NUM_SAMPLES],
 		   uniform float3 currentNormal,
-		   uniform float3 currentViewDir,
 		   uniform float4 centerPosition
+		   //,uniform float3 currentViewDir
 		   )
 {
@@ -52,4 +52,5 @@
 
 	float total_ao = 0.0;
+	float numSamples = 0;
 
 	for (int i = 0; i < NUM_SAMPLES; ++ i) 
@@ -68,6 +69,7 @@
 		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
 
-		//if ((texcoord.x <= 1.0f) || (texcoord.x >= 0.0f) || (texcoord.y <= 1.0f) || (texcoord.y >= 0.0f))
-		
+		if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
+			++ numSamples;
+
 		// sample downsampled texture in order to speed up texture accesses
 		float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
@@ -92,7 +94,9 @@
 
 		total_ao += cos_angle * distance_intensity;
+
+		//total_ao /= j;
 	}
 
-	return max(0.0f, 1.0f - total_ao);
+	return float2(max(0.0f, 1.0f - total_ao), numSamples);
 	//return saturate(dot(currentViewDir, currentNormal));
 }
@@ -124,5 +128,5 @@
 	
 	/// the current view direction
-	float3 viewDir;// = normalize(IN.view);
+	//float3 viewDir = normalize(IN.view);
 
 	// the current world position
@@ -133,5 +137,5 @@
 	const float currentDepth = currentCol.w;
 
-	const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, centerPosition);
+	const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition);//, viewDir);
 		
 
@@ -154,23 +158,31 @@
 
 	float oldWeight = clamp(oldCol.z, 0, temporalCoherence);
+	float oldNumSamples = oldCol.y;
+
 	float newWeight;
 
-	if ((temporalCoherence > 0) &&
+	if (//(temporalCoherence > 0) &&
 		(tex.x >= 0.0f) && (tex.x < 1.0f) && 
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
-		(abs(depthDif) < 1e-4f))
+		(abs(depthDif) < 1e-3f) 
+		&& (oldNumSamples > ao.y - 1.5f) // check if something happened in the surrounding area
+		)
 	{
-		newWeight = oldWeight + 1;
+		// increase the weight for convergence
+		newWeight = oldWeight + 1.0f;
 
 		//OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
-		OUT.illum_col = (float4)(ao + oldCol.x * oldWeight) / newWeight;
+		OUT.illum_col.xy = (ao.xy + oldCol.xy * oldWeight) / newWeight;
+
+		//if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
 	}
 	else
 	{
-		OUT.illum_col = (float4)ao;
+		OUT.illum_col.xy = ao.xy;
 		newWeight = 0;
 	}
 
 
+	//OUT.illum_col.y = ao.y;
 	OUT.illum_col.z = newWeight;
 	OUT.illum_col.w = currentDepth;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2903)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2904)
@@ -37,7 +37,14 @@
 }
 
+struct GiStruct
+{
+	float3 illum;
+	float2 ao;
+};
+
+
 /** Computes  diffuse reflections + ambient occlusion
 */
-float4 globIllum(fragment IN,
+GiStruct globIllum(fragment IN,
 				 uniform sampler2D colors,
 				 uniform sampler2D positions,
@@ -49,4 +56,6 @@
 				 )
 {
+	GiStruct gi;
+
 	// the w coordinate from the persp. projection
 	float w = centerPosition.w;
@@ -57,6 +66,7 @@
 
 	// ao is in stored in the w component
-	float4 total_color = float4(0, 0, 0, 1);
-
+	float3 total_color = float3(0, 0, 0);
+	float total_ao = 0.0f;
+	float numSamples = 0.0f;
 
 	////////////
@@ -78,4 +88,7 @@
 		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
 
+		if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
+			++ numSamples;
+
 		// use lower lod level to improve cache coherence
 		float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
@@ -100,9 +113,13 @@
 		total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY;
 		*/
-		total_color.w -= cos_angle * distance_intensity;
-		total_color.xyz += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
+		total_ao += cos_angle * distance_intensity;
+		total_color += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
 	}
 
-	return saturate(total_color);
+	gi.illum = saturate(total_color);
+	gi.ao = float2(max(0.0f, 1.0f - total_ao), numSamples);
+
+	//return saturate(total_color);
+	return gi;
 }
 
@@ -144,6 +161,5 @@
 	const float currentDepth = currentCol.w;
 
-	float4 new_color = globIllum(IN, colors, positions, noiseTexture, 
-                                 samples, normal, viewDir, centerPosition);
+	GiStruct gi = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, centerPosition);
 	
 
@@ -168,18 +184,18 @@
 	float oldWeight = clamp(oldSsao.z, 0, temporalCoherence);
 	float newWeight;
+
+	const float oldNumSamples = oldSsao.y;
 
 	if ((temporalCoherence > 0.0f) &&
 		(tex.x >= 0.0f) && (tex.x < 1.0f) && 
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
-		(abs(depthDif)  < 1e-3f))
+		(abs(depthDif)  < 1e-3f)
+		&& (oldNumSamples > gi.ao.y - 1.5f) // check if something happened in the surrounding area
+		)
 	{
 		newWeight = oldWeight + 1;
 
-		//OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
-		OUT.ssao_col.x = (new_color.w + oldSsao.x * oldWeight) / newWeight;
-		OUT.illum_col  = (new_color + oldIllum * oldWeight) / newWeight;
-
-		//OUT.ssao_col.x = new_color.w * expFactor + oldSsao.x * (1.0f - expFactor);
-		//OUT.illum_col = new_color * expFactor + oldIllum * (1.0f - expFactor);
+		OUT.ssao_col.xy = (gi.ao + oldSsao.xy * oldWeight) / newWeight;
+		OUT.illum_col.xyz  = (gi.illum + oldIllum.xyz * oldWeight) / newWeight;
 	}
 	else
@@ -187,6 +203,6 @@
 		newWeight = 0;
 
-		OUT.ssao_col.x = new_color.w;
-		OUT.illum_col = new_color;
+		OUT.ssao_col.xy = gi.ao.xy;
+		OUT.illum_col.xyz = gi.illum;
 	}
 
@@ -211,4 +227,6 @@
 	
 	OUT.illum_col = (col + illum) * ao;
+	//OUT.illum_col = ao;
+
 	OUT.illum_col.w = col.w;
 
