Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln	(revision 2961)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln	(revision 3010)
@@ -2,7 +2,12 @@
 Microsoft Visual Studio Solution File, Format Version 9.00
 # Visual Studio 2005
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "friendlyculling", "FriendlyCulling.vcproj", "{03661866-4093-4B02-B26A-028EA91AF023}"
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DemoEngine", "FriendlyCulling.vcproj", "{03661866-4093-4B02-B26A-028EA91AF023}"
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Converter", "Converter.vcproj", "{93A522E1-76F0-4D46-9C97-30DC2DDB531D}"
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FriendlyCulling", "MainApp.vcproj", "{A564071F-D054-4556-883B-999D7F9CC58C}"
+	ProjectSection(ProjectDependencies) = postProject
+		{03661866-4093-4B02-B26A-028EA91AF023} = {03661866-4093-4B02-B26A-028EA91AF023}
+	EndProjectSection
 EndProject
 Global
@@ -20,4 +25,8 @@
 		{93A522E1-76F0-4D46-9C97-30DC2DDB531D}.Release|Win32.ActiveCfg = Release|Win32
 		{93A522E1-76F0-4D46-9C97-30DC2DDB531D}.Release|Win32.Build.0 = Release|Win32
+		{A564071F-D054-4556-883B-999D7F9CC58C}.Debug|Win32.ActiveCfg = Debug|Win32
+		{A564071F-D054-4556-883B-999D7F9CC58C}.Debug|Win32.Build.0 = Debug|Win32
+		{A564071F-D054-4556-883B-999D7F9CC58C}.Release|Win32.ActiveCfg = Release|Win32
+		{A564071F-D054-4556-883B-999D7F9CC58C}.Release|Win32.Build.0 = Release|Win32
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/IrradianceMap.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/IrradianceMap.cpp	(revision 3010)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/IrradianceMap.cpp	(revision 3010)
@@ -0,0 +1,12 @@
+#include "IrradianceMap.h"
+
+
+namespace CHCDemoEngine 
+{
+
+IrradianceMap::IrradianceMap()
+{
+}
+
+
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3009)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3010)
@@ -9,16 +9,9 @@
 //#define NUM_SAMPLES 16
 
-// for linear falloff
-//#define SAMPLE_INTENSITY 500.0f
+// for quadratic falloff
+#define SAMPLE_INTENSITY 0.35f
+//#define SAMPLE_INTENSITY 0.32f
 
-// for quadratic falloff
-#define SAMPLE_INTENSITY 0.32f
-//#define SAMPLE_INTENSITY 0.85f
-
-//#define AREA_SIZE 15e-1f
 #define AREA_SIZE 8e-1f
-
-#define VIEW_CORRECTION_SCALE 0.0f
-//#define VIEW_CORRECTION_SCALE 0.1f
 
 //#define DISTANCE_SCALE 1e-4f
@@ -26,8 +19,6 @@
 
 #define ILLUM_INTENSITY 5e-1f;
-//#define ILLUM_INTENSITY 9e-1f;
 
-#define SSAO_MIPMAP_LEVEL 0//1
-#define GI_MIPMAP_LEVEL 0//2
+#define VIEW_CORRECTION_SCALE 0.1f
 
 
@@ -47,7 +38,13 @@
 #define LOGLUM_RANGE 20.0f
 
-#define MAX_LOD_LEVEL 99
+#define MAX_LOD_LEVEL 10
 
-#define MIN_DEPTH_DIFF 1e-5f
+#define MIN_DEPTH_DIFF 1e-3f
+// burnout
+#define WHITE_LUMINANCE 3e4f
+
+///////////////////
+
+#define NUM_DOWNSAMPLES 9
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg	(revision 2998)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg	(revision 3010)
@@ -76,5 +76,5 @@
 			++ j;
 			// sample downsampled texture in order to speed up texture accesses
-			float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).xyz;
+			float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 0)).xyz;
 			//float3 sample_position = tex2D(positions, texcoord).xyz;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 3008)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 3010)
