Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 2960)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 2968)
@@ -89,5 +89,5 @@
 	bool mAlphaTestEnabled;
 	bool mCullFaceEnabled;
-	/// the assciated texture
+	/// the associated texture
 	Texture *mTexture;
 };
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2968)
@@ -35,7 +35,9 @@
 //-- tone mapping
 
+//#define MIDDLE_GRAY 0.5f
 //#define MIDDLE_GRAY 0.72f
-//#define MIDDLE_GRAY 0.36f
-#define MIDDLE_GRAY 0.18f
+#define MIDDLE_GRAY 0.36f
+//#define MIDDLE_GRAY 0.18f
+//#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 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2968)
@@ -24,6 +24,6 @@
 	float3 normal: TEXCOORD2;
 	float4 mypos: TEXCOORD3;
+	float4 color2: TEXCOORD4;
 };
-
 
 
@@ -34,4 +34,5 @@
   float4 norm: COLOR2;
 };
+
 
 // fragment input
@@ -46,4 +47,5 @@
 	float3 normal: TEXCOORD2;
 	float4 mypos: TEXCOORD3;
+	float4 color2: TEXCOORD4;
 };
 //--------------------------------------------------------------------------------------
@@ -92,8 +94,8 @@
 	OUT.color = float4(hcol, 1.0);
 
-	//OUT.color.rgb *= 1e-2f; 
+	//OUT.color.rgb *= 2e-3f; 
 	//OUT.color.rgb *= 2e-5f; 
 
-	//OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
+	OUT.color2 = OUT.color;
 	OUT.worldPos = mul(ModelView, IN.position);
 
@@ -109,11 +111,11 @@
 	pixel pix;
 
-	pix.col = IN.color;
+	pix.col = IN.color2;
 	pix.pos = IN.worldPos * 1e20;// * maxDepth;
 
 	pix.norm.xyz = IN.normal;
 	
-	// hack: squeeze some information about the ambient term into the target
-	pix.norm.w = 1;
+	// hack: squeeze some information about an ambient term into the target
+	pix.norm.w = 2;
 	pix.pos.w = IN.mypos.w;
 	
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2968)
@@ -37,9 +37,4 @@
 			 float3 lightDir)
 {
-	/*
-	float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
-	float3 light2 = normalize(lightDir2.xyz);
-	float diffuseLight2 = saturate(dot(normal, light2));
-	*/
 	// diffuse intensity
 	const float angle = saturate(dot(normal, lightDir)); 
@@ -51,7 +46,12 @@
 	const float4 ambient = glstate.light[0].ambient;
 	
-	return (emmisive < 0.95) ? (ambient + diffuse) * color : color;
+	float4 outColor;
 
-	//return (saturate(((ambient + diffuse))) * (1.0f - emmisive) + emmisive) * color;
+	if (emmisive > 1.5) outColor = color;
+	//else if (emmisive > 0.95)	outColor = color * lightDiffuse;
+	else
+		outColor = (ambient + diffuse) * color;
+
+	return outColor;
 	//return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
 }
@@ -82,4 +82,5 @@
 	
 	OUT.color = col;
+	//OUT.color = float4(100.0, 1, 1, 0);
 	OUT.color.w = color.w;
 
@@ -123,5 +124,5 @@
 	}
 
-	total_d /= (float)NUM_SAMPLES;
+	total_d /= (float)NUM_PCF_TABS;
 
 	return total_d;
@@ -182,5 +183,5 @@
 	
 	// base lighting
-	OUT.color = (emmisive < 0.95) ? (ambient + diffuse) * color : color;
+	OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
 
 	// also write out depth component
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2968)
@@ -21,5 +21,5 @@
 
 
-float3 ToneMap(const float imageKey, float3 color)
+float3 ToneMap(float imageKey, float whiteLum, float3 color)
 {
 	float3 outCol;
@@ -29,12 +29,8 @@
 	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);*/
+	// map to range and calc burnout
+	const float burnedLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
 	
-	outCol = color * scaledLum;
+	outCol = color * burnedLum / pixLum;
 	//vColor.rgb += 0.6f * vBloom;
 	
