Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3130)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3132)
@@ -119,4 +119,17 @@
 	if (col.w < 1e10f)
 	{
+		const float xoffs = 4.0f / 1024.0f;
+		const float yoffs = 4.0f / 768.0f;
+/*
+		//float3 id = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
+		float3 x2 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(xoffs, 0), 0, 0)).xyz;
+		float3 x3 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(0, yoffs), 0, 0)).xyz;
+		float3 x4 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(-xoffs, 0), 0, 0)).xyz;
+		float3 x5 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(0, -yoffs), 0, 0)).xyz;
+
+		//ao.x = (ao.x + x2 + x3 + x4 + x5) * 0.2f;
+		ao.x = (x2 + x3 + x4 + x5) * 0.25f;
+
+*/
 		const static float scaleFactor = 1.0f;
 		//const static float scaleFactor = 10.0f;
@@ -126,10 +139,10 @@
 		//ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
 		//ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor * ao.z  / (adaptFactor + ao.y), 0);
-		ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor  / (adaptFactor + ao.y), 0);
-	}
-
-	//OUT.illum_col.xyz = col.xyz * ao.x;
+		//ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor  / (adaptFactor + ao.y), 0);
+	}
+
+	OUT.illum_col.xyz = col.xyz * ao.x;
 	//OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
-	OUT.illum_col.xyz = float3(1.0f - ao.x, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
+	//OUT.illum_col.xyz = float3(1.0f - ao.x, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
 	OUT.illum_col.w = col.w;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 3125)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 3132)
@@ -29,10 +29,8 @@
 /*
     float4 average = .0f;
-
     for (int i = 0; i < NUM_DOWNSAMPLES; ++ i)
     {
 		average += tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i], 0, 0));
-    }
-       
+    }  
 	average *= 1.0f / (float)NUM_DOWNSAMPLES;
 
@@ -151,5 +149,5 @@
 
 	float lum = dot(color.rgb, w);
-	float logLum = log(1e-5f + lum);
+	float logLum = log(max(1e-3f, lum));
 
 	float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
@@ -158,5 +156,5 @@
 	const float expFactor = 0.1f;
 	// exponential smoothing of tone mapping over time
-	if (oldLogLum > 1e-5f) // check if loglum from last frame too small (=> tm too bright)
+	if (oldLogLum > 1e-3f) // check if loglum from last frame too small (=> tm too bright)
 		OUT.col.w = lerp(oldLogLum, logLumScaled, expFactor);
 	else
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3129)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3132)
@@ -143,5 +143,5 @@
 {
 	// compute position from old frame for dynamic objects + translational portion
-	const float3 translatedPos = worldPos.xyz - oldEyePos + diffVec;
+	const float3 translatedPos = diffVec + worldPos.xyz - oldEyePos;
 
 
@@ -186,5 +186,5 @@
 	const float squaredLen = diffVec.x * diffVec.x + diffVec.y * diffVec.y + diffVec.z * diffVec.z;
 
-#if 1
+#if 0
 	if (squaredLen < 1e-8f) // object not dynamic
 	{
@@ -209,6 +209,6 @@
 #endif
 
-	//const float oldWeight = 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;
@@ -236,5 +236,5 @@
 	//	newWeight = min(temporalCoherence + 1, max(oldPixel.y - 70, 50));
 	//if (newWeight >= 2000) newWeight = 1000;
-	newWeight -= step(512.0f, newWeight) * 256.0f;
+	//newWeight -= step(512.0f, newWeight) * 256.0f;
 
 	return float3(oldPixel.x, newWeight, eyeSpaceDepth);
@@ -279,5 +279,6 @@
 		//-- add random noise: reflect around random normal vector (rather slow!)
 
-		float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f + noiseOffs, 0, 0)).xy;
+		//float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f + noiseOffs, 0, 0)).xy;
+		float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord, .0f, .0f)).xy;
 		//float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
 		const float2 offsetTransformed = myreflect(offset, mynoise);
@@ -357,5 +358,16 @@
 	const float4 eyeSpacePos = float4(-viewDir * eyeSpaceDepth, 1.0f);
 
+	/*const float xoffs = 2.0f / 1024.0f;
+	const float yoffs = 2.0f / 768.0f;
+
 	//float3 id = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
+	float3 x1 = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
+	float3 x2 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(xoffs, 0), 0, 0)).xyz;
+	float3 x3 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(0, yoffs), 0, 0)).xyz;
+	float3 x4 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(-xoffs, 0), 0, 0)).xyz;
+	float3 x5 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(0, -yoffs), 0, 0)).xyz;
+
+	float3 diffVec = (x1+x2+x3+x4+x5) * .25f;
+*/
 	float3 diffVec = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
 	
