Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2867)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2868)
@@ -1,18 +1,2 @@
-////////////////////
-// Screen Spaced Ambient Occlusion shader
-// mainly based on shader of Alexander Kusternig
-
-//#define NUM_SAMPLES 8
-#define NUM_SAMPLES 16
-
-// rule of thumb: approx 1 / NUM_SAMPLES
-#define SAMPLE_INTENSITY 0.15
-//#define SAMPLE_INTENSITY 0.125f
-
-#define AREA_SIZE 9e-1f
-//#define VIEW_CORRECTION_SCALE 0.3f
-#define VIEW_CORRECTION_SCALE 0.5f
-#define DISTANCE_SCALE 1e-6f
-
 struct fragment
 {
@@ -30,161 +14,10 @@
 
 
-float2 reflect(float2 pt, float2 n)
-{
-  // distance to plane
-  float d = dot(n, pt);
-  // reflect around plane
-  float2 rpt = pt - d * 2.0f * n;
-
-  return rpt;
-}
-
-
-float2 rotate(float2 pt, float2 n)
-{
-	float2 ptTransformed;
-	ptTransformed.x = n.r * pt.x - n.g * pt.y;
-	ptTransformed.y = n.g * pt.x + n.r * pt.y;
-
-	return ptTransformed;
-}
-
-
-/** The ssao shader returning the an intensity value between 0 and 1
-*/
-float ssao(fragment IN,
-		   uniform sampler2D positions,
-		   uniform sampler2D noiseTexture,
-		   uniform float2 samples[NUM_SAMPLES],
-		   uniform float3 currentNormal,
-		   uniform float3 currentViewDir,
-		   uniform float noiseMultiplier,
-		   uniform float4 centerPosition
-		   )
-{
-	// the w coordinate from the persp. projection
-	float w = centerPosition.w;
-
-	// Check in a circular area around the current position.
-	// Shoot vectors to the positions there, and check the angle to these positions.
-	// Summing up these angles gives an estimation of the occlusion at the current position.
-
-	float total_ao = 0.0;
-
-	const float areaSize = 5e-1f;
-
-	for (int i = 0; i < NUM_SAMPLES; i ++) 
-	{
-		float2 offset = samples[i];
-
-		//sample noisetex; r stores costheta, g stores sintheta
-		//float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
-		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
-
-		// rotation
-		//float2 offsetTransformed = offset;
-		//float2 offsetTransformed = rotate(offset, mynoise);
-		float2 offsetTransformed = reflect(offset, mynoise);
-
-		// weight with projected coordinate to reach similar kernel size for near and far
-		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
-
-		// sample downsampled texture in order to speed up texture accesses
-		float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
-		//float3 sample_position = tex2D(positions, texcoord).xyz;
-
-		float3 vector_to_sample = sample_position - centerPosition.xyz;
-		float length_to_sample = length(vector_to_sample);
-		float3 direction_to_sample = vector_to_sample / length_to_sample;
-
-		// Angle between current normal and direction to sample controls AO intensity.
-		float cos_angle = dot(direction_to_sample, currentNormal);
-		cos_angle = max(cos_angle, 0.0f);
-
-		// distance between current position and sample position controls AO intensity.
-		float distance_intensity = 
-			(SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
-
-		// if surface normal perpenticular to view dir, some samples probably count less 
-		// => compensate for this
-		float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
-
-		total_ao += cos_angle * distance_intensity * view_correction;
-	}
-
-	return (1.0f - total_ao);
-	//return dot(currentViewDir, currentNormal);
-}
-
-
-/** Computes ambient occlusion + diffuse reflections
-*/
-float4 globIllum(fragment IN,
-				 uniform sampler2D colors,
-				 uniform sampler2D positions,
-				 uniform sampler2D noiseTexture,
-				 uniform float2 samples[NUM_SAMPLES],
-				 uniform float3 currentNormal,
-				 uniform float3 currentViewDir,
-				 uniform float noiseMultiplier,
-				 uniform float4 centerPosition
-				 )
-{
-	// the w coordinate from the persp. projection
-	float w = centerPosition.w;
-
-	// Check in a circular area around the current position.
-	// Shoot vectors to the positions there, and check the angle to these positions.
-	// Summing up these angles gives an estimation of the occlusion at the current position.
-
-	float total_ao = 0.0;
-	float3 total_color = float3(0.0f);
-
-	const float areaSize = 5e-1f;
-
-	for (int i = 0; i < NUM_SAMPLES; i ++) 
-	{
-		float2 offset = samples[i];
-
-		//sample noisetex; r stores costheta, g stores sintheta
-		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
-
-		// rotation
-		float2 offsetTransformed = rotate(offset, mynoise);
-
-		// weight with projected coordinate to reach similar kernel size for near and far
-		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
-
-		float3 sample_position = tex2D(positions, texcoord).xyz;
-		float3 sample_color = tex2D(colors, texcoord).xyz;
-
-		float3 vector_to_sample = sample_position - centerPosition.xyz;
-		float length_to_sample = length(vector_to_sample);
-		float3 direction_to_sample = vector_to_sample / length_to_sample;
-
-		// Angle between current normal and direction to sample controls AO intensity.
-		float cos_angle = dot(direction_to_sample, currentNormal);
-		cos_angle = max(cos_angle, 0.0f);
-
-		// distance between current position and sample position controls AO intensity.
-		float distance_intensity = 
-			(SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
-
-		// if normal perpenticular to view dir, only half of the samples count
-		float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
-
-		total_ao += cos_angle * distance_intensity * view_correction;
-		total_color += cos_angle * distance_intensity * view_correction * sample_color * 0.3f;
-	}
-
-	return float4(total_color, 1.0f - total_ao);
-}
-
 
 /** function for standard deferred shading
 */
 float4 shade(fragment IN, 
-			 uniform sampler2D colors,
-			 uniform sampler2D positions,
+			 uniform float4 color,
+			 uniform float4 position,
 			 uniform float3 normal,
 			 uniform float amb)
@@ -193,9 +26,6 @@
 	float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
 
-	float4 color = tex2D(colors, IN.texCoord.xy);
-
-	float4 position = tex2D(positions, IN.texCoord.xy);
-
-	float4 ambient = 0.3f;
+	// global ambient
+	const float4 ambient = 0.3f;
 
 	// float3 L = normalize(lightPosition - position);
@@ -212,77 +42,32 @@
 
 
-/** The mrt shader for screen space ambient occlusion
+/** The mrt shader for standard rendering
 */
-pixel main_ssao(fragment IN, 
-				uniform sampler2D colors,
-				uniform sampler2D positions,
-				uniform sampler2D normals,
-				uniform sampler2D noiseTexture,
-				uniform float2 samples[NUM_SAMPLES],
-				uniform float noiseMultiplier,
-				uniform sampler2D oldTex,
-				const uniform float4x4 oldModelViewProj,
-				uniform float maxDepth,
-				uniform float expFactor
-				)
+pixel main2(fragment IN, 
+		   uniform sampler2D colors,
+		   uniform sampler2D positions,
+		   uniform sampler2D normals
+		   )
 {
 	pixel OUT;
 
-	float4 normal = tex2D(normals, IN.texCoord.xy);
-	
-	// the ambient term
-	float amb = normal.w;
+	float4 norm = tex2D(normals, IN.texCoord.xy);
+	float4 color = tex2D(colors, IN.texCoord.xy);
+	float4 position = tex2D(positions, IN.texCoord.xy);
+
+	// an ambient color term
+	float amb = norm.w;
 
 	// expand normal
-	normal = normalize(normal);// * 2.0f - 1.0f);
-	/// the current view direction
-	float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
+	float3 normal = normalize(norm.xyz);// * 2.0f - float4(1.0f));
 
-	// the current world position
-	float4 centerPosition = tex2D(positions, IN.texCoord.xy);
+	float4 col = shade(IN, color, position, normal, amb);
 	
-	float4 col = shade(IN, colors, positions, normal.xyz, amb);
-
-	float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);
-	float4 attenuated_color = ao * col;
-	//float4 attenuated_color = ao;
-
-	//float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 
-	//float4 attenuated_color = ao * col + new_col;
-	
-	const float x = expFactor;
-
-	float4 dummy = centerPosition * maxDepth;
-	dummy.w = 1.0f;
-
-	float4 oldPos = mul(oldModelViewProj, dummy);
-
-	float newDepth = oldPos.z / oldPos.w;
- 
-	float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
-	float4 col1 = tex2D(oldTex, tex);
-
-	float oldDepth = col1.w;
-	float depthDif = 1.0f - newDepth / oldDepth;
-
-	if ((tex.x >= 0.0f) && (tex.x < 1.0f) && 
-		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
-		(abs(depthDif)  < 1e-4f))
-	{
-		OUT.color = attenuated_color * expFactor + col1 * float4(1.0f - expFactor);
-	}
-	else
-	{
-		OUT.color = attenuated_color;
-	}
-
-	//OUT.color.xyz = viewDir;
-	//OUT.color = attenuated_color;
-	
-	OUT.color.w = tex2D(colors, IN.texCoord.xy).w;
+	//OUT.color = float4(1.0f);
+	OUT.color = col;
+	OUT.color.w = color.w;
 
 	return OUT;
 }
-
 
 /** The mrt shader for standard rendering
@@ -296,14 +81,17 @@
 	pixel OUT;
 
-	float4 normal = tex2D(normals, IN.texCoord.xy);
+	float4 norm = tex2D(normals, IN.texCoord.xy);
+	float4 color = tex2D(colors, IN.texCoord.xy);
+	float4 position = tex2D(positions, IN.texCoord.xy);
 
 	// an ambient color term
-	float amb = normal.w;
+	float amb = norm.w;
 
 	// expand normal
-	normal = normalize(normal);// * 2.0f - float4(1.0f));
+	float3 normal = normalize(norm.xyz);// * 2.0f - float4(1.0f));
 
-	float4 col = shade(IN, colors, positions, normal.xyz, amb);
+	float4 col = shade(IN, color, position, normal.xyz, amb);
 	
+	//OUT.color = float4(1.0f);
 	OUT.color = col;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2867)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2868)
@@ -11,5 +11,5 @@
 
 // the barrier for detecting a discontinuity
-uniform float4 e_barrier = float4(5e-3, 5e-3, 0, 0); // x = normal, y = depth
+uniform float4 e_barrier = float4(5e-4, 5e-4, 0, 0); // x = normal, y = depth
 // the weights for normal / depth discontinuity
 uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // x = normal, y = depth
@@ -32,6 +32,6 @@
 	nd.w = dot(nc, float3(tex2D(normals, IN.lb.xy)));
 
-	nd -= e_barrier.x;
-	nd = step(0.0f, nd);
+	nd.x -= e_barrier.x;
+	nd = step((float4)0.0f, nd);
 
 	float ne = saturate(dot(nd, e_weights.x));
@@ -83,3 +83,4 @@
 
 	return (s0 + s1 + s2 + s3) / 4.0f;
+	//return float4(w);
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/temporal.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/temporal.cg	(revision 2868)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/temporal.cg	(revision 2868)
@@ -0,0 +1,60 @@
+
+/** Computes the temporally smoothed indirect illumination buffer and combines it
+	with the color buffer
+*/
+pixel main(fragment IN, 
+		   uniform sampler2D colors,
+		   uniform sampler2D ssao,
+		   uniform sampler2D oldssao,
+		   uniform sampler2D positions
+		   const uniform float4x4 oldModelViewProj,
+		   uniform float maxDepth,
+		   uniform float expFactor
+		   )
+{
+	pixel OUT;
+
+	float4 centerPosition = tex2D(positions, IN.texCoord.xy);
+	
+	float4 col = tex2D(colors, IN.texCoord.xy);
+
+	float ao = tex2D(ssao, IN.texCoord.xy);
+	float4 attenuated_color = ao * col;
+	//float4 attenuated_color = ao;
+
+	//float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 
+	//float4 attenuated_color = ao * col + new_col;
+	
+	const float x = expFactor;
+
+	float4 dummy = centerPosition * maxDepth;
+	dummy.w = 1.0f;
+
+	float4 oldPos = mul(oldModelViewProj, dummy);
+
+	float newDepth = oldPos.z / oldPos.w;
+ 
+	float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
+	float4 col1 = tex2D(oldTex, tex);
+
+	float oldDepth = col1.w;
+	float depthDif = 1.0f - newDepth / oldDepth;
+
+	if ((tex.x >= 0.0f) && (tex.x < 1.0f) && 
+		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
+		(abs(depthDif)  < 1e-4f))
+	{
+		OUT.color = attenuated_color * expFactor + col1 * float4(1.0f - expFactor);
+	}
+	else
+	{
+		OUT.color = attenuated_color;
+	}
+
+	//OUT.color.xyz = viewDir;
+	//OUT.color = attenuated_color;
+	
+	//OUT.color.w = tex2D(colors, IN.texCoord.xy).w;
+
+	return OUT;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2809)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2868)
@@ -1,8 +1,25 @@
+////////////////////
+// Screen Spaced Ambient Occlusion shader
+// based on shader of Alexander Kusternig
+
+//#define NUM_SAMPLES 8
+#define NUM_SAMPLES 16
+
+// rule of thumb: approx 1 / NUM_SAMPLES
+#define SAMPLE_INTENSITY 0.15
+//#define SAMPLE_INTENSITY 0.125f
+
+#define AREA_SIZE 9e-1f
+//#define VIEW_CORRECTION_SCALE 0.3f
+#define VIEW_CORRECTION_SCALE 0.5f
+#define DISTANCE_SCALE 1e-6f
+
+
 struct fragment
 {
-    float4 pos: WPOS; // normalized screen position
-    float4 view: COLOR;
-    float4 texcoord: TEXCOORD0;      
-    float4 lindepth: TEXCOORD1;
+	 // normalized screen position
+	float4 pos: WPOS;
+	float4 texCoord: TEXCOORD0; 
+	float3 view: COLOR0;
 };
 
@@ -10,79 +27,231 @@
 struct pixel
 {
-    float4 col: COLOR0;
+	float4 color: COLOR0;
 };
 
 
-float3 reflect(float3 pt, float3 n)
+float2 reflect(float2 pt, float2 n)
 {
   // distance to plane
   float d = dot(n, pt);
   // reflect around plane
-  float3 rpt = pt - d * 2.0f * n;
-  
-  //return pt;
+  float2 rpt = pt - d * 2.0f * n;
+
   return rpt;
 }
 
 
+float2 rotate(float2 pt, float2 n)
+{
+	float2 ptTransformed;
+	ptTransformed.x = n.r * pt.x - n.g * pt.y;
+	ptTransformed.y = n.g * pt.x + n.r * pt.y;
+
+	return ptTransformed;
+}
+
+
+/** The ssao shader returning the an intensity value between 0 and 1
+*/
+float ssao(fragment IN,
+		   uniform sampler2D positions,
+		   uniform sampler2D noiseTexture,
+		   uniform float2 samples[NUM_SAMPLES],
+		   uniform float3 currentNormal,
+		   uniform float3 currentViewDir,
+		   uniform float noiseMultiplier,
+		   uniform float4 centerPosition
+		   )
+{
+	// the w coordinate from the persp. projection
+	float w = centerPosition.w;
+
+	// Check in a circular area around the current position.
+	// Shoot vectors to the positions there, and check the angle to these positions.
+	// Summing up these angles gives an estimation of the occlusion at the current position.
+
+	float total_ao = 0.0;
+
+	const float areaSize = 5e-1f;
+
+	for (int i = 0; i < NUM_SAMPLES; i ++) 
+	{
+		float2 offset = samples[i];
+
+		//sample noisetex; r stores costheta, g stores sintheta
+		//float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
+		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
+
+		// rotation
+		//float2 offsetTransformed = offset;
+		//float2 offsetTransformed = rotate(offset, mynoise);
+		float2 offsetTransformed = reflect(offset, mynoise);
+
+		// weight with projected coordinate to reach similar kernel size for near and far
+		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
+
+		// sample downsampled texture in order to speed up texture accesses
+		float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
+		//float3 sample_position = tex2D(positions, texcoord).xyz;
+
+		float3 vector_to_sample = sample_position - centerPosition.xyz;
+		float length_to_sample = length(vector_to_sample);
+		float3 direction_to_sample = vector_to_sample / length_to_sample;
+
+		// Angle between current normal and direction to sample controls AO intensity.
+		float cos_angle = dot(direction_to_sample, currentNormal);
+		cos_angle = max(cos_angle, 0.0f);
+
+		// distance between current position and sample position controls AO intensity.
+		float distance_intensity = 
+			(SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
+
+		// if surface normal perpenticular to view dir, some samples probably count less 
+		// => compensate for this
+		float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
+
+		total_ao += cos_angle * distance_intensity * view_correction;
+	}
+
+	return (1.0f - total_ao);
+	//return dot(currentViewDir, currentNormal);
+}
+
+
+/** Computes ambient occlusion + diffuse reflections
+*/
+float4 globIllum(fragment IN,
+				 uniform sampler2D colors,
+				 uniform sampler2D positions,
+				 uniform sampler2D noiseTexture,
+				 uniform float2 samples[NUM_SAMPLES],
+				 uniform float3 currentNormal,
+				 uniform float3 currentViewDir,
+				 uniform float noiseMultiplier,
+				 uniform float4 centerPosition
+				 )
+{
+	// the w coordinate from the persp. projection
+	float w = centerPosition.w;
+
+	// Check in a circular area around the current position.
+	// Shoot vectors to the positions there, and check the angle to these positions.
+	// Summing up these angles gives an estimation of the occlusion at the current position.
+
+	float total_ao = 0.0;
+	float3 total_color = float3(0.0f);
+
+	const float areaSize = 5e-1f;
+
+	for (int i = 0; i < NUM_SAMPLES; i ++) 
+	{
+		float2 offset = samples[i];
+
+		//sample noisetex; r stores costheta, g stores sintheta
+		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
+
+		// rotation
+		float2 offsetTransformed = rotate(offset, mynoise);
+
+		// weight with projected coordinate to reach similar kernel size for near and far
+		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
+
+		float3 sample_position = tex2D(positions, texcoord).xyz;
+		float3 sample_color = tex2D(colors, texcoord).xyz;
+
+		float3 vector_to_sample = sample_position - centerPosition.xyz;
+		float length_to_sample = length(vector_to_sample);
+		float3 direction_to_sample = vector_to_sample / length_to_sample;
+
+		// Angle between current normal and direction to sample controls AO intensity.
+		float cos_angle = dot(direction_to_sample, currentNormal);
+		cos_angle = max(cos_angle, 0.0f);
+
+		// distance between current position and sample position controls AO intensity.
+		float distance_intensity = 
+			(SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
+
+		// if normal perpenticular to view dir, only half of the samples count
+		float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
+
+		total_ao += cos_angle * distance_intensity * view_correction;
+		total_color += cos_angle * distance_intensity * view_correction * sample_color * 0.3f;
+	}
+
+	return float4(total_color, 1.0f - total_ao);
+}
+
+
+/** The mrt shader for screen space ambient occlusion
+*/
 pixel main(fragment IN, 
-           uniform sampler2D lindepth,
-           uniform sampler2D scene,
-           uniform sampler2D normal,
-           uniform float invTexSize,
-           uniform float radius,
-	   uniform float3 eyevec,
-	   uniform float3 samples[32])
-{
-    pixel OUT;
+		   uniform sampler2D colors,
+		   uniform sampler2D positions,
+		   uniform sampler2D normals,
+		   uniform sampler2D noiseTexture,
+		   uniform float2 samples[NUM_SAMPLES],
+		   uniform float noiseMultiplier,
+		   uniform sampler2D oldTex,
+		   const uniform float4x4 oldModelViewProj,
+		   uniform float maxDepth,
+		   uniform float expFactor
+		   )
+{
+	pixel OUT;
+
+	float4 normal = tex2D(normals, IN.texCoord.xy);
+	
+	// the ambient term
+	float amb = normal.w;
+
+	// expand normal
+	normal = normalize(normal);// * 2.0f - 1.0f);
+	/// the current view direction
+	float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
+
+	// the current world position
+	float4 centerPosition = tex2D(positions, IN.texCoord.xy);
+	
+	float4 col = tex2D(colors, IN.texCoord.xy);
+	float currentDepth = col.w;
+
+	float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);
+	float4 attenuated_color = ao * col;
+	//float4 attenuated_color = ao;
+
+	//float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 
+	//float4 attenuated_color = ao * col + new_col;
+	
+	const float x = expFactor;
+
+	float4 dummy = centerPosition * maxDepth;
+	dummy.w = 1.0f;
+
+	float4 oldPos = mul(oldModelViewProj, dummy);
+
+	float newDepth = oldPos.z / oldPos.w;
  
-    // eye space z
-    float eyez = tex2D(lindepth, IN.texcoord.xy).x;
-
-    float3 viewvec = IN.view.xyz * 2.0f - float3(1.0f);
-    viewvec /= viewvec.z;
-
-    //return float4(viewvec.xyz, 1.0f);
-    // eye point
-    //float3 eyept = eyez * viewvec;
-    float3 eyept = float3(IN.texcoord.xy, eyez);
-
-    float4 pl = tex2D(normal, IN.pos.xy * invTexSize);
-    pl = pl * 2.0 - float4(1.0);
-
-    float occlusion = 0.0;
-
-    // gather occlusion from surrounding samples
-    for (int i = 0; i < 32; i ++)
-    {
-        // generate new sample point
-        //float3 samplepos = eyept + radius * samples[i];
-        // create some dithering by reflecting the sample point alond some random plane
-        float3 samplepos = eyept + radius * reflect(samples[i], pl.xyz);
-
-        // project to texture space q: why the scaling?
-        float2 sampletex = (samplepos.xy / samplepos.z);// * float2(0.75, 1.0);
-        
-        //float2 sampletexn = sampletex;
-	// normalize to [0 .. 1], move eye point to center
-	//float2 sampletexn = sampletex * 0.5f + float2(0.5f);
-        float2 sampletexn = sampletex + float2(0.5f);
-
-        // look up depth at sample point
-        float depth = tex2D(lindepth, sampletexn).x;
-        // compare: is point occluded?
-        //float zdist = 50.0f * max(-samplepos.z + depth, 0.0f);
-	float zdist = 50.0f * max(samplepos.z - depth, 0.0f);
-        // occlusion factor shrinks quadratic with distance to occluder
-	occlusion += 1.0 / (1.0 + zdist * zdist);
-	//occlusion += 1.0 / (1.0 + zdist);
-   }
-
-   // normalize
-   occlusion /= 32.0f;
-  
-   OUT.col = 1.0f - occlusion;
-   //   OUT.col = tex2D(scene, IN.texcoord.xy) * 0.5 + (1.0f - occlusion);
-
-   return OUT;
-}
+	float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
+	float4 col1 = tex2D(oldTex, tex);
+
+	float oldDepth = col1.w;
+	float depthDif = 1.0f - newDepth / oldDepth;
+
+	if ((tex.x >= 0.0f) && (tex.x < 1.0f) && 
+		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
+		(abs(depthDif)  < 1e-4f))
+	{
+		OUT.color = attenuated_color * expFactor + col1 * float4(1.0f - expFactor);
+	}
+	else
+	{
+		OUT.color = attenuated_color;
+	}
+
+	//OUT.color.xyz = viewDir;
+	//OUT.color = attenuated_color;
+	
+	OUT.color.w = currentDepth;
+
+	return OUT;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2867)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2868)
@@ -19,8 +19,16 @@
 
 static CGprogram sCgSsaoProgram = NULL;