@@ -42,8 +38,10 @@
 }
 
+
 float4 main(v2p IN, 
 			uniform sampler2D colors,
 			uniform sampler2D normals,
-			uniform float imageKey
+			uniform float imageKey,
+			uniform float whiteLum
 			): COLOR
 {
@@ -115,5 +113,5 @@
 
 	//return float4(w);
-	col.xyz = ToneMap(imageKey, col.xyz);
+	col.xyz = ToneMap(imageKey, whiteLum, col.xyz);
 
 	return col;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp	(revision 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp	(revision 2968)
@@ -15,16 +15,55 @@
 
 
-float ToneMapper::CalcImageKey(ColorBufferObject *colorBuffer) const
+float ToneMapper::CalcImageKey(float *pixels, int w, int h) const
 {
 	float totalLum = .0f;
 
-	// we assume that we have a floating point rgba texture
-	float *pixels = (float *)colorBuffer->ReadTexture();
+	int size = w * h * 4;
 
-	const int w = colorBuffer->GetHeight();
-	const int h = colorBuffer->GetWidth();
+	// get avg log luminance
+	for (int i = 0; i < size; i += 4)
+	{
+		float r = pixels[i + 0];
+		float g = pixels[i + 1];
+		float b = pixels[i + 2];
 
-	// Get full scene luminance
-	for (int i = 0; i < h; ++ i)
+		float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b;
+
+		totalLum += log(pixLum + 1e-6f);
+	}
+
+	const float key = exp(totalLum / (float)(w * h));
+
+	static float previousKey = key;
+	const float factor = 1.0f;
+
+	const float imageKey = key;// * factor + previousKey * (1.0f - factor);
+	previousKey = imageKey;
+	
+	cout << "key: " << key << endl;
+
+	return imageKey;
+}
+
+
+float ToneMapper::CalcMaxLuminance(float *pixels, int w, int h) const
+{
+	float maxLum = 1e-6f;
+
+	int size = w * h * 4;
+
+	for (int i = 0; i < size; i += 4)
+	{
+		float r = pixels[i + 0];
+		float g = pixels[i + 1];
+		float b = pixels[i + 2];
+
+		float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b;
+
+		if (pixLum > maxLum)
+			maxLum = pixLum;
+	}
+
+/*	for (int i = 0; i < h; ++ i)
 	{
 		for (int j = 0; j < w; ++ j)
@@ -36,32 +75,12 @@
 			float b = pixels[4 * idx + 2];
 
-			float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b;
-
-			totalLum += log(pixLum + 1e-6f);
+			if (pixLum > maxLum)
+				maxLum = pixLum;
 		}
 	}
-
-	//cout << "totalLum: " << totalLum <<  endl;
-
-	const float key = exp(totalLum / (float)(w * h));
-
-	static float previousKey = key;
-
-	//float delta = imageKey - mPreviousImageKey;
-	// Constaint change for temporal coherence
-	//const float maxChange = 5-e2f;
-	//Clamp(delta, -maxChange, maxChange);
-
-	const float factor = 1.0f;
-
-	const float imageKey = key;// * factor + previousKey * (1.0f - factor);
-	previousKey = imageKey;
-	
-	cout << "key: " << key << endl;
-
-	delete [] pixels;
-
-	return imageKey;
+*/
+	return maxLum;
 }
 
+
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2968)
@@ -284,9 +284,9 @@
 	// downscale ambient color
 	//ambient *= 5e-5f;
-	ambient *= 2e-5f;
+	//ambient *= 2e-3f;
 
 	// simulate the sun intensity by modulating the ambient term.
 	//ambient *= (1.0f - 0.9f * DotProd(sunDir, Vector3::UNIT_Z()));
-	ambient += Vector3(0.2f);
+	//ambient += Vector3(0.2f);
 
 	Vector3 num;
@@ -321,7 +321,7 @@
 
 	// Calculate final sun diffuse color.
-	diffuse = color * 1.7e-4f;
-	//diffuse = color;
-
-	cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl;
-}
+	//diffuse = color * 2e-3f;
+	diffuse = color;
+
+	//cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.h	(revision 2966)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.h	(revision 2968)
@@ -7,5 +7,4 @@
 {
 
-class ColorBufferObject;
 
 /** Class representing a tone mapping algorithm
@@ -18,5 +17,7 @@
 	/** Caculates average log lumincance.
 	*/
-	float CalcImageKey(ColorBufferObject *colorBuffer) const;
+	float CalcImageKey(float *pixels, int w, int h) const;
+
+	float CalcMaxLuminance(float *pixels, int w, int h) const;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2968)
@@ -377,5 +377,5 @@
 
 	// create a new light
