Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3166)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3167)
@@ -6,11 +6,11 @@
 //-- ssao + gi parameters
 
-//#define NUM_SAMPLES 8
-#define NUM_SAMPLES 24
+#define NUM_SAMPLES 8
+//#define NUM_SAMPLES 24
 
 // for quadratic falloff
 //#define SAMPLE_INTENSITY 0.2f
-//#define SAMPLE_INTENSITY 0.07f
-#define SAMPLE_INTENSITY 0.01f
+#define SAMPLE_INTENSITY 0.07f
+//#define SAMPLE_INTENSITY 0.01f
 
 //#define SAMPLE_INTENSITY 0.075f
@@ -27,4 +27,9 @@
 
 #define VIEW_CORRECTION_SCALE 1.0f
+
+//#define NUM_SSAO_FILTERSAMPLES 80
+#define NUM_SSAO_FILTER_SAMPLES 16
+#define NUM_SSAO_FILTER_WIDTH 10.0f
+#define SSAO_CONVERGENCE_WEIGHT 25.0f
 
 
@@ -60,9 +65,4 @@
 #define NUM_DOWNSAMPLES 9
 
-//#define NUM_SSAO_FILTERSAMPLES 25
-//#define NUM_SSAO_FILTERSAMPLES 50
-//#define NUM_SSAO_FILTERSAMPLES 80
-#define NUM_SSAO_FILTERSAMPLES 80
-
 
 #define DYNAMIC_OBJECTS_THRESHOLD 1e-8f
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg	(revision 3017)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg	(revision 3167)
@@ -127,9 +127,7 @@
 	
 	// the ambient term
-	const float amb = norm.w;
-
+	//const float amb = norm.w;
 	// expand normal
 	float3 normal = normalize(norm.xyz);
-	
 	/// the current view direction
 	float3 viewDir;// = normalize(IN.view);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3164)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3167)
@@ -14,4 +14,6 @@
 {
 	float4 color: COLOR0;
+	float3 normal: COLOR1;
+	float3 diffVal: COLOR2;
 };
 
@@ -230,7 +232,7 @@
 	valid in the current frame
 	*/
