Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2966)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2967)
@@ -35,4 +35,5 @@
 //-- tone mapping
 
+//#define MIDDLE_GRAY 0.72f
 //#define MIDDLE_GRAY 0.36f
 #define MIDDLE_GRAY 0.18f
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2964)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2967)
@@ -92,5 +92,6 @@
 	OUT.color = float4(hcol, 1.0);
 
-	OUT.color.rgb *= 5e-5f; 
+	//OUT.color.rgb *= 1e-2f; 
+	//OUT.color.rgb *= 2e-5f; 
 
 	//OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2966)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2967)
@@ -15,27 +15,4 @@
 	float4 color: COLOR0;
 };
-
-
-
-float3 ToneMap(const float imageKey, float3 color)
-{
-	float3 outCol;
-	const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
-	
-	// adjust to middle gray
-	const float lum = MIDDLE_GRAY * pixLum / imageKey;
-	 
-    // map to range
-	const float scaledLum = lum / (1.0f + lum);
-
-    /*outCol *= MIDDLE_GRAY / (vLum + 1e-3f);
-    outCol *= (1.0f + vColor / LUM_WHITE);
-	outCol /= (1.0f + vColor);*/
-	
-	outCol = color / scaledLum;
-	//vColor.rgb += 0.6f * vBloom;
-	
-	return outCol;
-}
 
 
@@ -74,6 +51,8 @@
 	const float4 ambient = glstate.light[0].ambient;
 	
+	return (emmisive < 0.95) ? (ambient + diffuse) * color : color;
+
 	//return (saturate(((ambient + diffuse))) * (1.0f - emmisive) + emmisive) * color;
-	return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
+	//return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
 }
 
@@ -86,6 +65,5 @@
 		   uniform sampler2D positions,
 		   uniform sampler2D normals,
-		   uniform float3 lightDir,
-		   uniform float imageKey
+		   uniform float3 lightDir
 		   )
 {
@@ -161,6 +139,5 @@
 				  uniform sampler2D noiseTexture,
 				  uniform float2 samples[NUM_PCF_TABS],
-				  uniform float3 lightDir,
-				  uniform float imageKey
+				  uniform float3 lightDir
 				  )
 {
@@ -207,6 +184,4 @@
 	OUT.color = (emmisive < 0.95) ? (ambient + diffuse) * color : color;
 
-	OUT.color.xyz = ToneMap(imageKey, OUT.color.xyz);
-
 	// also write out depth component
 	OUT.color.w = color.w;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2967)
@@ -1,2 +1,4 @@
+#include "../shaderenv.h"
+
 struct v2p
 {
@@ -18,7 +20,30 @@
 
 
+
+float3 ToneMap(const float imageKey, float3 color)
+{
+	float3 outCol;
+	const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
+	
+	// adjust to middle gray
+	const float lum = MIDDLE_GRAY * pixLum / imageKey;
+	 
+    // map to range
+	const float scaledLum = lum / (1.0f + lum);
+
+    /*outCol *= MIDDLE_GRAY / (vLum + 1e-3f);
+    outCol *= (1.0f + vColor / LUM_WHITE);
+	outCol /= (1.0f + vColor);*/
+	
+	outCol = color * scaledLum;
+	//vColor.rgb += 0.6f * vBloom;
+	
+	return outCol;
+}
+
 float4 main(v2p IN, 
 			uniform sampler2D colors,
-			uniform sampler2D normals
+			uniform sampler2D normals,
+			uniform float imageKey
 			): COLOR
 {
@@ -86,6 +111,10 @@
 	float4 sc = tex2Dlod(colors, float4(IN.c.xy, 0, 0));
 
-	return (s0 + s1 + s2 + s3 + sc) * 0.2f;
+	float4 col = (s0 + s1 + s2 + s3 + sc) * 0.2f;
 	//return (s0 + s1 + s2 + s3) * 0.25f;
+
 	//return float4(w);
+	col.xyz = ToneMap(imageKey, col.xyz);
+
+	return col;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp	(revision 2966)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp	(revision 2967)
@@ -1,4 +1,7 @@
 #include "ToneMapper.h"
 #include "FrameBufferObject.h"
+
+
+using namespace std;
 
 
@@ -14,5 +17,5 @@
 float ToneMapper::CalcImageKey(ColorBufferObject *colorBuffer) const
 {
-	float totalLum = 0;
+	float totalLum = .0f;
 
 	// we assume that we have a floating point rgba texture
@@ -35,9 +38,11 @@
 			float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b;
 
-			totalLum += log(pixLum + 1e-3f);
+			totalLum += log(pixLum + 1e-6f);
 		}
 	}
 
-	const float key = exp(totalLum) / (float)(w * h);
+	//cout << "totalLum: " << totalLum <<  endl;
+
+	const float key = exp(totalLum / (float)(w * h));
 
 	static float previousKey = key;
@@ -48,9 +53,11 @@
 	//Clamp(delta, -maxChange, maxChange);
 
-	const float factor = 0.5f;
+	const float factor = 1.0f;
 
-	const float imageKey = key * factor + previousKey * (1.0f - factor);
+	const float imageKey = key;// * factor + previousKey * (1.0f - factor);
 	previousKey = imageKey;
 	
+	cout << "key: " << key << endl;
+
 	delete [] pixels;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2961)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2967)
