Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2899)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2900)
@@ -7,9 +7,10 @@
 // rule of thumb: approx 1 / NUM_SAMPLES
 //#define SAMPLE_INTENSITY 0.14f
-#define SAMPLE_INTENSITY 0.28f
-//#define SAMPLE_INTENSITY 1.0f
+//#define SAMPLE_INTENSITY 0.28f
+#define SAMPLE_INTENSITY 0.32f
 
-//#define AREA_SIZE 20e-1f
-#define AREA_SIZE 5e-1f
+//#define AREA_SIZE 18e-1f
+#define AREA_SIZE 8e-1f
+//#define AREA_SIZE 5e-1f
 
 #define VIEW_CORRECTION_SCALE 0.0f
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.cpp	(revision 2899)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.cpp	(revision 2900)
@@ -17,5 +17,5 @@
 
 
-void PoissonDiscSampleGenerator::Generate(Sample2 *samples) const
+void PoissonDiscSampleGenerator::Generate(float *samples) const
 {
 	// this is a hacky poisson sampling generator which does random dart-throwing
@@ -35,4 +35,5 @@
 
 	//cout << "minDist before= " << minDist << endl;
+	Sample2 *s = (Sample2 *)samples;
 
 	for (int i = 0; i < mNumSamples; ++ i)
@@ -61,6 +62,6 @@
 			{
 				const float dist = 
-					sqrt((samples[j].x - rx) * (samples[j].x - rx) +
-					     (samples[j].y - ry) * (samples[j].y - ry));
+					sqrt((s[j].x - rx) * (s[j].x - rx) +
+					     (s[j].y - ry) * (s[j].y - ry));
 			
 				if (dist < minDist)
@@ -70,6 +71,6 @@
 			if (sampleValid)
 			{
-				samples[i].x = rx;
-				samples[i].y = ry;
+				s[i].x = rx;
+				s[i].y = ry;
 				break;
 			}
@@ -95,5 +96,5 @@
 
 
-void GaussianSampleGenerator::Generate(Sample2 *samples) const
+void GaussianSampleGenerator::Generate(float *samples) const
 {
 	static HaltonSequence halton;
@@ -101,4 +102,6 @@
 
 	const float sigma = mRadius;
+
+	Sample2 *s = (Sample2 *)samples;
 
 	const float p0 = 1.0f / (sigma * sqrt(2.0f * M_PI));
@@ -112,6 +115,6 @@
 		float exp_y = -(r[1] * r[1]) / (2.0f * sigma * sigma);
 
-		samples[i].x = p0 * pow(M_E, exp_x);
-		samples[i].y = p1 * pow(M_E, exp_y);
+		s[i].x = p0 * pow(M_E, exp_x);
+		s[i].y = p1 * pow(M_E, exp_y);
 	}
 
@@ -125,10 +128,10 @@
 
 
-
-
-void SphericalSampleGenerator::Generate(Sample2 *samples) const
+void SphericalSampleGenerator::Generate(float *samples) const
 {
 	static HaltonSequence halton;
 	float r[2];
+
+	Sample3 *s = (Sample3 *)samples;
 
 	for (int i = 0; i < mNumSamples; ++ i)
@@ -137,17 +140,10 @@
 
 		// create stratified samples over sphere
-		const float rx = r[0];
-		const float ry = r[1];
+		const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
+		const float phi = 2.0f * M_PI * r[1];
 
-		const float theta = 2.0f * acos(sqrt(1.0f - rx));
-		const float phi = 2.0f * M_PI * ry;
-
-		float x = sin(theta) * cos(phi);
-		float y = sin(theta) * sin(phi);
-		float z = cos(theta);
-
-		// project to disc
-		samples[i].x = x / z;
-		samples[i].y = y / z;
+		s[i].x = sin(theta) * cos(phi);
+		s[i].y = sin(theta) * sin(phi);
+		s[i].z = cos(theta);
 	}
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2897)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2900)
@@ -38,5 +38,9 @@
 		a smoothing factor for temporal coherence
 	*/
