Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2875)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2875)
@@ -0,0 +1,193 @@
+////////////////////
+// Screen Spaced Ambient Occlusion shader
+// based on shader of Alexander Kusternig
+
+#define NUM_SAMPLES 10
+//#define NUM_SAMPLES 16
+
+// rule of thumb: approx 1 / NUM_SAMPLES
+//#define SAMPLE_INTENSITY 0.15f
+#define SAMPLE_INTENSITY 0.2f
+
+#define AREA_SIZE 7e-1f
+//#define AREA_SIZE 3e-1f
+#define VIEW_CORRECTION_SCALE 0.3f
+//#define VIEW_CORRECTION_SCALE 0.5f
+#define DISTANCE_SCALE 1e-6f
+
+#define ILLUM_SCALE 5e-1f;
+
+
+struct fragment
+{
+	 // normalized screen position
+	float4 pos: WPOS;
+	float4 texCoord: TEXCOORD0; 
+	float3 view: COLOR0;
+};
+
+
+
+struct pixel
+{
+	float4 ssao_col: COLOR0;
+	float4 combined_col: COLOR1;
+	float4 illum_col: COLOR2;
+};
+
+
+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;
+}
+
+/** Computes  diffuse reflections + ambient occlusion
+*/
+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.
+
+	// ao is in stored in the w component
+	float4 total_color = float4(0, 0, 0, 1);
+
+
+	////////////
+	//-- the main sampling loop
+
+	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;
+
+		// rotation
+		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;
+
+		float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
+		float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 1)).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_color.w -= cos_angle * distance_intensity * view_correction;
+
+		total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_SCALE;
+	}
+
+	return saturate(total_color);
+}
+
+
+/** The mrt shader for screen space ambient occlusion + indirect illumination
+*/
+pixel main(fragment IN, 
+		   uniform sampler2D colors,
+		   uniform sampler2D positions,
+		   uniform sampler2D normals,
+		   uniform sampler2D noiseTexture,
+		   uniform float2 samples[NUM_SAMPLES],
+		   uniform float noiseMultiplier,
+		   uniform sampler2D oldSsaoTex,
+		   uniform sampler2D oldIllumTex,
+		   const uniform float4x4 oldModelViewProj,
+		   uniform float maxDepth,
+		   uniform float expFactor
+		   )
+{
+	pixel OUT;
+
+	float4 norm = tex2D(normals, IN.texCoord.xy);
+	
+	// the ambient term
+	const float amb = norm.w;
+
+	// expand normal
+	float3 normal = normalize(norm.xyz);
+	/// the current view direction
+	float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
+
+	// the current world position
+	const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
+	
+	// the current color
+	const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
+	// the current depth is stored in the w component
+	const float currentDepth = currentCol.w;
+
+	float4 new_color = globIllum(IN, colors, positions, noiseTexture, 
+                                 samples, normal, viewDir, noiseMultiplier, centerPosition);
+	
+
+	/////////////////
+	//-- compute temporally smoothing
+
+	float4 realPos = centerPosition * maxDepth;
+	realPos.w = 1.0f;
+
+	float4 oldPos = mul(oldModelViewProj, realPos);
+
+	const float newDepth = oldPos.z / oldPos.w;
+
+	float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
+
+	float4 oldSsao = tex2D(oldSsaoTex, tex);
+	float4 oldIllum = tex2D(oldIllumTex, tex);
+
+	const float oldDepth = oldSsao.w;
+	const 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-3f))
+	{
+		OUT.ssao_col.x = new_color.w * expFactor + oldSsao.x * (1.0f - expFactor);
+		OUT.illum_col = new_color * expFactor + oldIllum * (1.0f - expFactor);
+	}
+	else
+	{
+		OUT.ssao_col.x = new_color.w;
+		OUT.illum_col = new_color;
+	}
+
+	OUT.combined_col = (currentCol + OUT.illum_col) * OUT.ssao_col.x;
+	//OUT.combined_col = float4(newDepth, oldDepth, 1, 1) ;
+
+	OUT.ssao_col.w = currentDepth;
+
+	return OUT;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2874)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2875)
@@ -104,5 +104,7 @@
 mWidth(w), mHeight(h), 
 mCamera(cam),