-inline float PixelValid(sampler2D diffVals,
-						sampler2D oldTex,
+inline float PixelValid(sampler2D oldTex,
 						float4 color,
+						float3 diffVec,
 						float2 texCoord,
 						float3 viewDir,
@@ -248,5 +250,4 @@
 	const float4 worldPos = float4(-viewDir * eyeSpaceDepth, 1.0f);
 
-	float3 diffVec = tex2Dlod(diffVals, float4(texCoord, 0, 0)).xyz;
 	
 
@@ -308,6 +309,8 @@
 
 
-float4 PrepareSsao(fragment IN,
+pixel PrepareSsao(fragment IN,
 				   uniform sampler2D colorsTex,
+				   uniform sampler2D normalsTex,
+				   uniform sampler2D diffVals,
 				   uniform sampler2D oldTex,
 				   uniform float4x4 modelViewProj,
@@ -317,9 +320,12 @@
 				   uniform float3 oldtl,
 				   uniform float3 oldtr,
-				   uniform float3 oldEyePos,
-				   uniform sampler2D diffVals
-				   ): COLOR
+				   uniform float3 oldEyePos
+				   )
 {   
-	//const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
+	pixel pix;
+
+	const float3 normal = normalize(tex2Dlod(normalsTex, float4(IN.texCoord, 0 ,0)).xyz);
+	const float3 difVec = tex2Dlod(diffVals, float4(IN.texCoord, 0 ,0)).xyz;
+
 	const float3 viewDir = IN.view;
 	float4 color = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
@@ -330,7 +336,8 @@
 
 	// do reprojection and filter out the pixels that are not save
-	float pValid = PixelValid(diffVals,
+	float pValid = PixelValid(
 		oldTex,
 		color, 
+		difVec.xyz,
 		IN.texCoord,
 		viewDir,
@@ -341,6 +348,11 @@
 		);
 
-	color.x = pValid;
-	return color;
+	pix.color = color;
+	pix.color.x = pValid;
+
+	pix.normal = normal;
+	pix.diffVal = difVec;
+
+	return pix;
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3164)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3167)
@@ -30,6 +30,6 @@
 						  uniform sampler2D normalsTex,
 						  uniform sampler2D colorsTex,
-						  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
-						  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
+						  uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
+						  uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
 						  float scale
 						  )
@@ -40,6 +40,5 @@
 	const float eyeSpaceDepth = color.w;
 
-	const float3 centerNormal = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
-	//const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
+	const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
 
 	float4 aoSample;
@@ -53,11 +52,10 @@
 	float sampleDepth;
 
-	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
+	for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
 	{
 		sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
 
 		aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
-		sampleNorm = normalize(tex2Dlod(normalsTex, sampleTexCoord).xyz);
-		//sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
+		sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
 
 		// check depth discontinuity
@@ -96,5 +94,5 @@
 						   uniform sampler2D normalsTex,
 						   uniform sampler2D colorsTex,
-						   uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
+						   uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
 						   float scale,
 						   float3 bl,
@@ -107,6 +105,5 @@
 
 	const float3 centerPos = ReconstructSamplePos(ssaoTex, texCoord, bl, br, tl, tr);
-	const float3 centerNormal = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
-	//const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
+	const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
 
 	float4 aoSample;
@@ -118,17 +115,16 @@
 	float normalFactor;
 	float convergenceFactor;
-	float sampleDepth;
-
-	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
+	float len;
+
+	for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
 	{
 		sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
 
 		aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
-		sampleNorm = normalize(tex2Dlod(normalsTex, sampleTexCoord).xyz);
-		//sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
+		sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
 
 		// check spatial discontinuity
 		samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
-		float len = SqrLen(centerPos - samplePos);
+		len = min(SqrLen(centerPos - samplePos), 1e2f);
 
 		spatialFactor = 1.0f / max(len, 1e-3f);
@@ -159,6 +155,6 @@
 			  uniform sampler2D ssaoTex,
 			  uniform sampler2D normalsTex,
-			  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
-			  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
+			  uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
+			  uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
 			  uniform float4x4 modelViewProj,
 			  uniform float3 bl,
@@ -180,4 +176,5 @@
 */
 
+	// filter up to a certain convergance value and leave out background (sky) by checking depth
 	if ((ao.y < 100.0f) && (col.w < 1e10f))
 	//if (col.w < 1e10f)
@@ -189,10 +186,10 @@
 
 		const float convergence = ao.y;
-		const float adaptWeight = 5.0f;
-		const float convergenceScale = adaptWeight / (convergence + adaptWeight);
-
-		const float scale = convergenceScale * distanceScale;
-
-
+		const float adaptWeight = 25.0f;
+		const float convergenceScale = SSAO_CONVERGENCE_WEIGHT / (convergence + SSAO_CONVERGENCE_WEIGHT);
+
+		const float scale = NUM_SSAO_FILTER_WIDTH * convergenceScale * distanceScale;
+
+		// the filtered ssao value
 		ao.x = DiscontinuityFilter2(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, scale, bl, br, tl, tr);
 	}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 3154)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 3167)
@@ -91,6 +91,6 @@
 
 
-	//float4 col = (s0 + s1 + s2 + s3 + centerColor) * 0.2f;
-	float4 col = (s0 + s1 + s2 + s3) * 0.25f;
+	float4 col = (s0 + s1 + s2 + s3 + centerColor) * 0.2f;
+	//float4 col = (s0 + s1 + s2 + s3) * 0.25f;
 	//float4 col = float4(ne.x, ne.x, ne.x, 0);
 	//float4 col = float4(de.x, de.x, de.x, 0);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3161)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3167)
@@ -213,5 +213,6 @@
 		const float3 samplePos = ReconstructSamplePos(sampleColor.w, texcoord, bl, br, tl, tr);
 		// the normal of the current sample
-		const float3 sampleNormal = normalize(tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz);
+		//const float3 sampleNormal = normalize(tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz);
+		const float3 sampleNormal = tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz;
 
 
@@ -357,5 +358,6 @@
 	pixel OUT;
 
-	const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
+	//const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
+	const float3 normal = tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz;
 
 	// reconstruct position from the eye space depth
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3155)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3167)
@@ -109,4 +109,5 @@
 	void PrepareSsao(FrameBufferObject *fbo);
 