@@ -387,7 +399,8 @@
 
 	const float oldSsao = temporalVals.x;
-	const float newWeight = clamp(temporalVals.y, 1.0f, temporalCoherence);
-	float2 noiseOffs = float2((temporalVals.y - 1)/ 139.0f, .0f);
-	//float2 noiseOffs = float2(.0f);
+	//const float newWeight = clamp(temporalVals.y, 1.0f, temporalCoherence);
+	const float newWeight = temporalVals.y;
+	//float2 noiseOffs = float2((temporalVals.y - 1)/ 139.0f, .0f);
+	float2 noiseOffs = float2(.0f);
 
 	float2 ao;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3128)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3132)
@@ -94,4 +94,6 @@
 	void DrawQuad(ShaderProgram *p);
 
+	void Output(FrameBufferObject *fbo);
+
 	/** Initialises the deferred shader and loads the required shaders:
 		This function has to be called only once.
@@ -101,4 +103,6 @@
 	*/
 	void InitFrame();
+
+	void FlipFbos(FrameBufferObject *fbo);
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3129)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3132)
@@ -144,7 +144,15 @@
 
 
+void DeferredRenderer::FlipFbos(FrameBufferObject *fbo)
+{
+	fbo->Bind();
+	colorBufferIdx = 3 - colorBufferIdx;
+	glDrawBuffers(1, mrt + colorBufferIdx);
+}
+
+
 void DeferredRenderer::DrawQuad(ShaderProgram *p)
 {
-	p->Bind();
+	if (p) p->Bind();
 
 	// interpolate the view vector
@@ -288,6 +296,6 @@
 	//-- the flip-flop fbos
 
-	const int dsw = w / 2; const int dsh = h / 2;
-	//const int dsw = w; const int dsh = h;
+	//const int dsw = w / 2; const int dsh = h / 2;
+	const int dsw = w; const int dsh = h;
 
 	mIllumFbo = new FrameBufferObject(dsw, dsh, FrameBufferObject::DEPTH_NONE);
@@ -322,6 +330,6 @@
 
 	// create noise texture for ssao
-	//CreateNoiseTex2D(mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
-	CreateNoiseTex2D(mWidth / 8, mHeight / 8);
+	CreateNoiseTex2D(mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
+	//CreateNoiseTex2D(mIllumFbo->GetWidth() / 4, mIllumFbo->GetWidth() / 4);
 	CreateNoiseTex1D(mWidth / 8);
 
@@ -504,9 +512,11 @@
 	}
 
+	AntiAliasing(fbo, light);
+
 	switch (mShadingMethod)
 	{
 	case SSAO:
 		ComputeSsao(fbo, tempCohFactor);
-		SmoothSsao(fbo);
+		//SmoothSsao(fbo);
 		CombineSsao(fbo);
 		break;
@@ -528,5 +538,5 @@
 	}
 
-	AntiAliasing(fbo, light);
+	Output(fbo);
 
 	glEnable(GL_LIGHTING);
@@ -552,5 +562,5 @@
 	GLuint colorsTex, normalsTex, attribsTex;
 
-	if (0)
+	if (1)
 	{
 		colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
@@ -563,4 +573,5 @@
 		normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
 		attribsTex = mDownSampleFbo->GetColorBuffer(2)->GetTexture();
+		//attribsTex = fbo->GetColorBuffer(2)->GetTexture();
 	}
 
@@ -644,10 +655,13 @@
 void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light)
 {
-	FrameBufferObject::Release();
-
 	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
-
 	GLuint colorsTex = colorBuffer->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+
+	//FrameBufferObject::Release();
+
+	// read the second buffer, write to the first buffer
+	FlipFbos(fbo);
+	
 
 	sCgAntiAliasingProgram->SetTexture(0, colorsTex);
@@ -678,8 +692,5 @@
 	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
 
-	fbo->Bind();
-
-	colorBufferIdx = 3 - colorBufferIdx;
-	glDrawBuffers(1, mrt + colorBufferIdx);
+	FlipFbos(fbo);
 
 	const Vector3 lightDir = -light->GetDirection();
@@ -767,11 +778,5 @@
 	GLuint illumTex = mIllumFbo->GetColorBuffer(mIllumFboIndex + 1)->GetTexture();
 
-
-	fbo->Bind();
-
-	// overwrite old color texture
-	colorBufferIdx = 3 - colorBufferIdx;
-
-	glDrawBuffers(1, mrt + colorBufferIdx);
+	FlipFbos(fbo);
 
 	sCgCombineIllumProgram->SetTexture(0, colorsTex);
@@ -787,14 +792,10 @@
 void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
 {
-	fbo->Bind();
-
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint ssaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
-	//GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
-
-	// overwrite old color texture
-	colorBufferIdx = 3 - colorBufferIdx;
-	glDrawBuffers(1, mrt + colorBufferIdx);
+	//GLuint ssaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
+	GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
+	
+	FlipFbos(fbo);
 
 	int i = 0;
@@ -816,6 +817,4 @@
 									   ShadowMap *shadowMap)
 {
-	fbo->Bind();
-
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
@@ -826,6 +825,6 @@
 	shadowMap->GetTextureMatrix(shadowMatrix);
 
-	colorBufferIdx = 3 - colorBufferIdx;
-	glDrawBuffers(1, mrt + colorBufferIdx);
+
+	FlipFbos(fbo);
 
 	sCgDeferredShadowProgram->SetTexture(0, colorsTex);
@@ -891,8 +890,5 @@
 	GLuint colorsTex = colorBuffer->GetTexture();
 
-	fbo->Bind();
-
-	colorBufferIdx = 3 - colorBufferIdx;
-	glDrawBuffers(1, mrt + colorBufferIdx);
+	FlipFbos(fbo);
 	
 	sCgLogLumProgram->SetTexture(0, colorsTex);
@@ -973,9 +969,7 @@
 	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
 	GLuint colorsTex = colorBuffer->GetTexture();
-
-	fbo->Bind();
-	
-	colorBufferIdx = 3 - colorBufferIdx;
-	glDrawBuffers(1, mrt + colorBufferIdx);
+	//FrameBufferObject::Release();
+
+	FlipFbos(fbo);
 
 	sCgToneProgram->SetTexture(0, colorsTex);
@@ -987,4 +981,21 @@
 
 	PrintGLerror("ToneMap");
+}
+
+
+void DeferredRenderer::Output(FrameBufferObject *fbo)
+{
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, fbo->GetWidth(), fbo->GetHeight());
+	
+	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
+	GLuint colorsTex = colorBuffer->GetTexture();
+
+	sCgDownSampleProgram->SetTexture(0, colorsTex);
+
+	FrameBufferObject::Release();
+	DrawQuad(sCgDownSampleProgram);
+
+	PrintGLerror("OUTPUT");
 }
 
@@ -1030,17 +1041,8 @@
 	const float oldFar = mCamera->GetFar();
 	const float oldNear = mCamera->GetNear();
-	//mCamera->SetFar(1e3f);
-	//mCamera->SetNear(3.0f);
-	//mCamera->SetFar(1e2f);
+	
 
 	Matrix4x4 matViewing, matProjection;
-/*
-	mCamera->SetPosition(mOldEyePos - mEyePos);
-
-	mCamera->GetModelViewMatrix(matViewing);
-	mCamera->GetProjectionMatrix(matProjection);
-
-	mOldProjViewMatrix = matViewing * matProjection;
-	mCamera->SetPosition(mEyePos);*/
+
 
 	///////////////////
