Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2978)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2982)
@@ -28,5 +28,5 @@
 //#define ILLUM_INTENSITY 9e-1f;
 
-#define SSAO_MIPMAP_LEVEL 0//1
+#define SSAO_MIPMAP_LEVEL 1
 #define GI_MIPMAP_LEVEL 0//2
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2974)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2982)
@@ -63,5 +63,6 @@
 				  uniform float3 dColor,
 				  uniform float3 eColor,
-				  uniform float4x4 ModelView)
+				  uniform float4x4 ModelView,
+				  uniform float multiplier)
 {
 	vtxout OUT;
@@ -94,5 +95,5 @@
 	OUT.color = float4(hcol, 1.0);
 
-	//OUT.color.rgb *= 2e-5f; 
+	OUT.color.rgb *= multiplier;
 
 	OUT.color2 = OUT.color;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2978)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2982)
@@ -98,5 +98,5 @@
 
 	if (oldColor.w > 0)
-		OUT.color.w = lerp(oldColor.w, logLumScaled, 0.2f);
+		OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
 	else
 		OUT.color.w = logLumScaled;
@@ -217,5 +217,5 @@
 
 	if (oldColor.w > 0)
-		OUT.color.w = lerp(oldColor.w, logLumScaled, 0.2f);
+		OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
 	else
 		OUT.color.w = logLumScaled;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2974)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2982)
@@ -94,4 +94,5 @@
 	// hack: squeeze some information about ambient into the texture
 	pix.col.w = glstate.material.emission.x;
+	//pix.col.w = length(currentPos - IN.worldPos)
 
 	return pix;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2979)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2982)
@@ -157,11 +157,14 @@
 	float newWeight;
 
+	float newNumSamples = ao.y;
+
 	if (//(temporalCoherence > 0) &&
 		(tex.x >= 0.0f) && (tex.x < 1.0f) && 
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
-		(abs(depthDif) < 1e-3f) 
+		(abs(depthDif) < 1e-4f) 
 		// check if visibility changed in the surrounding area:
 		// then we have to recompute
-		&& (oldNumSamples > ao.y - 1.5f) 
+		&& ((newWeight < 10) || (oldNumSamples > 0.9f * newNumSamples))
+		//&& (oldAvgDepth / newAvgDepth > 0.99)
 		)
 	{
@@ -173,5 +176,5 @@
 	}
 	else
-	{
+	{	
 		OUT.illum_col.xy = ao.xy;
 		newWeight = 0;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 2981)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 2982)
@@ -164,5 +164,5 @@
 	str.read(reinterpret_cast<char *>(&texId), sizeof(int));
 
-	if (0)//texId >= 0)
+	if (texId >= 0)
 		mat->SetTexture(mTextureTable[texId]);
 	
@@ -182,7 +182,4 @@
 		str.read(reinterpret_cast<char *>(&mat->mEmmisiveColor), sizeof(Vector3));
 		str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
-
-		mat->mAmbientColor = RandomColor();
-		mat->mDiffuseColor = RgbaColor(0,0,0,1);
 	}
 
@@ -228,6 +225,6 @@
 	}
 
-	// return new Geometry(vertices, normals, texcoords, vertexCount, true);
-	return new Geometry(vertices, normals, texcoords, vertexCount, false);
+	return new Geometry(vertices, normals, texcoords, vertexCount, true);
+	//return new Geometry(vertices, normals, texcoords, vertexCount, false);
 }
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2981)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2982)
@@ -24,4 +24,6 @@
 static CGparameter sDColorParam;
 static CGparameter sEColorParam;
+static CGparameter sMultiplierParam;
+
 
 
@@ -60,4 +62,6 @@
 		sDColorParam = cgGetNamedParameter(sCgSkyProgram, "dColor");
 		sEColorParam = cgGetNamedParameter(sCgSkyProgram, "eColor");
