Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3092)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3095)
@@ -42,5 +42,5 @@
 #define MAX_LOD_LEVEL 10
 
-#define MIN_DEPTH_DIFF 1e-3f
+#define MIN_DEPTH_DIFF 5e-2f
 #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 3092)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3095)
@@ -73,9 +73,6 @@
 	OUT.color = col;
 
-	// store scaled view vector from now on so wie don't have to normalize for e.g., ssao
-	float3 viewDir = IN.view;
-	const float lenView = length(viewDir);
-
-	OUT.color.w = color.w / lenView;
+	// store scaled view vector so wie don't have to normalize for e.g., ssao
+	OUT.color.w = color.w;// / length(IN.view);
 
 	return OUT;
@@ -132,5 +129,5 @@
 	float3 x1 = lerp(bl, tl, w.y);
 	float3 x2 = lerp(br, tr, w.y); 
-	float3 v = lerp(x1, x2, w.x); 
+	float3 v  = lerp(x1, x2, w.x); 
 
 	return v;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3094)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3095)
@@ -55,5 +55,5 @@
 {
 	const float eyeSpaceDepth = tex2Dlod(tex, float4(texcoord, 0, 0)).w;
-	float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
+	float3 viewVec = normalize(Interpol(texcoord, bl, br, tl, tr));
 	float3 samplePos = -viewVec * eyeSpaceDepth;
 
@@ -81,9 +81,9 @@
 							   )
 {
-	float2 mynoise = tex2D(noiseTex, texcoord0).xy;
+	const float2 mynoise = tex2D(noiseTex, texcoord0).xy;
 
 	const float2 offsetTransformed = myreflect(offset, mynoise);
 	float2 texCoord = texcoord0 + offsetTransformed * scaleFactor;
-	//texCoord.x += 0.5f / 1024.0f; texCoord.y += 0.5f / 768.0f;
+	
 	const float3 samplePos = ReconstructSamplePos(colors, texCoord, bl, br, tl, tr);
 
@@ -93,16 +93,15 @@
 
 	// fit from unit cube into 0 .. 1
-	float2 oldTexCoords = projPos.xy * 0.5f + 0.5f;
+	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.w;
-	float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
-	float3 oldSamplePos = oldEyePos - viewVec * oldEyeSpaceDepth;
-
+	const float4 oldPixel = tex2Dlod(oldTex, float4(oldTexCoords, .0f, .0f));
+
+	const float oldEyeSpaceDepth = oldPixel.w;
+	const float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
+
+	const float3 oldSamplePos = oldEyePos - viewVec * oldEyeSpaceDepth;
+	
 	float dDiff = length(oldSamplePos - samplePos.xyz);
-
-	if (length(worldPos.xyz - samplePos.xyz) > 5.0f)
-		dDiff = .0f;
+	if (length(worldPos.xyz - samplePos.xyz) > 5.0f) dDiff = .0f;
 
 	return dDiff;
@@ -113,26 +112,26 @@
 	as well as a boolean that 
 */
-float4 temporalSmoothing(float4 eyeSpacePos,
-						 uniform sampler2D oldTex,
-						 const uniform float4x4 oldModelViewProj,
-						 uniform float temporalCoherence,
-						 uniform float2 ao,
-						 uniform float2 samples[NUM_SAMPLES],
-						 uniform sampler2D colors,
-						 uniform sampler2D noiseTex,
-						 uniform float scaleFactor,
-						 uniform float3 bl,
-						 uniform float3 br,
-						 uniform float3 tl,
-						 uniform float3 tr, 
-						 float2 texcoord0,
-						 float eyeSpaceDepth,
-						 float3 oldEyePos,
-						 uniform float3 oldbl,
-						 uniform float3 oldbr,
-						 uniform float3 oldtl,
-						 uniform float3 oldtr
-						 //,const uniform float4x4 inverseModelTrafo
-						 //, float id
+inline float4 temporalSmoothing(float4 worldPos,
+								float eyeSpaceDepth,
+								float2 ao,
+								float2 texcoord0,
+								float3 oldEyePos,
+								sampler2D oldTex,
+								float4x4 oldModelViewProj,
+								float temporalCoherence,
+								float2 samples[NUM_SAMPLES],
+								sampler2D colors,
+								sampler2D noiseTex,
+								float scaleFactor,
+								float3 bl,
+								float3 br,
+								float3 tl,
+								float3 tr, 
+								float3 oldbl,
+								float3 oldbr,
+								float3 oldtl,
+								float3 oldtr
+								//,const uniform float4x4 inverseModelTrafo
+								//, float id
 						 )
 {
@@ -147,21 +146,29 @@
 	//else trafo = inverseModelTrafo * oldModelViewProj;
 
-	// reproject into old frame and calculate projected depth
-	float4 backProjPos = mul(oldModelViewProj, eyeSpacePos);
+	float4 dummyPt = worldPos - float4(oldEyePos,0);
+
+	// reproject into old frame and calculate texture position of sample in old frame
+	float4 backProjPos = mul(oldModelViewProj, dummyPt);
 	backProjPos /= backProjPos.w;
 	// fit from unit cube into 0 .. 1
 	const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
-	//float2 oldTexCoords = texcoord0;
+	//const float2 oldTexCoords = texcoord0;
 
 	// retrieve the sample from the last frame
-	float4 oldCol = tex2Dlod(oldTex, float4(oldTexCoords, 0, 0));
-	//float4 oldCol = tex2D(oldTex, oldTexCoords);
-
-	const float oldEyeSpaceDepth = oldCol.w;
-
-	float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
-	float3 oldEyeSpacePos = oldEyePos - viewVec * oldEyeSpaceDepth;
-
-	const float depthDif = length(oldEyeSpacePos - eyeSpacePos.xyz);
+	const float4 oldPixel = tex2Dlod(oldTex, float4(oldTexCoords, .0f, .0f));
+	// calculate eye space position of sample in old frame
+	const float oldEyeSpaceDepth = oldPixel.w;
+
+	// vector from eye pos to old sample 
+	const float3 viewVec = normalize(Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr));
+	const float3 oldWorldPos = oldEyeSpaceDepth * (-viewVec) + oldEyePos;
+	//const float3 oldWorldPos = - viewVec * eyeSpaceDepth;
+	//const float3 dummy14 = Interpol(texcoord0, bl, br, tl, tr);
+
+	// the euclidian world space distance
+	const float depthDif = length(oldWorldPos - worldPos.xyz);
+	//const float depthDif = length(dummy14 - viewVec);
+	//const float depthDif = length(oldEyeSpaceDepth - eyeSpaceDepth);
+	//const float depthDif = length(oldEyePos);
 
 	float notValid = 0.5f;
@@ -184,10 +191,9 @@
 	}*/
 
-	//if (notValid < 1.0f) temporalCoherence = 4.0f;
 	// the number of valid samples in this frame
 	//const float newNumSamples = ao.y;
-
 	//const float oldNumSamples = oldCol.y;
-	const float oldWeight = clamp(oldCol.y, .0f, temporalCoherence);
+
+	const float oldWeight = clamp(oldPixel.y, .0f, temporalCoherence);
 	float newWeight;
 
@@ -203,6 +209,6 @@
 		// increase the weight for convergence
 		newWeight = oldWeight + 1.0f;
-		illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight;
-		if (notValid > 1.0f) newWeight = 2.0f;
+		illum_col.x = (ao.x + oldPixel.x * oldWeight) / newWeight;
+		//if (notValid > 1.0f) newWeight = 2.0f;
 	}
 	else