@@ -20,23 +20,55 @@
 
 
-float4 DownSample(frag IN,
+/*float4 DownSample(frag IN,
 				  uniform sampler2D colors,
-				  uniform float2 downSampleOffs[4]): COLOR
+				  uniform float2 downSampleOffs[9]): COLOR
 {    
     float4 average = .0f;
 
-    for(int i = 0; i < 4; ++ i)
+	const float4 avgColor = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
+
+	float j = 0;
+	float4 color;
+
+    for (int i = 0; i < 9; ++ i)
     {
-        average += tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i],0,0));
+		color = tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i], 0, 0));
+		if (abs(color.w - avgColor.w) < 1e-3f)
+		{
+			average += color;
+			++ j;
+		}
     }
         
-    average *= 1.0f / 4.0f;
+    //average *= 1.0f / 9.0f;
+    average *= 1.0f / j;
+
+    return average;
+}*/
+  
+float4 DownSample(frag IN,
+				  uniform sampler2D colors,
+				  uniform float2 downSampleOffs[NUM_DOWNSAMPLES]): COLOR
+{    
+    float4 average = .0f;
+
+    for (int i = 0; i < NUM_DOWNSAMPLES; ++ i)
+    {
+		average += tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i], 0, 0));
+    }
+       
+	average *= 1.0f / (float)NUM_DOWNSAMPLES;
 
     return average;
 }
-    
+
+
+/** Does the first downsampling step and on the same time calculates the 
+	intensity.
+*/
 
 float4 GreyScaleDownSample(frag IN,
-						   uniform sampler2D colors
+						   uniform sampler2D colors,
+						   uniform float2 downSampleOffs[4]
 						   ): COLOR
 {
@@ -50,13 +82,10 @@
 	//float3 w = float3(0.2125f, 0.7154f, 0.0721f);
 
-	float4 cols[4];
-
-	cols[0] = tex2D(colors, IN.lt);
-	cols[1] = tex2D(colors, IN.lb);
-	cols[2] = tex2D(colors, IN.rt);
-	cols[3] = tex2D(colors, IN.rb);
+	float4 color;
 
 	for (int i = 0; i < 4; ++ i)
 	{
+		color = tex2D(colors, downSampleOffs[i]);
+
 		const float intensity = dot(cols[i].rgb, w);
 
@@ -65,13 +94,38 @@
 	}
 
-	average = exp(average * 0.25f);
+	average *= 0.25f;
 
-	float4 c = cols[0] + cols[1] + cols[2] + cols[3];
-
-	return c * 0.25f;
 	// Output the luminance to the render target
-	//return float4(average, maximum, 0.0f, 1.0f);
+	return float4(average, maximum, 0.0f, 1.0f);
 }
     
+
+/** Used for downsampling the tone map parameters (average loglum, maximum)
+	to the next lower level. This has to be applied until there is only
+	a 1x1 texture which holds the required numbers.
+*/
+float4 DownSampleForToneMapping(frag IN,
+								uniform sampler2D colors,
+								uniform float2 downSampleOffs[4]): COLOR
+{    
+	float average = .0f;
+	float maximum = .0f;
+	
+	float4 color;
+
+	for (int i = 0; i < 4; ++ i)
+	{
+		color = tex2D(colors, downSampleOffs[i]);
+
+		maximum = max(maximum, color.y);
+		average += color.x;
+	}
+
+	average *= 1.0f / (float)NUM_DOWNSAMPLES;
+
+	return float4(average, maximum, 0.0f, 1.0f);
+}
+
+
 
 pixel ToneMap(frag IN,
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3009)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3010)
@@ -52,5 +52,5 @@
 {
 #if USE_EYE_SPACE_DEPTH
-	float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).w;
+	float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w;
 	//float3 rotView = normalize(Interpol(texcoord, bl, br, tl, tr));
 	float3 rotView = Interpol(texcoord, bl, br, tl, tr);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3009)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3010)
@@ -103,8 +103,9 @@
 		//if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples;
 
+
 		//////////
 		//-- reconstruct world space position from sample
 