+static CGprogram sCgDeferredProgram2 = NULL;
+static CGprogram sCgAntiAliasingProgram = NULL;
+
+static CGparameter sColorsTexDeferredParam;
+static CGparameter sPositionsTexDeferredParam;
+static CGparameter sNormalsTexDeferredParam;
 
 static CGparameter sColorsTexParam;
 static CGparameter sPositionsTexParam;
 static CGparameter sNormalsTexParam;
+
+
 static CGparameter sOldModelViewProjMatrixParam;
 static CGparameter sMaxDepthParam;
@@ -30,5 +38,4 @@
 static CGparameter sNoiseMultiplierParam;
 static CGparameter sExpFactorParam;
-static CGprogram sCgAntiAliasingProgram;
 
 static CGparameter sColorsTexAntiAliasingParam;
@@ -81,4 +88,8 @@
 	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 	
+	mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
+	// the diffuse color buffer
+	mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	
 
 	// create noise texture for ssao
@@ -101,12 +112,33 @@
 void SsaoShader::Init(CGcontext context)
 {	
-	///////////////
-
-	sCgSsaoProgram = 
+	sCgDeferredProgram2 = 
 		cgCreateProgramFromFile(context, 
 								CG_SOURCE,
 								"src/shaders/deferred.cg", 
 								RenderState::sCgFragmentProfile,
-								"main_ssao",
+								"main2",
+								NULL);
+
+	if (sCgDeferredProgram2 != NULL)
+	{
+		cgGLLoadProgram(sCgDeferredProgram2);
+
+		// we need size of texture for scaling
+		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "positions");  
+		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "colors");  
+		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "normals"); 
+	}
+	else
+		cerr << "deferred program failed to load" << endl;
+
+
+	///////////////
+
+	sCgSsaoProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/ssao.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
 								NULL);
 