@@ -214,5 +220,5 @@
 	illum_col.y = newWeight;
 	illum_col.w = eyeSpaceDepth;
-	
+	//illum_col.y = depthDif;
 
 	return illum_col;
@@ -269,6 +275,6 @@
 		float3 dirSample = samplePos - centerPosition;
 		const float lengthToSample = length(dirSample);
-		// normalize
-		dirSample /= lengthToSample;
+
+		dirSample /= lengthToSample; // normalize
 
 		// angle between current normal and direction to sample controls AO intensity.
@@ -283,4 +289,5 @@
 		// 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 * dot(viewDir, currentNormal);
 		total_ao += cosAngle * aoContrib * viewCorrection;
@@ -292,6 +299,4 @@
 	return float2(max(0.0f, 1.0f - total_ao), numSamples);
 }
-
-//#pragma position_invariant main
 
 /** The mrt shader for screen space ambient occlusion
@@ -322,7 +327,7 @@
 
 	// reconstruct position from the eye space depth
-	const float3 viewDir = IN.view;
+	const float3 viewDir = normalize(IN.view);
 	const float eyeSpaceDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
-	const float4 eyeSpacePos = float4(-viewDir * eyeSpaceDepth, 1);
+	const float4 eyeSpacePos = float4(eyeSpaceDepth * (-viewDir), 1.0f);
 
 
@@ -333,13 +338,15 @@
 	float w = SAMPLE_RADIUS / projPos.w;
 	
-	const float2 ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, w, bl, br, tl, tr, normalize(viewDir));
+	const float2 ao = ssao(IN, colors, noiseTex, samples, normal, 
+		                   eyeSpacePos.xyz, w, bl, br, tl, tr, normalize(viewDir));
 
 
 	/////////////////
 	//-- compute temporally smoothing
-	
-	OUT.illum_col = temporalSmoothing(eyeSpacePos, oldTex, oldModelViewProj, temporalCoherence, ao,
-		                              samples, colors, noiseTex, w, bl, br, tl, tr, IN.texCoord, eyeSpaceDepth,
-									  oldEyePos, oldbl, oldbr, oldtl, oldtr);
+
+	OUT.illum_col = temporalSmoothing(eyeSpacePos, eyeSpaceDepth, ao, IN.texCoord, oldEyePos,
+		                              oldTex, oldModelViewProj, temporalCoherence,
+		                              samples, colors, noiseTex, w, bl, br, tl, tr,
+									  oldbl, oldbr, oldtl, oldtr);
 
 	return OUT;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3094)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3095)
@@ -422,40 +422,4 @@
 	InitFrame();
 
-	// switch roles of old and new fbo
-	// the algorihm uses two input fbos, where the one
-	// contais the color buffer from the last frame, 
-	// the other one will be written
-
-	mIllumFboIndex = 2 - mIllumFboIndex;
-	
-	// enable fragment shading
-	ShaderManager::GetSingleton()->EnableFragmentProfile();
-
-	glDisable(GL_ALPHA_TEST);
-	glDisable(GL_TEXTURE_2D);
-	glDisable(GL_LIGHTING);
-	glDisable(GL_BLEND);
-	glDisable(GL_DEPTH_TEST);
-
-	glPolygonMode(GL_FRONT, GL_FILL);
-
-	glMatrixMode(GL_PROJECTION);
-	glPushMatrix();
-	glLoadIdentity();
-
-	const float offs = 0.5f;
-	//glOrtho(-offs, offs, -offs, offs, 0, 1);
-	//glOrtho(0, 1, 0, 1, 0, 1);
-	gluOrtho2D(0, 1, 0, 1);
-
-
-	glMatrixMode(GL_MODELVIEW);
-	glPushMatrix();
-	glLoadIdentity();
-
-	
-	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mWidth, mHeight);
-
 	if (shadowMap)
 		FirstPassShadow(fbo, light, shadowMap);
@@ -534,10 +498,12 @@
 	glDrawBuffers(1, mrt + mIllumFboIndex);
 
-	sCgSsaoProgram->SetTexture(0, colorsTex);
-	sCgSsaoProgram->SetTexture(1, normalsTex);
-	sCgSsaoProgram->SetTexture(2, oldTex);
-	sCgSsaoProgram->SetTexture(3, noiseTex);
-
-	sCgSsaoProgram->SetValue1f(4, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
+	int i = 0;
+
+	sCgSsaoProgram->SetTexture(i ++, colorsTex);
+	sCgSsaoProgram->SetTexture(i ++, normalsTex);
+	sCgSsaoProgram->SetTexture(i ++, oldTex);
+	sCgSsaoProgram->SetTexture(i ++, noiseTex);
+
+	sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
 	
 	if (mUseTemporalCoherence || mRegenerateSamples)
@@ -549,33 +515,22 @@
 		// needs longer to converge
 		GenerateSamples(mSamplingMethod); 
-		sCgSsaoProgram->SetArray2f(5, (float *)samples2, NUM_SAMPLES);
-	}
-	
-	Vector3 bl = mCornersView[0];
-	Vector3 br = mCornersView[1];
-	Vector3 tl = mCornersView[2];
-	Vector3 tr = mCornersView[3];
-
-	sCgSsaoProgram->SetValue3f(6, bl.x, bl.y, bl.z);
-	sCgSsaoProgram->SetValue3f(7, br.x, br.y, br.z);
-	sCgSsaoProgram->SetValue3f(8, tl.x, tl.y, tl.z);
-	sCgSsaoProgram->SetValue3f(9, tr.x, tr.y, tr.z);
-
-	sCgSsaoProgram->SetMatrix(10, mProjViewMatrix);
-	sCgSsaoProgram->SetMatrix(11, mOldProjViewMatrix);
-
-	bl = mOldCornersView[0];
-	br = mOldCornersView[1];
-	tl = mOldCornersView[2];
-	tr = mOldCornersView[3];
-
-	Vector3 d = mOldEyePos - mEyePos;
-	//Vector3 d = mEyePos - mOldEyePos;
-
-	sCgSsaoProgram->SetValue3f(12, d.x, d.y, d.z);
-	sCgSsaoProgram->SetValue3f(13, bl.x, bl.y, bl.z);
-	sCgSsaoProgram->SetValue3f(14, br.x, br.y, br.z);
-	sCgSsaoProgram->SetValue3f(15, tl.x, tl.y, tl.z);
-	sCgSsaoProgram->SetValue3f(16, tr.x, tr.y, tr.z);
+		sCgSsaoProgram->SetArray2f(i, (float *)samples2, NUM_SAMPLES);
+	}
+	
+	++ i;
+
+	for (int j = 0; j < 4; ++ j, ++ i)
+		sCgSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
+
+	sCgSsaoProgram->SetMatrix(i ++, mProjViewMatrix);
+	sCgSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);
+
+	Vector3 de = mOldEyePos - mEyePos;
+	//Vector3 de = mEyePos - mOldEyePos;
+
+	sCgSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);
+
+	for (int j = 0; j < 4; ++ j, ++ i)
+		sCgSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
 
 	DrawQuad(sCgSsaoProgram);
@@ -1001,5 +956,12 @@
 void DeferredRenderer::InitFrame()
 {
+	for (int i = 0; i < 4; ++ i)
+		mOldCornersView[i] = mCornersView[i];
+
 	mOldProjViewMatrix = mProjViewMatrix;
+	mOldEyePos = mEyePos;
+
+
+	///////////////////
 
 	Matrix4x4 matViewing, matProjection;
@@ -1010,14 +972,40 @@
 
 	mProjViewMatrix = matViewing * matProjection;
-
-	for (int i = 0; i < 4; ++ i)
-	{
-		mOldCornersView[i] = mCornersView[i];
-	}
-
 	ComputeViewVectors(mCamera, mCornersView[0], mCornersView[1], mCornersView[2], mCornersView[3]);
-
-	mOldEyePos = mEyePos;
 	mEyePos = mCamera->GetPosition();
+
+
+	// switch roles of old and new fbo
+	// the algorihm uses two input fbos, where the one
+	// contais the color buffer from the last frame, 
+	// the other one will be written
+
+	mIllumFboIndex = 2 - mIllumFboIndex;
+	
+	// enable fragment shading
+	ShaderManager::GetSingleton()->EnableFragmentProfile();
+
+	glDisable(GL_ALPHA_TEST);
+	glDisable(GL_TEXTURE_2D);
+	glDisable(GL_LIGHTING);
+	glDisable(GL_BLEND);
+	glDisable(GL_DEPTH_TEST);
+
+	glPolygonMode(GL_FRONT, GL_FILL);
+
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+	glLoadIdentity();
+
+	gluOrtho2D(0, 1, 0, 1);
+
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	glLoadIdentity();
+
+	
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mWidth, mHeight);
 }
 