+
+		sMultiplierParam = cgGetNamedParameter(sCgSkyProgram, "multiplier");
 	}
 	else
@@ -75,8 +79,5 @@
 	{
 		cgGLLoadProgram(sCgMrtFragmentSkyDomeProgram);
-
-		/*
-		cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
-		*/
+		//cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
 	}
 	else
@@ -129,13 +130,20 @@
 	cgGLSetParameter3f(sEColorParam, ABCDE[4].x, ABCDE[4].y, ABCDE[4].z);
 
-	cgGLEnableProfile(RenderState::sCgVertexProfile);
-	cgGLBindProgram(sCgSkyProgram);
-
-	
 	if (state->GetRenderPassType() == RenderState::DEFERRED)
 	{
 		cgGLEnableProfile(RenderState::sCgFragmentProfile);
 		cgGLBindProgram(sCgMrtFragmentSkyDomeProgram);
+		
+		// can use tone mapping
+		cgGLSetParameter1f(sMultiplierParam, 1.0f);
 	}	
+	else 
+	{
+		// no tone mapping => scale
+		cgGLSetParameter1f(sMultiplierParam, 8e-5f);
+	}
+
+	cgGLEnableProfile(RenderState::sCgVertexProfile);
+	cgGLBindProgram(sCgSkyProgram);
 
 	// Render sky dome.
@@ -258,5 +266,5 @@
 
 
-void SkyPreetham::ComputeSunColor(const Vector3 &sunDir, Vector3 &ambient, Vector3 &diffuse) const
+void SkyPreetham::ComputeSunColor(const Vector3 &sunDir, Vector3 &ambient, Vector3 &diffuse, bool scaleToRange) const
 {
 	// sunDir is sun direction
@@ -280,6 +288,6 @@
 
 	// downscale ambient color
-	if (1)
-		ambient *= 5e-5f;
+	if (scaleToRange)
+		ambient *= 2e-5f;
 	else
 		ambient *= 1e-1f;
@@ -320,11 +328,10 @@
 
 	// Calculate final sun diffuse color.
-	if (1)
-		diffuse = color * 1.7e-4f;
+	if (scaleToRange)
+		diffuse = color * 5e-2f;
 	else
 		diffuse = color * 3e-1f;
 
 	diffuse *= (2.0f - 1.0f * DotProd(sunDir, Vector3::UNIT_Z()));
-	
 
 	//cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 2960)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 2982)
@@ -31,5 +31,8 @@
 						std::pair<float, float> &sunThetha) const;
 
-	void ComputeSunColor(const CHCDemoEngine::Vector3 &sunDir, CHCDemoEngine::Vector3 &ambient, CHCDemoEngine::Vector3 &diffuse) const;
+	void ComputeSunColor(const CHCDemoEngine::Vector3 &sunDir, 
+		                 CHCDemoEngine::Vector3 &ambient, 
+						 CHCDemoEngine::Vector3 &diffuse,
+						 bool scaleToRange) const;
 
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2981)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2982)
@@ -455,5 +455,5 @@
 	
 	SceneEntity *oldEnt = NULL;
-
+#if 0
 	cout << "merging entities .. " << endl;
 
@@ -489,5 +489,5 @@
 
 	cout << "merged " << merged << " of " << (int)sceneEntities.size() << " entities " << endl;
-
+#endif
 	// set far plane based on scene extent
 	farDist = 10.0f * Magnitude(bvh->GetBox().Diagonal());
@@ -502,18 +502,9 @@
 
 	Vector3 cubeCenter(470.398f, 240.364f, 182.5f);
-	/*AxisAlignedBox3 box(cubeCenter - Vector3(2.0f), cubeCenter + Vector3(2.0f));
-
-	Material *mat = new Material(RgbaColor(1, 0.4, 0.4, 0));
-	Transform3 *tf = new Transform3();
-	cube = SceneEntityConverter().ConvertBox(box, mat, tf);
-*/
-
+	
 	Matrix4x4 transl = TranslationMatrix(cubeCenter);
 	
