Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3122)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3123)
@@ -12,5 +12,5 @@
 //#define SAMPLE_INTENSITY 0.1f
 //#define SAMPLE_INTENSITY 0.07f
-#define SAMPLE_INTENSITY 0.03f
+#define SAMPLE_INTENSITY 0.015f
 
 #define SAMPLE_RADIUS 8e-1f
@@ -58,5 +58,5 @@
 #define NUM_DOWNSAMPLES 9
 
-#define NUM_SSAO_FILTERSAMPLES 20
+#define NUM_SSAO_FILTERSAMPLES 50
 //#define NUM_SSAO_FILTERSAMPLES 40
 //#define NUM_SSAO_FILTERSAMPLES 100
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3074)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3123)
@@ -64,5 +64,6 @@
 Bvh *BvhLoader::Load(const string &filename,  
 					 const SceneEntityContainer &staticEntities,
-					 const SceneEntityContainer &dynamicEntities)
+					 const SceneEntityContainer &dynamicEntities,
+					 int maxDepthForTestingChildren)
 {
 	queue<BvhNode *> tQueue;
@@ -73,5 +74,5 @@
 	cout << "loading bvh" << endl;
 
-	Bvh *bvh = new Bvh(staticEntities, dynamicEntities);
+	Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren);
 
 	BvhNode *root = LoadNextNode(stream, NULL);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3122)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3123)
@@ -57,11 +57,11 @@
 
 
-float BilateralFilter(float2 texCoord,
-					  float4 ao,
-					  uniform sampler2D ssaoTex,
-					  uniform sampler2D normalsTex,
-					  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
-					  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
-					  float scale)
+float DiscontinuityFilter(float2 texCoord,
+						  float4 ao,
+						  uniform sampler2D ssaoTex,
+						  uniform sampler2D normalsTex,
+						  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
+						  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
+						  float scale)
 {
 	float average = .0f;
@@ -76,4 +76,5 @@
 	float4 offs;
 	float depthFactor;
+	float normalFactor;
 
 	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
@@ -83,8 +84,12 @@
 		
 		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), 1e-3f) * depthFactor;
+		//depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
+		
+		depthFactor = max(step(5e-2f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
+		normalFactor = max(step(0.5f, dot(sampleNorm, norm)), 1e-3f);
+
+		w = filterWeights[i] * normalFactor * depthFactor;
+		//w = filterWeights[i] * depthFactor;
 
 		average += aoSample.x * w;
@@ -111,11 +116,13 @@
 	float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
 
-	if ((ao.y < 60.0f) && (col.w < 1e10f))
+	//if ((ao.y < 60.0f) && (col.w < 1e10f))
+	if (col.w < 1e10f)
 	{
-		const static float scaleFactor = 20.0f;
+		//const static float scaleFactor = 10.0f;
+		const static float scaleFactor = 50.0f;
 
 		//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 = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor / (scaleFactor + ao.y));
 		//ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1.0f);
 	}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3122)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3123)
@@ -6,6 +6,4 @@
 
 #define USE_EYESPACE_DEPTH 1
-//#extension GL_EXT_gpu_shader4 : enable
-
 
 
@@ -110,5 +108,5 @@
 	const float projectedEyeSpaceDepth = invlen * length(translatedPos);
 
-	float depthDif = (abs(eyeSpaceDepth - sampleEyeSpaceDepth) > 3.0f) ? 
+	float depthDif = (abs(eyeSpaceDepth - sampleEyeSpaceDepth) > 1.0f) ? 
 		0 : abs(1.0f - oldEyeSpaceDepth / projectedEyeSpaceDepth);
 
@@ -183,5 +181,6 @@
 
 	float notValid = 0.5f;
-	//float overallDepth = 0;
+	float overallDepth = 0;
+
 	const float squaredLen = diffVec.x * diffVec.x + diffVec.y * diffVec.y + diffVec.z * diffVec.z;
 
@@ -209,6 +208,6 @@
 #endif
 
-	//const float clampedOldWeight = clamp(oldPixel.y, .0f, temporalCoherence);
-	const float oldWeight = oldPixel.y;
+	const float oldWeight = clamp(oldPixel.y, .0f, temporalCoherence);
+	//const float oldWeight = oldPixel.y;
 
 	float newWeight;
