Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3085)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3087)
@@ -42,5 +42,5 @@
 #define MAX_LOD_LEVEL 10
 
-#define MIN_DEPTH_DIFF 1e-3f
+#define MIN_DEPTH_DIFF 1e-1f
 #define PRECISION_SCALE 1e-1f
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3081)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3087)
@@ -205,5 +205,5 @@
 	OUT.color = useShading ? (ambient + diffuse) * color : color;
 
-	// store scaled view vector from now on so wie don't have to normalize for e.g., ssao
+	// store scaled view vector from now on so wie don't have to normalize later (e.g., for ssao)
 	OUT.color.w = color.w / lenView;
 
@@ -215,7 +215,7 @@
 	as well as a boolean that 
 */
-pixel (fragment IN, 
-		   uniform sampler2D colors,
-		   uniform sampler2D normals,
+pixel Reproject(fragment IN, 
+				uniform sampler2D colors,
+				uniform sampler2D normals)
 {
 	float4 norm = tex2Dlod(normals, float4(IN.texCoord, 0 ,0));
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3086)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3087)
@@ -54,5 +54,5 @@
 								   float3 bl, float3 br, float3 tl, float3 tr)
 {
-	const float eyeSpaceDepth = tex2D(tex, texcoord).w;
+	const float eyeSpaceDepth = tex2Dlod(tex, float4(texcoord, 0, 0)).w;
 	float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
 	float3 samplePos = -viewVec * eyeSpaceDepth;
@@ -62,4 +62,51 @@
 
 #pragma position_invariant temporalSmoothing
+
+
+inline float ComputeDifference(float2 offset,
+							   uniform sampler2D oldTex,
+							   const uniform float4x4 oldModelViewProj,
+							   uniform sampler2D colors,
+							   uniform sampler2D noiseTex,
+							   uniform float scaleFactor,
+							   uniform float3 bl,
+							   uniform float3 br,
+							   uniform float3 tl,
+							   uniform float3 tr, 
+							   float2 texcoord0,
+							   float3 eyePos,
+							   float3 oldEyePos,
+							   uniform float3 oldbl,
+							   uniform float3 oldbr,
+							   uniform float3 oldtl,
+							   uniform float3 oldtr,
+							   float dummy3
+							   )
+{
+	float2 mynoise = tex2D(noiseTex, texcoord0).xy;
+
+	const float2 offsetTransformed = myreflect(offset, mynoise);
+	const float2 texCoord = texcoord0 + offsetTransformed * scaleFactor;
+	const float3 samplePos = ReconstructSamplePos(colors, texCoord, bl, br, tl, tr) + eyePos;
+
+	// reproject into old frame and calculate projected depth
+	float4 projPos = mul(oldModelViewProj, float4(samplePos, 1.0f));
+	projPos /= projPos.w;
+
+	// fit from unit cube into 0 .. 1
+	const float2 oldTexCoords = projPos.xy * 0.5f + 0.5f;
+
+	// retrieve the sample from the last frame
+	float4 oldCol = tex2Dlod(oldTex, float4(oldTexCoords, 0, 0));
+
+	float oldEyeSpaceDepth = oldCol.z / dummy3;
+	float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
+	float3 oldSamplePos = oldEyePos - viewVec * oldEyeSpaceDepth;
+
+	float dDiff = length(oldSamplePos - samplePos.xyz);
+
+	return dDiff;
+}
+
 
 /** This shader computes the reprojection and stores reprojected color / depth values
@@ -101,16 +148,27 @@
 	// fit from unit cube into 0 .. 1
 	const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
+	//float2 oldTexCoords = texcoord0;
 
 	// retrieve the sample from the last frame
-	float4 oldCol = tex2D(oldTex, oldTexCoords);
-	const float oldEyeSpaceDepth = oldCol.z;
+	float4 oldCol = tex2Dlod(oldTex, float4(oldTexCoords, 0, 0));
+
+	//eyeSpaceDepth = 1e6f;
+	
+	const float dummy3 = 1e-4f;
+	//const float oldEyeSpaceDepth = 1e5f * dummy3;
+	const float oldEyeSpaceDepth = oldCol.z / dummy3;
+
+	//oldTexCoords.x += 0.5f / 1024.0f; oldTexCoords.y += 0.5f / 768.0f;
 
 	float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
 	//float3 oldSamplePos = -viewVec * eyeSpaceDepth + oldEyePos;
-	float3 oldSamplePos = - viewVec * oldCol.z;
-	oldSamplePos += oldEyePos;
+	float3 oldSamplePos = oldEyePos - viewVec * oldEyeSpaceDepth;
 
 	//const float oldDepth = oldCol.z;
 	const float depthDif = length(oldSamplePos - worldPos.xyz);
+	eyeSpaceDepth *= dummy3;
+	
+	//const float depthDif = abs(oldEyeSpaceDepth - eyeSpaceDepth);// * 1e4f;
+	//const float depthDif = abs(oldEyeSpaceDepth - dummy2) * 1e3f;
 
 	//const float oldNumSamples = oldCol.y;
@@ -118,29 +176,23 @@
 	float newWeight;
 
-	/*bool isValid = true;
+	bool isValid = true;
 
 	for (int i = 0; i < NUM_SAMPLES; ++ i) 
 	{
-		const float2 offset = samples[i];
-
-		float2 mynoise = tex2D(noiseTex, texcoord0).xy;
-		const float2 offsetTransformed = myreflect(offset, mynoise);
-
-		const float2 texCoord = texcoord0;// + offsetTransformed * scaleFactor;
-		const float3 samplePos = ReconstructSamplePos(colors, texCoord, bl, br, tl, tr) + eyePos;
-
-		// 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;
-	}*/
+		float sampleDif  = ComputeDifference(samples[i],
+			                  oldTex,
+							  oldModelViewProj,
+							  colors,
+							  noiseTex,
+							  scaleFactor,
+							  bl, br, tl, tr, 
+							  texcoord0,
+							  eyePos,
+							  oldEyePos,
+							  oldbl, oldbr, oldtl, oldtr,
+							  dummy3);
+
+		if (sampleDif > MIN_DEPTH_DIFF) isValid = false;
+	}
 
 	// the number of valid samples in this frame