-mScaleFactor(scaleFactor)
+mScaleFactor(scaleFactor),
+mUseTemporalCoherence(true),
+mUseGlobIllum(false)
 {
 	///////////
@@ -140,4 +142,16 @@
 
 	glDeleteTextures(1, &noiseTex);
+}
+
+
+void SsaoShader::SetUseGlobIllum(bool useGlobIllum)
+{
+	mUseGlobIllum = useGlobIllum;
+}
+
+
+void SsaoShader::SetUseTemporalCoherence(bool temporal)
+{
+	mUseTemporalCoherence = temporal;
 }
 
@@ -198,4 +212,6 @@
 		GenerateSamples(); 
 		cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
+
+		cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
 	}
 	else
@@ -233,4 +249,6 @@
 		GenerateSamples(); 
 		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
+
+		cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
 	}
 	else
@@ -298,8 +316,9 @@
 	FirstPass(fbo);
 	
-	//ComputeSsao(fbo, expFactor, oldProjViewMatrix);
-	ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
-
-	//Combine(fbo);
+	if (!mUseGlobIllum)
+		ComputeSsao(fbo, expFactor, oldProjViewMatrix);
+	else
+		ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
+
 	AntiAliasing(fbo);
 
@@ -363,16 +382,23 @@
 	cgGLSetTextureParameter(sOldTexParam, oldTex);
 	cgGLEnableTextureParameter(sOldTexParam);
-
-	cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
 	
 	cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
-	cgGLSetParameter1f(sExpFactorParam, expFactor);
-
-
-	// q: should we generate new samples or only rotate the old ones?
-	// in the first case, the sample patterns look nicer, but the kernel
-	// needs longer to converge
-	GenerateSamples(); 
-	cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
+	
+	if (mUseTemporalCoherence)
+	{
+		cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
+		cgGLSetParameter1f(sExpFactorParam, expFactor);
+
+
+		// q: should we generate new samples or only rotate the old ones?
+		// in the first case, the sample patterns look nicer, but the kernel
+		// needs longer to converge
+		GenerateSamples(); 
+		cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
+	}
+	else
+	{
+			cgGLSetParameter1f(sExpFactorParam, 1.0f);
+	}
 
 	Vector3 tl, tr, bl, br;
@@ -638,15 +664,23 @@
 	cgGLEnableTextureParameter(sOldIllumTexGiParam);
 
-	cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
-	
 	cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
-	cgGLSetParameter1f(sExpFactorGiParam, expFactor);
-
-
-	// q: should we generate new samples or only rotate the old ones?
-	// in the first case, the sample patterns look nicer, but the kernel
-	// needs longer to converge
-	GenerateSamples(); 
-	cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
+
+
+	if (mUseTemporalCoherence)
+	{
+		cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
+		cgGLSetParameter1f(sExpFactorGiParam, expFactor);
+
+
+		// q: should we generate new samples or only rotate the old ones?
+		// in the first case, the sample patterns look nicer, but the kernel
+		// needs longer to converge
+		GenerateSamples(); 
+		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
+	}
+	else
+	{
+			cgGLSetParameter1f(sExpFactorGiParam, 1.0f);
+	}
 
 	Vector3 tl, tr, bl, br;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2874)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2875)
@@ -44,5 +44,7 @@
 	~SsaoShader();
 
-	void SetType(bool useGlobIllum, bool useTemporalCoherence);
+	void SetUseGlobIllum(bool useGlobIllum);
+	void SetUseTemporalCoherence(bool temporal);
+
 
 protected:
@@ -73,4 +75,7 @@
 	FrameBufferObject *mNewFbo;
 	FrameBufferObject *mFbo3;
+
+	bool mUseGlobIllum;
+	bool mUseTemporalCoherence;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2874)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2875)
@@ -154,5 +154,8 @@
 bool ascendKeyPressed = false;
 
+bool useGlobIllum = false;
 bool useSsao = false;
+bool useTemporalCoherence = true;
+
 static float ssaoExpFactor = 0.1f;
 
@@ -1096,4 +1099,14 @@
 		SceneEntity::SetUseLODs(useLODs);
 		break;
+	case 'g':
+	case 'G':
+		useGlobIllum = !useGlobIllum;
+		ssaoShader->SetUseGlobIllum(useGlobIllum);
+		break;
+	case 't':
+	case 'T':
+		useTemporalCoherence = !useTemporalCoherence;
+		ssaoShader->SetUseTemporalCoherence(useTemporalCoherence);
+		break;
 	case 'o':
 	case 'O':
