Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2859)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2860)
@@ -3,12 +3,18 @@
 #include "RenderState.h"
 #include "SampleGenerator.h"
+#include "Vector3.h"
+#include "Camera.h"
 
 
 using namespace std;
 
-GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
+static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
 
 namespace CHCDemoEngine
 {
+
+// number of ssao samples
+#define NUM_SAMPLES 16
+
 
 static CGprogram sCgSsaoProgram = NULL;
@@ -25,6 +31,5 @@
 static CGparameter sExpFactorParam;
 
-
-#define NUM_SAMPLES 16
+static GLuint noiseTex;
 
 // ssao random spherical samples
@@ -54,6 +59,13 @@
 
 
-SsaoShader::SsaoShader(int w, int h, float expFactor, float scaleFactor):
-mWidth(w), mHeight(h), mExpFactor(expFactor), mScaleFactor(scaleFactor)
+SsaoShader::SsaoShader(int w, int h, 
+					   Camera *cam,
+					   float expFactor, 
+					   float scaleFactor
+					   ):
+mWidth(w), mHeight(h), 
+mCamera(cam),
+mExpFactor(expFactor), 
+mScaleFactor(scaleFactor)
 {}
 
@@ -113,8 +125,10 @@
 	GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture();
 
+	GLuint oldTex = fbo2->GetColorBuffer(0)->GetTexture();
+
 	glPushAttrib(GL_VIEWPORT_BIT);
 	glViewport(0, 0, mWidth, mHeight);
 
-	glDrawBuffers(1, mrt);
+	glDrawBuffers(1, mymrt);
 
 	glDisable(GL_ALPHA_TEST);
@@ -172,19 +186,19 @@
 
 	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
-	//float offs2 = 0.55f;
-	float offs2 = 0.5f;
-
-	glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
-	glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
-	glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
-	glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
+	//const float new_offs = 0.55f;
+	const float new_offs = 0.5f;
+	
+	glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
+	glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
+	glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
+	glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
 
 	glEnd();
 
-	cgGLDisableTextureParameter(sColorsTexParamSsao);
-	cgGLDisableTextureParameter(sPositionsTexParamSsao);
-	cgGLDisableTextureParameter(sNormalsTexParamSsao);
-	cgGLDisableTextureParameter(sNoiseTexParamSsao);
-	cgGLDisableTextureParameter(sOldTexParamSsao);
+	cgGLDisableTextureParameter(sColorsTexParam);
+	cgGLDisableTextureParameter(sPositionsTexParam);
+	cgGLDisableTextureParameter(sNormalsTexParam);
+	cgGLDisableTextureParameter(sNoiseTexParam);
+	cgGLDisableTextureParameter(sOldTexParam);
 
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
@@ -254,4 +268,70 @@
 
 
+void SsaoShader::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);
+
+#if 1 // matT: debug this!!
+	
+	bl = Normalize(nbl - fbl);
+	br = Normalize(nbr - fbr);
+	tl = Normalize(ftl - ntl);
+	tr = Normalize(ftr - ntr);
+
+#else // just take camera direction
+	
+	bl = -Normalize(mCamera->GetDirection());
+	br = -Normalize(mCamera->GetDirection());
+	tl = -Normalize(mCamera->GetDirection());
+	tr = -Normalize(mCamera->GetDirection());
+
+#endif
+
+	// normalize to 0 .. 1
+	bl = bl * 0.5f + 0.5f;
+	br = br * 0.5f + 0.5f;
+	tl = tl * 0.5f + 0.5f;
+	tr = tr * 0.5f + 0.5f;
+}
+
+
+void SsaoShader::CreateNoiseTex2D()
+{
+	GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
+
+	for (int i = 0; i < mWidth * mHeight * 3; i += 3)
+	{
+		// create random samples over sphere
+		const float rx = RandomValue(0, 1);
+		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 + 2] = 0;
+	}
+
+	glEnable(GL_TEXTURE_2D);
+	glGenTextures(1, &noiseTex);
+	glBindTexture(GL_TEXTURE_2D, noiseTex);
+		
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
+
+	glBindTexture(GL_TEXTURE_2D, 0);
+	glDisable(GL_TEXTURE_2D);
+
+	delete [] randomNormals;
+
+	cout << "created noise texture" << endl;
+
+	PrintGLerror("noisetexture");
+}
+
 
 } // namespace
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2859)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2860)
@@ -12,4 +12,6 @@
 
 class FrameBufferObject;
