Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h	(revision 3071)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h	(revision 3085)
@@ -2,7 +2,4 @@
 #define __SCENEENTITY_H
 
-#include "glInterface.h"
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
 #include "common.h"
 #include "AxisAlignedBox3.h"
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3084)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3085)
@@ -42,6 +42,6 @@
 #define MAX_LOD_LEVEL 10
 
-#define MIN_DEPTH_DIFF 1e-6f
-#define PRECISION_SCALE 1e-3f
+#define MIN_DEPTH_DIFF 1e-3f
+#define PRECISION_SCALE 1e-1f
 
 // burnout
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3084)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3085)
@@ -50,9 +50,9 @@
 
 // reconstruct world space position
-inline float3 ReconstructSamplePos(uniform sampler2D colors,
+inline float3 ReconstructSamplePos(uniform sampler2D tex,
 								   float2 texcoord, 
 								   float3 bl, float3 br, float3 tl, float3 tr)
 {
-	const float eyeSpaceDepth = tex2D(colors, texcoord).w;
+	const float eyeSpaceDepth = tex2D(tex, texcoord).w;
 	float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
 	float3 samplePos = -viewVec * eyeSpaceDepth;
@@ -81,5 +81,11 @@
 						 uniform float3 tr, 
 						 float2 texcoord0,
-						 float3 eyePos
+						 float3 eyePos,
+						 float eyeSpaceDepth,
+						 float3 oldEyePos,
+						 uniform float3 oldbl,
+						 uniform float3 oldbr,
+						 uniform float3 oldtl,
+						 uniform float3 oldtr
 						 )
 {
@@ -90,19 +96,19 @@
 
 	// reproject into old frame and calculate projected depth
-	const float3 dummyPos = ReconstructSamplePos(colors, texcoord0, bl, br, tl, tr) + eyePos;
-	float4 backProjPos = mul(oldModelViewProj, float4(dummyPos, 1.0f));
-	//float4 backProjPos = mul(oldModelViewProj, worldPos);
-	
+	float4 backProjPos = mul(oldModelViewProj, worldPos);
 	backProjPos /= backProjPos.w;
-	
-	// the current depth projected into the old frame
-	const float backProjDepth = backProjPos.z * PRECISION_SCALE;
 	// fit from unit cube into 0 .. 1
-	const float2 tex = backProjPos.xy * 0.5f + 0.5f;
+	const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
+
 	// retrieve the sample from the last frame
-	float4 oldCol = tex2D(oldTex, tex);
-
-	const float oldDepth = oldCol.z;
-	const float depthDif = backProjDepth - oldDepth;
+	float4 oldCol = tex2D(oldTex, oldTexCoords);
+	const float oldEyeSpaceDepth = oldCol.z;
+
+	float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
+	//float3 oldSamplePos = -viewVec * eyeSpaceDepth + oldEyePos;
+	float3 oldSamplePos = oldEyePos - viewVec * oldCol.z;
+
+	//const float oldDepth = oldCol.z;
+	const float depthDif = length(oldSamplePos - worldPos.xyz);
 
 	//const float oldNumSamples = oldCol.y;
@@ -110,5 +116,5 @@
 	float newWeight;
 
-	bool isValid = true;
+	/*bool isValid = true;
 
 	for (int i = 0; i < NUM_SAMPLES; ++ i) 
@@ -134,5 +140,5 @@
 
 		if (abs(dDiff) > 1e-5f) isValid = false;
-	}
+	}*/
 
 	// the number of valid samples in this frame
@@ -140,10 +146,10 @@
 
 	if ((temporalCoherence > 1e-6f)
-		&& (tex.x >= 0.0f) && (tex.x < 1.0f)
-		&& (tex.y >= 0.0f) && (tex.y < 1.0f)
+		&& (oldTexCoords.x >= 0.0f) && (oldTexCoords.x < 1.0f)
+		&& (oldTexCoords.y >= 0.0f) && (oldTexCoords.y < 1.0f)
 		&& (abs(depthDif) <= MIN_DEPTH_DIFF) 
 		// if visibility changed in the surrounding area we have to recompute
 		//&& (oldNumSamples > 0.8f * newNumSamples)
-		&& isValid
+	//	&& isValid
 		)
 	{
@@ -162,5 +168,5 @@
 	//illum_col.y = isValid / 16.0f;
 	illum_col.y = newWeight;
-	illum_col.z = currentDepth;
+	illum_col.z = eyeSpaceDepth;
 
 	return illum_col;
@@ -252,6 +258,6 @@
 		   uniform float2 samples[NUM_SAMPLES],
 		   uniform sampler2D oldTex,
-		   const uniform float4x4 modelViewProj,
-		   const uniform float4x4 oldModelViewProj,
+		   uniform float4x4 modelViewProj,
+		   uniform float4x4 oldModelViewProj,
 		   uniform float temporalCoherence,
 		   uniform float3 eyePos,
@@ -259,11 +265,15 @@
 		   uniform float3 br,
 		   uniform float3 tl,
-		   uniform float3 tr
+		   uniform float3 tr,
+		   uniform float3 oldEyePos,
+		   uniform float3 oldbl,
+		   uniform float3 oldbr,
+		   uniform float3 oldtl,
+		   uniform float3 oldtr
 		   )
 {
 	pixel OUT;
 
-	const float3 normal = 
-		normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
+	const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
 
 	// reconstruct position from the eye space depth
@@ -292,5 +302,6 @@
 	
 	OUT.illum_col = temporalSmoothing(projPos, worldPos, currentDepth, oldTex, oldModelViewProj, temporalCoherence, ao,
-		                              samples, colors, noiseTex, w, bl, br, tl, tr, IN.texCoord, eyePos);
+		                              samples, colors, noiseTex, w, bl, br, tl, tr, IN.texCoord, eyePos, eyeDepth,
+									  oldEyePos,oldbl, oldbr, oldtl, oldtr);
 
 	return OUT;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3068)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3085)
