Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3189)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3192)
@@ -7,21 +7,19 @@
 
 //#define NUM_SAMPLES 8
-#define NUM_SAMPLES 16
-//#define NUM_SAMPLES 24
+//#define NUM_SAMPLES 16
+#define NUM_SAMPLES 48
 
 // for quadratic falloff
-//#define SAMPLE_INTENSITY 0.2f
-//#define SAMPLE_INTENSITY 0.07f
-#define SAMPLE_INTENSITY 0.008f
+#define SAMPLE_INTENSITY 0.2f
+//#define SAMPLE_INTENSITY 0.1f
 
 //#define SAMPLE_INTENSITY 0.075f
 //#define SAMPLE_INTENSITY 0.2f
 
-//#define SAMPLE_RADIUS 8e-1f
-#define SAMPLE_RADIUS 4e-1f
+#define SAMPLE_RADIUS 8e-1f
+//#define SAMPLE_RADIUS 4e-1f
 
 //#define DISTANCE_SCALE 1e-1f
 #define DISTANCE_SCALE 1e-2f
-//#define DISTANCE_SCALE 0.2f
 
 //#define ILLUM_INTENSITY 8e-2f
@@ -32,6 +30,6 @@
 //#define NUM_SSAO_FILTERSAMPLES 80
 #define NUM_SSAO_FILTER_SAMPLES 16
-#define NUM_SSAO_FILTER_WIDTH 10.0f
-#define SSAO_CONVERGENCE_WEIGHT 25.0f
+#define NUM_SSAO_FILTER_WIDTH 15.0f
+#define SSAO_CONVERGENCE_WEIGHT 200.0f
 
 
@@ -67,5 +65,4 @@
 #define NUM_DOWNSAMPLES 9
 
-
 #define DYNAMIC_OBJECTS_THRESHOLD 1e-8f
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3190)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3192)
@@ -337,15 +337,14 @@
 
 	// do reprojection and filter out the pixels that are not save
-	float pValid = PixelValid(
-		oldTex,
-		color, 
-		difVec.xyz,
-		IN.texCoord,
-		viewDir,
-		oldEyePos,
-		modelViewProj,
-		oldModelViewProj,
-		oldbl, oldbr, oldtl, oldtr
-		);
+	float pValid = PixelValid(oldTex,
+                              color, 
+							  difVec.xyz,
+							  IN.texCoord,
+							  viewDir,
+							  oldEyePos,
+							  modelViewProj,
+							  oldModelViewProj,
+							  oldbl, oldbr, oldtl, oldtr
+							  );
 
 	pix.color = color;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3191)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3192)
@@ -117,4 +117,6 @@
 	float len;
 
+	const float convergenceThresh = 200.0f;
+
 	for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
 	{
@@ -126,16 +128,16 @@
 		// check spatial discontinuity
 		samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
-		len = min(SqrLen(centerPos - samplePos), 1e2f);
+		len = min(SqrLen(centerPos - samplePos), 1e3f);
 
 		spatialFactor = 1.0f / max(len, 1e-3f);
 
-		normalFactor = max(step(.0f, dot(sampleNorm, centerNormal)), 1e-3f);
+		normalFactor = max(step(.01f, dot(sampleNorm, centerNormal)), 1e-3f);
 		//normalFactor = max(dot(sampleNorm, samplePos), 1e-3f);
-		convergenceFactor = min(50.0f, aoSample.y);
+		convergenceFactor = min(convergenceThresh, aoSample.y + 1);
 		//convergenceFactor = aoSample.y;
 
 		// combine the weights
 		w = convergenceFactor * spatialFactor * normalFactor;
-		//w = convergenceFactor * spatialFactor;
+		//w = spatialFactor * normalFactor;
 
 		average += aoSample.x * w;
@@ -171,5 +173,5 @@
 
 	// filter up to a certain convergance value and leave out background (sky) by checking depth
-	if ((ao.y < 100.0f) && (col.w < 1e10f))
+	if ((ao.y < 500.0f) && (col.w < 1e10f))
 	//if (col.w < 1e10f)
 	{
@@ -178,6 +180,5 @@
 		const float distanceScale = 1.0f;
 
-		const float convergence = ao.y;
-		const float adaptWeight = 25.0f;
+		const float convergence = ao.y + 1;
 		const float convergenceScale = SSAO_CONVERGENCE_WEIGHT / (convergence + SSAO_CONVERGENCE_WEIGHT);
 
@@ -188,5 +189,9 @@
 	}
 
-	OUT.illum_col.xyz = col.xyz * ao.x;
+	if (col.w < 1e10f)
+		OUT.illum_col.xyz = col.xyz * ao.x;
+	else
+		OUT.illum_col.xyz = col.xyz;
+
 	//OUT.illum_col.xyz = float3(ao.x, ao.x, ao.x);
 	//OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3175)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3192)
@@ -79,5 +79,4 @@
 								sampler2D oldTex,
 								float4x4 oldModelViewProj,
-								float temporalCoherence,
 								sampler2D colors,
 								float3 projPos,
@@ -87,6 +86,5 @@
 								float3 oldtl,
 								float3 oldtr,
-								float3 diffVec,
-								float pixelValid
+								float3 diffVec
 								)
 {
@@ -131,32 +129,18 @@
 #endif
 
-	const float squaredLen = SqrLen(diffVec);
-
-	const float oldWeight = clamp(oldPixel.y, .0f, temporalCoherence);
-	//const float oldWeight = oldPixel.y;
-
 	float newWeight;
 
-	if ((temporalCoherence > 1e-6f)
+	if (1
 		&& (oldTexCoords.x >= 0.0f) && (oldTexCoords.x < 1.0f)
 		&& (oldTexCoords.y >= 0.0f) && (oldTexCoords.y < 1.0f)
 		&& (depthDif <= MIN_DEPTH_DIFF) 
-		// if visibility changed in the surrounding area we have to recompute
-		//&& (oldNumSamples > 0.8f * newNumSamples)
-		//&& (pixelValid < 1.0f)
 		)
 	{
 		// increase the weight for convergence
-		newWeight = oldWeight + 1.0f;
-		
-		if ((pixelValid > 1.0f) && (squaredLen < DYNAMIC_OBJECTS_THRESHOLD))
-		{		
-			newWeight = 4.0f;
-			//newWeight = 1.0f;
-		}
+		newWeight =  oldPixel.y;
 	}
 	else
 	{	
-		newWeight = 1.0f;
+		newWeight = 0.0f;
 	}
 
@@ -245,5 +229,6 @@
 #endif
 		// check if the samples have been valid in the last frame
-		numSamples += (1.0f - step(1.0f, lengthToSample)) * sampleColor.x;
+		//numSamples += (1.0f - step(1.0f, lengthToSample)) * sampleColor.x;
+		numSamples += 1.0f - sampleColor.x;
 	}
 
@@ -256,5 +241,5 @@
 	pixel-to-sample direction and sample normal as weight.
 */
-float2 ssao(fragment IN,
+float3 ssao(fragment IN,
 			sampler2D colors,
 			sampler2D noiseTex,
@@ -267,5 +252,6 @@
 			float3 tl,
 			float3 tr, 
-			float3 viewDir
+			float3 viewDir,
+			float newWeight
 			)
 {
@@ -275,4 +261,5 @@
 
 	float total_ao = .0f;
+	float validSamples = .0f;
 	float numSamples = .0f;
 
@@ -325,8 +312,14 @@
 
 		// check if the samples have been valid in the last frame
-		numSamples += (1.0f - step(1.0f, lengthToSample)) * sampleColor.x;
-	}
-
-	return float2(max(0.0f, 1.0f - total_ao), numSamples);
+		validSamples += (1.0f - step(1.0f, lengthToSample)) * sampleColor.x;
+		//validSamples += sampleColor.x;
+
+		++ numSamples;
+		if ((validSamples < 1.0f) && (newWeight > 20) && (numSamples >= 8)) break;
+	}
+
+	total_ao /= numSamples;
+
+	return float3(max(0.0f, 1.0f - total_ao), validSamples, numSamples);
 }
 