+	void SortSamples();
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3155)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3167)
@@ -587,5 +587,5 @@
 	//fbo->AddColorBuffer(ColorBufferObject::RGB_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
 	// buffer holding the difference vector to the old frame
-	fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
+	fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
 	// another color buffer
 	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3166)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3167)
@@ -71,6 +71,6 @@
 
 
-static float ssaoFilterOffsets[NUM_SSAO_FILTERSAMPLES * 2];
-static float ssaoFilterWeights[NUM_SSAO_FILTERSAMPLES];
+static float ssaoFilterOffsets[NUM_SSAO_FILTER_SAMPLES * 2];
+static float ssaoFilterWeights[NUM_SSAO_FILTER_SAMPLES];
 
 
@@ -445,18 +445,16 @@
 	
 	string prepareSsaoParams[] = 
-		{"colorsTex", "oldTex", "oldEyePos", "modelViewProj", "oldModelViewProj",
-		 "oldbl", "oldbr", "oldtl", "oldtr", "diffVals"};
-
-	sCgPrepareSsaoProgram->AddParameters(prepareSsaoParams, 0, 10);
+		{"colorsTex", "normalsTex", "diffVals", "oldTex", 
+		 "oldEyePos", "modelViewProj", "oldModelViewProj",
+		 "oldbl", "oldbr", "oldtl", "oldtr"};
+
+	sCgPrepareSsaoProgram->AddParameters(prepareSsaoParams, 0, 11);
 
 
 	////////////////
 
-
-	//const float filterWidth = 100.0f;
-	const float filterWidth = 30.0f;
-	//const float filterWidth = 15.0f;
-
-	PoissonDiscSampleGenerator2 poisson(NUM_SSAO_FILTERSAMPLES, 1.0f);
+	const float filterWidth = 1.0f;
+
+	PoissonDiscSampleGenerator2 poisson(NUM_SSAO_FILTER_SAMPLES, 1.0f);
 	poisson.Generate((float *)ssaoFilterOffsets);
 
@@ -464,5 +462,5 @@
 	const float yoffs = (float)filterWidth / mHeight;
 
-	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
+	for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
 	{
 		float x = ssaoFilterOffsets[2 * i + 0];
@@ -516,6 +514,6 @@
 		//DownSample(fbo, colorBufferIdx, mDownSampleFbo, 0, sCgScaleDepthProgram);
 		PrepareSsao(fbo);
-		DownSample(fbo, 1, mDownSampleFbo, 1, sCgDownSampleProgram); // normals
-		DownSample(fbo, 2, mDownSampleFbo, 2, sCgDownSampleProgram); // offsets
+		//DownSample(fbo, 1, mDownSampleFbo, 1, sCgDownSampleProgram); // normals
+		//DownSample(fbo, 2, mDownSampleFbo, 2, sCgDownSampleProgram); // offsets
 	}
 