@@ -153,5 +205,5 @@
 		// if visibility changed in the surrounding area we have to recompute
 		//&& (oldNumSamples > 0.8f * newNumSamples)
-	//	&& isValid
+		&& isValid
 		)
 	{
@@ -166,4 +218,6 @@
 		newWeight = .0f;
 	}
+
+	//illum_col.x = depthDif;
 	
 	//isValid = 0.0f;
@@ -171,4 +225,5 @@
 	illum_col.y = newWeight;
 	illum_col.z = eyeSpaceDepth;
+	//illum_col.z = dummy2;
 
 	return illum_col;
@@ -347,6 +402,7 @@
 	//	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 = col * ao.x;
+	//OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
+	//OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1);
 	//OUT.illum_col.xyz = float3(1.0f - ao.x, ao.y, 0);
 	OUT.illum_col.w = col.w;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3085)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3087)
@@ -267,4 +267,5 @@
 	mDownSampleFbo = new FrameBufferObject(w / 2, h / 2, FrameBufferObject::DEPTH_NONE);
 	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
+	// downsample buffer for the normal texture
 	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
 
@@ -272,5 +273,5 @@
 
 	// create noise texture for ssao
-	CreateNoiseTex2D(mDownSampleFbo->GetWidth(), mDownSampleFbo->GetHeight());
+	CreateNoiseTex2D(mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
 
 	mProjViewMatrix = IdentityMatrix();
@@ -504,7 +505,5 @@
 
 void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo, 
-								   float tempCohFactor
-								   
-								   )
+								   float tempCohFactor)
 {
 #if 0
@@ -516,4 +515,5 @@
 #endif
 
+	// flip flop between illumination buffers
 	GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
 
@@ -571,5 +571,4 @@
 	sCgSsaoProgram->SetValue3f(16, tl.x, tl.y, tl.z);
 	sCgSsaoProgram->SetValue3f(17, tr.x, tr.y, tr.z);
-
 
 	DrawQuad(sCgSsaoProgram);
