Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3324)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3325)
@@ -9,5 +9,5 @@
 //#define NUM_SAMPLES 16
 //#define NUM_SAMPLES 24
-#define NUM_SAMPLES 48
+#define NUM_SAMPLES 8
 
 //#define MIN_SAMPLES 48
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3324)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3325)
@@ -17,8 +17,16 @@
 
 
-struct pixel
+/*struct pixel
 {
 	float4 illum_col: COLOR0;
 };
+*/
+
+struct pixel2
+{
+	float4 illum_col: COLOR0;
+	float4 col: COLOR1;
+};
+
 
 // this function is inspired from the paper of shamulgaan in order
@@ -125,6 +133,5 @@
 	// the weight of the accumulated samples from the previous frames
 	float w;
-
-	float idx = oldPixel.z;
+	float idx;
 
 	//////////////
@@ -147,8 +154,11 @@
 		//w = ComputeConvergence(oldTex, oldTexCoords, float2(1024.0f, 768.0f));
 		w = oldPixel.y;
+		idx = floor(oldPixel.z);
+
 	}
 	else
 	{	
 		w = 0.0f;
+		idx = .0f;
 	}
 
@@ -182,5 +192,5 @@
 			 bool isMovingObject,
 			 sampler2D normalTex,
-			 int idx
+			 float idx
 			 )
 {
@@ -200,5 +210,5 @@
 		//-- (affects performance for some reason!)
 
-		if (convergence < SSAO_CONVERGENCE_THRESHOLD)
+		if (1)//convergence < SSAO_CONVERGENCE_THRESHOLD)
 		{
 			float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
@@ -444,28 +454,28 @@
 /** The mrt shader for screen space ambient occlusion
 */
-pixel main(fragment IN, 
-		   uniform sampler2D colors,
-		   uniform sampler2D normals,
-		   uniform sampler2D noiseTex,
-		   uniform sampler2D samples,
-		   uniform sampler2D oldTex,
-		   uniform float4x4 modelViewProj,
-		   uniform float4x4 oldModelViewProj,
-		   uniform float temporalCoherence,
-		   uniform float3 bl,
-		   uniform float3 br,
-		   uniform float3 tl,
-		   uniform float3 tr,
-		   uniform float3 oldEyePos,
-		   uniform float3 oldbl,
-		   uniform float3 oldbr,
-		   uniform float3 oldtl,
-		   uniform float3 oldtr,
-		   uniform sampler2D attribsTex,
-		   uniform float kernelRadius,
-		   uniform float sampleIntensity
-		   )
-{
-	pixel OUT;
+pixel2 main(fragment IN, 
+			uniform sampler2D colors,
+			uniform sampler2D normals,
+			uniform sampler2D noiseTex,
+			uniform sampler2D samples,
+			uniform sampler2D oldTex,
+			uniform float4x4 modelViewProj,
+			uniform float4x4 oldModelViewProj,
+			uniform float temporalCoherence,
+			uniform float3 bl,
+			uniform float3 br,
+			uniform float3 tl,
+			uniform float3 tr,
+			uniform float3 oldEyePos,
+			uniform float3 oldbl,
+			uniform float3 oldbr,
+			uniform float3 oldtl,
+			uniform float3 oldtr,
+			uniform sampler2D attribsTex,
+			uniform float kernelRadius,
+			uniform float sampleIntensity
+			)
+{
+	pixel2 OUT;
 
 	//const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
@@ -515,5 +525,4 @@
 		//ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity, isMovingObject);
 		ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity, isMovingObject, normals, oldIdx);
-		//ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), normals, sampleIntensity);
 	}
 	else
@@ -535,14 +544,17 @@
 	const float partlyResetThres = 1.0f;
 	
+	// don't check for moving objects, otherwise almost no coherence
 	if (!isMovingObject)
 	{
 		if (ao.y > completelyResetThres) 
 		{
+			oldWeight = .0f;
+			oldIdx = .0f;
+		}
+		else if (ao.y > partlyResetThres)
+		{
+			//oldWeight = min(oldWeight, 4.0f * newWeight);
 			oldWeight = .0f; 
-		}
-		else if (ao.y > partlyResetThres)
-		{
-			oldWeight = min(oldWeight, 4.0f * newWeight);
-			//oldWeight = .0f; 
+			oldIdx = .0f;
 		}
 	}
@@ -551,6 +563,5 @@
 	//-- blend ao between old and new samples (and avoid division by zero)
 
-	OUT.illum_col.x = (ao.x * newWeight + oldSsao * oldWeight);// / (newWeight + oldWeight);//max(1e-6f, newWeight + oldWeight);
-
+	OUT.illum_col.x = (ao.x * newWeight + oldSsao * oldWeight);
 	OUT.illum_col.x /= (newWeight + oldWeight);
 
@@ -559,11 +570,11 @@
 
 	OUT.illum_col.y = combinedWeight;
+	OUT.illum_col.z = oldIdx + newWeight; // the new index
+	OUT.illum_col.w = eyeSpaceDepth;
+
 	// can be used to check if this pixel belongs to a moving object
+	OUT.col.x = SqrLen(diffVec);
 	//OUT.illum_col.z = SqrLen(diffVec);
-	OUT.illum_col.z = oldIdx + newWeight;
-	OUT.illum_col.w = eyeSpaceDepth;
-
-	//OUT.illum_col.yzw = diffVec;
-
+	
 	return OUT;
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3324)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3325)
@@ -38,6 +38,12 @@
 
 	// hack: prevent shading the sky
