Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3368)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3369)
@@ -10,11 +10,10 @@
 //#define NUM_PRECOMPUTED_SAMPLES 2048
 
-#define NUM_SAMPLES 48
 //#define NUM_SAMPLES 16
 //#define NUM_SAMPLES 24
 //#define NUM_SAMPLES 32
-//#define NUM_SAMPLES 48
+#define NUM_SAMPLES 48
 
-#define MIN_SAMPLES 48
+#define MIN_SAMPLES 8
 //#define MIN_SAMPLES 48
 
@@ -28,4 +27,5 @@
 //#define SSAO_CONVERGENCE_THRESHOLD 300.0f
 #define SSAO_CONVERGENCE_THRESHOLD 700.0f
+//#define SSAO_CONVERGENCE_THRESHOLD 1300.0f
 //#define SSAO_CONVERGENCE_THRESHOLD 1500.0f
 
@@ -74,5 +74,5 @@
 #define PERFORMANCE_TEST 1
 
-#define USE_OPTIMIZATION 0
+#define USE_OPTIMIZATION 1
 
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3368)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3369)
@@ -359,5 +359,4 @@
 		{
 			float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
-			//offset = myreflect(samples[i], mynoise);
 			//offset = myrotate(samples[i], mynoise.x);
 			offset = myrotate(ssaoOffset, mynoise.x);
@@ -418,5 +417,5 @@
 		const float tooFarAway = step(.5f, lengthToSample - changeFactor);
 		if (0)//pixelValid <= partlyResetThres)
-			validSamples = max(validSamples, pixelValid * (1.0f - tooFarAway) * step(-0.1f, cosAngle));
+			validSamples = max(validSamples, pixelValid * (1.0f - tooFarAway) * step(-.1f, cosAngle));
 		else
 			validSamples = max(validSamples, pixelValid);
@@ -550,15 +549,18 @@
 		const float3 oldVector = oldSamplePos - oldPos;
 		const float oldDistance = length(oldVector);
-		const float cosAngle2 = dot(oldVector, normal);
-
-		if ((cosAngle >= 0) || (cosAngle2 >= 0))
-		{
-			const float oldDistance = length(oldSamplePos - oldPos);
-			const float distanceDiff = abs(oldDistance - lengthToSample);
-
-			float pixelValid = (distanceDiff > 1e-3f) ? 100.0f : 0;
-
-			validSamples = max(validSamples, pixelValid);
-		}
+		//const float cosAngle2 = dot(oldVector, normal);
+		
+		const float distanceDiff = abs(oldDistance - lengthToSample);
+
+		float pixelValid = (oldSamplePos.x > 1e14f) ? 100.0f : .0f;
+
+		if ((cosAngle >= 0) && (pixelValid < 90.0f))// || (cosAngle2 >= 0))
+		{
+			//pixelValid = (distanceDiff > 1e-3f) ? 100.0f : .0f;
+			pixelValid = (distanceDiff > 1e-3f) ? 5.0f : .0f;
+		}
+
+		validSamples = max(validSamples, pixelValid);
+
 #ifdef USE_GTX
 		// we can bail out early and use a minimal #samples)
@@ -682,6 +684,6 @@
 
 	float3 ao;
-	// $$temp matt
-	oldIdx = dummyIdx;
+	//$$temp matt
+	//oldIdx = dummyIdx;
 
 	// cull background note: this should be done with the stencil buffer
@@ -708,5 +710,5 @@
 			           bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity, 
 					   isMovingObject, normals, oldIdx);
-					   */
+			*/
 		}
 	}
@@ -730,20 +732,31 @@
 	const float partlyResetThres = 1.0f;
 	
+	// the new index for the next frame
+	float newIdx;
+	// the new weight for the next frame
+	float combinedWeight;
+
 	// don't check for moving objects, otherwise almost no coherence
