Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2970)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2974)
@@ -38,4 +38,9 @@
 
 
+//////////
+//-- Tone mapping
+
+#define MINLOGLUM 10.0f
+#define MAXLOGLUM 10.0f
 
 #endif // __SHADERENV_H
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2968)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2974)
@@ -94,5 +94,4 @@
 	OUT.color = float4(hcol, 1.0);
 
-	//OUT.color.rgb *= 2e-3f; 
 	//OUT.color.rgb *= 2e-5f; 
 
@@ -112,14 +111,12 @@
 
 	pix.col = IN.color2;
-	pix.pos = IN.worldPos * 1e20;// * maxDepth;
+	pix.pos = 1e20f;
 
 	pix.norm.xyz = IN.normal;
 	
-	// hack: squeeze some information about an ambient term into the target
-	pix.norm.w = 2;
-	pix.pos.w = IN.mypos.w;
+	pix.norm.w = IN.mypos.w;
+	pix.pos.w = 1e20f;
 	
-	// the projected depth
-	pix.col.w = IN.mypos.z / IN.mypos.w;
+	pix.col.w = 2.0f;
 
 	return pix;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2968)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2974)
@@ -48,8 +48,7 @@
 	float4 outColor;
 
-	if (emmisive > 1.5) outColor = color;
-	//else if (emmisive > 0.95)	outColor = color * lightDiffuse;
-	else
-		outColor = (ambient + diffuse) * color;
+	//if (color.w > 1e19f) outColor = color;
+	if (emmisive > 1.5f) outColor = color;
+	else outColor = (ambient + diffuse) * color;
 
 	return outColor;
@@ -75,5 +74,5 @@
 
 	// an ambient color term
-	float amb = norm.w;
+	float amb = color.w;
 
 	float3 normal = normalize(norm.xyz);
@@ -82,6 +81,17 @@
 	
 	OUT.color = col;
-	//OUT.color = float4(100.0, 1, 1, 0);
 	OUT.color.w = color.w;
+
+	// write out logaritmic luminance for tone mapping
+	const float3 w = float3(0.299f, 0.587f, 0.114f);
+
+	float lum = dot(OUT.color.rgb, w);
+	float logLum = log(1e-5f + lum);
+
+	float invLogLumRange =  1 / (MAXLOGLUM + MINLOGLUM);
+	float logLumOffset = MINLOGLUM * invLogLumRange;
+	float logLumScaled = logLum * invLogLumRange + logLumOffset;
+
+	OUT.color.w = logLumScaled;
 
 	return OUT;
@@ -152,5 +162,5 @@
 	
 	// hack: an emmisive color term
-	float emmisive = norm.w;
+	float emmisive = color.w;
 
 	// diffuse intensity
@@ -163,5 +173,5 @@
 
 	// calc diffuse illumination + shadow term
-	if ((emmisive < 0.95f) // hack: prevent shadowing the sky	
+	if ((emmisive < 1.5f) // hack: prevent shadowing the sky	
 		&& (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
 		)
@@ -178,6 +188,5 @@
 	}
 
-	// global ambient
-	//const float4 ambient = 0.25f;
+	// light ambient term
 	const float4 ambient = glstate.light[0].ambient;
 	
@@ -185,7 +194,12 @@
 	OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
 
-	// also write out depth component
-	OUT.color.w = color.w;
-
+	// write out logaritmic luminance for tone mapping
+	const float3 w = float3(0.299f, 0.587f, 0.114f);
+
+	float lum = dot(OUT.color.rgb, w);
+	float logLum = log(1e-5f + lum);
+
+	OUT.color.w = 100;//logLum;
+	
 	return OUT;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2965)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2974)
@@ -75,16 +75,16 @@
 
 	// save color in first render target
-	// hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
+	// hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
 	pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor; 
 	
 	// save world position in second render target
 	pix.pos = IN.worldPos * maxDepth;
-	// save normal in third rt
+	// save world space normal in third rt
 	pix.norm.xyz = IN.normal;
 