@@ -121,4 +153,5 @@
 		sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
 		sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
+		
 		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
 		sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
@@ -162,28 +195,5 @@
 						float expFactor)
 {
-	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
-
-	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mWidth, mHeight);
-
-	glDrawBuffers(1, mymrt);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
-	glDisable(GL_ALPHA_TEST);
-	glDisable(GL_TEXTURE_2D);
-	glDisable(GL_LIGHTING);
-
-	glMatrixMode(GL_PROJECTION);
-	glPushMatrix();
-	glLoadIdentity();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPushMatrix();
-	glLoadIdentity();
-
-	const float offs = 0.5f;
-	glOrtho(-offs, offs, -offs, offs, 0, 1);
-
+	
 	// switch roles of old and new fbo
 	// the algorihm uses two input fbos, where the one
@@ -192,7 +202,32 @@
 	swap(mNewFbo, mOldFbo);	
 
+	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mWidth, mHeight);
+
+	FrameBufferObject::Release();
+
+	glDrawBuffers(1, mymrt);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	glDisable(GL_ALPHA_TEST);
+	glDisable(GL_TEXTURE_2D);
+	glDisable(GL_LIGHTING);
+
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+	glLoadIdentity();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	glLoadIdentity();
+
+	const float offs = 0.5f;
+	glOrtho(-offs, offs, -offs, offs, 0, 1);
+
+	FirstPass(fbo);
 	ComputeSsao(fbo, expFactor);