@@ -570,4 +568,84 @@
 
 
+
+static inline float SqrMag(const Sample2 &s)
+{
+	return (s.x * s.x + s.y * s.y);
+}
+
+
+static inline float SqrDist(const Sample2 &a, const Sample2 &b)
+{
+	float x = a.x - b.x;
+	float y = a.y - b.y;
+	
+	return x * x + y * y;
+}
+
+
+static inline bool lt(const Sample2 &a, const Sample2 &b)
+{
+	return SqrMag(a) < SqrMag(b);
+}
+
+
+void DeferredRenderer::SortSamples()
+{
+	//for (int i = 0; i < NUM_SAMPLES; ++ i)cout << SqrDist(samples2[i-1], samples2[i]) << "|";
+	float dist = 0;
+	for (int i = 1; i < NUM_SAMPLES; ++ i)
+		dist += SqrDist(samples2[i-1], samples2[i]);
+		//cout << samples2[i].x << " " << samples2[i].y 
+
+	//cout << "\nbefore: " << dist << endl;
+
+	//sort(samples2, samples2 + sizeof(samples2) / sizeof(Sample2), lt2);
+
+
+	static Sample2 tempSamples[NUM_SAMPLES];
+	static bool checked[NUM_SAMPLES];
+
+	for (int i = 0; i < NUM_SAMPLES; ++ i)
+		checked[i] = false;
+
+	Sample2 currentSample;
+	currentSample.x = 0; currentSample.y = 0;
+	int ns = 0; // the next sample index
+
+	for (int i = 0; i < NUM_SAMPLES; ++ i)
+	{
+		float minLen = 1e20f;
+
+		for (int j = 0; j < NUM_SAMPLES; ++ j)
+		{
+			if (checked[j]) continue;
+
+			Sample2 s = samples2[j];
+			const float len = SqrDist(s, currentSample);
+
+			if (len < minLen) 
+			{
+				minLen = len;
+				ns = j;
+			}
+		}
+
+		tempSamples[i] = samples2[ns];
+		currentSample = samples2[ns];
+		checked[ns] = true;
+	}
+
+	for (int i = 0; i < NUM_SAMPLES; ++ i)
+		samples2[i] = tempSamples[i];
+
+	dist = 0;
+	for (int i = 1; i < NUM_SAMPLES; ++ i)
+		dist += SqrDist(samples2[i-1], samples2[i]);
+		//cout << samples2[i].x << " " << samples2[i].y << " " << SqrDist(samples2[i-1], samples2[i]) << "|";
+
+	//cout << "after:  " << dist << endl;
+}
+
+
 void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo, 
 								   float tempCohFactor)
@@ -616,4 +694,6 @@
 		// needs longer to converge
 		GenerateSamples(mSamplingMethod);
+
+		SortSamples();
 		sCgSsaoProgram->SetArray2f(i, (float *)samples2, NUM_SAMPLES);
 	}
@@ -821,6 +901,6 @@
 	sCgCombineSsaoProgram->SetTexture(i ++, ssaoTex);
 
-	sCgCombineSsaoProgram->SetArray2f(i ++, (float *)ssaoFilterOffsets, NUM_SSAO_FILTERSAMPLES);
-	sCgCombineSsaoProgram->SetArray1f(i ++, (float *)ssaoFilterWeights, NUM_SSAO_FILTERSAMPLES);
+	sCgCombineSsaoProgram->SetArray2f(i ++, (float *)ssaoFilterOffsets, NUM_SSAO_FILTER_SAMPLES);
+	sCgCombineSsaoProgram->SetArray1f(i ++, (float *)ssaoFilterWeights, NUM_SSAO_FILTER_SAMPLES);
 	
 	sCgCombineSsaoProgram->SetMatrix(i++, mProjViewMatrix);
@@ -957,8 +1037,8 @@
 void DeferredRenderer::PrepareSsao(FrameBufferObject *fbo)
 {
-	GLuint colorsTex, diffVals, oldTex;
+	GLuint colorsTex, normalsTex, diffVals, oldTex;
 
 	colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
-	//normalsTex = fbo->GetColorBuffer(1)->GetTexture();
+	normalsTex = fbo->GetColorBuffer(1)->GetTexture();
 	diffVals = fbo->GetColorBuffer(2)->GetTexture();
 	//diffVals = mDownSampleFbo->GetColorBuffer(2)->GetTexture();
@@ -970,5 +1050,6 @@
 
 	sCgPrepareSsaoProgram->SetTexture(i ++, colorsTex);
-	//sCgSsaoProgram->SetTexture(i ++, normalsTex);
+	sCgPrepareSsaoProgram->SetTexture(i ++, normalsTex);
+	sCgPrepareSsaoProgram->SetTexture(i ++, diffVals);
 	sCgPrepareSsaoProgram->SetTexture(i ++, oldTex);
 
@@ -986,7 +1067,4 @@
 		sCgPrepareSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
 
-	sCgPrepareSsaoProgram->SetTexture(i ++, diffVals);
-
-
 	glPushAttrib(GL_VIEWPORT_BIT);
 	glViewport(0, 0, mDownSampleFbo->GetWidth(), mDownSampleFbo->GetHeight());
@@ -994,5 +1072,5 @@
 	mDownSampleFbo->Bind();
 
-	glDrawBuffers(1, mrt + 0);
+	glDrawBuffers(3, mrt + 0);
 
 	DrawQuad(sCgPrepareSsaoProgram);