-	// hack: squeeze some information about ambient into the texture
-	pix.norm.w = glstate.material.emission.x;
 	// store projection coordinates with positions (used for ssao)
-	pix.pos.w = IN.projPos.w;
+	pix.norm.w = IN.projPos.w;
+	// write the depth
+	pix.pos.w = IN.mypos.z / IN.mypos.w;
 
 	// account for alpha blending
@@ -92,6 +92,6 @@
 		discard;
 
-	// write the depth
-	pix.col.w = IN.mypos.z / IN.mypos.w;
+	// hack: squeeze some information about ambient into the texture
+	pix.col.w = glstate.material.emission.x;
 
 	return pix;
@@ -110,10 +110,11 @@
 	pix.norm.xyz = IN.normal;
 	
+	// store projection coordinates with positions (used for ssao)
+	pix.norm.w = IN.mypos.w;
+	// the projected depth
+	pix.pos.w = IN.mypos.z / IN.mypos.w;
+	
 	// hack: squeeze some information about the ambient term into the target
-	pix.norm.w = glstate.material.emission.x;
-	pix.pos.w = IN.mypos.w;
-	
-	// the projected depth
-	pix.col.w = IN.mypos.z / IN.mypos.w;
+	pix.col.w = glstate.material.emission.x;
 
 	return pix;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2973)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2974)
@@ -1,5 +1,5 @@
 #include "../shaderenv.h"
 