-	void Render(FrameBufferObject *fbo, const Matrix4x4 &oldProjViewMatrix, float expFactor, ShadowMap *shadowMap = NULL);
+	void Render(FrameBufferObject *fbo, 
+		        const Matrix4x4 &oldProjViewMatrix, 
+				const Matrix4x4 &projViewMatrix, 
+				float expFactor, 
+				ShadowMap *shadowMap = NULL);
 
 	/** Initialises the deferred shader and loads the required shaders:
@@ -58,5 +62,5 @@
 protected:
 
-	void ComputeSsao(FrameBufferObject *fbo, float expFactor, const Matrix4x4 &oldProjViewMatrix);
+	void ComputeSsao(FrameBufferObject *fbo, float expFactor, const Matrix4x4 &oldProjViewMatrix, const Matrix4x4 &projViewMatrix);
 
 	void ComputeGlobIllum(FrameBufferObject *fbo, float expFactor, const Matrix4x4 &oldProjViewMatrix);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.h	(revision 2899)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.h	(revision 2900)
@@ -16,4 +16,16 @@
 
 
+struct Sample3
+{
+	Sample3() {}
+	Sample3(float _x, float _y, float _z): x(_x), y(_y), z(_z) {}
+
+	float x; 
+	float y;
+	float z;
+};
+
+/** Class generating random samples on a disc or a sphere, respectively.
+*/
 class SampleGenerator
 {
@@ -22,5 +34,5 @@
 	SampleGenerator(int numSamples, float radius);
 
-	virtual void Generate(Sample2 *samples) const = 0;
+	virtual void Generate(float *samples) const = 0;
 
 protected:
@@ -39,5 +51,5 @@
 	PoissonDiscSampleGenerator(int numSamples, float radius);
 
-	virtual void Generate(Sample2 *samples) const;
+	virtual void Generate(float *samples) const;
 };
 
@@ -49,5 +61,5 @@
 	GaussianSampleGenerator(int numSamples, float radius);
 
-	virtual void Generate(Sample2 *samples) const;
+	virtual void Generate(float *samples) const;
 };
 
@@ -59,5 +71,5 @@
 	SphericalSampleGenerator(int numSamples, float radius);
 
-	virtual void Generate(Sample2 *samples) const;
+	virtual void Generate(float *samples) const;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2899)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2900)
@@ -1032,5 +1032,5 @@
 
 		ShadowMap *sm = showShadowMap ? shadowMap : NULL;
-		ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor, sm);
+		ssaoShader->Render(fbo, oldViewProjMatrix, matProjectionView, ssaoExpFactor, sm);
 	}
 
@@ -1300,5 +1300,6 @@
 		break;
 	case GLUT_KEY_F8:
-		shadingMethod = (DeferredRenderer::SHADING_METHOD)((shadingMethod + 1) % 3);
+		//shadingMethod = (DeferredRenderer::SHADING_METHOD)((shadingMethod + 1) % 3);
+		shadingMethod = (DeferredRenderer::SHADING_METHOD)((shadingMethod + 1) % 2);
 		
 		break;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2899)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2900)
@@ -42,4 +42,5 @@
 
 static CGparameter sOldModelViewProjMatrixParam;
+static CGparameter sModelViewProjMatrixParam;
 static CGparameter sMaxDepthParam;
 static CGparameter sSamplesParam; 
@@ -96,5 +97,6 @@
 
 // ssao random spherical samples
-static Sample2 samples[NUM_SAMPLES];
+static Sample2 samples2[NUM_SAMPLES];
+static Sample2 samples3[NUM_SAMPLES];
 
 static int colorBufferIdx = 0;
@@ -117,4 +119,7 @@
 static void GenerateSamples(int sampling)
 {
+	static SphericalSampleGenerator gauss(NUM_SAMPLES, 0.5f);
+	gauss.Generate((float *)samples3);
+/*
 	switch (sampling)
 	{
@@ -122,5 +127,5 @@
 		{
 			static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
-			poisson.Generate((Sample2 *)samples);
+			poisson.Generate((float *)samples2);
 		}
 		break;
@@ -128,13 +133,13 @@
 		{
 			//static GaussianSampleGenerator gauss(NUM_SAMPLES, 0.5f);
-			//gauss.Generate((Sample2 *)samples);
+			//gauss.Generate((float *)samples2);
 
 			static SphericalSampleGenerator gauss(NUM_SAMPLES, 0.5f);
-			gauss.Generate((Sample2 *)samples);
+			gauss.Generate((float *)samples3);
 		}
 		break;
 	default:
 		break;
-	}
+	}*/
 }
 
