Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp	(revision 2979)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp	(revision 2991)
@@ -193,9 +193,8 @@
 	
 	GLint width, height;
-
 	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
 	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
 
-	cout << "w: " << width << "h: " << height << endl;
+	//cout << "w: " << width << "h: " << height << endl;
 
 	unsigned char *data = new unsigned char[bytes * 4 * width * height];
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2985)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2991)
@@ -49,3 +49,5 @@
 #define MAX_LOD_LEVEL 99
 
+
+
 #endif // __SHADERENV_H
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2990)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2991)
@@ -64,6 +64,6 @@
 		   uniform sampler2D positions,
 		   uniform sampler2D normals,
-		   uniform float3 lightDir,
-		   uniform sampler2D oldColors
+		   uniform float3 lightDir
+		  //, uniform sampler2D oldColors
 		   )
 {
@@ -83,5 +83,5 @@
 	OUT.color = col;
 	
-#if 1
+#if 0
 
 	OUT.color.w = color.w;
@@ -92,5 +92,6 @@
 	//-- write out logaritmic luminance for tone mapping
 
-	float4 oldColor = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL));
+	// the old loglum is stored in the hightest mipmap-level
+	float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
 
 	const float3 w = float3(0.299f, 0.587f, 0.114f);
@@ -102,6 +103,6 @@
 	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
 
-	if (oldColor.w > 0)
-		OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
+	if (oldLogLum > 0)
+		OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
 	else
 		OUT.color.w = logLumScaled;
@@ -164,6 +165,6 @@
 				  uniform sampler2D noiseTexture,
 				  uniform float2 samples[NUM_PCF_TABS],
-				  uniform float3 lightDir,
-				  uniform sampler2D oldColors
+				  uniform float3 lightDir
+				  //, uniform sampler2D oldColors
 				  )
 {
@@ -190,5 +191,5 @@
 	//if ((emmisive < 1.5f) // hack: prevent shadowing the sky	
 	if (
-		(position.w > 1e19f)
+		(position.w < 1e19f)
 		&& (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
 		)
@@ -216,5 +217,11 @@
 	//-- write out logaritmic luminance for tone mapping
 
-	float4 oldColor = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL));
+#if 0
+
+	OUT.color.w = color.w;
+
+#else
+
+	float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
 
 	const float3 w = float3(0.299f, 0.587f, 0.114f);
@@ -226,9 +233,9 @@
 	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
 
-	if (oldColor.w > 0)
-		OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
+	if (oldLogLum > 0)
+		OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
 	else
 		OUT.color.w = logLumScaled;
-	
+#endif	
 	return OUT;
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2975)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2991)
@@ -105,2 +105,31 @@
 	return OUT;
 }
+
+
+pixel CalcAvgLogLum(frag IN, uniform sampler2D colors)
+{
+	////////////
+	//-- write out logaritmic luminance for tone mapping
+
+	pixel OUT;
+
+	// the old loglum is stored in the hightest mipmap-level
+	float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
+	float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
+
+	// the intensity weights
+	const float3 w = float3(0.299f, 0.587f, 0.114f);
+
+	float lum = dot(color.rgb, w);
+	float logLum = log(1e-5f + lum);
+
+	float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
+	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
+
+	OUT.color = color;
+
+	if (oldLogLum > 0)
+		OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
+	else
+		OUT.color.w = logLumScaled;
+}
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2990)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2991)
@@ -34,9 +34,7 @@
 inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
 {
-	//float3 x1 = lerp(oldColor.w, logLumScaled, 0.1f);
-	float3 x1 = lerp(bl, tl, w.y); //bl * (1.0f - w.x) + br * w.x;
-	float3 x2 = lerp(br, tr, w.y); //tl * (1.0f - w.x) + tr * w.x;
-
-	float3 v = lerp(x1, x2, w.x); //x1 * (1.0f - w.y) + x2 * w.y;
+	float3 x1 = lerp(bl, tl, w.y);
+	float3 x2 = lerp(br, tr, w.y); 
+	float3 v = lerp(x1, x2, w.x); 
 
 	return v;
@@ -44,5 +42,8 @@
 
 
-inline float3 ReconstructSamplePosition(uniform sampler2D colors, float2 texcoord, float3 bl, float3 br, float3 tl, float3 tr)
+inline float3 ReconstructSamplePosition(float3 eyePos, 
+										uniform sampler2D colors,
+										float2 texcoord, 
+										float3 bl, float3 br, float3 tl, float3 tr)
 {
 	/*float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).w;
@@ -50,6 +51,5 @@
 	// reconstruct world space position
 	float3 rotView = normalize(Interpol(texcoord, bl, br, tl, tr));
-	float3 sample_position = eyePos - rotView * eyeSpaceDepth;
-	*/
+	float3 sample_position = eyePos - rotView * eyeSpaceDepth;*/
 	float3 sample_position = tex2Dlod(colors, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).xyz;
 
@@ -102,5 +102,5 @@
 			++ numSamples;
 
-		float3 sample_position = ReconstructSamplePosition(colors, texcoord, bl, br, tl, tr);
+		float3 sample_position = ReconstructSamplePosition(eyePos, colors, texcoord, bl, br, tl, tr);
 
 		float3 vector_to_sample = sample_position - centerPosition.xyz;
@@ -174,5 +174,5 @@
 
 	//const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w, viewDir, eyePos);
-	const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w, /*viewDir, */eyePos, bl, br, tl, tr);
+	const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w, eyePos, bl, br, tl, tr);
 		
 
@@ -208,7 +208,6 @@
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
 		(abs(depthDif) < 1e-4f) 