-struct v2p
+struct fragment
 {
 	float2 c:  TEXCOORD0; // center
@@ -21,5 +21,5 @@
 
 
-float4 main(v2p IN, 
+float4 main(fragment IN, 
 			uniform sampler2D colors,
 			uniform sampler2D normals
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2973)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2974)
@@ -2,9 +2,8 @@
 
 
-struct fragment
+struct frag
 {
 	 // normalized screen position
-	float4 pos: WPOS;
-	float4 texCoord: TEXCOORD0; 
+	float2 texCoord: TEXCOORD0; 
 
 	float2 lt: TEXCOORD1; // left top
@@ -21,5 +20,5 @@
 
 
-float4 DownSample(fragment IN,
+float4 DownSample(frag IN,
 				  uniform sampler2D colors,
 				  float2 downSampleOffs[16]) : COLOR
@@ -29,5 +28,5 @@
     for(int i = 0; i < 16; ++ i)
     {
-        average += tex2D(colors, IN.texCoord.xy + downSampleOffs[i]);
+        average += tex2D(colors, IN.texCoord + downSampleOffs[i]);
     }
         
@@ -38,11 +37,12 @@
     
 
-float4 GreyScaleDownSample(fragment IN,
-						   uniform sampler2D colors): COLOR
+float4 GreyScaleDownSample(frag IN,
+						   uniform sampler2D colors
+						   ): COLOR
 {
 
 	// Compute the average of the 4 necessary samples
-	float average = 0.0f;
-	float maximum = -1e20;
+	float average = .0f;
+	float maximum = .0f;
 
 	// the rgb weights
@@ -52,9 +52,8 @@
 	float4 cols[4];
 
-	cols[0] = tex2D(colors, IN.texCoord.xy + IN.lt.xy);
-	cols[1] = tex2D(colors, IN.texCoord.xy + IN.lb.xy);
-	cols[2] = tex2D(colors, IN.texCoord.xy + IN.rt.xy);
-	cols[3] = tex2D(colors, IN.texCoord.xy + IN.rb.xy);
-
+	cols[0] = tex2D(colors, IN.lt);
+	cols[1] = tex2D(colors, IN.lb);
+	cols[2] = tex2D(colors, IN.rt);
+	cols[3] = tex2D(colors, IN.rb);
 
 	for (int i = 0; i < 4; ++ i)
@@ -63,17 +62,20 @@
 
 		maximum = max(maximum, intensity);
-		average += (0.25f * log(1e-5f + intensity));
+		average += log(1e-5f + intensity);
 	}
 
-	average = exp(average);
+	average = exp(average * 0.25f);
 
-	return tex2D(colors, IN.texCoord.xy);
+	float4 hcols = cols[0] + cols[1] + cols[2] + cols[3];
+
+	return hcols * 0.25f;
+	//return float4(average, maximum, 0.0f, cols[0].w);
 
 	// Output the luminance to the render target
-	return float4(average, maximum, 0.0f, 1.0f);
+	//return float4(average, maximum, 0.0f, 1.0f);
 }
     
 
-pixel ToneMap(fragment IN,
+pixel ToneMap(frag IN,
 			  uniform sampler2D colors,
 			  uniform float imageKey, 
@@ -82,10 +84,19 @@
 {
 	pixel OUT;
-	float4 color = tex2D(colors, IN.texCoord.xy);
+	float4 color = tex2D(colors, IN.texCoord);
 
 	const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
 	
+	// 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 newImageKey = exp(logLum);
+
 	// adjust to middle gray
-	const float lum = middleGrey * pixLum / imageKey;
+	const float lum = middleGrey * pixLum / newImageKey;
 	 
 	// map to range and calc burnout
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2964)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2974)
@@ -40,11 +40,9 @@
 		   uniform float2 samples[NUM_SAMPLES],
 		   uniform float3 currentNormal,
-		   uniform float4 centerPosition
+		   uniform float4 centerPosition,
+		   uniform float scaleFactor
 		   //,uniform float3 currentViewDir
 		   )
 {
-	// the w coordinate from the persp. projection
-	float w = centerPosition.w;
-
 	// Check in a circular area around the current position.
 	// Shoot vectors to the positions there, and check the angle to these positions.
@@ -67,5 +65,5 @@
 #endif
 		// weight with projected coordinate to reach similar kernel size for near and far
-		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
+		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * scaleFactor;
 
 		if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
@@ -120,12 +118,11 @@
 	float4 norm = tex2D(normals, IN.texCoord.xy);
 	
-	// the ambient term
-	const float amb = norm.w;
-
 	// expand normal
 	float3 normal = normalize(norm.xyz);
 	
-	/// the current view direction
-	//float3 viewDir = normalize(IN.view);
+	// a constant ambient term
+	const float amb = norm.w;
+	// the w coordinate from the persp. projection
+	float w = norm.w;
 
 	// the current world position
@@ -133,8 +130,7 @@
 	
 	// the current color
-	const float4 currentCol = tex2D(colors, IN.texCoord.xy);
-	const float currentDepth = currentCol.w;
-
-	const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition);//, viewDir);
+	const float currentDepth = centerPosition.w;
+
+	const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w);//, viewDir);
 		
 
@@ -182,6 +178,4 @@
 	}
 
-
-	//OUT.illum_col.y = ao.y;
 	OUT.illum_col.z = newWeight;
 	OUT.illum_col.w = currentDepth;
@@ -193,14 +187,14 @@
 pixel combine(fragment IN, 
 			  uniform sampler2D colors,
-			  uniform sampler2D ssaoTex)
+			  uniform sampler2D ssaoTex,
+			  uniform sampler2D positions)
 {
 	pixel OUT;
 
 	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 = (float4)ao.x;
-
 	OUT.illum_col.w = ao.w;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2964)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2974)
@@ -227,6 +227,4 @@
 	
 	OUT.illum_col = (col + illum) * ao;
-	//OUT.illum_col = ao;
-
 	OUT.illum_col.w = col.w;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2968)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2974)
@@ -618,9 +618,7 @@
 	// the diffuse color buffer
 	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
-	//fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
 
 	// the positions buffer
 	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
-	//fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
 
 	// the normals buffer
@@ -1054,5 +1052,6 @@
 		
 		// draw to 3 color buffers
-		glDrawBuffers(3, mrt);
+		// a color, normal, and positions buffer
+		glDrawBuffers(4, mrt);
 
 		glEnableClientState(GL_NORMAL_ARRAY);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2973)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2974)
@@ -80,4 +80,5 @@
 static CGparameter sColorsTexCombinedSsaoParam;
 static CGparameter sSsaoTexCombinedSsaoParam;