@@ -3,10 +3,6 @@
 
 #include "common.h"
-#include "glInterface.h"
-#include "ShaderProgram.h"
-
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
-
+#include "Matrix4x4.h"
+#include "Vector3.h"
 
 namespace CHCDemoEngine 
@@ -42,6 +38,4 @@
 
 	void Render(FrameBufferObject *fbo, 
-		        const Matrix4x4 &oldProjViewMatrix, 
-				const Matrix4x4 &projViewMatrix, 
 				float tempCohFactor, 
 				DirectionalLight *light,
@@ -69,14 +63,7 @@
 protected:
 
-	void ComputeSsao(FrameBufferObject *fbo, 
-					 float tempCohFactor, 
-					 const Matrix4x4 &projViewMatrix,
-					 const Matrix4x4 &oldProjViewMatrix
-					 );
+	void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
 
-	void ComputeGlobIllum(FrameBufferObject *fbo,
-		                  float tempCohFactor,
-						  const Matrix4x4 &projViewMatrix,
-						  const Matrix4x4 &oldProjViewMatrix);
+	void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
 
 	void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
@@ -97,8 +84,4 @@
 
 	void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light);
-	/** Helper method that computes the view vectors in the corners of the current view frustum.
-	*/
-	void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br);
-
 	/** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
 		resolution of fbo.
@@ -113,4 +96,7 @@
 	*/
 	void InitCg();
+	/** Is called once per render call and sets the most important parameters.
+	*/
+	void InitFrame();
 
 
@@ -139,4 +125,13 @@
 
 	static ShaderContainer sShaders;
+
+	Matrix4x4 mProjViewMatrix;
+	Matrix4x4 mOldProjViewMatrix;
+
+	Vector3 mCornersView[4];
+	Vector3 mOldCornersView[4];
+
+	Vector3 mEyePos;
+	Vector3 mOldEyePos;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 3071)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 3085)
@@ -7,7 +7,4 @@
 #include "Camera.h"
 
-#include "glInterface.h"
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 3038)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 3085)
@@ -2,10 +2,7 @@
 #define _SKYPREETHAM_H__
 
-#include "glInterface.h"
 #include "common.h"
 #include "ShaderProgram.h"
 
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3080)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3085)
@@ -1065,5 +1065,5 @@
 
 		ShadowMap *sm = showShadowMap ? shadowMap : NULL;