-	// the second pass just renders the combined solution
-	//DisplayTexture();
 	AntiAliasing(fbo);
 
@@ -214,5 +249,5 @@
 void SsaoShader::ComputeSsao(FrameBufferObject *fbo, float expFactor)
 {
-	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
+	GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture();
 	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
@@ -228,10 +263,11 @@
 	// read the second buffer, write to the first buffer
 	mNewFbo->Bind();
+	glDrawBuffers(1, mymrt);
+
 	GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
 
-	glDrawBuffers(1, mymrt);
-
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
 	cgGLBindProgram(sCgSsaoProgram);
 
@@ -385,7 +421,6 @@
 
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
 	cgGLBindProgram(sCgAntiAliasingProgram);
-
+	
 	cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
 	cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
@@ -393,5 +428,5 @@
 	cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
 	cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
-	
+
 	glColor3f(1.0f, 1.0f, 1.0f);
 
@@ -418,3 +453,53 @@
 
 
+void SsaoShader::FirstPass(FrameBufferObject *fbo)
+{
+	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+
+	mFbo3->Bind();
+	//mNewFbo->Bind();
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+	glDrawBuffers(1, mymrt);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	cgGLBindProgram(sCgDeferredProgram2);
+
+	cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexDeferredParam);
+
+	cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
+	cgGLEnableTextureParameter(sPositionsTexDeferredParam);
+
+	cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
+	cgGLEnableTextureParameter(sNormalsTexDeferredParam);
+	
+	glColor3f(1.0f, 1.0f, 1.0f);
+
+	const float offs = 0.5f;
+
+	glBegin(GL_QUADS);
+
+	glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
+	glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
+	glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
+	glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexDeferredParam);
+	cgGLDisableTextureParameter(sPositionsTexDeferredParam);
+	cgGLDisableTextureParameter(sNormalsTexDeferredParam);
+
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+
+	FrameBufferObject::Release();
+
+	PrintGLerror("deferred shading");
+}
+
 } // namespace
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2865)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2868)
@@ -50,5 +50,6 @@
 	void ComputeSsao(FrameBufferObject *fbo, float expFactor);
 