-	if (1)//!isMovingObject)
-	{
-		if (ao.y > completelyResetThres) 
-		{
-			oldWeight = .0f;
-			oldIdx    = .0f;
-		}
-		else if (ao.y > partlyResetThres)
-		{
-			const float factor = 4.0f;
-			//if (oldIdx >= factor * newWeight) oldIdx = 0;
-			//oldWeight = min(oldWeight, factor * newWeight);
-			oldWeight = oldIdx = .0f; 
-		}
-	}
+	if (ao.y > completelyResetThres) 
+	{
+		oldWeight = newIdx = .0f;
+	}
+	else
+	{
+		newIdx = oldIdx + newWeight;
+	}
+
+	if (ao.y > partlyResetThres)
+	{
+		const float factor = 3.0f;
+
+		oldWeight = min(oldWeight, factor * NUM_SAMPLES);
+		if (newIdx >= factor * NUM_SAMPLES) {newIdx = 0; /*oldWeight = .0f;*/ }
+
+		//combinedWeight = oldWeight;
+	}
+
+	// the new weight for the next frame
+	combinedWeight = clamp(newWeight + oldWeight, .0f, temporalCoherence);
 
 
@@ -756,9 +769,6 @@
 	//OUT.illum_col.x = ao.y;
 
-	// the new weight for the next frame
-	const float combinedWeight = clamp(newWeight + oldWeight, .0f, temporalCoherence);
-
 	OUT.illum_col.y = combinedWeight;
-	OUT.illum_col.z = oldIdx + newWeight; // the new index
+	OUT.illum_col.z = newIdx; // the new index
 	OUT.illum_col.w = eyeSpaceDepth;
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg	(revision 3365)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg	(revision 3369)
@@ -253,6 +253,6 @@
 		// the filtered ssao value
 		ao.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, 
-			             bl, br, tl, tr, 
-						 xyStep, convergence, maxConvergence, spatialWeight);
+			            bl, br, tl, tr, 
+						xyStep, convergence, maxConvergence, spatialWeight);
 	}
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3366)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3369)
@@ -305,5 +305,6 @@
 						 float3 difVec,
 						 float3 viewDir,
-						 float3 oldEyePos
+						 float3 oldEyePos,
+						 float4x4 oldModelViewProj
 						 )
 {
@@ -312,6 +313,24 @@
 	const float4 worldPos = float4(-viewDir * eyeSpaceDepth, 1.0f);
 	// compute position from old frame for dynamic objects + translational portion
-	const float3 translatedPos = -oldEyePos + worldPos.xyz + difVec;
-	
+	float3 translatedPos = -oldEyePos + worldPos.xyz + difVec;
+	
+	/////////////////
+	//-- reproject into old frame and calculate texture position of sample in old frame
+
+	// note: the old model view matrix only holds the view orientation part
+	float4 backProjPos = mul(oldModelViewProj, float4(translatedPos, 1.0f));
+	backProjPos /= backProjPos.w;
+	
+	// fit from unit cube into 0 .. 1
+	const float2 oldTexCoords = backProjPos.xy * .5f + .5f;
+
+	// check if the pixel was outside of the frame buffer
+	if ((oldTexCoords.x <= .0f) || (oldTexCoords.x >= 1.0f) || 
+		(oldTexCoords.y <= .0f) || (oldTexCoords.y >= 1.0f)
+		)
+	{
+		translatedPos = float3(1e15f);
+	}
+
 	return translatedPos;
 }
@@ -362,5 +381,5 @@
 	pix.color.z = color.w;
 */
-	const float3 translatedPos = ComputeTranslatedPos(color, difVec.xyz, IN.view, oldEyePos);
+	const float3 translatedPos = ComputeTranslatedPos(color, difVec.xyz, IN.view, oldEyePos, oldModelViewProj);
 
 	pix.color.xyz = translatedPos;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3368)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3369)
@@ -104,5 +104,6 @@
 float farDist = 1e6f;
 /// the field of view
-const float fov = 50.0f;
+//const float fov = 50.0f;
+const float fov = 90.0f;
 
 float skyDomeScaleFactor = 80.0f;
@@ -627,5 +628,5 @@
 	//LoadModel("elephal.dem", dynamicObjects);
 
-	if (0) LoadPompeiiFloor();
+	if (1) LoadPompeiiFloor();
 
 #if 1
@@ -643,5 +644,7 @@
 	// sibenik positions
 	VertexArray positions;
-	const float factor2 = 1.6f;
+	//const float factor2 = 1.6f;
+	const float factor2 = 1.5f;
+
 	// for buddha (lowres)
 	positions.push_back(Vector3(6.07307, 8.20723, 6.62 - factor2));
