Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2974)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2975)
@@ -28,6 +28,6 @@
 //#define ILLUM_INTENSITY 9e-1f;
 
-#define SSAO_MIPMAP_LEVEL 1
-#define GI_MIPMAP_LEVEL 2
+#define SSAO_MIPMAP_LEVEL 0//1
+#define GI_MIPMAP_LEVEL 0//2
 
 
@@ -41,6 +41,9 @@
 //-- Tone mapping
 
-#define MINLOGLUM 10.0f
+#define MINLOGLUM (-10.0f)
 #define MAXLOGLUM 10.0f
 
+#define INV_LOGLUM_RANGE 0.05f
+#define LOGLUM_RANGE 20.0f
+
 #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 2974)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2975)
@@ -81,7 +81,9 @@
 	
 	OUT.color = col;
-	OUT.color.w = color.w;
-
-	// write out logaritmic luminance for tone mapping
+	
+
+	////////////
+	//-- write out logaritmic luminance for tone mapping
+
 	const float3 w = float3(0.299f, 0.587f, 0.114f);
 
@@ -89,7 +91,6 @@
 	float logLum = log(1e-5f + lum);
 
-	float invLogLumRange =  1 / (MAXLOGLUM + MINLOGLUM);
-	float logLumOffset = MINLOGLUM * invLogLumRange;
-	float logLumScaled = logLum * invLogLumRange + logLumOffset;
+	float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
+	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
 
 	OUT.color.w = logLumScaled;
@@ -194,5 +195,7 @@
 	OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
 
-	// write out logaritmic luminance for tone mapping
+	////////////
+	//-- write out logaritmic luminance for tone mapping
+
 	const float3 w = float3(0.299f, 0.587f, 0.114f);
 
@@ -200,5 +203,8 @@
 	float logLum = log(1e-5f + lum);
 
-	OUT.color.w = 100;//logLum;
+	float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
+	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
+
+	OUT.color.w = logLumScaled;
 	
 	return OUT;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2974)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2975)
@@ -90,8 +90,5 @@
 	// obtain new image key from highest mipmap level
 	float logLumScaled = tex2Dlod(colors, float4(.5f, .5f, 0, 99)).w;
-
-	float logLumRange = MAXLOGLUM + MINLOGLUM;
-	//float logLumOffset = MINLOGLUM * logLumRange;
-	float logLum = logLumScaled * logLumRange - MINLOGLUM;
+	float logLum = logLumScaled * LOGLUM_RANGE + MINLOGLUM;
 
 	float newImageKey = exp(logLum);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2974)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2975)
@@ -117,16 +117,12 @@
 
 	float4 norm = tex2D(normals, IN.texCoord.xy);
-	
-	// expand normal
 	float3 normal = normalize(norm.xyz);
-	
+
 	// a constant ambient term
 	const float amb = norm.w;
 	// the w coordinate from the persp. projection
 	float w = norm.w;
-
 	// the current world position
 	const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
-	
 	// the current color
 	const float currentDepth = centerPosition.w;
@@ -141,5 +137,8 @@
 	realPos.w = 1.0f;
 
-	// reproject
+
+	///////////
+	//-- reprojection from new frame into old one
+
 	float4 oldPos = mul(oldModelViewProj, realPos);
 
@@ -161,11 +160,11 @@
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
 		(abs(depthDif) < 1e-3f) 
-		&& (oldNumSamples > ao.y - 1.5f) // check if something happened in the surrounding area
+		// check if visibility changed in the surrounding area:
+		// then we have to recompute
+		&& (oldNumSamples > ao.y - 1.5f) 
 		)
 	{
 		// increase the weight for convergence
 		newWeight = oldWeight + 1.0f;
-
-		//OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
 		OUT.illum_col.xy = (ao.xy + oldCol.xy * oldWeight) / newWeight;
 
@@ -193,9 +192,9 @@
 
 	float4 col = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
-	float depth = tex2Dlod(positions, float4(IN.texCoord.xy, 0, 0)).w;
 	float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
 
 	OUT.illum_col = col * ao.x;
-	OUT.illum_col.w = ao.w;
+	//OUT.illum_col.w = ao.w;
+	OUT.illum_col.w = col.w;
 
 	return OUT;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2974)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2975)
@@ -52,12 +52,10 @@
 				 uniform float2 samples[NUM_SAMPLES],
 				 uniform float3 currentNormal,
-				 uniform float3 currentViewDir,
-				 uniform float4 centerPosition
+				 uniform float4 centerPosition,
+				 float w
+				 //, uniform float3 currentViewDir
 				 )
 {
 	GiStruct gi;
-
-	// the w coordinate from the persp. projection
-	float w = centerPosition.w;
 
 	// Check in a circular area around the current position.
@@ -117,5 +115,5 @@
 	}
 
-	gi.illum = saturate(total_color);
+	gi.illum = total_color;
 	gi.ao = float2(max(0.0f, 1.0f - total_ao), numSamples);
 
@@ -143,23 +141,20 @@
 
 	float4 norm = tex2D(normals, IN.texCoord.xy);
-	
-	// the ambient term
+	float3 normal = normalize(norm.xyz);	
+	// something like a constant ambient term
 	const float amb = norm.w;
-
-	// expand normal
-	float3 normal = normalize(norm.xyz);
-
 	/// the current view direction
-	float3 viewDir;// = normalize(IN.view);
-
+	//float3 viewDir = normalize(IN.view);
+
+	// the w coordinate from the persp. projection
+	float w = norm.w;
 	// the current world position
 	const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
-	
 	// the current color
 	const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
 	// the current depth is stored in the w component