-		// check if visibility changed in the surrounding area:
-		// then we have to recompute
-		&& ((newWeight < 10) || (oldNumSamples > 0.9f * newNumSamples))
+		// if visibility changed in the surrounding area we have to recompute
+		&& (oldNumSamples > 0.8f * newNumSamples)
 		//&& (oldAvgDepth / newAvgDepth > 0.99)
 		)
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2990)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2991)
@@ -44,11 +44,10 @@
 
 
-float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
-{
-	//float3 x1 = lerp(oldColor.w, logLumScaled, 0.1f);
-	float3 x1 = lerp(bl, tl, w.y); //bl * (1.0f - w.x) + br * w.x;
-	float3 x2 = lerp(br, tr, w.y); //tl * (1.0f - w.x) + tr * w.x;
-
-	float3 v = lerp(x1, x2, w.x); //x1 * (1.0f - w.y) + x2 * w.y;
+
+inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
+{
+	float3 x1 = lerp(bl, tl, w.y);
+	float3 x2 = lerp(br, tr, w.y); 
+	float3 v = lerp(x1, x2, w.x); 
 
 	return v;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2976)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2991)
@@ -44,5 +44,7 @@
 				float tempCohFactor, 
 				DirectionalLight *light,
-				ShadowMap *shadowMap = NULL);
+				bool useToneMapping,
+				ShadowMap *shadowMap = NULL
+				);
 
 	/** Initialises the deferred shader and loads the required shaders:
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2990)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2991)
@@ -215,4 +215,7 @@
 bool altKeyPressed = false;
 
+#define USE_TONE_MAPPING 1
+
+
 // function forward declarations
 void InitExtensions();
@@ -842,6 +845,5 @@
 	
 
-	//const bool useToneMapping = (renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED);
-	const bool useToneMapping = false;
+	const bool useToneMapping = ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED)) && USE_TONE_MAPPING;
 
 	Vector3 sunAmbient;
@@ -854,10 +856,8 @@
 	ambient[2] = sunAmbient.z;
 
-
 	// no tone mapping => scale
 	if (!useToneMapping)
 	{
 		const float maxComponent = sunDiffuse.MaxComponent();
-		//cout<< "sunambient: " << sunAmbient << " mag " << Magnitude(sunDiffuse) << " max: " << maxComponent << endl;
 		sunDiffuse /= maxComponent;
 	}
@@ -1160,7 +1160,4 @@
 	RenderSky();
 
-	//state.Reset(); glEnable(GL_TEXTURE_2D);
-	//aeroplane->Render(&state);
-
 
 	if ((renderMethod == RENDER_DEFERRED) ||
@@ -1193,5 +1190,5 @@
 
 		ShadowMap *sm = showShadowMap ? shadowMap : NULL;
-		ssaoShader->Render(fbo, oldViewProjMatrix, matProjectionView, ssaoTempCohFactor, light, sm);
+		ssaoShader->Render(fbo, oldViewProjMatrix, matProjectionView, ssaoTempCohFactor, light, USE_TONE_MAPPING, sm);
 	}
 
@@ -2030,6 +2027,5 @@
 		state.SetRenderPassType(RenderState::DEFERRED);
 
-	//const bool useToneMapping = (renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED);
-	const bool useToneMapping = false;
+	const bool useToneMapping = ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED)) && USE_TONE_MAPPING;
 
 	preetham->RenderSkyDome(-light->GetDirection(), camera, &state, !useToneMapping);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2990)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2991)
@@ -28,7 +28,4 @@
 
 static CGparameter sColorsTexDeferredParam;
-static CGparameter sOldColorsTexDeferredParam;
-static CGparameter sOldColorsTexShadowParam;
-
 static CGparameter sPositionsTexDeferredParam;
 static CGparameter sNormalsTexDeferredParam;
@@ -326,5 +323,5 @@
 		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
 		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
-		sOldColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "oldColors");
+		//sOldColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "oldColors");
 		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
 		
@@ -495,5 +492,5 @@
 		sSamplesShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "samples");
 		sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir");
-		sOldColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "oldColors");
+		//sOldColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "oldColors");
 
 
@@ -556,5 +553,7 @@
 							  float tempCohFactor,
 							  DirectionalLight *light,
-							  ShadowMap *shadowMap)
+							  bool useToneMapping,
+							  ShadowMap *shadowMap
+							  )
 {
 	// switch roles of old and new fbo
@@ -591,7 +590,4 @@
 	else
 		FirstPass(fbo, light);
-
-	glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
-	glGenerateMipmapEXT(GL_TEXTURE_2D);
 
 	switch (mShadingMethod)
@@ -610,14 +606,12 @@
 	}
 
-	float imageKey, whiteLum, middleGrey;
-/*
-	glPixelStorei(GL_PACK_ALIGNMENT, 1);
-	// generate mip map levels for average loglum and tone mapping 
-	glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
-	glGenerateMipmapEXT(GL_TEXTURE_2D);
-	
-	ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
-	ToneMap(fbo, light, imageKey, whiteLum, middleGrey);
-*/
+	if (useToneMapping)
+	{
+		float imageKey, whiteLum, middleGrey;
+
+		ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
+		ToneMap(fbo, light, imageKey, whiteLum, middleGrey);
+	}
+
 	AntiAliasing(fbo, light);
 
