Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3361)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3362)
@@ -26,4 +26,5 @@
 #define VIEW_CORRECTION_SCALE 1.0f
 
+//#define SSAO_CONVERGENCE_THRESHOLD 300.0f
 #define SSAO_CONVERGENCE_THRESHOLD 700.0f
 //#define SSAO_CONVERGENCE_THRESHOLD 1500.0f
@@ -73,3 +74,6 @@
 #define PERFORMANCE_TEST 1
 
+#define USE_OPTIMIZATION 0
+
+
 #endif // __SHADERENV_H
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3360)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3362)
@@ -83,5 +83,5 @@
 
 	// account for alpha blending
-	if (texColor.w < 0.5f) discard;
+	if (texColor.w < .5f) discard;
 
 	pixel pix;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3361)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3362)
@@ -205,5 +205,6 @@
 		//-- (affects performance for some reason!)
 
-		if (convergence < SSAO_CONVERGENCE_THRESHOLD)
+		if (!USE_OPTIMIZATION ||
+			(convergence < SSAO_CONVERGENCE_THRESHOLD))
 		{
 			float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
@@ -355,5 +356,6 @@
 		//-- (affects performance for some reason!)
 
-		if (convergence < SSAO_CONVERGENCE_THRESHOLD)
+		if (!USE_OPTIMIZATION ||
+			(convergence < SSAO_CONVERGENCE_THRESHOLD))
 		{
 			float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
@@ -493,5 +495,6 @@
 		//-- (affects performance for some reason!)
 
-		if (convergence < SSAO_CONVERGENCE_THRESHOLD)
+		if (!USE_OPTIMIZATION ||
+			(convergence < SSAO_CONVERGENCE_THRESHOLD))
 		{
 			float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
@@ -539,40 +542,39 @@
 
 
-#ifdef PERFORMANCE_TEST
+//#ifdef PERFORMANCE_TEST
+#if 1
 		// check if the samples have been valid in the last frame
 		// only mark sample as invalid if in the last / current frame
 		// they possibly have any influence on the ao
 
-		//const float pixelValid = sampleColor.x;
-		//const float pixelValid = length(sampleDiffVec - diffVec) < 1e-3f ? .0f : 10.0f;
-		//float pixelValid = max(sampleColor.x, length(sampleDiffVec - diffVec) < 1e-3f ? .0f : 10.0f);
-		float pixelValid = sampleColor.x;
-
-		if (pixelValid < 1)
-		{
-			const float3 sampleDiffVec = tex2Dlod(attribsTex, float4(texcoord, .0f, .0f)).xyz;
-			pixelValid = length(sampleDiffVec - diffVec) < 1e-3f ? .0f : 10.0f;
-		}
-
-		// hack:
-		// we check if the sample could have been near enough to the current pixel 
-		// or if the angle is small enough
-		// to have any influence in the current or last frame
-
-#if 1
-		const float changeFactor = sampleColor.y;
-		const float partlyResetThres = 1.0f;
-
-		if (0)//pixelValid <= partlyResetThres)
-		{
-			const float tooFarAway = step(0.5f, lengthToSample - changeFactor);
-			validSamples = max(validSamples, pixelValid * (1.0f - tooFarAway) * step(-0.1f, cosAngle));
-		}
-		else
-		{
-			validSamples = max(validSamples, pixelValid);
-		}
-#endif
-
+		if (cosAngle > 0)
+		{
+			float pixelValid = sampleColor.x;
+
+			if (1)//pixelValid < 0.5f)
+			{
+				const float3 sampleDiffVec = tex2Dlod(attribsTex, float4(texcoord, .0f, .0f)).xyz;
+				pixelValid = max(pixelValid, length(sampleDiffVec - diffVec) < 5e-3f ? .0f : 10.0f);
+				//pixelValid = length(sampleDiffVec - diffVec) < 1e-3f ? .0f : 10.0f;
+			}
+
+			// hack:
+			// we check if the sample could have been near enough to the current pixel 
+			// or if the angle is small enough
+			// to have any influence in the current or last frame
+
+			const float changeFactor = sampleColor.y;
+			const float partlyResetThres = 1.0f;
+
+			if (0)//pixelValid <= partlyResetThres)
+			{
+				const float tooFarAway = step(0.5f, lengthToSample - changeFactor);
+				validSamples = max(validSamples, pixelValid * (1.0f - tooFarAway) * step(-0.1f, cosAngle));
+			}
+			else
+			{
+				validSamples = max(validSamples, pixelValid);
+			}
+		}
 #ifdef USE_GTX
 		// we can bail out early and use a minimal #samples)
@@ -657,4 +659,5 @@
 	
 #ifdef PERFORMANCE_TEST	
+
 	float3 diffVec = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
 
@@ -750,7 +753,7 @@
 		{
 			const float factor = 4.0f;
+			//if (oldIdx >= factor * newWeight) oldIdx = 0;
 			oldWeight = min(oldWeight, factor * newWeight);
-			//oldWeight = .0f; 
-			//oldIdx    = .0f;
+			oldWeight = oldIdx = .0f; 
 		}
 	}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg	(revision 3361)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg	(revision 3362)
@@ -105,18 +105,29 @@
 			   )
 {
-	float2 result = float2(0.0f, 0.0f);
+	//return tex2Dlod(ssaoTex, float4(texCoord, .0f, .0f)).x;
+	float2 result = float2(.0f);
 	
 	const float3 centerPos = ReconstructSamplePos(colorsTex, texCoord, bl, br, tl, tr);
 
+#if 0
+	//const float scale = saturate((SSAO_CONVERGENCE_THRESHOLD - convergence + 100) / SSAO_CONVERGENCE_THRESHOLD);
 	const float scale = saturate((SSAO_CONVERGENCE_THRESHOLD - convergence) / SSAO_CONVERGENCE_THRESHOLD);
-	//const int radius = SSAO_FILTER_RADIUS * saturate((SSAO_CONVERGENCE_THRESHOLD - convergence) / SSAO_CONVERGENCE_THRESHOLD);
 	
-	//for (int i = -radius; i <= radius; ++ i)
-	for (int i = -SSAO_FILTER_RADIUS; i <= SSAO_FILTER_RADIUS; ++ i)
+	//for (int i = -SSAO_FILTER_RADIUS; i <= SSAO_FILTER_RADIUS; ++ i)
 	{
 		const float4 sampleTexCoord = float4(texCoord + i * xyStep * scale, .0f, .0f);
 		result += FilterSample(sampleTexCoord, ssaoTex, colorsTex, centerPos, bl, br, tl, tr, maxConvergence, spatialWeight);
 	}
-
+#else
+
+	const int radius = SSAO_FILTER_RADIUS * saturate((SSAO_CONVERGENCE_THRESHOLD - convergence + 100) / SSAO_CONVERGENCE_THRESHOLD);
+
+	for (int i = -radius; i <= radius; ++ i)
+	{
+		const float4 sampleTexCoord = float4(texCoord + i * xyStep, .0f, .0f);
+		result += FilterSample(sampleTexCoord, ssaoTex, colorsTex, centerPos, bl, br, tl, tr, maxConvergence, spatialWeight);
+	}
+
+#endif
 	result.x /= max(result.y, 1e-6f);
 	return saturate(result.x);
@@ -133,5 +144,8 @@
 						uniform float3 tl,
 						uniform float3 tr,
-						uniform float2 res
+						uniform float2 res,
+						uniform float maxConvergence,
+						uniform float spatialWeight
+
 						 )
 {
@@ -151,8 +165,11 @@
 
 	// filter up to a certain convergance value and leave out background (sky) by checking depth
-	if ((convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < DEPTH_THRESHOLD))
+	if ((!USE_OPTIMIZATION || (convergence < SSAO_CONVERGENCE_THRESHOLD)) && 
+		(depth < DEPTH_THRESHOLD))
 	{
 		// the filtered ssao value
-		OUT.illum_col.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, bl, br, tl, tr, xyStep, convergence, maxConvergence, spatialWeight);
+		OUT.illum_col.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, 
+			                       bl, br, tl, tr, xyStep, 
+								   convergence, maxConvergence, spatialWeight);
 	}
 