-		float4 sample = tex2Dlod(colors, float4(texcoord, 0, SSAO_MIPMAP_LEVEL));
+		float4 sample = tex2Dlod(colors, float4(texcoord, 0, 0));
 		const float eyeSpaceDepth = sample.w;
 		//float3 rotView = normalize(Interpol(texcoord, bl, br, tl, tr));
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3009)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3010)
@@ -59,5 +59,4 @@
 /// the renderable scene geometry
 SceneEntityContainer sceneEntities;
-SceneEntityContainer sceneEntities2;
 // traverses and renders the hierarchy
 RenderTraverser *traverser = NULL;
@@ -1828,6 +1827,4 @@
 	DEL_PTR(ssaoShader);
 
-	CLEAR_CONTAINER(sceneEntities2);
-
 	if (sCgMrtVertexProgram)
 		cgDestroyProgram(sCgMrtVertexProgram);
@@ -2030,12 +2027,8 @@
 		
 		if (!showAlgorithmTime)
-		{
-			sprintf(msg[7], "%s:  %6.1f fps", alg_str[renderMode], fps);
-		}
+				sprintf(msg[7], "%s:  %6.1f fps", alg_str[renderMode], fps);
 		else
-		{
 			sprintf(msg[7], "%s:  %6.1f ms", alg_str[renderMode], rTime);
-		}
-
+		
 		myfont.DrawString(msg[7], 1.3f, 690.0f, 760.0f);//, top_color, bottom_color);
 		
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/IrradianceMap.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/IrradianceMap.h	(revision 3010)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/IrradianceMap.h	(revision 3010)
@@ -0,0 +1,23 @@
+#ifndef __IRRADIANCEMAP_H
+#define __IRRADIANCEMAP_H
+
+#include "common.h"
+#include "Vector3.h"
+
+
+namespace CHCDemoEngine 
+{
+
+/** Class representing an irradiance map, an environment map
+	that is used for illumination
+*/
+class IrradianceMap
+{
+public:
+	
+	IrradianceMap();
+};
+
+}
+
+#endif // __IRRADIANCEMAP_H
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3009)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3010)
@@ -573,19 +573,4 @@
 		sColorsTexDownSampleParam = cgGetNamedParameter(sCgDownSampleProgram, "colors"); 
 		sDownSampleOffsetParam = cgGetNamedParameter(sCgDownSampleProgram, "downSampleOffs");
-		
-		float downSampleOffsets[8];
-
-		float w = 1028;
-		float h = 768;
-
-		float xoffs = 0.5f / w;
-		float yoffs = 0.5f / h;
-
-		downSampleOffsets[0] =  xoffs; downSampleOffsets[1] =  yoffs;
-		downSampleOffsets[2] =  xoffs; downSampleOffsets[3] = -yoffs;
-		downSampleOffsets[4] = -xoffs; downSampleOffsets[5] = -yoffs;
-		downSampleOffsets[6] = -xoffs; downSampleOffsets[7] =  yoffs;
-
-		cgGLSetParameterArray2f(sDownSampleOffsetParam, 0, 4, (const float *)downSampleOffsets);
 	}
 	else
@@ -1214,5 +1199,5 @@
 {
 	// hack: estimate value where sky burns out
-	whiteLum = log(1e4f);
+	whiteLum = log(WHITE_LUMINANCE);
 	
 	////////////////////
@@ -1298,8 +1283,42 @@
 
 
+static void ComputeDownsSampleOffsets(float *downSampleOffsets, int w, int h)
+{
+	/*
+	float downSampleOffsets[8];
+
+	const float xoffs = 0.5f / w;
+	const float yoffs = 0.5f / h;
+
+	downSampleOffsets[0] =  xoffs; downSampleOffsets[1] =  yoffs;
+	downSampleOffsets[2] =  xoffs; downSampleOffsets[3] = -yoffs;
+	downSampleOffsets[4] = -xoffs; downSampleOffsets[5] = -yoffs;
+	downSampleOffsets[6] = -xoffs; downSampleOffsets[7] =  yoffs;
+	*/
+	const float xoffs = .5f / w;
+	const float yoffs = .5f / h;
+
+	int idx  = 0;
+
+	for (int x = -1; x <= 1; ++ x)
+	{
+		for (int y = -1; y <= 1; ++ y)
+		{
+			downSampleOffsets[idx + 0] = x * xoffs;
+			downSampleOffsets[idx + 1] = y * yoffs;
+
+			idx += 2;
+		}
+	}
+}
+
+
 void DeferredRenderer::DownSample(FrameBufferObject *fbo, int bufferIdx,
 								  FrameBufferObject *downSampleFbo, 
 								  int downSampleBufferIdx)
 {
+	float downSampleOffsets[NUM_DOWNSAMPLES * 2];
+	ComputeDownsSampleOffsets(downSampleOffsets, fbo->GetWidth(), fbo->GetHeight());
+
 	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(bufferIdx);
 	GLuint colorsTex = colorBuffer->GetTexture();
@@ -1312,4 +1331,6 @@
 	cgGLSetTextureParameter(sColorsTexDownSampleParam, colorsTex);
 	cgGLEnableTextureParameter(sColorsTexDownSampleParam);
+
+	cgGLSetParameterArray2f(sDownSampleOffsetParam, 0, NUM_DOWNSAMPLES, (const float *)downSampleOffsets);
 
 	mDownSampleFbo->Bind();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2980)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 3010)