@@ -836,5 +830,5 @@
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	GLuint oldColorsTex = fbo->GetColorBuffer(3 - colorBufferIdx)->GetTexture();
+	//GLuint oldColorsTex = fbo->GetColorBuffer(3 - colorBufferIdx)->GetTexture();
 
 	fbo->Bind();
@@ -852,6 +846,6 @@
 	cgGLEnableTextureParameter(sColorsTexDeferredParam);
 
-	cgGLSetTextureParameter(sOldColorsTexDeferredParam, oldColorsTex);
-	cgGLEnableTextureParameter(sOldColorsTexDeferredParam);
+	//cgGLSetTextureParameter(sOldColorsTexDeferredParam, oldColorsTex);
+	//cgGLEnableTextureParameter(sOldColorsTexDeferredParam);
 
 	cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
@@ -879,5 +873,5 @@
 
 	cgGLDisableTextureParameter(sColorsTexDeferredParam);
-	cgGLDisableTextureParameter(sOldColorsTexDeferredParam);
+	//cgGLDisableTextureParameter(sOldColorsTexDeferredParam);
 	cgGLDisableTextureParameter(sPositionsTexDeferredParam);
 	cgGLDisableTextureParameter(sNormalsTexDeferredParam);
@@ -903,6 +897,5 @@
 
 	// generate mip map levels for position texture
-	//glBindTexture(GL_TEXTURE_2D, positionsTex);
-	//glGenerateMipmapEXT(GL_TEXTURE_2D);
+	//glBindTexture(GL_TEXTURE_2D, positionsTex); glGenerateMipmapEXT(GL_TEXTURE_2D);
 
 	// read the second buffer, write to the first buffer
@@ -1118,5 +1111,5 @@
 	GLuint shadowTex = shadowMap->GetDepthTexture();
 
-	GLuint oldColorsTex = fbo->GetColorBuffer(3 - colorBufferIdx)->GetTexture();
+	//GLuint oldColorsTex = fbo->GetColorBuffer(3 - colorBufferIdx)->GetTexture();
 
 	Matrix4x4 shadowMatrix;
@@ -1143,6 +1136,6 @@
 	cgGLEnableTextureParameter(sShadowMapParam);
 
-	cgGLSetTextureParameter(sOldColorsTexShadowParam, oldColorsTex);
-	cgGLEnableTextureParameter(sOldColorsTexShadowParam);
+	//cgGLSetTextureParameter(sOldColorsTexShadowParam, oldColorsTex);
+	//cgGLEnableTextureParameter(sOldColorsTexShadowParam);
 
 	cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor);
@@ -1178,8 +1171,6 @@
 	cgGLDisableTextureParameter(sNormalsTexShadowParam);
 	cgGLDisableTextureParameter(sShadowMapParam);
-
-	cgGLDisableTextureParameter(sOldColorsTexShadowParam);
-
 	cgGLDisableTextureParameter(sNoiseTexShadowParam);
+	//cgGLDisableTextureParameter(sOldColorsTexShadowParam);
 
 	FrameBufferObject::Release();
@@ -1226,4 +1217,54 @@
 	const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
 	middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
+
+
+	//////////
+	//-- compute avg loglum
+
+	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
+
+	fbo->Bind();
+
+	colorBufferIdx = 3 - colorBufferIdx;
+	glDrawBuffers(1, mrt + colorBufferIdx);
+
+	GLuint colorsTex = colorBuffer->GetTexture();
+	
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(sCgToneProgram);
+	
+	cgGLSetTextureParameter(sColorsTexLogLumParam, colorsTex);
+	
+	glColor3f(1.0f, 1.0f, 1.0f);
+
+	glBegin(GL_QUADS);
+
+	glColor3f(1.0f, 1.0f, 1.0f);
+
+	const float offs = 0.5f;
+
+	glBegin(GL_QUADS);
+
+	glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
+	glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
+	glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
+	glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexLogLumParam);
+
+	FrameBufferObject::Release();
+
+	PrintGLerror("ToneMapParams");
+
+
+	///////////////////
+	//-- compute avg loglum in scene using mipmapping
+
+	glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
+	glGenerateMipmapEXT(GL_TEXTURE_2D);
 }
 