@@ -185,11 +202,13 @@
 	// afterwards we do not use the filter anymore
 
-	float2 xyStep = float2(1.0f / res.x, 0);
+	float2 xyStep = float2(1.0f / res.x, .0f);
 
 	// filter up to a certain convergance value and leave out background (sky) by checking depth
-	if ((convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < DEPTH_THRESHOLD))
+	if ((!USE_OPTIMIZATION || convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < DEPTH_THRESHOLD))
 	{
 		// the filtered ssao value
-		OUT.illum_col.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, bl, br, tl, tr, xyStep, convergence, maxConvergence, spatialWeight);
+		OUT.illum_col.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, 
+			                       bl, br, tl, tr, xyStep, 
+								   convergence, maxConvergence, spatialWeight);
 	}
 
@@ -228,5 +247,6 @@
 
 	// filter up to a certain convergance value and leave out background (sky) by checking depth
-	if ((convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < DEPTH_THRESHOLD))
+	if ((!USE_OPTIMIZATION || (convergence < SSAO_CONVERGENCE_THRESHOLD)) 
+		&& (depth < DEPTH_THRESHOLD))
 	{
 		// the filtered ssao value
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3361)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3362)
@@ -307,9 +307,9 @@
 	else if ( // check if changed from dynamic to not dynamic object
    	         (
-			  (oldDynamic && !newDynamic) || (!oldDynamic && newDynamic) ||
+			 // (oldDynamic && !newDynamic) || (!oldDynamic && newDynamic) ||
 			 (
 			  (oldEyeSpaceDepth < DEPTH_THRESHOLD) && (projectedEyeSpaceDepth < DEPTH_THRESHOLD) &&
-			  (oldDynamic || newDynamic) &&  // check if we have a dynamic object 
-			  (depthDif > MIN_DEPTH_DIFF)))
+			  //(oldDynamic || newDynamic) &&  // check if we have a dynamic object 
+			  (depthDif > 5e-3f)))//MIN_DEPTH_DIFF)))
 			  ) // and there is a depth discontinuity
 	{	
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3361)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3362)
@@ -628,4 +628,5 @@
 
 	if (0) LoadPompeiiFloor();