@@ -225,5 +224,6 @@
 		// increase the weight for convergence
 		newWeight = oldWeight + 1.0f;
-		//if (notValid > 1.0f) newWeight = 2.0f;
+		//if (notValid > 1.0f) newWeight = 10.0f;
+		//if (notValid > 1.0f) newWeight = max(15.0f - notValid * 2.0f, 1.0f);
 	}
 	else
@@ -234,5 +234,5 @@
 	//if (oldPixel.y >= 2000)
 	//	newWeight = min(temporalCoherence + 1, max(oldPixel.y - 70, 50));
-	if (newWeight >= 2000) newWeight = 1000;
+	//if (newWeight >= 2000) newWeight = 1000;
 
 	return float3(oldPixel.x, newWeight, eyeSpaceDepth);
@@ -397,7 +397,5 @@
 	}
 
-	const float clampedWeight = clamp(newWeight, 1, temporalCoherence);
-
-	OUT.illum_col.x = (ao.x + oldSsao * (clampedWeight - 1.0f)) / clampedWeight;
+	OUT.illum_col.x = (ao.x + oldSsao * (newWeight - 1.0f)) / newWeight;
 	OUT.illum_col.y = newWeight;
 	OUT.illum_col.z = invw;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 3102)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 3123)
@@ -665,4 +665,10 @@
 	const Bvh(const SceneEntityContainer &staticEntities, 
 		      const SceneEntityContainer &dynamicEntities);
+	/** Protected constructor taking scene geometry into account
+		Sets the static and dynamic objects for the hierarchy.
+	*/
+	const Bvh(const SceneEntityContainer &staticEntities, 
+		      const SceneEntityContainer &dynamicEntities,
+			  int maxDepthForTestingChildren);
 	/** Called by the constructor. Initializes important members.
 	*/
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 3077)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 3123)
@@ -137,5 +137,5 @@
 
 	if (!scaleToRange)
-	{	
+	{
 		// use tone mapping
 		vtxParams->SetValue1f(8, 1.0f);
@@ -143,5 +143,5 @@
 	else
 	{
-		// no tone mapping => scale to range
+		// no tone mapping => scale to range 
 		vtxParams->SetValue1f(8, 8e-5f);
 	}
@@ -275,4 +275,5 @@
 	// diffuse color: sun color
 	pair<float, float> sun_theta;
+
 	Vector3 zenithColor;
 	vector<Vector3> ABCDE;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h	(revision 3072)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h	(revision 3123)
@@ -23,5 +23,6 @@
 	Bvh *Load(const std::string &filename, 
 			  const SceneEntityContainer &staticEntities,
-			  const SceneEntityContainer &dynamicEntities);
+			  const SceneEntityContainer &dynamicEntities,
+			  int maxDepthForTestingChildren);
 
 protected:
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3075)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3123)
@@ -158,6 +158,5 @@
 	
 	if (!numEntities)
-	{
-		// no box=> just initialize
+	{	// no box=> just initialize
 		box.Initialize();
 	}
@@ -202,4 +201,30 @@
 		mGeometry[mStaticGeometrySize + i] = dynamicEntities[i];
 	}
+}
+
+
+Bvh::Bvh(const SceneEntityContainer &staticEntities, 
+		 const SceneEntityContainer &dynamicEntities,
+		 int maxDepthForTestingChildren)
+{
+	Init();
+
+	mGeometrySize = staticEntities.size() + dynamicEntities.size();
+	mGeometry = new SceneEntity*[mGeometrySize];
+
+	mStaticGeometrySize = staticEntities.size();
+	mDynamicGeometrySize = dynamicEntities.size();
+
+	for (size_t i = 0; i < mStaticGeometrySize; ++ i)
+	{
+		mGeometry[i] = staticEntities[i];
+	}
+
+	for (size_t i = 0; i < mDynamicGeometrySize; ++ i)
+	{
+		mGeometry[mStaticGeometrySize + i] = dynamicEntities[i];
+	}
+
+	mMaxDepthForTestingChildren = maxDepthForTestingChildren;
 }
 
