Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h	(revision 3110)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h	(revision 3120)
@@ -89,4 +89,7 @@
 	*/
 	int GetNumLODLevels() const { return (int)mLODLevels.size(); }
+	/** Returns center point of this shape.
+	*/
+	Vector3 GetCenter() const {return mCenter; }
 	/** Returns transformed center point of this shape.
 	*/
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3115)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3120)
@@ -29,5 +29,6 @@
 //-- reprojection
 
-#define MIN_DEPTH_DIFF 2e-2f
+//#define MIN_DEPTH_DIFF 2e-2f
+#define MIN_DEPTH_DIFF 5e-3f
 #define PRECISION_SCALE 1e-1f
 
@@ -57,7 +58,7 @@
 #define NUM_DOWNSAMPLES 9
 
-//#define NUM_SSAO_FILTERSAMPLES 28
+#define NUM_SSAO_FILTERSAMPLES 20
 //#define NUM_SSAO_FILTERSAMPLES 40
-#define NUM_SSAO_FILTERSAMPLES 100
+//#define NUM_SSAO_FILTERSAMPLES 100
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3109)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3120)
@@ -36,4 +36,25 @@
 
 
+float Filter(float2 texCoord, 
+			 uniform sampler2D ssaoTex,
+			 uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
+			 uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
+			 float scale)
+{
+	float average = .0f;
+	float w = .0f;
+
+	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
+	{	
+		average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i] * scale, 0, 0)).x;
+		w += filterWeights[i];
+	}
+
+	average *= 1.0f / (float)w;
+
+	return average;
+}
+
+
 float BilateralFilter(float2 texCoord,
 					  float4 ao,
@@ -41,10 +62,11 @@
 					  uniform sampler2D normalsTex,
 					  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
-					  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES])
+					  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
+					  float scale)
 {
 	float average = .0f;
 	float total_w = .0f;
 
-	//const float eyeSpaceDepth = ao.w;
+	const float eyeSpaceDepth = ao.w;
 	const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
 
@@ -52,22 +74,23 @@
 	float3 sampleNorm;
 	float w;
+	float4 offs;
+	float depthFactor;
 
 	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
 	{
-		float4 offs = float4(texCoord + filterOffs[i], 0, 0);
+		offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
 		aoSample = tex2Dlod(ssaoTex, offs);
-		//aoSample = tex2D(ssaoTex, texCoord + filterOffs[i]);
-		//float sampleEyeSpaceDepth = aoSample.w;
-		//factor = abs(eyeSpaceDepth - sampleEyeSpaceDepth) / eyeSpaceDepth;
+		
 		sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
+		depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
 		//sampleNorm = tex2Dlod(normalsTex, offs).xyz;
 
-		w = filterWeights[i] * max(dot(sampleNorm, norm), .0f);
+		w = filterWeights[i] * max(dot(sampleNorm, norm), .0f) * depthFactor;
 
-		average += w * aoSample.x;
+		average += aoSample.x * w;
 		total_w += w;
 	}
 
-	average *= 1.0f / max(total_w, 1e-5f);
+	average *= 1.0f / max(total_w, 1e-6f);
 
 	return average;
@@ -88,12 +111,17 @@
 	float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
 
-	//if (ao.y < 10.0f) ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
-	//if (ao.y < 10.0f) ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights);
+	if ((ao.y < 60.0f) && (col.w < 1e10f))
+	{
+		const static float scaleFactor = 10.0f;
 
-	//OUT.illum_col = col * ao.x;
-	//OUT.illum_col = float4(ao.x, ao.y, ao.z, col.w);
-	OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
+		//ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
+		//ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
+		ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor / (scaleFactor + ao.y));
+		//ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1.0f);
+	}
+
+	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/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3119)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3120)
@@ -189,5 +189,5 @@
 	//float overallDepth = 0;
 	const float squaredLen = diffVec.x * diffVec.x + diffVec.y * diffVec.y + diffVec.z * diffVec.z;
-
+#if 1
 	if (squaredLen < 1e-8f) // object not dynamic
 	{
@@ -210,5 +210,5 @@
 		}
 	}
-
+#endif
 	const float oldWeight = clamp(oldPixel.y, .0f, temporalCoherence);
 	float newWeight;
@@ -220,5 +220,5 @@
 		// if visibility changed in the surrounding area we have to recompute
 		//&& (oldNumSamples > 0.8f * newNumSamples)
