Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3226)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3227)
@@ -1,4 +1,4 @@
 #include "../shaderenv.h"
-
+#include "common.h"
 
 ////////////////////
@@ -28,30 +28,4 @@
 }
 
-
-inline float SqrLen(float3 v)
-{
-	return v.x * v.x + v.y * v.y + v.z * v.z;
-}
-
-
-inline float2 myreflect(float2 pt, float2 n)
-{
-	// distance to plane
-	float d = dot(n, pt);
-	// reflect around plane
-	float2 rpt = pt - d * 2.0f * n;
-
-	return rpt;
-}
-
-
-inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
-{
-	float3 x1 = lerp(bl, tl, w.y);
-	float3 x2 = lerp(br, tr, w.y); 
-	float3 v = lerp(x1, x2, w.x); 
-
-	return v;
-}
 
 
@@ -225,14 +199,13 @@
 	total_ao /= numSamples;
 
-#if 0
+#if 1
 	// if surface normal perpenticular to view dir, approx. half of the samples will not count
 	// => compensate for this (on the other hand, projected sampling area could be larger!)
 	const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f);
-	total_ao *= viewCorrection;
+	total_ao += cosAngle * aoContrib * viewCorrection;
 
 #endif
 
-	//return float3(max(0.0f, 1.0f - total_ao), validSamples, numSamples);
-	return float3(total_ao, validSamples, numSamples);
+	return float3(max(0.0f, 1.0f - total_ao), validSamples, numSamples);
 }
 
@@ -261,6 +234,5 @@
 			float newWeight,
 			float sampleIntensity,
-			bool isMovingObject,
-			sampler2D normalTex
+			bool isMovingObject
 			)
 {
@@ -273,14 +245,15 @@
 		const float2 offset = samples[i];
 
-#if 1
+		float2 offsetTransformed;
+
 		////////////////////
 		//-- add random noise: reflect around random normal vector 
-		//-- (slows down the computation for some reason!)
+		//-- (affects performance for some reason!)
 
 		float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
-		const float2 offsetTransformed = myreflect(offset, mynoise);
-#else
-		const float2 offsetTransformed = offset;
-#endif
+		//float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord, 0, 0)).xy;
+		//offsetTransformed = myreflect(offset, mynoise);
+		offsetTransformed = myrotate(offset, mynoise.x);
+		
 		// weight with projected coordinate to reach similar kernel size for near and far
 		const float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor;
@@ -290,9 +263,4 @@
 		
 
-		// the normal of the current sample
-		//const float3 sampleNormal = tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz;
-		// angle between current normal and direction to sample controls AO intensity.
-		//float cosAngle2 = .5f + dot(sampleNormal, -normal) * 0.5f;
-
 		////////////////
 		//-- compute contribution of sample using the direction and angle
@@ -300,16 +268,18 @@
 		float3 dirSample = samplePos - centerPosition;
 
-		const float sqrLen = max(SqrLen(dirSample), 1e-2f);
-		const float lengthToSample = sqrt(sqrLen);
+		//const float sqrLen = max(SqrLen(dirSample), 1e-2f);
+		//const float lengthToSample = sqrt(sqrLen);
+		const float lengthToSample =  max(length(dirSample), 1e-2f);
 
 		dirSample /= lengthToSample; // normalize
 
 		// angle between current normal and direction to sample controls AO intensity.
-		const float cosAngle = max(dot(dirSample, normal), .0f);
-		const float aoContrib = sampleIntensity / sqrLen;
+		float cosAngle = dot(dirSample, normal);
+
+		//const float aoContrib = sampleIntensity / sqrLen;
+		const float aoContrib = sampleIntensity / lengthToSample;
 		//const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f;
 
-		//total_ao += cosAngle2 * cosAngle * aoContrib;
-		total_ao += cosAngle * aoContrib;
+		total_ao += max(cosAngle, 0) * aoContrib;
 
 		++ numSamples;
@@ -324,7 +294,7 @@
 		// to have any influence in the current or last frame
 		const float tooFarAway = step(0.5f, lengthToSample - changeFactor);
-		validSamples = max(validSamples, (1.0f - tooFarAway) * pixelValid);
-
-#if 1//#ifdef U//USE_GTX
+		validSamples = max(validSamples, (1.0f - tooFarAway) * pixelValid * step(-0.1f, cosAngle));
+
+#ifdef USE_GTX
 		// we can bail out early and use a minimal #samples)
 		// if some conditions are met as long as the hardware supports it
@@ -351,5 +321,4 @@
 
 	return float3(total_ao, validSamples, numSamples);
-	//return float3(max(0.0f, 1.0f - total_ao), validSamples, numSamples);
 }
 
@@ -426,5 +395,5 @@
 	if (eyeSpaceDepth < 1e10f)
 	{
-		ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity, isMovingObject, normals);
+		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), normals, sampleIntensity);
 	}
@@ -460,13 +429,13 @@
 	}
 
+	//////////
+	//-- 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 /= (newWeight + oldWeight);
+
 	// the new weight for the next frame
 	const float combinedWeight = clamp(newWeight + oldWeight, .0f, temporalCoherence);
-
-
-	//////////
-	//-- blend ao 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.x = (ao.x * newWeight + oldSsao * oldWeight) / (newWeight + oldWeight);
 
 	OUT.illum_col.y = combinedWeight;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3226)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3227)
@@ -229,5 +229,6 @@
 
 	// compute position from old frame for dynamic objects + translational portion
-	const float3 translatedPos = difVec - oldEyePos + worldPos.xyz;
+	//const float3 translatedPos = difVec - oldEyePos + worldPos.xyz;
+	const float3 translatedPos = - oldEyePos + worldPos.xyz;
 
 
@@ -275,9 +276,11 @@
 		pixelValid = pixelNotValid;
 	}
-	else if (
+	else if (!((oldEyeSpaceDepth > 1e10f) || (projectedEyeSpaceDepth > 1e10f)) &&
 		// check if changed from dynamic to not dynamic object
-		(oldDynamic && !newDynamic) || (!oldDynamic && newDynamic) ||
+		((oldDynamic && !newDynamic) || (!oldDynamic && newDynamic) ||
 		// check if we have a dynamic object and is a depth discontinuity
-		((oldDynamic || newDynamic) && (depthDif <= MIN_DEPTH_DIFF)))
+		(
+		//(oldDynamic || newDynamic) && 
+		(depthDif > MIN_DEPTH_DIFF))))
 	{	
 		pixelValid = pixelCouldBeValid; 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/common.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/common.h	(revision 3213)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/common.h	(revision 3227)
@@ -45,2 +45,16 @@
 	return rpt;
 }
+
+
+
+
+inline float2 myrotate(float2 pt, float angle)
+{
+	float2 rpt;
+
+	rpt.x = pt.x * cos(angle) - pt.y * sin(angle);
+	rpt.y = pt.y * cos(angle) + pt.x * sin(angle);
+
+	//normalize(rpt);
+	return rpt;
+}