-	//void DisplayTexture();
+	void FirstPass(FrameBufferObject *fbo);
+
 	void AntiAliasing(FrameBufferObject *fbo);
 
@@ -69,4 +70,5 @@
 	FrameBufferObject *mOldFbo;
 	FrameBufferObject *mNewFbo;
+	FrameBufferObject *mFbo3;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2867)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2868)
@@ -425,9 +425,9 @@
 	InitCg();
 
+	DeferredShader::Init(sCgContext);
 	SsaoShader::Init(sCgContext);
-	DeferredShader::Init(sCgContext);
-
+
+	deferredShader = new DeferredShader(texWidth, texHeight);
 	ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f);
-	deferredShader = new DeferredShader(texWidth, texHeight);
 
 	// initialize the render traverser
@@ -539,7 +539,9 @@
 	// the diffuse color buffer
 	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
+
 	// the positions buffer
 	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true);
 	//fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, true);
+	
 	// the normals buffer
 	//fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2867)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2868)
@@ -55,5 +55,4 @@
 void DeferredShader::Init(CGcontext context)
 {
-	
 	sCgDeferredProgram = 
 		cgCreateProgramFromFile(context, 
@@ -112,13 +111,4 @@
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	if (1)
-	{
-		glEnable(GL_TEXTURE_2D);
-		// generate mip map levels for position texture
-		glBindTexture(GL_TEXTURE_2D, colorsTex);
-		glGenerateMipmapEXT(GL_TEXTURE_2D);
-	}
-
-	// read the second buffer, write to the first buffer
 	mFbo->Bind();
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 2867)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 2868)
@@ -11,5 +11,5 @@
 camPosition=483.398f 242.364f 186.078f
 camDirection=1 0 0
-useFullScreen=1
+useFullScreen=0
 useLODs=1
 #modelPath=data/city/model/
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2864)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2868)
@@ -795,4 +795,12 @@
 				>
 			</File>
+			<File
+				RelativePath=".\src\shaders\ssao.cg"
+				>
+			</File>
+			<File
+				RelativePath=".\src\shaders\temporal.cg"
+				>
+			</File>
 		</Filter>
 		<File