-		&& (notValid < 1.0f)
+		//&& (notValid < 1.0f)
 		)
 	{
@@ -226,5 +226,5 @@
 		newWeight = oldWeight + 1.0f;
 		illum_col.x = (ao.x + oldPixel.x * oldWeight) / newWeight;
-		//if (notValid > 1.0f) newWeight = 2.0f;
+		if (notValid > 1.0f) newWeight = 2.0f;
 	}
 	else
@@ -292,4 +292,5 @@
 
 		//if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples;
+
 		const float3 samplePos = ReconstructSamplePos(colors, texcoord, bl, br, tl, tr);
 
@@ -378,4 +379,5 @@
 	float3 diffVec = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
 
+
 	/////////////////
 	//-- compute temporal reprojection
@@ -393,54 +395,4 @@
 									  diffVec);
 
-	//OUT.illum_col.xyz = id;
-	//OUT.illum_col.xyz = normal * 0.5f + 0.5f;
 	return OUT;
 }
-
-
-float Filter(float2 texCoord, 
-			 uniform sampler2D ssaoTex,
-			 uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
-			 uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
-			 float scale)
-{
-	float average = .0f;
-	float w = .0f;
-
-	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
-	{	
-		average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i] * scale * 2, 0, 0)).x;
-		w += filterWeights[i];
-	}
-
-	average *= 1.0f / (float)w;
-
-	return average;
-}
-
-
-pixel combine(fragment IN, 
-			  uniform sampler2D colorsTex,
-			  uniform sampler2D ssaoTex,
-			  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
-			  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
-			  )
-{
-	pixel OUT;
-
-	const float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
-	float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
-
-	//if ((ao.y < 10.0f) && (col.w < 1e10f))
-	//	ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));//ao.z);
-
-	OUT.illum_col = col * ao.x;
-	//OUT.illum_col = float4(ao.y, ao.y, ao.y, col.w);
-	//OUT.illum_col = float4(ao.x, ao.y, ao.z, col.w);
-	//OUT.illum_col = float4(ao.x, 0, 0, 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;
-
-	return OUT;
-}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 3114)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 3120)
@@ -164,5 +164,5 @@
 {
 	if (mTransform->IsIdentity()) return mCenter;
-	return mTransform->GetMatrix() *  mCenter;
+	return mTransform->GetMatrix() * mCenter;
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3119)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3120)
@@ -301,7 +301,10 @@
 void CreateAnimation();
 
-
-// the new and the old viewProjection matrix of the current camera
+SceneQuery *GetOrCreateSceneQuery();
+
+
+// new view projection matrix of the camera
 static Matrix4x4 viewProjMat = IdentityMatrix();
+// the old view projection matrix of the camera
 static Matrix4x4 oldViewProjMat = IdentityMatrix();
 
@@ -463,6 +466,6 @@
 	//-- load some dynamic stuff
 
-	LoadModel("fisch.dem", dynamicObjects);
-	//LoadModel("hbuddha.dem", dynamicObjects);
+	//LoadModel("fisch.dem", dynamicObjects);
+	LoadModel("hbuddha.dem", dynamicObjects);
 	buddha = dynamicObjects.back();
 	
@@ -482,5 +485,7 @@
 		offs.y = RandomValue(.0f, 50.0f);
 
-		transl = TranslationMatrix(sceneCenter + offs);
+		Vector3 newPos = sceneCenter + offs;
+
+		transl = TranslationMatrix(newPos);
 		Transform3 *transform = resourceManager->CreateTransform(transl);
 
@@ -912,7 +917,19 @@
 	buddha->GetTransform()->SetMatrix(trafo);
 
+	/*for (int i = 0; i < 10; ++ i)
+	{
+		SceneEntity *ent = dynamicObjects[i];
+		Vector3 newPos = ent->GetWorldCenter();
+
+		if (GetOrCreateSceneQuery()->CalcIntersection(newPos))
+		{
+			Matrix4x4 mat = TranslationMatrix(newPos - ent->GetCenter());// + Vector3(0, 0, ent->GetBoundingBox().Size(2) * 0.5f));
+			ent->GetTransform()->SetMatrix(mat);
+		}
+	}*/
+
 	Matrix4x4 rotMatrix = RotationZMatrix(M_PI * 1e-3f);
-
 	dynamicObjects[1]->GetTransform()->MultMatrix(rotMatrix);