-		ssaoShader->Render(fbo, oldViewProjMat, viewProjMat, ssaoTempCohFactor, light, useHDR, sm);
+		ssaoShader->Render(fbo, ssaoTempCohFactor, light, useHDR, sm);
 	}
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3084)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3085)
@@ -71,4 +71,18 @@
 
 
+/** Helper method that computes the view vectors in the corners of the current view frustum.
+*/
+static void ComputeViewVectors(PerspectiveCamera *cam, Vector3 &bl, Vector3 &br, Vector3 &tl, Vector3 &tr)
+{
+	Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
+
+	cam->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
+
+	bl = Normalize(nbl - fbl);
+	br = Normalize(nbr - fbr);
+	tl = Normalize(ntl - ftl);
+	tr = Normalize(ntr - ftr);
+}
+
 
 static float GaussianDistribution(float x, float y, float rho)
@@ -127,8 +141,10 @@
 void DeferredRenderer::DrawQuad(ShaderProgram *p)
 {
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
-
 	p->Bind();
+
+	Vector3 bl = mCornersView[0];
+	Vector3 br = mCornersView[1];
+	Vector3 tl = mCornersView[2];
+	Vector3 tr = mCornersView[3];
 
 	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
@@ -258,4 +274,14 @@
 	CreateNoiseTex2D(mDownSampleFbo->GetWidth(), mDownSampleFbo->GetHeight());
 
+	mProjViewMatrix = IdentityMatrix();
+	mOldProjViewMatrix = IdentityMatrix();
+
+	for (int i = 0; i < 4; ++ i)
+	{
+		mCornersView[i] = mOldCornersView[i] = Vector3::UNIT_X();
+	}
+
+	mEyePos = mOldEyePos = Vector3::ZERO();
+
 	InitCg();
 }
@@ -308,4 +334,10 @@
 	sCgSsaoProgram->AddParameter("oldModelViewProj", i ++);
 
+	sCgSsaoProgram->AddParameter("oldEyePos", i ++);
+	sCgSsaoProgram->AddParameter("oldbl", i ++);
+	sCgSsaoProgram->AddParameter("oldbr", i ++);
+	sCgSsaoProgram->AddParameter("oldtl", i ++);
+	sCgSsaoProgram->AddParameter("oldtr", i ++);
+	
 	i = 0;
 	sCgGiProgram->AddParameter("colors", i ++);
@@ -379,6 +411,4 @@
 
 void DeferredRenderer::Render(FrameBufferObject *fbo, 
-							  const Matrix4x4 &oldProjViewMatrix,
-							  const Matrix4x4 &projViewMatrix,
 							  float tempCohFactor,
 							  DirectionalLight *light,
@@ -387,4 +417,6 @@
 							  )
 {
+	InitFrame();
+
 	// switch roles of old and new fbo
 	// the algorihm uses two input fbos, where the one
@@ -429,5 +461,5 @@
 		DownSample(fbo, 1, mDownSampleFbo, 1);
 
-		ComputeSsao(fbo, tempCohFactor, projViewMatrix, oldProjViewMatrix);
+		ComputeSsao(fbo, tempCohFactor);
 		CombineSsao(fbo);
 		break;
@@ -437,5 +469,5 @@
 		DownSample(fbo, 1, mDownSampleFbo, 1);
 
-		ComputeGlobIllum(fbo, tempCohFactor, projViewMatrix, oldProjViewMatrix);
+		ComputeGlobIllum(fbo, tempCohFactor);
 		CombineIllum(fbo);
 		break;
@@ -472,7 +504,6 @@
 
 void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo, 
-								   float tempCohFactor,
-								   const Matrix4x4 &projViewMatrix,
-								   const Matrix4x4 &oldProjViewMatrix
+								   float tempCohFactor
+								   
 								   )
 {
@@ -499,6 +530,5 @@
 	sCgSsaoProgram->SetTexture(3, noiseTex);
 
-	const Vector3 pos = mCamera->GetPosition();
-	sCgSsaoProgram->SetValue3f(4, pos.x, pos.y, pos.z);
+	sCgSsaoProgram->SetValue3f(4, mEyePos.x, mEyePos.y, mEyePos.z);
 
 	sCgSsaoProgram->SetValue1f(5, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
@@ -514,7 +544,9 @@
 		sCgSsaoProgram->SetArray2f(6, (float *)samples2, NUM_SAMPLES);
 	}
-
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
+	
+	Vector3 bl = mCornersView[0];
+	Vector3 br = mCornersView[1];
+	Vector3 tl = mCornersView[2];
+	Vector3 tr = mCornersView[3];
 
 	sCgSsaoProgram->SetValue3f(7, bl.x, bl.y, bl.z);
@@ -526,6 +558,17 @@
 	//cout << "old projview:\n" << oldProjViewMatrix << endl;
 
-	sCgSsaoProgram->SetMatrix(11, projViewMatrix);
-	sCgSsaoProgram->SetMatrix(12, oldProjViewMatrix);
+	sCgSsaoProgram->SetMatrix(11, mProjViewMatrix);
+	sCgSsaoProgram->SetMatrix(12, mOldProjViewMatrix);
+
+	bl = mOldCornersView[0];
+	br = mOldCornersView[1];
+	tl = mOldCornersView[2];
+	tr = mOldCornersView[3];
+
+	sCgSsaoProgram->SetValue3f(13, mOldEyePos.x, mOldEyePos.y, mOldEyePos.z);
+	sCgSsaoProgram->SetValue3f(14, bl.x, bl.y, bl.z);
+	sCgSsaoProgram->SetValue3f(15, br.x, br.y, br.z);
+	sCgSsaoProgram->SetValue3f(16, tl.x, tl.y, tl.z);
+	sCgSsaoProgram->SetValue3f(17, tr.x, tr.y, tr.z);
 
 
@@ -535,17 +578,4 @@
 
 	PrintGLerror("ssao first pass");
-}
-
-
-void DeferredRenderer::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
-{
-	Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
-
-	mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
-
-	bl = Normalize(nbl - fbl);
-	br = Normalize(nbr - fbr);
-	tl = Normalize(ntl - ftl);
-	tr = Normalize(ntr - ftr);
 }
 
