Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2991)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3009)
@@ -49,4 +49,5 @@
 #define MAX_LOD_LEVEL 99
 
+#define MIN_DEPTH_DIFF 1e-5f
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 3005)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 3009)
@@ -30,7 +30,7 @@
 struct pixel
 {
-  float4 col: COLOR0;
-  float3 pos: COLOR1;
-  float4 norm: COLOR2;
+	float4 col: COLOR0;
+	float4 norm: COLOR1;
+	float3 pos: COLOR2;
 };
 
@@ -112,10 +112,10 @@
 
 	pix.col = IN.color2;
-	pix.col.w = 2.0f;
+	pix.col.w = 1e20f;
 
 	pix.norm.xyz = IN.normal;
 	pix.norm.w = IN.mypos.w;
 
-	pix.pos = 1e20f;
+	//pix.pos = 1e20f;
 	
 	return pix;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3005)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3009)
@@ -6,6 +6,6 @@
 	 // normalized screen position
 	float4 pos: WPOS;
-	float4 texCoord: TEXCOORD0; 
-	float3 view: COLOR0;
+	float2 texCoord: TEXCOORD0; 
+	float3 view: TEXCOORD1;
 };
 
@@ -32,5 +32,4 @@
 float4 shade(fragment IN, 
 			 uniform float4 color,
-			 uniform float4 position,
 			 uniform float3 normal,
 			 uniform float emmisive,
@@ -49,5 +48,5 @@
 
 	// hack: prevent shading the sky
-	if (position.z > 1e19f) outColor = color;
+	if (color.w > 1e19f) outColor = color;
 	//if (emmisive > 1.5f) outColor = color;
 	else outColor = (ambient + diffuse) * color;
@@ -70,41 +69,15 @@
 	pixel OUT;
 
-	float4 norm = tex2D(normals, IN.texCoord.xy);
-	float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
-	float4 position = tex2D(positions, IN.texCoord.xy);
+	float4 norm = tex2D(normals, IN.texCoord);
+	float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
+	
 
 	// an ambient color term
 	float amb = color.w;
 	float3 normal = normalize(norm.xyz);
-	float4 col = shade(IN, color, position, normal, amb, lightDir);
+	float4 col = shade(IN, color, normal, amb, lightDir);
 	
 	OUT.color = col;
-	
-#if 1
-
 	OUT.color.w = color.w;
-
-#else
-
-	////////////
-	//-- write out logaritmic luminance for tone mapping
-
-	// the old loglum is stored in the hightest mipmap-level
-	float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
-
-	const float3 w = float3(0.299f, 0.587f, 0.114f);
-
-	float lum = dot(OUT.color.rgb, w);
-	float logLum = log(1e-5f + lum);
-
-	float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
-	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
-
-	if (oldLogLum > 0)
-		OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
-	else
-		OUT.color.w = logLumScaled;
-
-#endif
 
 	return OUT;
@@ -134,5 +107,5 @@
 		//-- add random noise: reflect around random normal vector (warning: slow!)
 
-		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
+		float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
 		const float2 offsetTransformed = myreflect(offset, mynoise);
 #else
@@ -150,4 +123,13 @@
 
 	return total_d;
+}
+
+inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
+{
+	float3 x1 = lerp(bl, tl, w.y);
+	float3 x2 = lerp(br, tr, w.y); 
+	float3 v = lerp(x1, x2, w.x); 
+
+	return v;
 }
 
@@ -163,6 +145,10 @@
 				  uniform sampler2D noiseTexture,
 				  uniform float2 samples[NUM_PCF_TABS],