@@ -3,5 +3,5 @@
 	ProjectType="Visual C++"
 	Version="8,00"
-	Name="friendlyculling"
+	Name="DemoEngine"
 	ProjectGUID="{03661866-4093-4B02-B26A-028EA91AF023}"
 	RootNamespace="chc_revisited"
@@ -98,7 +98,7 @@
 		<Configuration
 			Name="Release|Win32"
-			OutputDirectory="$(SolutionDir)/bin/$(ConfigurationName)"
+			OutputDirectory="$(SolutionDir)/lib/$(ConfigurationName)"
 			IntermediateDirectory="obj/$(ConfigurationName)"
-			ConfigurationType="1"
+			ConfigurationType="4"
 			CharacterSet="1"
 			WholeProgramOptimization="1"
@@ -150,16 +150,5 @@
 			/>
 			<Tool
-				Name="VCLinkerTool"
-				AdditionalDependencies="glut32.lib glew32s.lib glew32.lib DevIL.lib ILUT.lib zlib.lib cg.lib cgGL.lib"
-				LinkIncremental="1"
-				AdditionalLibraryDirectories="libs/GL;libs/Devil/lib;libs/Zlib/lib;&quot;$(CG_LIB_PATH)&quot;"
-				IgnoreAllDefaultLibraries="false"
-				IgnoreDefaultLibraryNames="LIBCMT"
-				GenerateDebugInformation="false"
-				SubSystem="1"
-				OptimizeReferences="2"
-				EnableCOMDATFolding="2"
-				OptimizeForWindows98="1"
-				TargetMachine="1"
+				Name="VCLibrarianTool"
 			/>
 			<Tool
@@ -167,7 +156,4 @@
 			/>
 			<Tool
-				Name="VCManifestTool"
-			/>
-			<Tool
 				Name="VCXDCMakeTool"
 			/>
@@ -177,10 +163,4 @@
 			<Tool
 				Name="VCFxCopTool"
-			/>
-			<Tool
-				Name="VCAppVerifierTool"
-			/>
-			<Tool
-				Name="VCWebDeploymentTool"
 			/>
 			<Tool
@@ -240,4 +220,12 @@
 			</File>
 			<File
+				RelativePath=".\src\IrradianceMap.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\src\IrradianceMap.h"
+				>
+			</File>
+			<File
 				RelativePath=".\src\MotionPath.cpp"
 				>
@@ -269,8 +257,4 @@
 			<File
 				RelativePath=".\src\SceneEntityConverter.h"
-				>
-			</File>
-			<File
-				RelativePath=".\src\shaderenv.h"
 				>
 			</File>
@@ -531,4 +515,8 @@
 				</File>
 				<File
+					RelativePath=".\src\shaderenv.h"
+					>
+				</File>
+				<File
 					RelativePath=".\src\ShadowMapping.h"
 					>
@@ -599,8 +587,4 @@
 						/>
 					</FileConfiguration>
-				</File>
-				<File
-					RelativePath=".\src\chcdemo.cpp"
-					>
 				</File>
 				<File