-	if (color.w > 1e19f) outColor = color;
-	else outColor = (ambient + diffuse) * color;
+	if (color.w > DEPTH_THRESHOLD)
+	{
+		outColor = color;
+	}
+	else 
+	{
+		outColor = (ambient + diffuse) * color;
+	}
 
 	return outColor;
@@ -203,16 +209,17 @@
 	*/
 inline float2 PixelValid(sampler2D oldTex,
-						float4 color,
-						float3 difVec,
-						float2 texCoord,
-						float3 viewDir,
-						float3 oldEyePos,
-						float4x4 modelViewProj,
-						float4x4 oldModelViewProj,
-						float3 oldbl,
-						float3 oldbr,
-						float3 oldtl,
-						float3 oldtr
-						)
+						 sampler2D oldTex2,
+						 float4 color,
+						 float3 difVec,
+						 float2 texCoord,
+						 float3 viewDir,
+						 float3 oldEyePos,
+						 float4x4 modelViewProj,
+						 float4x4 oldModelViewProj,
+						 float3 oldbl,
+						 float3 oldbr,
+						 float3 oldtl,
+						 float3 oldtr
+						 )
 {
 	// reconstruct position from the eye space depth
@@ -220,5 +227,4 @@
 	const float4 worldPos = float4(-viewDir * eyeSpaceDepth, 1.0f);
 
-	
 
 	////////////////
@@ -247,4 +253,5 @@
 	// retrieve the sample from the last frame
 	const float4 oldPixel = tex2Dlod(oldTex, float4(oldTexCoords, .0f, .0f));
+	const float4 oldPixel2 = tex2Dlod(oldTex2, float4(oldTexCoords, .0f, .0f));
 
 	// calculate eye space position of sample in old frame
@@ -260,9 +267,9 @@
 	
 	// test if this pixel was valid in the old frame
-	float IsPixelValid;
-
-	// check if the pixel belonged to a dyanmic object in the last frame
-	const bool oldDynamic =  false;(squaredLen > DYNAMIC_OBJECTS_THRESHOLD);
-	const bool newDynamic = false; (oldPixel.z > DYNAMIC_OBJECTS_THRESHOLD);
+	float isPixelValid;
+
+	// check if the pixel belonged to a dynamic object in the last frame
+	const bool newDynamic = (squaredLen > DYNAMIC_OBJECTS_THRESHOLD);
+	const bool oldDynamic = (oldPixel2.x > DYNAMIC_OBJECTS_THRESHOLD);
 
 	// actually 0 means pixel is valid
@@ -279,5 +286,5 @@
 		)
 	{
-		IsPixelValid = pixelIsNotValid;
+		isPixelValid = pixelIsNotValid;
 	}
 	else if (// check if changed from dynamic to not dynamic object
@@ -287,15 +294,15 @@
 		// check if we have a dynamic object 
 		(oldDynamic || newDynamic) && 
-		// and is a depth discontinuity
+		// and there is a depth discontinuity
 		(depthDif > MIN_DEPTH_DIFF))))
 	{	
-		IsPixelValid = pixelCouldBeValid; 
+		isPixelValid = pixelCouldBeValid; 
 	}
 	else 
 	{
-		IsPixelValid = pixelIsValid;
-	}
-
-	return float2(IsPixelValid, abs(oldEyeSpaceDepth - projectedEyeSpaceDepth));
+		isPixelValid = pixelIsValid;
+	}
+
+	return float2(isPixelValid, abs(oldEyeSpaceDepth - projectedEyeSpaceDepth));
 }
 