-				  uniform float3 lightDir
-				  //, uniform sampler2D oldColors
+				  uniform float3 lightDir,
+				  uniform float3 eyePos,
+				  uniform float3 bl,
+				  uniform float3 br,
+				  uniform float3 tl,
+				  uniform float3 tr
 				  )
 {
@@ -170,12 +156,16 @@
 
 	float4 norm = tex2D(normals, IN.texCoord.xy);
-	float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
-	float4 position = tex2D(positions, IN.texCoord.xy);
-	
 	const float3 normal = normalize(norm.xyz);
-	
-	// hack: an emmisive color term
-	float emmisive = color.w;
-
+
+	float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
+
+	/// reconstruct position from the eye space depth
+	float3 viewDir = IN.view;
+	const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
+
+	float4 position;
+	position.xyz = eyePos - viewDir * eyeDepth;
+	
+	
 	// diffuse intensity
 	const float angle = saturate(dot(normal, lightDir)); 
@@ -186,8 +176,10 @@
 	float4 diffuse = lightDiffuse * angle;
 
+	// hack: prevent shadowing the sky	
+	const bool useShading = (color.w < 1e19f);
+
 	// calc diffuse illumination + shadow term
-	//if ((emmisive < 1.5f) // hack: prevent shadowing the sky	
-	if ((position.z < 1e19f)
-		&& (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
+	if (useShading && 
+		(angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
 		)
 	{
@@ -205,32 +197,9 @@
 	// light ambient term
 	const float4 ambient = glstate.light[0].ambient;
-	
-	// base lighting
-	//OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
-	OUT.color = (position.z > 1e19f) ? color: (ambient + diffuse) * color;
-
-
-	////////////
-	//-- write out logaritmic luminance for tone mapping
-
-#if 1
+	// compute shading
+	OUT.color = useShading ? (ambient + diffuse) * color : color;
+
 	OUT.color.w = color.w;
-#else
-
-	float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
-
-	const float3 w = float3(0.299f, 0.587f, 0.114f);
-
-	float lum = dot(OUT.color.rgb, w);
-	float logLum = log(1e-5f + lum);
-
-	float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
-	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
-
-	if (oldLogLum > 0)
-		OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
-	else
-		OUT.color.w = logLumScaled;
-#endif	
+
 	return OUT;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3005)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3009)
@@ -38,7 +38,6 @@
 {
 	float4 col: COLOR0;
-	//float4 pos: COLOR1;
-	float3 pos: COLOR1;
-	float4 norm: COLOR2;
+	float4 norm: COLOR1;
+	float3 pos: COLOR2;
 };
 
@@ -113,5 +112,5 @@
 	pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView;
 
-#if 1
+#if 0
 	// save world position in second render target
 	pix.pos = IN.worldPos.xyz * maxDepth;
@@ -145,10 +144,10 @@
 	//pix.col.w = glstate.material.emission.x;
 	
-	// compute eye linear depth
+	// compute eye linear depth and scale with lenght to avoid sqr root in pixel shader
 	float2 screenCoord = projPos.xy * 0.5f + 0.5f;
 	const float magView = length(Interpol(screenCoord, bl, br, tl, tr));
 	pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView;
 
-#if 1
+#if 0
 	pix.pos = IN.worldPos.xyz * maxDepth;
 #endif
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3006)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3009)
@@ -230,5 +230,5 @@
 		(tex.x >= 0.0f) && (tex.x < 1.0f) && 
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
-		(abs(depthDif) < 1e-4f) 
+		(abs(depthDif) < MIN_DEPTH_DIFF) 
 		// if visibility changed in the surrounding area we have to recompute
 		//&& (oldNumSamples > 0.8f * newNumSamples)
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3006)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3009)
@@ -11,5 +11,5 @@
 	float4 pos: WPOS;
 	float4 texCoord: TEXCOORD0; 
-	float3 view: COLOR0;
+	float3 view: TEXCOORD1;
 };
 
@@ -177,5 +177,5 @@
 	//-- reconstruct position from the eye space depth
 
-	float3 viewDir = Interpol(IN.texCoord.xy, bl, br, tl, tr);//IN.view;
+	float3 viewDir = IN.view;
 	const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0)).w;
 	
@@ -234,5 +234,5 @@
 		(tex.x >= 0.0f) && (tex.x < 1.0f) && 
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
-		(abs(depthDif) < 1e-3f)
+		(abs(depthDif) < MIN_DEPTH_DIFF)
 		// check if something changed in the surrounding area
 		//&& (oldNumSamples > 0.2 * gi.ao.y)
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3005)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3009)
@@ -663,9 +663,9 @@
 	// the diffuse color buffer
 	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
+	// the normals buffer
+	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
 	// the positions buffer
 	//fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