-	const float currentDepth = currentCol.w;
-
-	GiStruct gi = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, centerPosition);
+	const float currentDepth = centerPosition.w;
+
+	GiStruct gi = globIllum(IN, colors, positions, noiseTexture, samples, normal, centerPosition, w); //, viewDir);
 	
 
@@ -186,10 +181,13 @@
 
 	const float oldNumSamples = oldSsao.y;
+	const float oldAvgDepth = oldSsao.z;
 
 	if ((temporalCoherence > 0.0f) &&
 		(tex.x >= 0.0f) && (tex.x < 1.0f) && 
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
-		(abs(depthDif)  < 1e-3f)
-		&& (oldNumSamples > gi.ao.y - 1.5f) // check if something happened in the surrounding area
+		(abs(depthDif) < 1e-3f)
+		// check if something changed in the surrounding area
+		&& (oldNumSamples > 0.2 * gi.ao.y)
+		&& (oldAvgDepth / newAvgDepth > 0.99)
 		)
 	{
@@ -218,5 +216,5 @@
 			  uniform sampler2D ssaoTex,
 			  uniform sampler2D illumTex
-		   )
+			  )
 {
 	pixel OUT;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2974)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2975)
@@ -9,5 +9,4 @@
 #include "ShadowMapping.h"
 #include "Light.h"
-#include "ToneMapper.h"
 
 
@@ -553,9 +552,4 @@
 		FirstPass(fbo, light);
 
-	glEnable(GL_TEXTURE_2D);
-	// generate mip map levels for position texture
-	glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
-	glGenerateMipmapEXT(GL_TEXTURE_2D);
-
 
 	switch (mShadingMethod)
@@ -576,4 +570,8 @@
 	float imageKey, whiteLum, middleGrey;
 
+	// generate mip map levels of position texture
+	glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
+	glGenerateMipmapEXT(GL_TEXTURE_2D);
+
 	ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
 	ToneMap(fbo, light, imageKey, whiteLum, middleGrey);
@@ -603,8 +601,9 @@
 {
 #ifdef USE_3D_SSAO
+	// bias from [-1, 1] to [0, 1]
 	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
 								0.0f, 0.5f, 0.0f, 0.5f,
 								0.0f, 0.0f, 0.5f, 0.5f,
-								0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1]
+								0.0f, 0.0f, 0.0f, 1.0f); 
 
 	Matrix4x4 m = projViewMatrix * biasMatrix; 
@@ -615,9 +614,9 @@
 	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
 
-	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
+	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	// generate mip map levels for position texture
+	// generate mip map levels of position texture
 	glBindTexture(GL_TEXTURE_2D, positionsTex);
 	glGenerateMipmapEXT(GL_TEXTURE_2D);
@@ -628,6 +627,4 @@
 	glDrawBuffers(1, mrt + mFboIndex);
 
-
-	//GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
 	GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
 
@@ -677,12 +674,11 @@
 	glBegin(GL_QUADS);
 
-	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
-	//const float new_offs = 0.55f;
-	const float new_offs = 0.5f;
-	
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
+	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
+	const float offs = 0.5f;
+	
+	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
+	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
+	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
+	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
 
 	glEnd();
@@ -1133,19 +1129,10 @@
 											 float &middleGrey)
 {
-	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
-
-	// we assume that we have a floating point rgba texture
-	float *pixels = (float *)colorBuffer->ReadTexture();
-	
-	const int w = colorBuffer->GetHeight();
-	const int h = colorBuffer->GetWidth();
-
-	imageKey = ToneMapper().CalcImageKey(pixels, w, h);
-	whiteLum = 1.0f * ToneMapper().CalcMaxLuminance(pixels, w, h);
-
-	//const float minKey = 0.18f;
-	//const float maxKey = 0.72f;
-
-	//const float minKey = 0.0045f;
+	// hack: estimate value where sky burns out
+	whiteLum = log(1e4f);
+	
+	////////////////////
+	//-- linear interpolate brightness key depending on the current sun position
+
 	const float minKey = 0.09f;
 	const float maxKey = 0.5f;
@@ -1153,61 +1140,4 @@
 	const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
 	middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
-
-	//cout << "middlegrey: " << middleGrey << endl;
-	delete [] pixels;
-
-
-	/////////////
-
-	/*GLuint colorsTex = colorBuffer->GetTexture();
-
-	fbo->Bind();
-
-	colorBufferIdx = 3 - colorBufferIdx;
-	glDrawBuffers(1, mrt + colorBufferIdx);
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgInitialIntensityProgram);
-	
-	cgGLSetTextureParameter(sColorsTexInitialParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexInitialParam);
-
-	// first pass: create high res intensity texture
-	glColor3f(1.0f, 1.0f, 1.0f);
-
-	float offs2 = 0.5f;
-
-	glBegin(GL_QUADS);
-
-	// the neighbouring texels
-	float x_offs = 0;//1.0f / mWidth;
-	float y_offs = 0;//1.0f / mHeight;
-
-	SetVertex(0, 0, x_offs, y_offs);
-	SetVertex(1, 0, x_offs, y_offs);
-	SetVertex(1, 1, x_offs, y_offs);
-	SetVertex(0, 1, x_offs, y_offs);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexToneParam);
-
-	FrameBufferObject::Release();
-
-	
-	////////////
-	//-- second phase: down sample the texture in order to compute the average
-	
-	// q: required passes for computing 1x1 texture?
-	int passes = 32;
-
-	for (int i = 0; i < 32; ++ i)
-	{
-		//DownSample(fbo);
-	}
-*/
-	PrintGLerror("antialiasing");
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2972)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2975)
@@ -283,12 +283,4 @@
 				>
 			</File>
-			<File
-				RelativePath=".\src\ToneMapper.cpp"
-				>
-			</File>
-			<File
-				RelativePath=".\src\ToneMapper.h"
-				>
-			</File>
 			<Filter
 				Name="timer"