@@ -309,4 +316,5 @@
 				   uniform sampler2D diffVals,
 				   uniform sampler2D oldTex,
+				   uniform sampler2D oldTex2,
 				   uniform float4x4 modelViewProj,
 				   uniform float4x4 oldModelViewProj,
@@ -319,4 +327,5 @@
 {   
 	pixel pix;
+
 	float4 color = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
 	// store scaled view vector so wie don't have to normalize for e.g., ssao
@@ -329,13 +338,14 @@
 	// do reprojection and filter out the pixels that are not save
 	float2 pValid = PixelValid(oldTex,
-                              color, 
-							  difVec.xyz,
-							  IN.texCoord,
-							  IN.view,
-							  oldEyePos,
-							  modelViewProj,
-							  oldModelViewProj,
-							  oldbl, oldbr, oldtl, oldtr
-							  );
+		                       oldTex2,
+		                       color, 
+							   difVec.xyz,
+							   IN.texCoord,
+							   IN.view,
+							   oldEyePos,
+							   modelViewProj,
+							   oldModelViewProj,
+							   oldbl, oldbr, oldtl, oldtr
+							   );
 
 	pix.color = color;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3321)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3325)
@@ -657,6 +657,6 @@
 	Matrix4x4 transl = TranslationMatrix(positions[0]);
 	
-	//LoadModel("hbuddha.dem", dynamicObjects);
-	LoadModel("venusm.dem", dynamicObjects);
+	LoadModel("hbuddha.dem", dynamicObjects);
+	//LoadModel("venusm.dem", dynamicObjects);
 
 	buddha = dynamicObjects.back();
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3324)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3325)
@@ -549,9 +549,9 @@
 	
 	string prepareSsaoParams[] = 
-		{"colorsTex", "normalsTex", "diffVals", "oldTex", 
+		{"colorsTex", "normalsTex", "diffVals", "oldTex", "oldTex2", 
 		 "oldEyePos", "modelViewProj", "oldModelViewProj",
 		 "oldbl", "oldbr", "oldtl", "oldtr"};
 
-	sCgPrepareSsaoProgram->AddParameters(prepareSsaoParams, 0, 11);
+	sCgPrepareSsaoProgram->AddParameters(prepareSsaoParams, 0, 12);
 
 
@@ -770,6 +770,9 @@
 	attribsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	// flip flop between illumination buffers
-	GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
+
+	/////////
+	//-- flip flop between illumination buffers
+
+	GLuint oldSsaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
 
 	glPushAttrib(GL_VIEWPORT_BIT);
@@ -778,5 +781,5 @@
 	// read the second buffer, write to the first buffer
 	mIllumFbo->Bind();
-	glDrawBuffers(1, mrt + mIllumFboIndex);
+	glDrawBuffers(2, mrt + mIllumFboIndex);
 
 	int i = 0;
@@ -784,9 +787,10 @@
 	sCgSsaoProgram->SetTexture(i ++, colorsTex);
 	sCgSsaoProgram->SetTexture(i ++, normalsTex);
-	sCgSsaoProgram->SetTexture(i ++, oldTex);
+	sCgSsaoProgram->SetTexture(i ++, oldSsaoTex);
 	sCgSsaoProgram->SetTexture(i ++, noiseTex2D);
 
 	sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
 	
+
 	if (/*mUseTemporalCoherence || */mRegenerateSamples)
 	{
@@ -1166,6 +1170,8 @@
 	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
 	GLuint diffVals = fbo->GetColorBuffer(2)->GetTexture();
+
 	// flip flop between illumination buffers
 	GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
+	GLuint oldTex2 = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex + 1)->GetTexture();
 
 	int i = 0;
@@ -1175,4 +1181,5 @@
 	sCgPrepareSsaoProgram->SetTexture(i ++, diffVals);
 	sCgPrepareSsaoProgram->SetTexture(i ++, oldTex);
+	sCgPrepareSsaoProgram->SetTexture(i ++, oldTex2);
 
 	Vector3 de;
@@ -1187,5 +1194,7 @@
 
 	for (int j = 0; j < 4; ++ j, ++ i)
+	{
 		sCgPrepareSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
+	}
 
 	glPushAttrib(GL_VIEWPORT_BIT);