+class Vector3;
+class Camera;
 
 
@@ -21,11 +23,12 @@
 public:
 	/** constructor for a deferred shader taking the requested output image size,
-		the exponential smoothing factor for temporal reprojection.
-		
-		The last parameter is a just a scale factor
-		for the scene depth in order to get better floating point precision in the shader
-		This must be reciprocal value of the scale factor used in the mrt shader.
+		the current camera, the exponential smoothing factor for temporal reprojection, 
+		and a scaling factor.
+			
+		The parameter scaleFactor must be reciprocal value of the 
+		scale factor used for creating the positions texture. It is used recover the 
+		exact scene size that was scaled in order to improve floating point precision.
 	*/
-	SsaoShader(int w, int h, float expFactor, float scaleFactor);
+	SsaoShader(int w, int h, Camera *cam, float expFactor, float scaleFactor);
 	/** The algorithm renders the scene given an fbo.
 		The fbo must have color buffer, position buffer, normal buffer.
@@ -55,4 +58,13 @@
 	/// this is just a scale factor for the scene depth in order to get better float precision in the shader
 	float mScaleFactor;
+
+	Camera *mCamera;
+
+
+private:
+
+	void CreateNoiseTex2D();
+	void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br);
+
 };
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2859)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2860)
@@ -668,26 +668,16 @@
 	// this fbo basicly stores the scene information we get from standard rendering of a frame
 	// we store colors, normals, positions (for the ssao)
-	myfbo = new FrameBufferObject(texWidth, texHeight, true, FrameBufferObject::DEPTH_24);
+	fbo = new FrameBufferObject(texWidth, texHeight, true, FrameBufferObject::DEPTH_24);
 
 
 	// the diffuse color buffer
-	myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, 
-						  ColorBufferObject::WRAP_CLAMP_TO_EDGE, 
-						  ColorBufferObject::FILTER_LINEAR, 
-						  false, false);
+	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false);
 
 	// the positions buffer
-	myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, 
-	                      ColorBufferObject::WRAP_CLAMP_TO_EDGE, 
-						  ColorBufferObject::FILTER_NEAREST, 
-						  false, false);
+	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false);
 
 	// the normals buffer
-	myfbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, 
-	                      ColorBufferObject::WRAP_CLAMP_TO_EDGE, 
-						  ColorBufferObject::FILTER_NEAREST, 
-						  false, false);
-
-	
+	fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false);
+
 
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2844)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2860)
@@ -42,9 +42,4 @@
 void Camera::Precompute(const Vector3 &direction) 
 {
-	/*
-	Vector3 side = CrossProd(Vector3(1, 0, 0), direction);
-	Vector3 up = -Normalize(CrossProd(side, direction));
-	Vector3 right = -Normalize(CrossProd(direction, up));
-	*/
 	Vector3 up = Vector3(0, 0, 1);
 	Vector3 right = Normalize(CrossProd(up, direction));
@@ -53,9 +48,4 @@
 	mBaseOrientation = Matrix4x4(right, up, direction);
 	mViewOrientation = mBaseOrientation;
-
-	/*cout << "right: " << right << endl;
-	cout << "up:    " << up << endl;
-	cout << "dir:   " << direction << endl;
-	*/
 }
 
@@ -241,10 +231,10 @@
 	}
 
-	Vector3 h2 = direction;// h2.x = 0; 
+	Vector3 h2 = direction; 
 	
 	if (SqrMagnitude(h2) > 0)
 	{
 		h2.Normalize();
-		mYaw = -acos(DotProd(h2, h1));//Vector3(0, 1, 0)));
+		mYaw = -acos(DotProd(h2, h1));
 	}
 