+
 #if 1
 
@@ -642,9 +643,9 @@
 	// sibenik positions
 	VertexArray positions;
-	
+	const float factor2 = 1.6f;
 	// for buddha (lowres)
-	positions.push_back(Vector3(6.07307, 8.20723, 6.62));
-	positions.push_back(Vector3(-17.1935, 11.1687, 8.8781));
-	positions.push_back(Vector3(1.50032, 31.1943, 19.1f));
+	positions.push_back(Vector3(6.07307, 8.20723, 6.62 - factor2));
+	positions.push_back(Vector3(-17.1935, 11.1687, 8.8781 - factor2));
+	//positions.push_back(Vector3(1.50032, 31.1943, 19.1f));
 
 	// for happy buddha
@@ -655,5 +656,5 @@
 	// for dragon
 	//positions.push_back(Vector3(6.07307, 8.20723, 5.0f));
-	//positions.push_back(Vector3(0, 0, -0.8));
+	//positions.push_back(Vector3(0, 0, -0.8f));
 
 #endif
@@ -661,9 +662,11 @@
 	//const Vector3 sceneCenter(470.398f, 240.364f, 180.3);
 	Matrix4x4 transl = TranslationMatrix(positions[0]);
-	
+	//LoadModel("dragon.dem", dynamicObjects);
+
+#if 1
+
 	//LoadModel("hbuddha.dem", dynamicObjects);
 	//LoadModel("horse.dem", dynamicObjects);
-	LoadModel("happy.dem", dynamicObjects);
-	//LoadModel("dragon.dem", dynamicObjects);
+	LoadModel("happy2.dem", dynamicObjects);
 	//LoadModel("torus.dem", dynamicObjects);
 	//LoadModel("venusm.dem", dynamicObjects);
@@ -678,5 +681,5 @@
 	}
 