+
 
 
@@ -2007,13 +2024,17 @@
 
 
+SceneQuery *GetOrCreateSceneQuery()
+{
+	if (!sceneQuery)
+		sceneQuery = new SceneQuery(bvh->GetBox(), traverser, &renderState);
+
+	return sceneQuery;
+}
+
+
 void PlaceViewer(const Vector3 &oldPos)
 {
-	if (!sceneQuery)
-	{
-		sceneQuery = new SceneQuery(bvh->GetBox(), traverser, &renderState);
-	}
-
 	Vector3 playerPos = camera->GetPosition();
-	bool validIntersect = sceneQuery->CalcIntersection(playerPos);
+	bool validIntersect = GetOrCreateSceneQuery()->CalcIntersection(playerPos);
 
 	if (validIntersect) 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3118)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3120)
@@ -321,6 +321,6 @@
 	sCgGiProgram = sm->CreateFragmentProgram("globillum", "main", "giFrag");
 	sCgCombineIllumProgram = sm->CreateFragmentProgram("globillum", "combine", "combineGi");
-	//sCgCombineSsaoProgram = sm->CreateFragmentProgram("combineSsao", "combine", "combineSsao");
-	sCgCombineSsaoProgram = sm->CreateFragmentProgram("ssao", "combine", "combineSsao");
+	sCgCombineSsaoProgram = sm->CreateFragmentProgram("combineSsao", "combine", "combineSsao");
+	//sCgCombineSsaoProgram = sm->CreateFragmentProgram("ssao", "combine", "combineSsao");
 	sCgAntiAliasingProgram = sm->CreateFragmentProgram("antialiasing", "main", "antiAliasing");
 	sCgToneProgram = sm->CreateFragmentProgram("tonemap", "ToneMap", "toneMap");
@@ -364,6 +364,6 @@
 	////////////////
 
-	string combineSsaoParams[] = {"colorsTex", "ssaoTex", "filterOffs", "filterWeights"};
-	sCgCombineSsaoProgram->AddParameters(combineSsaoParams, 0, 4);
+	string combineSsaoParams[] = {"colorsTex", "normalsTex", "ssaoTex", "filterOffs", "filterWeights"};
+	sCgCombineSsaoProgram->AddParameters(combineSsaoParams, 0, 5);
 
 	//////////////
@@ -395,6 +395,6 @@
 
 	//const float filterWidth = 10.0f;
-	const float filterWidth = 5.0f;
-	//const float filterWidth = 60.0f;
+	//const float filterWidth = 6.0f;
+	const float filterWidth = 20.0f;
 
 	//const float xoffs = filterWidth / mDownSampleFbo->GetWidth();
@@ -408,8 +408,8 @@
 		float y = ssaoFilterOffsets[2 * i + 1];
 
+		ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
+
 		ssaoFilterOffsets[2 * i + 0] *= xoffs;
 		ssaoFilterOffsets[2 * i + 1] *= yoffs;
-
-		ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
 	}
 
@@ -747,5 +747,5 @@
 	int i = 0;
 	sCgCombineSsaoProgram->SetTexture(i ++, colorsTex);
-	//sCgCombineSsaoProgram->SetTexture(i ++, normalsTex);
+	sCgCombineSsaoProgram->SetTexture(i ++, normalsTex);
 	sCgCombineSsaoProgram->SetTexture(i ++, ssaoTex);
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter/VboFormatConverter.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter/VboFormatConverter.cpp	(revision 3119)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter/VboFormatConverter.cpp	(revision 3120)
@@ -97,7 +97,8 @@
 			   &tangent.x, &tangent.y, &tangent.z);
 
-		vtx.x *= 0.07f;
-		vtx.y *= 0.07f;
-		vtx.z *= 0.07f;
+		const float scale = 0.05f;
+		vtx.x *= scale;
+		vtx.y *= scale;
+		vtx.z *= scale;
 
 		geom->mVertices[line] = vtx;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/toto.txt
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/toto.txt	(revision 3098)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/toto.txt	(revision 3120)
@@ -76,2 +76,23 @@
 to compare the depth of the current pixel, but the depth of the
 samples taken for ao
+
+
+idea for incorporation of dynamic objects:
+
+store object id with render target
+for each object we know the trafo
+when doing the back projection =>
+
+as usual:
+we have the world space position of the current pixel
+find pixel from last frame using the old projection view trafo
+
+but now we first apply the inverse transformation that brought the
+last pixel to the current pixel and then the old projection view!!
+
+=> now we do the equality comparison as usual
+
+
+for reducing flickering:
+
+