@@ -272,11 +272,9 @@
 	ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta);
 
-	ambient = zenithColor;
-
 	Vector3 zenith_XYZ;                                                                                             
 
-	zenith_XYZ.x = (ambient.x / ambient.y) * ambient.z;                                                                        
-	zenith_XYZ.y = ambient.z;                                                                                          
-	zenith_XYZ.z = ((1.0f - ambient.x - ambient.y) / ambient.y) * ambient.z;                                                                
+	zenith_XYZ.x = (zenithColor.x / zenithColor.y) * zenithColor.z;                                                                        
+	zenith_XYZ.y = zenithColor.z;                                                                                          
+	zenith_XYZ.z = ((1.0f - zenithColor.x - zenithColor.y) / zenithColor.y) * zenithColor.z;                                                                
 
 	ambient.x =  3.240479f * zenith_XYZ.x - 1.537150f * zenith_XYZ.y - 0.498535f * zenith_XYZ.z;
@@ -285,8 +283,9 @@
 
 	// downscale ambient color
-	ambient *= 5e-5f;
+	//ambient *= 5e-5f;
+	ambient *= 2e-5f;
 
 	// simulate the sun intensity by modulating the ambient term.
-	ambient *= (1.0f - 0.9f * DotProd(sunDir, Vector3::UNIT_Z()));
+	//ambient *= (1.0f - 0.9f * DotProd(sunDir, Vector3::UNIT_Z()));
 	ambient += Vector3(0.2f);
 
@@ -304,5 +303,5 @@
 
 
-	Vector3 xyY = (num / den) * zenithColor;    
+	Vector3 xyY = zenithColor * num / den;    
 
 	Vector3 XYZ;                                                                                             
@@ -312,4 +311,7 @@
 	XYZ.z = ((1.0f - xyY.x - xyY.y) / xyY.y) * xyY.z;                                                                
 
+
+	/////////////
+	//-- transform to rgb
 
 	Vector3 color;	
@@ -319,4 +321,7 @@
 
 	// Calculate final sun diffuse color.
-	diffuse = color * 0.00017f;
-}
+	diffuse = color * 1.7e-4f;
+	//diffuse = color;
+
+	cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2965)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2967)
@@ -828,11 +828,4 @@
 	Vector3 sunDiffuse;
 
-	Vector3 h;
-
-	h.x = lightDir.x;
-	h.y = lightDir.z;
-	h.z = lightDir.y;
-
-
 	preetham->ComputeSunColor(lightDir, sunAmbient, sunDiffuse);
 
@@ -844,5 +837,5 @@
 	ambient[2] = sunAmbient.z;
 
-	sunDiffuse /= maxComponent;
+	//sunDiffuse /= maxComponent;
 
 	diffuse[0] = sunDiffuse.x;
@@ -1575,4 +1568,7 @@
 	lightDir = roty * lightDir;
 	lightDir = rotx * lightDir;
+
+	// normalize to avoid accumulating errors
+	lightDir.Normalize();
 
 	light->SetDirection(lightDir);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2966)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2967)
@@ -35,4 +35,5 @@
 static CGprogram sCgCombinedIllumProgram = NULL;
 
+
 ///////////////////////////////////////
 
@@ -98,5 +99,4 @@
 
 static CGparameter sImageKeyParam;
-static CGparameter sImageKeyShadowParam;
 
 //#define USE_3D_SSAO
@@ -286,6 +286,4 @@
 		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
 		sLightDirParam = cgGetNamedParameter(sCgDeferredProgram, "lightDir");
-
-		sImageKeyParam = cgGetNamedParameter(sCgDeferredProgram, "imageKey");
 	}
 	else
@@ -408,4 +406,6 @@
 		cgGLLoadProgram(sCgAntiAliasingProgram);
 
+		sImageKeyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "imageKey");
+
 		sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");  
 		sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
@@ -440,5 +440,4 @@
 		sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir");
 
-		sImageKeyShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "imageKey");
 
 		PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f);
@@ -668,4 +667,6 @@
 void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo)
 {
+	const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(colorBufferIdx));
+
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
@@ -681,4 +682,6 @@
 	cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
 	cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
+
+	cgGLSetParameter1f(sImageKeyParam, imageKey);
 
 	glColor3f(1.0f, 1.0f, 1.0f);
@@ -708,6 +711,4 @@
 void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light)
 {
-	const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(0));
-
 	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
 	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
@@ -737,5 +738,4 @@
 	cgGLSetParameter3f(sLightDirParam, lightDir.x, lightDir.y, lightDir.z);
 
-	cgGLSetParameter1f(sImageKeyParam, imageKey);
 
 	glColor3f(1.0f, 1.0f, 1.0f);
@@ -978,6 +978,4 @@
 									   ShadowMap *shadowMap)
 {
-	const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(0));
-
 	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
 
@@ -1024,6 +1022,4 @@
 	cgGLSetParameter3f(sLightDirShadowParam, lightDir.x, lightDir.y, lightDir.z);
 
-	// the average log luminance for tone mapping
-	cgGLSetParameter1f(sImageKeyShadowParam, imageKey);
 
 	glColor3f(1.0f, 1.0f, 1.0f);