-	light = new DirectionalLight(lightDir, RgbaColor(1, 1, 1, 1));
+	light = new DirectionalLight(lightDir, RgbaColor(1, 1, 1, 1), RgbaColor(1, 1, 1, 1));
 
 
@@ -576,5 +576,5 @@
 		sMaxDepthParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "maxDepth");
 		RenderState::sTexParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "tex");
-
+	
 		cgGLSetParameter1f(sMaxDepthParamTex, MAX_DEPTH_CONST / farDist);
 	}
@@ -595,4 +595,5 @@
 
 		sMaxDepthParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "maxDepth");
+		
 		cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
 	}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2967)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2968)
@@ -97,6 +97,6 @@
 static CGparameter sLightDirParam;
 static CGparameter sLightDirShadowParam;
-
 static CGparameter sImageKeyParam;
+static CGparameter sWhiteLumParam;
 
 //#define USE_3D_SSAO
@@ -285,4 +285,5 @@
 		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
 		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
+		
 		sLightDirParam = cgGetNamedParameter(sCgDeferredProgram, "lightDir");
 	}
@@ -407,4 +408,5 @@
 
 		sImageKeyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "imageKey");
+		sWhiteLumParam = cgGetNamedParameter(sCgAntiAliasingProgram, "whiteLum");
 
 		sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");  
@@ -438,4 +440,5 @@
 		sNoiseTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "noiseTexture");
 		sSamplesShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "samples");
+
 		sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir");
 
@@ -667,7 +670,18 @@
 void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo)
 {
-	const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(colorBufferIdx));
-
-	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
+	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();
+
+	const float imageKey = ToneMapper().CalcImageKey(pixels, w, h);
+	const float whiteLum = 0.5f * ToneMapper().CalcMaxLuminance(pixels, w, h);
+
+	delete [] pixels;
+
+	GLuint colorsTex = colorBuffer->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 	
@@ -684,4 +698,6 @@
 
 	cgGLSetParameter1f(sImageKeyParam, imageKey);
+	cgGLSetParameter1f(sWhiteLumParam, whiteLum);
+
 
 	glColor3f(1.0f, 1.0f, 1.0f);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.h	(revision 2954)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.h	(revision 2968)
@@ -16,10 +16,16 @@
 public:
 	
-	DirectionalLight(const Vector3 &dir, const RgbaColor &col): mColor(col) { mDirection = Normalize(dir); }
+	DirectionalLight(const Vector3 &dir, const RgbaColor &amb, const RgbaColor &dif): 
+	mAmbient(amb), mDiffuse(dif) { mDirection = Normalize(dir); }
+
+	void SetDirection(const Vector3 &dir) { mDirection = dir; }
+	
+	void SetDiffuseColor(const RgbaColor &dif) { mDiffuse = dif; }
+	
+	void SetAmbientColor(const RgbaColor &amb) { mAmbient = amb; }
 
 	Vector3 GetDirection() const { return mDirection; }
-	void SetDirection(const Vector3 &dir) { mDirection = dir; }
-
-	RgbaColor GetColor() const { return mColor; }
+	RgbaColor GetDiffuseColor() const { return mDiffuse; }
+	RgbaColor GetAmbientColor() const { return mAmbient; }
 
 
@@ -27,5 +33,7 @@
 
 	Vector3 mDirection;
-	RgbaColor mColor;
+
+	RgbaColor mDiffuse;
+	RgbaColor mAmbient;
 };
 