-	//const string aeroplaneStr = model_path + "toyplane.dem";
-	//const string aeroplaneStr = model_path + "city_full.dem";
 
 	SceneEntityContainer dummy;
-
 	
 	string skyDomeStr(model_path + "sky.dem");
@@ -532,19 +523,5 @@
 
 
-	/*if (loader->Load(aeroplaneStr, dummy))
-		cout << "successfully loaded " << dummy.size() << " scene entities" << endl;
-	else
-	{
-		cerr << "loading file " << aeroplaneStr << " failed" << endl;
-
-		CleanUp();
-		exit(0);
-	}
-
-	aeroplane = dummy[1];
-
-	aeroplane->GetTransform()->MultMatrix(transl);
-*/
-	const float turbitiy = 3.0f;
+	const float turbitiy = 9.0f;
 	preetham = new SkyPreetham(turbitiy, skyDome);
 
@@ -859,13 +836,14 @@
 	GLfloat diffuse[] = {1.0f, 0.95f, 0.85f, 1.0f};
 	GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
-	//GLfloat specular[] = {0.0f, 0.0f, 0.0f, 1.0f};
+	
+
+	const bool useToneMapping = 
+		(renderMethod == RENDER_DEPTH_PASS_DEFERRED) ||
+		(renderMethod == RENDER_DEFERRED);
 
 	Vector3 sunAmbient;
 	Vector3 sunDiffuse;
 
-	preetham->ComputeSunColor(lightDir, sunAmbient, sunDiffuse);
-
-	const float maxComponent = sunDiffuse.MaxComponent();
-	//cout<< "sunambient: " << sunAmbient << " mag " << Magnitude(sunDiffuse) << " max: " << maxComponent << endl;
+	preetham->ComputeSunColor(lightDir, sunAmbient, sunDiffuse, !useToneMapping);
 
 	ambient[0] = sunAmbient.x;
@@ -873,12 +851,16 @@
 	ambient[2] = sunAmbient.z;
 
-	//sunDiffuse /= maxComponent;
+
+	// no tone mapping => scale
+	if (!useToneMapping)
+	{
+		const float maxComponent = sunDiffuse.MaxComponent();
+		//cout<< "sunambient: " << sunAmbient << " mag " << Magnitude(sunDiffuse) << " max: " << maxComponent << endl;
+		sunDiffuse /= maxComponent;
+	}
 
 	diffuse[0] = sunDiffuse.x;
 	diffuse[1] = sunDiffuse.y;
 	diffuse[2] = sunDiffuse.z;
-	
-	//cout<< "sunambient: " << sunAmbient << endl;
-	//cout<< "sundiffuse: " << sunDiffuse << endl;
 
 	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
@@ -1103,5 +1085,4 @@
 		
 		glEnableClientState(GL_NORMAL_ARRAY);
-
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -1133,6 +1114,6 @@
 	{
 		// actually render the scene geometry using the specified algorithm
-		//traverser->RenderScene();
-
+		traverser->RenderScene();
+/*
 #if 0
 		SceneEntityContainer::const_iterator sit, sit_end = sceneEntities.end();
@@ -1147,5 +1128,5 @@
 			renderQueue->Enqueue(*sit);
 #endif
-			
+*/			
 		renderQueue->Apply();
 	}
@@ -2063,6 +2044,4 @@
 
 		InitDeferredRendering();
-
-		glClear(GL_COLOR_BUFFER_BIT);
 	}
 	else
@@ -2078,4 +2057,6 @@
 	glDepthFunc(GL_LEQUAL);
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+
+	glClear(GL_COLOR_BUFFER_BIT);
 
 	state.SetUseAlphaToCoverage(true);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 2959)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 2982)
@@ -23,3 +23,3 @@
 
 # ssao temporal coherence factor
-tempCohFactor=100.0f
+tempCohFactor=50.0f