-	/*
+
 	const float rotAngle = M_PI / 2.0f;
 	const Matrix4x4 rotMatrix = RotationXMatrix(rotAngle);
@@ -685,7 +688,7 @@
 	// hack: second buddha
 	dynamicObjects[0]->GetTransform()->MultMatrix(rotMatrix);
-	dynamicObjects[0]->GetTransform()->MultMatrix(scaleMatrix);
-
-	dynamicObjects[1]->GetTransform()->MultMatrix(rotMatrix);
+	//dynamicObjects[0]->GetTransform()->MultMatrix(scaleMatrix);
+
+	/*dynamicObjects[1]->GetTransform()->MultMatrix(rotMatrix);
 	dynamicObjects[1]->GetTransform()->MultMatrix(scaleMatrix);
 
@@ -698,5 +701,5 @@
 
 #endif
-
+#endif
 	// hack
 	resourceManager->mUseNormalMapping = false;
@@ -732,6 +735,6 @@
 
 	// set far plane based on scene extent
-	//farDist = 10.0f * Magnitude(bvh->GetBox().Diagonal());
-	farDist = 1000.0f;
+	farDist = 10.0f * Magnitude(bvh->GetBox().Diagonal());
+	//farDist = 1000.0f;
 	camera->SetFar(farDist);
 
@@ -1180,4 +1183,10 @@
 		buddha->GetTransform()->SetMatrix(trafo);
 
+		const float rotAngle3 = M_PI / 2.0f;
+		const Matrix4x4 rotMatrix3 = RotationZMatrix(rotAngle3);
+
+		// hack: second buddha
+		buddha->GetTransform()->MultMatrix(rotMatrix3);
+
 #if TODO // drop objects on ground floor
 		for (int i = 0; i < 10; ++ i)
@@ -1194,11 +1203,11 @@
 #endif
 
-#if 1
 		/////////////////////////
 		//-- update animations
 
+
 		//const float rotAngle = M_PI * 1e-3f;
-		const float rotAngle = 0.3f * M_PI / 180.0f;
-		//const float rotAngle = 0.6f * M_PI / 180.0f;
+		//const float rotAngle = 0.3f * M_PI / 180.0f;
+		const float rotAngle = 0.6f * M_PI / 180.0f;
 
 		Matrix4x4 rotMatrix = RotationZMatrix(rotAngle);
@@ -1214,5 +1223,4 @@
 		const float moveSpeed = 1e-1f;
 		motionPath->Move(moveSpeed);
-#endif
 	}
 
@@ -1488,5 +1496,6 @@
 		}
 		
-		if (makeSnapShot) makeSnapShot = false;
+		static int dummy = 0;
+		if (makeSnapShot && (dummy ++ == 650)) makeSnapShot = false;
 
 		ShadowMap *sm = showShadowMap ? shadowMap : NULL;
@@ -2017,6 +2026,6 @@
 void RightMotionLight(int x, int y) 
 {
-	float theta = 0.2f * M_PI * (xEyeBegin - x) / 180.0f;
-	float phi = 0.2f * M_PI * (yMotionBegin - y) / 180.0f;
+	const float theta = .2f * M_PI * (xEyeBegin - x) / 180.0f;
+	const float phi   = .2f * M_PI * (yMotionBegin - y) / 180.0f;
 	
 	Vector3 lightDir = light->GetDirection();
@@ -2069,8 +2078,8 @@
 	Vector3 rVec(viewDir[0], viewDir[1], 0);
 	
-	Matrix4x4 rot = RotationZMatrix(M_PI * 0.5f);
+	Matrix4x4 rot = RotationZMatrix(M_PI * .5f);
 	rVec = rot * rVec;
 	
-	pos -= rVec * (x - horizontalMotionBegin) * mouseMotion;
+	pos    -= rVec * (x - horizontalMotionBegin) * mouseMotion;
 	pos[2] += (verticalMotionBegin - y) * mouseMotion;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3361)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3362)
@@ -1505,4 +1505,6 @@
 	sprintf(imageName, "%s_%05d.tga", mSavedFrameSuffix.c_str(), mSavedFrameNumber);
 
+	//cout << "writing " << imageName << endl;
+
 	ILstring fileName = ILstring(imageName);
 	ilRegisterType(IL_FLOAT);
@@ -1525,4 +1527,6 @@
 	}
 
+	//cout << "wrote " << fileName << endl;
+
 	delete [] data;
 	stopil();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/vienna.env
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/vienna.env	(revision 3353)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/vienna.env	(revision 3362)
@@ -20,5 +20,5 @@
 # the filename for the statistics
 statsFilename=mystats
-walkThroughSuffix=walkthrough_vienna
+walkThroughSuffix=walkthrough_vienna2
 
 # use potentially visible sets?
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter.vcproj	(revision 3359)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter.vcproj	(revision 3362)
@@ -156,5 +156,5 @@
 				AdditionalLibraryDirectories="libs\Zlib\lib"
 				IgnoreDefaultLibraryNames="libCMT"
-				GenerateDebugInformation="false"
+				GenerateDebugInformation="true"
 				SubSystem="1"
 				LargeAddressAware="2"
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/plot_friendly
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/plot_friendly	(revision 3294)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/plot_friendly	(revision 3362)
@@ -10,5 +10,5 @@
 PREFIX=myvienna
 PREFIX=vienna
-PREFIX=pp
+#PREFIX=pp
 
 #FILES="$PREFIX-vfc.log $PREFIX_chc.log $PREFIX-vfc_pvs.log $PREFIX-chc++_b20.log $PREFIX-chc++_pvs_b20.log"
@@ -20,4 +20,7 @@
 FILES="$PREFIX-notindexed.log $PREFIX-indexed.log"
 #FILES="$PREFIX-vfc.log $PREFIX-chc++.log $PREFIX-vfc_pvs.log"
+#FILES="$PREFIX-ssao.log $PREFIX-ssao_tmp.log $PREFIX-deferred.log"
+FILES="$PREFIX-ssao_1024.log $PREFIX-ssao_tmp.log"
+#FILES="$PREFIX-ssao.log"
 
 echo $FILES
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/ps_memory_plot2
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/ps_memory_plot2	(revision 3294)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/ps_memory_plot2	(revision 3362)
@@ -23,6 +23,6 @@
 #echo set xrange \[100:20000\] >> $FILE
 #echo set xrange \[4200:4600\] >> $FILE
-echo set xrange \[100:400\] >> $FILE
-echo set xrange \[1000:1600\] >> $FILE
+#echo set xrange \[100:400\] >> $FILE
+#echo set xrange \[1000:1600\] >> $FILE
 #echo set xrange \[1600:2100\] >> $FILE
 #echo set xrange \[3000:3500\] >> $FILE
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/PlyConverter.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/PlyConverter.cpp	(revision 3361)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/PlyConverter.cpp	(revision 3362)
@@ -41,11 +41,15 @@
 		Face face = faces[i];
 
+		if (faces[i].indices.empty()) cout << "error!!" << endl;
+
 		//cout << "size: " << face.indices.size() << endl;
-		for (size_t j = 0; j < face.indices.size(); ++ j)
+		for (int j = (int)face.indices.size() - 1; j >= 0; -- j)
 		{
-			SimpleVec h = vertices[faces[i].indices[j]];
+			//cout << "h " << j << " " << face.indices.size() << endl;
+
+			SimpleVec h = vertices[face.indices[j]];
 			SimpleVec v;
 			
-			v.x = h.x * 14;
+			v.x = -h.x * 14;
 			v.y = h.z * 14;
 			v.z = h.y * 14;
@@ -76,7 +80,7 @@
 
 		// no face normals? => create normals
-		const SimpleTri tri(geom->mVertices[idx + 2],
+		const SimpleTri tri(geom->mVertices[idx + 0],
 			                geom->mVertices[idx + 1], 
-							geom->mVertices[idx + 0]);
+							geom->mVertices[idx + 2]);
 
 		const SimpleVec n = tri.GetNormal();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/sibenik.env
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/sibenik.env	(revision 3360)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/sibenik.env	(revision 3362)
@@ -120,2 +120,7 @@
 camDirection=-0.0136068 0.974275 -0.224952
 lightDirection=0.556361 0.559966 -0.613923
+
+
+camPosition=-13.4875 10.9779 10.0881
+camDirection=-0.97437 -3.55818e-006 -0.224952 
+lightDirection=0.556361 0.559966 -0.613 923