@@ -622,7 +652,5 @@
 
 void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo, 
-										float tempCohFactor,
-										const Matrix4x4 &projViewMatrix,
-										const Matrix4x4 &oldProjViewMatrix)
+										float tempCohFactor)
 {
 #if 0
@@ -652,6 +680,5 @@
 	sCgGiProgram->SetTexture(4, oldIllumTex);
 
-	const Vector3 pos = mCamera->GetPosition();
-	sCgGiProgram->SetValue3f(5, pos.x, pos.y, pos.z);
+	sCgGiProgram->SetValue3f(5, mEyePos.x, mEyePos.y, mEyePos.z);
 
 
@@ -671,8 +698,9 @@
 	}
 
-
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
-	
+	Vector3 bl = mCornersView[0];
+	Vector3 br = mCornersView[1];
+	Vector3 tl = mCornersView[2];
+	Vector3 tr = mCornersView[3];
+
 	sCgGiProgram->SetValue3f(8, bl.x, bl.y, bl.z);
 	sCgGiProgram->SetValue3f(9, br.x, br.y, br.z);
@@ -680,6 +708,6 @@
 	sCgGiProgram->SetValue3f(11, tr.x, tr.y, tr.z);
 
-	sCgGiProgram->SetMatrix(12, oldProjViewMatrix);
-	sCgGiProgram->SetMatrix(13, projViewMatrix);
+	sCgGiProgram->SetMatrix(12, mOldProjViewMatrix);
+	sCgGiProgram->SetMatrix(13, mProjViewMatrix);
 
 
@@ -792,6 +820,5 @@
 	sCgDeferredShadowProgram->SetValue3f(6, lightDir.x, lightDir.y, lightDir.z);
 
-	const Vector3 pos = mCamera->GetPosition();
-	sCgDeferredShadowProgram->SetValue3f(7, pos.x, pos.y, pos.z);
+	sCgDeferredShadowProgram->SetValue3f(7, mEyePos.x, mEyePos.y, mEyePos.z);
 
 	DrawQuad(sCgDeferredShadowProgram);
@@ -972,4 +999,26 @@
 
 
+void DeferredRenderer::InitFrame()
+{
+	mOldProjViewMatrix = mProjViewMatrix;
+
+	Matrix4x4 matViewing, matProjection;
+
+	mCamera->GetModelViewMatrix(matViewing);
+	mCamera->GetProjectionMatrix(matProjection);
+
+	mProjViewMatrix = matViewing * matProjection;
+
+	for (int i = 0; i < 4; ++ i)
+	{
+		mOldCornersView[i] = mCornersView[i];
+	}
+
+	ComputeViewVectors(mCamera, mCornersView[0], mCornersView[1], mCornersView[2], mCornersView[3]);
+
+	mOldEyePos = mEyePos;
+	mEyePos = mCamera->GetPosition();
+}
+
 
 } // namespace