@@ -145,15 +150,25 @@
 	float *randomNormals = new float[w * h * 3];
 
+	static HaltonSequence halton;
+	float r[2];
+
 	for (int i = 0; i < w * h * 3; i += 3)
 	{
 		// create random samples on a circle
-		const float rx = RandomValue(0, 1);
+		halton.GetNext(2, r);
+
+		/*
 		const float theta = 2.0f * acos(sqrt(1.0f - rx));
 
-		//randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
-		//randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
 		randomNormals[i + 0] = cos(theta);
 		randomNormals[i + 1] = sin(theta);
-		randomNormals[i + 2] = 0;
+		randomNormals[i + 2] = 0;*/
+
+		const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
+		const float phi = 2.0f * M_PI * r[1];
+
+		randomNormals[i + 0] = sin(theta) * cos(phi);
+		randomNormals[i + 1] = sin(theta) * sin(phi);
+		randomNormals[i + 2] = cos(theta);
 	}
 
@@ -197,17 +212,4 @@
 	//-- the flip-flop fbos
 
-	/*mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
-	
-	mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	
-
-	///////////////////////
-
-	mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
-	
-	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);*/
-	
 	mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
 
@@ -269,5 +271,6 @@
 		cgCreateProgramFromFile(context, 
 								CG_SOURCE,
-								"src/shaders/ssao.cg", 
+								//"src/shaders/ssao.cg", 
+								"src/shaders/ssao2.cg", 
 								RenderState::sCgFragmentProfile,
 								"main",
@@ -285,4 +288,5 @@
 		
 		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
+		sModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "mymodelViewProj");
 		sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
 		sTemporalCoherenceParam = cgGetNamedParameter(sCgSsaoProgram, "temporalCoherence");
@@ -416,4 +420,5 @@
 void DeferredRenderer::Render(FrameBufferObject *fbo, 
 							  const Matrix4x4 &oldProjViewMatrix,
+							  const Matrix4x4 &projViewMatrix,
 							  float expFactor,
 							  ShadowMap *shadowMap)
@@ -457,5 +462,5 @@
 	{
 	case SSAO:
-		ComputeSsao(fbo, expFactor, oldProjViewMatrix);
+		ComputeSsao(fbo, expFactor, oldProjViewMatrix, projViewMatrix);
 		CombineSsao(fbo);
 		break;
@@ -487,9 +492,18 @@
 
 void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo, 
-							 float expFactor,
-							 const Matrix4x4 &oldProjViewMatrix
-							 )
-{
+								   float expFactor,
+								   const Matrix4x4 &oldProjViewMatrix,
+								   const Matrix4x4 &projViewMatrix
+								   )
+{
+	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
+								0.0f, 0.5f, 0.0f, 0.5f,
+								0.0f, 0.0f, 0.5f, 0.5f,
+								0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1]
+
+	Matrix4x4 m = projViewMatrix * biasMatrix; 
+
 	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
+	cgGLSetMatrixParameterfc(sModelViewProjMatrixParam, (const float *)m.x);
 
 	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
@@ -547,5 +561,7 @@
 		// needs longer to converge
 		GenerateSamples(mSamplingMethod); 
-		cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
+
+		//cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples2);
+		cgGLSetParameterArray3f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples3);
 	}
 
@@ -772,5 +788,5 @@
 		// needs longer to converge
 		GenerateSamples(mSamplingMethod); 
-		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
+		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples2);
 	}
 
@@ -865,5 +881,4 @@
 {
 	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
-	//GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
 	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2898)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2900)
@@ -807,4 +807,8 @@
 				>
 			</File>
+			<File
+				RelativePath=".\src\shaders\ssao2.cg"
+				>
+			</File>
 		</Filter>
 		<File