@@ -232,4 +257,5 @@
 	// nodes are tested using the subnodes from 3 levels below
 	mMaxDepthForTestingChildren = 3;
+	mMaxDepthForTestingChildren = 0;
 	//mMaxDepthForTestingChildren = 4;
 
@@ -567,4 +593,24 @@
 	return numNodes;
 }
+
+#if TODO
+void Bvh::RenderBoundsImmediate(const BvhNodeContainer &nodes, RenderState *state)
+{
+	/////////
+	//-- Render the tight bounds in immediate mode
+	BvhNodeContainer::const_iterator nit, nit_end = nodes.end();
+
+	for (nit = nodes.begin(); nit != nit_end; ++ nit)
+	{
+		BvhNode *node = *nit;
+
+		for (int size_t i = 0; i < node->mNumTestNodes; ++ i)
+		{
+			BvhNode *testNode = node->mTestNodesIdx
+			RenderBoundingBoxImmediate((*nit)->GetBox());
+		}
+	}
+}
+#endif
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3120)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3123)
@@ -107,4 +107,6 @@
 MotionPath *motionPath = NULL;
 
+int maxDepthForTestingChildren = 3;
+
 
 /// the technique used for rendering
@@ -358,15 +360,18 @@
 		env.GetIntParam(string("maxBatchSize"), maxBatchSize);
 		env.GetIntParam(string("trianglesPerVirtualLeaf"), trianglesPerVirtualLeaf);
+		env.GetIntParam(string("winWidth"), winWidth);
+		env.GetIntParam(string("winHeight"), winHeight);
+		env.GetIntParam(string("shadowSize"), shadowSize);
+		env.GetIntParam(string("maxDepthForTestingChildren"), maxDepthForTestingChildren);
 
 		env.GetFloatParam(string("keyForwardMotion"), keyForwardMotion);
 		env.GetFloatParam(string("keyRotation"), keyRotation);
-
-		env.GetIntParam(string("winWidth"), winWidth);
-		env.GetIntParam(string("winHeight"), winHeight);
 		env.GetFloatParam(string("tempCohFactor"), ssaoTempCohFactor);
+
+		
+		
 		env.GetVectorParam(string("camPosition"), camPos);
 		env.GetVectorParam(string("camDirection"), camDir);
 		env.GetVectorParam(string("lightDirection"), lightDir);
-		env.GetIntParam(string("shadowSize"), shadowSize);
 
 		env.GetBoolParam(string("useFullScreen"), useFullScreen);
@@ -467,5 +472,5 @@
 
 	//LoadModel("fisch.dem", dynamicObjects);
-	LoadModel("hbuddha.dem", dynamicObjects);
+	/*LoadModel("hbuddha.dem", dynamicObjects);
 	buddha = dynamicObjects.back();
 	
@@ -492,5 +497,5 @@
 		ent->SetTransform(transform);
 		dynamicObjects.push_back(ent);
-	}
+	}*/
 
 
@@ -501,5 +506,5 @@
 
 	BvhLoader bvhLoader;
-	bvh = bvhLoader.Load(bvh_filename, sceneEntities, dynamicObjects);
+	bvh = bvhLoader.Load(bvh_filename, sceneEntities, dynamicObjects, maxDepthForTestingChildren);
 
 	if (!bvh)
@@ -908,4 +913,5 @@
 void MainLoop() 
 {	
+#if 0
 	GPUProgramParameters *vtxParams = 
 		buddha->GetShape(0)->GetMaterial()->GetTechnique(1)->GetVertexProgramParameters();
@@ -931,5 +937,5 @@
 	Matrix4x4 rotMatrix = RotationZMatrix(M_PI * 1e-3f);
 	dynamicObjects[1]->GetTransform()->MultMatrix(rotMatrix);
-
+#endif
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3122)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3123)
@@ -409,4 +409,5 @@
 
 		ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
+		//ssaoFilterWeights[i] = 1.0f;
 
 		ssaoFilterOffsets[2 * i + 0] *= xoffs;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3119)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3123)
@@ -12,4 +12,6 @@
 # triangles per bvh leaf (influences hierarchy depth vs. occlusion power)
 trianglesPerVirtualLeaf=300
+
+maxDepthForTestingChildren=3
 
 ################