@@ -378,38 +371,45 @@
 
 	
-	float2 ao;
-
-	// note: this should be done with the stencil buffer
-	if (eyeSpaceDepth < 1e10f)
-	{
-		ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir));
-		//ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), normals);
-	}
-	else
-	{
-		 ao = float2(1.0f, 0);
-	}
-
-
 	/////////////////
 	//-- compute temporal reprojection
 
 	float2 temporalVals = temporalSmoothing(eyeSpacePos, eyeSpaceDepth, IN.texCoord, oldEyePos,
-	                                        oldTex, oldModelViewProj, temporalCoherence,
+	                                        oldTex, oldModelViewProj, 
 											colors, 
 											projPos.xyz, 
 											invw, 
 											oldbl, oldbr, oldtl, oldtr,
-											diffVec,
-											ao.y
+											diffVec
 											);
 
 	const float oldSsao = temporalVals.x;
-	const float newWeight = temporalVals.y;
-
-
-	OUT.illum_col.x = (ao.x + oldSsao * (newWeight - 1.0f)) / newWeight;
-	OUT.illum_col.y = newWeight;
-	OUT.illum_col.z = SqrLen(diffVec);//invw;
+	float oldWeight = temporalVals.y;
+	
+	float3 ao;
+
+	// cull background note: this should be done with the stencil buffer
+	if (eyeSpaceDepth < 1e10f)
+	{
+		ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight);
+		//ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), normals);
+	}
+	else
+	{
+		 ao = float3(1.0f, .0f, .0f);
+	}
+
+	const float squaredLen = SqrLen(diffVec);
+	
+	if ((ao.y > 1.0f) && (squaredLen < DYNAMIC_OBJECTS_THRESHOLD))
+	{		
+		oldWeight = min(oldWeight, 4.0f * NUM_SAMPLES);
+	}
+
+	const float newWeight = ao.z;
+
+	// blend between old and new samples (and avoid division by zero)
+	OUT.illum_col.x = (ao.x * newWeight + oldSsao * oldWeight) / max(1e-6f, (newWeight + oldWeight));
+	OUT.illum_col.y = clamp(newWeight + oldWeight, .0f, temporalCoherence);
+	OUT.illum_col.z = SqrLen(diffVec);
 	OUT.illum_col.w = eyeSpaceDepth;
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3190)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3192)
@@ -329,6 +329,5 @@
 	// for performance reasons we use a smaller texture and repeat it over the screen
 	CreateNoiseTex2D(mIllumFbo->GetWidth() / 4, mIllumFbo->GetWidth() / 4);
-	//CreateNoiseTex1D(mIllumFbo->GetWidth() / 4);
-
+	
 	mProjViewMatrix = IdentityMatrix();
 	mOldProjViewMatrix = IdentityMatrix();
@@ -674,5 +673,5 @@
 		GenerateSamples(mSamplingMethod);
 
-		if (mSortSamples) { SortSamples(); }
+		//if (mSortSamples) { SortSamples(); }
 		sCgSsaoProgram->SetArray2f(i, (float *)samples2, NUM_SAMPLES);
 	}
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3175)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3192)
@@ -63,5 +63,5 @@
 
 # ssao temporal coherence factor
-tempCohFactor=100.0f
+tempCohFactor=1000.0f
 # tone mapping
 useHDR=0
