Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3081)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3083)
@@ -11,6 +11,6 @@
 // for quadratic falloff
 //#define SAMPLE_INTENSITY 0.1f
-//#define SAMPLE_INTENSITY 0.07f
-#define SAMPLE_INTENSITY 0.03f
+#define SAMPLE_INTENSITY 0.07f
+//#define SAMPLE_INTENSITY 0.03f
 
 #define SAMPLE_RADIUS 8e-1f
@@ -53,3 +53,6 @@
 #define NUM_SSAO_FILTERSAMPLES 100
 
+#define PRECISION_SCALE 1e-3f
+
+
 #endif // __SHADERENV_H
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3082)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3083)
@@ -54,4 +54,6 @@
 								   float3 bl, float3 br, float3 tl, float3 tr)
 {
+	texcoord.x += 1.0f / 1024.0f; texcoord.y += 1.0f / 768.0f;
+
 	//const float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w;
 	const float eyeSpaceDepth = tex2D(colors, texcoord).w;
@@ -66,32 +68,74 @@
 	as well as a boolean that 
 */
-pixel TemporalSmoothing(float currentPos,
-						float currentDepth,
-						uniform sampler2D oldTex)
-{
+float4 temporalSmoothing(float4 currentProjPos,
+						 float4 worldPos,
+						 float currentDepth,
+						 uniform sampler2D oldTex,
+						 const uniform float4x4 oldModelViewProj,
+						 uniform float temporalCoherence,
+						 uniform float2 ao,
+						 uniform float2 samples[NUM_SAMPLES],
+						 uniform sampler2D colors,
+						 uniform sampler2D noiseTexture,
+						 uniform float scaleFactor,
+						 uniform float3 bl,
+						 uniform float3 br,
+						 uniform float3 tl,
+						 uniform float3 tr, 
+						 float2 texcoord0
+						 )
+{
+	float4 illum_col;
 
 	/////////////////
 	//-- compute reprojection for temporal smoothing
 
-
 	// reproject into old frame and calculate projected depth
-	float4 projPos = mul(oldModelViewProj, worldPos);
-	projPos /= projPos.w;
+	float4 backProjPos = mul(oldModelViewProj, worldPos);
+	backProjPos /= backProjPos.w;
 	
 	// the current depth projected into the old frame
-	const float projDepth = projPos.z * precisionScale;
+	const float backProjDepth = backProjPos.z * PRECISION_SCALE;
 	// fit from unit cube into 0 .. 1
-	const float2 tex = projPos.xy * 0.5f + 0.5f;
+	const float2 tex = backProjPos.xy * 0.5f + 0.5f;
 	// retrieve the sample from the last frame
 	float4 oldCol = tex2D(oldTex, tex);
 
 	const float oldDepth = oldCol.z;
-	//const float depthDif = 1.0f - projDepth / oldDepth;
-	const float depthDif = projDepth - oldDepth;
+	const float depthDif = backProjDepth - oldDepth;
 
 	//const float oldNumSamples = oldCol.y;
 	const float oldWeight = clamp(oldCol.y, 0, temporalCoherence);
-
 	float newWeight;
+
+	bool isValid = true;
+
+	for (int i = 0; i < NUM_SAMPLES; ++ i) 
+	{
+		const float2 offset = samples[i];
+
+		////////////////////
+		// add random noise: reflect around random normal vector (warning: slow!)
+
+		float2 mynoise = tex2D(noiseTexture, texcoord0).xy;
+		const float2 offsetTransformed = myreflect(offset, mynoise);
+
+		const float2 texCoord = texcoord0 + offsetTransformed * scaleFactor;
+		const float3 samplePos = ReconstructSamplePos(colors, texCoord, bl, br, tl, tr);
+
+		// reproject into old frame and calculate projected depth
+		float4 projPos = mul(oldModelViewProj, float4(samplePos, 1.0f));
+		projPos /= projPos.w;
+
+		// the current depth projected into the old frame
+		const float projDepth = projPos.z * PRECISION_SCALE;
+		// fit from unit cube into 0 .. 1
+		// retrieve the sample from the last frame
+		const float4 oldSample = tex2D(oldTex, projPos.xy * 0.5f + 0.5f);
+		const float dDiff = projDepth - oldSample.z;
+
+		if (abs(dDiff) > 1e-5f) isValid = false;
+		//if (oldSample.y < oldWeight) isValid = false;
+	}
 
 	// the number of valid samples in this frame
@@ -101,17 +145,18 @@
 		&& (tex.x >= 0.0f) && (tex.x < 1.0f)
 		&& (tex.y >= 0.0f) && (tex.y < 1.0f)
-		&& (abs(depthDif) < MIN_DEPTH_DIFF) 
+		//&& (abs(depthDif) < MIN_DEPTH_DIFF) 
 		// if visibility changed in the surrounding area we have to recompute
 		//&& (oldNumSamples > 0.8f * newNumSamples)
+		&& isValid
 		)
 	{
 		// increase the weight for convergence
 		newWeight = oldWeight + 1.0f;
-		OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight;
+		illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight;
 		//if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
 	}
 	else
 	{	
-		OUT.illum_col.x = ao.x;
+		illum_col.x = ao.x;
 		newWeight = .0f;
 	}
@@ -122,5 +167,4 @@
 	return illum_col;
 }
-
 
 
@@ -128,16 +172,16 @@
 */
 float2 ssao(fragment IN,
-		   uniform sampler2D colors,
-		   uniform sampler2D noiseTexture,
-		   uniform float2 samples[NUM_SAMPLES],
-		   uniform float3 currentNormal,
-		   uniform float3 centerPosition,
-		   uniform float scaleFactor,
-		   uniform float3 bl,
-		   uniform float3 br,
-		   uniform float3 tl,
-		   uniform float3 tr, 
-		   uniform float3 viewDir
-		   )
+			uniform sampler2D colors,
+			uniform sampler2D noiseTexture,
+			uniform float2 samples[NUM_SAMPLES],
+			uniform float3 currentNormal,
+			uniform float3 centerPosition,
+			uniform float scaleFactor,
+			uniform float3 bl,
+			uniform float3 br,
+			uniform float3 tl,
+			uniform float3 tr, 
+			uniform float3 viewDir
+			)
 {
 	// Check in a circular area around the current position.
@@ -209,6 +253,6 @@
 		   uniform float2 samples[NUM_SAMPLES],
 		   uniform sampler2D oldTex,
+		   const uniform float4x4 modelViewProj,
 		   const uniform float4x4 oldModelViewProj,
-		   const uniform float4x4 modelViewProj,
 		   uniform float temporalCoherence,
 		   uniform float3 eyePos,
@@ -234,11 +278,10 @@
 	//-- calculcate the current projected posiion (also used for next frame)
 	
-	float4 currentPos = mul(modelViewProj, worldPos);
-	
-	const float w = SAMPLE_RADIUS / currentPos.w;
-	currentPos /= currentPos.w;
-	
-	const float precisionScale = 1e-3f;
-	const float currentDepth = currentPos.z * precisionScale;
+	float4 projPos = mul(modelViewProj, worldPos);
+	
+	const float w = SAMPLE_RADIUS / projPos.w;
+	projPos /= projPos.w;
+	
+	const float currentDepth = projPos.z * PRECISION_SCALE;
 
 	const float2 ao = ssao(IN, colors, noise, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
@@ -248,5 +291,6 @@
 	//-- compute temporally smoothing
 	
-	OUT.illum_col = TemporalSmoothing(currentPos, currentDepth, oldTex);
+	OUT.illum_col = temporalSmoothing(projPos, worldPos, currentDepth, oldTex, oldModelViewProj, temporalCoherence, ao,
+		                              samples, colors, noise, w, bl, br, tl, tr, IN.texCoord);
 
 	return OUT;
@@ -287,9 +331,9 @@
 	float3 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
 
-	if (ao.y < 10.0f) 
-		ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
-
-	OUT.illum_col = col * ao.x;
-	//OUT.illum_col.xyz = float3(ao.x,1-ao.y*1e-2f, 0);
+	//if (ao.y < 10.0f) 
+	//	ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
+
+	//OUT.illum_col = col * ao.x;
+	OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1);
 	OUT.illum_col.w = col.w;
 