-	fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
-	// the normals buffer
-	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
+	fbo->AddColorBuffer(ColorBufferObject::RGB_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
 	// another color buffer
 	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
@@ -1005,10 +1005,10 @@
 	{
 		DeferredRenderer::colorBufferIdx = 0;
-		glDrawBuffers(3, mrt);
+		glDrawBuffers(2, mrt);
 	}
 	else 
 	{
 		DeferredRenderer::colorBufferIdx = 3;
-		glDrawBuffers(3, mrt2);
+		glDrawBuffers(2, mrt2);
 	}
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3008)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3009)
@@ -72,4 +72,5 @@
 static CGparameter sMaxDepthParam;
 static CGparameter sEyePosParam;
+static CGparameter sEyePosShadowParam;
 static CGparameter sSamplesParam; 
 static CGparameter sOldTexParam;
@@ -295,8 +296,7 @@
 
 	mDownSampleFbo = new FrameBufferObject(w / 2, h / 2, FrameBufferObject::DEPTH_NONE);
-	//mDownSampleFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
-	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
 	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
 	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
+	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
 }
 
@@ -509,4 +509,6 @@
 		sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir");
 
+		sEyePosShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "eyePos");
+
 		PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f);
 		poisson.Generate((float *)pcfSamples);
@@ -641,4 +643,5 @@
 	{
 	case SSAO:
+		// downsample fbo buffers for colors, normals
 		DownSample(fbo, colorBufferIdx, mDownSampleFbo, 0);
 		DownSample(fbo, 1, mDownSampleFbo, 1);
@@ -649,6 +652,8 @@
 		break;
 	case GI:
+		// downsample fbo buffers for colors, normals
 		DownSample(fbo, colorBufferIdx, mDownSampleFbo, 0);
-		DownSample(fbo, 2, mDownSampleFbo, 2);
+		DownSample(fbo, 1, mDownSampleFbo, 1);
+
 		ComputeGlobIllum(fbo, tempCohFactor, projViewMatrix, oldProjViewMatrix);
 		CombineIllum(fbo);
@@ -708,10 +713,11 @@
 
 	//GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
-	//GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
-	//GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+	//GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
+	//GLuint positionsTex = fbo->GetColorBuffer(2)->GetTexture();
 
 	GLuint colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
-	GLuint positionsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
-	GLuint normalsTex = mDownSampleFbo->GetColorBuffer(2)->GetTexture();
+	GLuint normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
+	GLuint positionsTex = mDownSampleFbo->GetColorBuffer(2)->GetTexture();
+	
 	GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
 
@@ -868,6 +874,6 @@
 {
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
-	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(2)->GetTexture();
 
 	fbo->Bind();
@@ -923,6 +929,6 @@
 	//GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
-	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint normalsTex = mDownSampleFbo->GetColorBuffer(2)->GetTexture();
+	GLuint normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(2)->GetTexture();
 
 	glPushAttrib(GL_VIEWPORT_BIT);
@@ -992,14 +998,13 @@
 
 
+	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
+	const float offs = 0.5f;
+	
 	glBegin(GL_QUADS);
 
-	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
-	//const float new_offs = 0.55f;
-	const float new_offs = 0.5f;
-		
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
+	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
+	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
+	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
+	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
 
 	glEnd();
@@ -1071,5 +1076,6 @@
 
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
-	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(2)->GetTexture();
+	
 	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
 
@@ -1114,7 +1120,7 @@
 
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
-
-	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(2)->GetTexture();
+
 	GLuint shadowTex = shadowMap->GetDepthTexture();
 
@@ -1153,15 +1159,19 @@
 	cgGLSetParameter3f(sLightDirShadowParam, lightDir.x, lightDir.y, lightDir.z);
 
-
-	glColor3f(1.0f, 1.0f, 1.0f);
+	Vector3 tl, tr, bl, br;
+	ComputeViewVectors(tl, tr, bl, br);
+	
+	const Vector3 pos = mCamera->GetPosition() / mScaleFactor;
+	cgGLSetParameter3f(sEyePosShadowParam, pos.x, pos.y, pos.z);
+
+	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
+	const float offs = 0.5f;
 	
 	glBegin(GL_QUADS);
 
-	float offs2 = 0.5f;
-
-	glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
-	glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
-	glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
-	glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
+	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
+	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
+	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
+	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
 
 	glEnd();