+static CGparameter sPositionsTexCombinedSsaoParam;
 
 
@@ -403,4 +404,5 @@
 		sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors");  
 		sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
+		sPositionsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "positions");
 	}
 	else
@@ -551,10 +553,9 @@
 		FirstPass(fbo, light);
 
-
-	float imageKey, whiteLum, middleGrey;
-
-	ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
-
-	ToneMap(fbo, light, imageKey, whiteLum, middleGrey);
+	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)
@@ -573,4 +574,9 @@
 	}
 
+	float imageKey, whiteLum, middleGrey;
+
+	ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
+	ToneMap(fbo, light, imageKey, whiteLum, middleGrey);
+
 	AntiAliasing(fbo, light);
 
@@ -613,11 +619,8 @@
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	if (1)
-	{
-		// generate mip map levels for position texture
-		glBindTexture(GL_TEXTURE_2D, positionsTex);
-		glGenerateMipmapEXT(GL_TEXTURE_2D);
-	}
-
+	// generate mip map levels for position texture
+	glBindTexture(GL_TEXTURE_2D, positionsTex);
+	glGenerateMipmapEXT(GL_TEXTURE_2D);
+	
 
 	// read the second buffer, write to the first buffer
@@ -748,6 +751,4 @@
 	glColor3f(1.0f, 1.0f, 1.0f);
 
-	float offs2 = 0.5f;
-
 	glBegin(GL_QUADS);
 
@@ -832,19 +833,11 @@
 	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (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();
 
-	if (1)
-	{
-		// generate mip map levels for position texture
-		glBindTexture(GL_TEXTURE_2D, positionsTex);
-		glGenerateMipmapEXT(GL_TEXTURE_2D);
-
-		// generate mip map levels for position texture
-		glBindTexture(GL_TEXTURE_2D, colorsTex);
-		glGenerateMipmapEXT(GL_TEXTURE_2D);
-	}
-
+	// generate mip map levels for position texture
+	glBindTexture(GL_TEXTURE_2D, positionsTex);
+	glGenerateMipmapEXT(GL_TEXTURE_2D);
 
 	// read the second buffer, write to the first buffer
@@ -991,4 +984,5 @@
 
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
 	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
 
@@ -1009,4 +1003,6 @@
 	cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
 
+	cgGLSetTextureParameter(sPositionsTexCombinedSsaoParam, positionsTex);
+	cgGLEnableTextureParameter(sPositionsTexCombinedSsaoParam);
 	
 	glColor3f(1.0f, 1.0f, 1.0f);
@@ -1025,5 +1021,6 @@
 	cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
 	cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
-
+	cgGLDisableTextureParameter(sPositionsTexCombinedSsaoParam);
+	
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
 
@@ -1163,5 +1160,5 @@
 	/////////////
 
-	GLuint colorsTex = colorBuffer->GetTexture();
+	/*GLuint colorsTex = colorBuffer->GetTexture();
 
 	fbo->Bind();
@@ -1186,6 +1183,6 @@
 
 	// the neighbouring texels
-	float x_offs = 1.0f / mWidth;
-	float y_offs = 1.0f / mHeight;
+	float x_offs = 0;//1.0f / mWidth;
+	float y_offs = 0;//1.0f / mHeight;
 
 	SetVertex(0, 0, x_offs, y_offs);
@@ -1211,5 +1208,5 @@
 		//DownSample(fbo);
 	}
-
+*/
 	PrintGLerror("antialiasing");
 }
@@ -1258,11 +1255,9 @@
 	glColor3f(1.0f, 1.0f, 1.0f);
 
-	float offs2 = 0.5f;
-
 	glBegin(GL_QUADS);
 
 	// the neighbouring texels
-	float x_offs = 1.0f / mWidth;
-	float y_offs = 1.0f / mHeight;
+	const float x_offs = 1.0f / mWidth;
+	const float y_offs = 1.0f / mHeight;
 
 	SetVertex(0, 0, x_offs, y_offs);
