Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2893)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2895)
@@ -81,5 +81,6 @@
 				  uniform sampler2D shadowMap,
 				  uniform float4x4 shadowMatrix,
-				  uniform float maxDepth
+				  uniform float maxDepth,
+				  uniform float sampleWidth
 				  )
 {
@@ -102,13 +103,45 @@
 	float4 lightSpacePos = mul(shadowMatrix, position);
 
-	float shadowDepth = tex2D(shadowMap, lightSpacePos.xy);
+	float shadowDepth[9];
+
+	float w = sampleWidth;
+
+	// pcf sampling using 3 x 3 tab
+
+	shadowDepth[0] = tex2D(shadowMap, lightSpacePos.xy).x;
+	
+	shadowDepth[1] = tex2D(shadowMap, lightSpacePos.xy + float2( w,  w)).x;
+	shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy - float2( w, -w)).x;
+	shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, -w)).x;
+	shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy - float2(-w,  w)).x;
+
+	shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy - float2( w,  0)).x;
+	shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy - float2( 0,  w)).x;
+	shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy - float2(-w,  0)).x;
+	shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, -w)).x;
 
 	OUT.color = col;
 	
-	// hack: prevent shadowing the sky
-	if ((amb < 0.9) &&	
+	float depth = lightSpacePos.z / lightSpacePos.w;
+
+	float d = 0.0f;
+
+	for (int i = 0; i < 9; ++ i)
+	{
+		d += step(shadowDepth[i], depth);
+	}
+
+	d /= 9.0f;
+	
+	/*if ((amb < 0.9) && // hack: prevent shadowing the sky	
 		(lightSpacePos.z / lightSpacePos.w > shadowDepth))
 	{
 		OUT.color *= 0.1f;
+	}*/
+	
+	if (amb < 0.9) // hack: prevent shadowing the sky	
+	{
+		const float x = 0.1f;
+		OUT.color *= x + (1.0f - x) * (1.0f - d);
 	}
 	
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2894)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2895)
@@ -12,4 +12,7 @@
 namespace CHCDemoEngine
 {
+
+// the number of visibility states
+#define NUM_STATES 2
 
 ////////
@@ -149,9 +152,9 @@
 	inline int CountPrimitives() const;
 
+	static void SetCurrentState(int _state) { sCurrentState = _state; }
+
 
 protected:
 
-	/////////////
-	
 	/// the depth of this node
 	unsigned char mDepth;
@@ -160,12 +163,23 @@
 	/// the parent node
 	BvhNode *mParent;
-	/// stores the visibility related info
-	VisibilityInfo mVisibility;
+
+
+	//////////////
+	//-- these members define the current state
+
+	/// stores the visibility related info 
+	VisibilityInfo mVisibility[NUM_STATES];
 
 	/////////
 	//-- used for view frustum culling
 
-	int mPlaneMask;
-	int mPreferredPlane;
+	int mPlaneMask[NUM_STATES];
+	int mPreferredPlane[NUM_STATES];
+
+	// the current state
+	static int sCurrentState;
+
+
+	////////////////////
 
 
@@ -221,5 +235,5 @@
 int BvhNode::GetLastVisitedFrame() const 
 { 
-	return mVisibility.mLastVisitedFrame; 
+	return mVisibility[sCurrentState].mLastVisitedFrame; 
 }
 
@@ -227,5 +241,5 @@
 void BvhNode::SetLastVisitedFrame(const int lastVisited) 
 { 
-	mVisibility.mLastVisitedFrame = lastVisited; 
+	mVisibility[sCurrentState].mLastVisitedFrame = lastVisited; 
 }
 
@@ -233,5 +247,5 @@
 bool BvhNode::IsVisible() const 
 { 
-	return mVisibility.mIsVisible; 
+	return mVisibility[sCurrentState].mIsVisible; 
 }
 
@@ -239,5 +253,5 @@
 void BvhNode::SetVisible(bool visible) 
 { 
-	mVisibility.mIsVisible = visible; 
+	mVisibility[sCurrentState].mIsVisible = visible; 
 }
 
@@ -245,5 +259,5 @@
 void BvhNode::IncTimesTestedInvisible() 
 { 
-	++ mVisibility.mTimesInvisible; 
+	++ mVisibility[sCurrentState].mTimesInvisible; 
 }
 
@@ -251,5 +265,5 @@
 int BvhNode::GetTimesTestedInvisible() const 
 { 
-	return mVisibility.mTimesInvisible; 
+	return mVisibility[sCurrentState].mTimesInvisible; 
 }
 
@@ -257,5 +271,5 @@
 void BvhNode::SetTimesTestedInvisible(int t) 
 { 
-	mVisibility.mTimesInvisible = t; 
+	mVisibility[sCurrentState].mTimesInvisible = t; 
 }
 
@@ -263,5 +277,5 @@
 bool BvhNode::IsViewFrustumCulled() const 
 { 
-	return mVisibility.mIsFrustumCulled; 
+	return mVisibility[sCurrentState].mIsFrustumCulled; 
 }
 
@@ -269,5 +283,5 @@
 void BvhNode::SetViewFrustumCulled(bool frustumCulled) 
 { 
-	mVisibility.mIsFrustumCulled = frustumCulled; 
+	mVisibility[sCurrentState].mIsFrustumCulled = frustumCulled; 
 }
 
@@ -275,5 +289,5 @@
 bool BvhNode::IsNew() const 
 { 
-	return mVisibility.mIsNew;
+	return mVisibility[sCurrentState].mIsNew;
 }
 
@@ -281,7 +295,30 @@
 void BvhNode::SetIsNew(bool isNew) 
 { 
-	mVisibility.mIsNew = isNew; 
-}
-
+	mVisibility[sCurrentState].mIsNew = isNew; 
+}
+
+
+void BvhNode::SetAssumedVisibleFrameId(int t) 
+{ 
+	mVisibility[sCurrentState].mAssumedVisibleFrameId = t; 
+}
+
+
+int BvhNode::GetAssumedVisibleFrameId() const 
+{ 
+	return mVisibility[sCurrentState].mAssumedVisibleFrameId;
+}
+
+
+int BvhNode::GetLastQueriedFrame() const
+{
+	return mVisibility[sCurrentState].mLastQueriedFrame;
+}
+
+
+void BvhNode::SetLastQueriedFrame(int lastTested)
+{
+	mVisibility[sCurrentState].mLastQueriedFrame = lastTested;
+}
 
 int BvhNode::GetLastRenderedFrame() const 
@@ -309,26 +346,4 @@
 
 
-void BvhNode::SetAssumedVisibleFrameId(int t) 
-{ 
-	mVisibility.mAssumedVisibleFrameId = t; 
-}
-
-
-int BvhNode::GetAssumedVisibleFrameId() const 
-{ 
-	return mVisibility.mAssumedVisibleFrameId;
-}
-
-
-int BvhNode::GetLastQueriedFrame() const
-{
-	return mVisibility.mLastQueriedFrame;
-}
-
-
-void BvhNode::SetLastQueriedFrame(int lastTested)
-{
-	mVisibility.mLastQueriedFrame = lastTested;
-}
 
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2894)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2895)
@@ -46,5 +46,7 @@
 	*/
 	void GetTextureMatrix(Matrix4x4 &m) const;
-
+	/** Return shadow size.
+	*/
+	int GetSize() const { return mSize; }
 
 protected:
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2894)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2895)
@@ -24,4 +24,6 @@
 using namespace std;
 
+
+int BvhNode::sCurrentState = 0;
 
 
@@ -75,6 +77,4 @@
 mAxis(-1), 
 mDepth(0), 
-mPlaneMask(0),
-mPreferredPlane(0),
 mLastRenderedFrame(-999),
 mFirst(-1), 
@@ -87,4 +87,9 @@
 mIsVirtualLeaf(false)
 {
+	for (int i = 0; i < NUM_STATES; ++ i)
+	{
+		mPlaneMask[i] = 0;
+		mPreferredPlane[i]= 0;
+	}
 }
 
@@ -97,5 +102,9 @@
 void BvhNode::ResetVisibility()
 {
-	mVisibility.Reset();
+	for (int i = 0; i < NUM_STATES; ++ i)
+	{
+		mVisibility[i].Reset();
+	}
+
 	mLastRenderedFrame = -999;
 }
@@ -301,5 +310,5 @@
 {
 	// do the test only if necessary
-	if (!(node->mPlaneMask & (1 << i)))
+	if (!(node->mPlaneMask[BvhNode::sCurrentState] & (1 << i)))
 		return true;
 			
@@ -310,5 +319,5 @@
 	{
 		// outside
-		node->mPreferredPlane = i;
+		node->mPreferredPlane[BvhNode::sCurrentState] = i;
 		return false;
 	}
@@ -320,5 +329,5 @@
 	{
 		// completely inside: children don't need to check against this plane no more
-		node->mPlaneMask^= 1 << i;
+		node->mPlaneMask[BvhNode::sCurrentState] ^= 1 << i;
 	}
 	else 
@@ -336,21 +345,17 @@
 
 	if (node->GetParent())
-		node->mPlaneMask = node->GetParent()->mPlaneMask;
-
-
-	//for (int i = 0; i < 6; ++ i)
-	//	if (!TestPlane(node, i, bIntersect)) return 0;
-	
+		node->mPlaneMask[BvhNode::sCurrentState] = node->GetParent()->mPlaneMask[BvhNode::sCurrentState];
 
 	////////
 	//-- apply frustum culling for the planes [mPreferredPlane - 5]
 
-	for (int i = node->mPreferredPlane; i < 6; ++ i)
+	for (int i = node->mPreferredPlane[BvhNode::sCurrentState]; i < 6; ++ i)
 		if (!TestPlane(node, i, bIntersect)) return 0;
 	
+
 	//////////
 	//-- do the view frustum culling for the planes [0 - m_iPreferredPlane)
 
-	for (int i = 0; i < node->mPreferredPlane; ++ i)
+	for (int i = 0; i < node->mPreferredPlane[BvhNode::sCurrentState]; ++ i)
 		if (!TestPlane(node, i, bIntersect)) return 0;
 	
@@ -362,5 +367,5 @@
 {
 	// = 0011 1111 which means that at the beginning, all six planes have to frustum culled
-	mRoot->mPlaneMask = 0x3f;
+	mRoot->mPlaneMask[BvhNode::sCurrentState] = 0x3f;
 
 	mCamera->CalcFrustum(sFrustum);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2894)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2895)
@@ -34,5 +34,4 @@
 #include "FrameBufferObject.h"
 #include "SsaoShader.h"
-#include "DeferredShader.h"
 #include "ShadowMapping.h"
 #include "Light.h"
@@ -95,7 +94,4 @@
 static int winHeight = 768;
 
-//static int winWidth = 512;
-//static int winHeight = 512;
-
 
 static float winAspectRatio = 1.0f;
@@ -172,5 +168,9 @@
 bool useLODs = true;
 
-int samplingMethod = SsaoShader::POISSON;
+SsaoShader::SAMPLING_METHOD samplingMethod = SsaoShader::POISSON;
+SsaoShader::SHADING_METHOD shadingMethod = SsaoShader::DEFAULT;
+
+bool showShadowMap = false;
+bool shadowChanged = true;
 
 static Matrix4x4 matProjectionView = IdentityMatrix();
@@ -179,4 +179,5 @@
 ShadowMap *shadowMap = NULL;
 Light *light = NULL;
+
 
 // function forward declarations
@@ -225,5 +226,4 @@
 
 SsaoShader *ssaoShader = NULL;
-DeferredShader *deferredShader = NULL;
 
 static GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
@@ -431,5 +431,4 @@
 	InitCg();
 
-	DeferredShader::Init(sCgContext);
 	SsaoShader::Init(sCgContext);
 
@@ -442,10 +441,10 @@
 	sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
 
-	Vector3 lightDir = Normalize(Vector3(0, 1, -1));
+	//Vector3 lightDir = Normalize(Vector3(0, 1, -1));
+	Vector3 lightDir = Normalize(-Vector3(0.8f, -1.0f, 0.7f));
+	
 	light = new Light(lightDir, RgbaColor(1, 1, 1, 1));
 
-	const float shadowSize = 4096;
-	shadowMap = new ShadowMap(shadowSize, bvh->GetBox(), camera);
-	
+
 	// frame time is restarted every frame
 	frameTimer.Start();
@@ -867,4 +866,10 @@
 	}
 	
+	if (showShadowMap && !shadowMap)
+	{
+		const float shadowSize = 4096;
+		shadowMap = new ShadowMap(shadowSize, bvh->GetBox(), camera);
+	}
+
 	// render without shading
 	switch (renderType)
@@ -897,5 +902,4 @@
 	
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
 		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
@@ -904,7 +908,28 @@
 	case RenderState::DEFERRED:
 
-		shadowMap->ComputeShadowMap(light, traverser);
+		// change CHC state (must be done for each change of camera)
+		BvhNode::SetCurrentState(1);
+
+		if (showShadowMap && shadowChanged)
+		{
+			shadowChanged = false;
+
+			cgGLDisableProfile(RenderState::sCgFragmentProfile);
+			cgGLDisableProfile(RenderState::sCgVertexProfile);
+
+			state.SetRenderType(RenderState::DEPTH_PASS);
+
+			// the scene is rendered withouth any shading	
+			shadowMap->ComputeShadowMap(light, traverser);
+
+			// change back state
+			BvhNode::SetCurrentState(0);
+
+			glEnable(GL_LIGHTING);
+			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+		}
 
 		if (!fbo) InitFBO();
+
 
 		// multisampling does not work with deferred shading
@@ -989,16 +1014,12 @@
 		cgGLDisableProfile(RenderState::sCgFragmentProfile);
 
-		if (useSsao)
-		{
-			if (!ssaoShader) ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f);
-			ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor);
-
-		}
-		else
-		{
-			if (!deferredShader) deferredShader = new DeferredShader(texWidth, texHeight, myfar / 10.0f);
-			
-			deferredShader->Render(fbo, shadowMap);
-		}
+		if (!ssaoShader) ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f);
+		
+		ssaoShader->SetShadingMethod(shadingMethod);
+		ssaoShader->SetSamplingMethod(samplingMethod);
+		ssaoShader->SetUseTemporalCoherence(useTemporalCoherence);
+
+		ShadowMap *sm = showShadowMap ? shadowMap : NULL;
+		ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor, sm);
 	}
 
@@ -1118,16 +1139,13 @@
 			samplingMethod = SsaoShader::GAUSS;
 
-		ssaoShader->SetSamplingMethod(samplingMethod);
-
+		break;
+	case 'Y':
+	case 'y':
+		showShadowMap = !showShadowMap;
 		break;
 	case 'g':
-	case 'G':
-		useGlobIllum = !useGlobIllum;
-		ssaoShader->SetUseGlobIllum(useGlobIllum);
-		break;
 	case 't':
 	case 'T':
 		useTemporalCoherence = !useTemporalCoherence;
-		ssaoShader->SetUseTemporalCoherence(useTemporalCoherence);
 		break;
 	case 'o':
@@ -1262,5 +1280,6 @@
 		break;
 	case GLUT_KEY_F8:
-		useSsao = !useSsao;
+		shadingMethod = (SsaoShader::SHADING_METHOD)((shadingMethod + 1) % 3);
+		
 		break;
 	case GLUT_KEY_F9:
@@ -1562,5 +1581,4 @@
 	DEL_PTR(fbo);
 	DEL_PTR(ssaoShader);
-	DEL_PTR(deferredShader);
 
 	if (sCgMrtVertexProgram)
@@ -1766,5 +1784,5 @@
 }
 
-
+// render visible object from depth pass
 void RenderVisibleObjects()
 {
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2894)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2895)
@@ -47,5 +47,5 @@
 static CGparameter sShadowMatrixParam;
 static CGparameter sMaxDepthParam;
-
+static CGparameter sSampleWidthParam;
 
 
@@ -122,7 +122,9 @@
 		sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");  
 		sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");  
-		sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 
+		sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
+
 		sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");  
 		sMaxDepthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
+		sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
 		sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
 	}
@@ -156,11 +158,9 @@
 	glOrtho(-offs, offs, -offs, offs, 0, 1);
 
-
 	FirstPass(fbo);
 	AntiAliasing(fbo);
 
 	glEnable(GL_LIGHTING);
-	glDisable(GL_TEXTURE_2D);
-	
+		
 	glMatrixMode(GL_PROJECTION);
 	glPopMatrix();
@@ -172,4 +172,5 @@
 
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+	FrameBufferObject::Release();
 }
 
@@ -178,4 +179,6 @@
 							ShadowMap *shadowMap)
 {
+	FrameBufferObject::Release();
+
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
 
@@ -183,5 +186,5 @@
 	glDisable(GL_TEXTURE_2D);
 	glDisable(GL_LIGHTING);
-	glDepthMask(GL_FALSE);
+	//glDepthMask(GL_FALSE);
 	
 
@@ -204,6 +207,6 @@
 
 	glEnable(GL_LIGHTING);
-	glEnable(GL_TEXTURE_2D);
-	glDepthMask(GL_TRUE);
+	//glEnable(GL_TEXTURE_2D);
+	//glDepthMask(GL_TRUE);
 	
 	glMatrixMode(GL_PROJECTION);
@@ -216,4 +219,5 @@
 
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+	FrameBufferObject::Release();
 }
 
@@ -258,6 +262,4 @@
 	cgGLDisableTextureParameter(sPositionsTexParam);
 	cgGLDisableTextureParameter(sNormalsTexParam);
-
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
 	
 	FrameBufferObject::Release();
@@ -290,6 +292,4 @@
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
 	cgGLBindProgram(sCgAntiAliasingProgram);
 
@@ -301,7 +301,5 @@
 	
 	glColor3f(1.0f, 1.0f, 1.0f);
-
-	float offs2 = 0.5f;
-
+	
 	glBegin(GL_QUADS);
 
@@ -320,6 +318,4 @@
 	cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
 
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-
 	PrintGLerror("antialiasing");
 }
@@ -343,6 +339,4 @@
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
 	cgGLBindProgram(sCgDeferredShadowProgram);
 
@@ -360,5 +354,6 @@
 
 	cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
-
+	cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize());
+	 
 	cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
 
@@ -381,6 +376,4 @@
 	cgGLDisableTextureParameter(sShadowMapParam);
 
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-	
 	FrameBufferObject::Release();
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2895)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2895)
@@ -0,0 +1,1006 @@
+#include "SsaoShader.h"
+#include "FrameBufferObject.h"
+#include "RenderState.h"
+#include "SampleGenerator.h"
+#include "Vector3.h"
+#include "Camera.h"
+#include "shaderenv.h"
+#include "RndGauss.h"
+#include "Halton.h"
+#include "ShadowMapping.h"
+
+
+using namespace std;
+
+static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
+
+
+namespace CHCDemoEngine
+{
+
+static CGprogram sCgSsaoProgram = NULL;
+static CGprogram sCgGiProgram = NULL;
+
+static CGprogram sCgDeferredProgram = NULL;
+static CGprogram sCgAntiAliasingProgram = NULL;
+static CGprogram sCgDeferredShadowProgram = NULL;
+
+static CGparameter sColorsTexCombineParam;
+static CGparameter sSsaoTexCombineParam;
+
+static CGparameter sColorsTexDeferredParam;
+static CGparameter sPositionsTexDeferredParam;
+static CGparameter sNormalsTexDeferredParam;
+
+
+static CGprogram sCgCombinedSsaoProgram = NULL;
+static CGprogram sCgCombinedIllumProgram = NULL;
+
+///////////////////////////////////////7
+
+
+static CGparameter sColorsTexParam;
+static CGparameter sPositionsTexParam;
+static CGparameter sNormalsTexParam;
+
+static CGparameter sOldModelViewProjMatrixParam;
+static CGparameter sMaxDepthParam;
+static CGparameter sSamplesParam; 
+static CGparameter sOldTexParam;
+static CGparameter sNoiseTexParam;
+static CGparameter sNoiseMultiplierParam;
+static CGparameter sExpFactorParam;
+
+
+
+///////////////////////////////////////
+
+
+static CGparameter sColorsTexGiParam;
+static CGparameter sPositionsTexGiParam;
+static CGparameter sNormalsTexGiParam;
+
+
+static CGparameter sOldModelViewProjMatrixGiParam;
+static CGparameter sMaxDepthGiParam;
+static CGparameter sSamplesGiParam; 
+static CGparameter sOldSsaoTexGiParam;
+static CGparameter sOldIllumTexGiParam;
+static CGparameter sNoiseTexGiParam;
+static CGparameter sNoiseMultiplierGiParam;
+static CGparameter sExpFactorGiParam;
+
+
+static CGparameter sColorsTexCombinedIllumParam;
+static CGparameter sSsaoTexCombinedIllumParam;
+static CGparameter sIllumTexCombinedIllumParam;
+
+static CGparameter sColorsTexCombinedSsaoParam;
+static CGparameter sSsaoTexCombinedSsaoParam;
+
+
+////////////
+
+static CGparameter sColorsTexAntiAliasingParam;
+static CGparameter sNormalsTexAntiAliasingParam;
+
+
+static CGparameter sShadowMapParam;
+static CGparameter sPositionsTexShadowParam;  
+static CGparameter sColorsTexShadowParam;  
+static CGparameter sNormalsTexShadowParam;
+
+static CGparameter sShadowMatrixParam;
+static CGparameter sMaxDepthShadowParam;
+static CGparameter sSampleWidthParam;
+
+
+
+static GLuint noiseTex = 0;
+
+// ssao random spherical samples
+static Sample2 samples[NUM_SAMPLES];
+
+static int colorBufferIdx = 0;
+
+static void PrintGLerror(char *msg)
+{
+	GLenum errCode;
+	const GLubyte *errStr;
+	
+	if ((errCode = glGetError()) != GL_NO_ERROR) 
+	{
+		errStr = gluErrorString(errCode);
+		fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
+	}
+}
+
+
+/** Generate poisson disc distributed sample points on the unit disc
+*/
+static void GenerateSamples(int sampling)
+{
+	switch (sampling)
+	{
+	case SsaoShader::POISSON:
+		{
+			static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
+			poisson.Generate((Sample2 *)samples);
+		}
+		break;
+	case SsaoShader::GAUSS:
+		{
+			const float radius = 1.0f;
+			const float sigma = 0.25f;//ComputeSigmaFromRadius(1.0f);
+
+			static HaltonSequence halton;
+
+			for (int i = 0; i < NUM_SAMPLES; ++ i)
+			{
+				Sample2 s;
+				Vector3 input;
+
+				halton.GetNext(3, (float *)&input.x);
+
+				//GaussianOn2D(cinput, // input (RND in interval [0-1)x[0-1))
+				//	         sigma,            // standard deviation of gaussian distribution
+				//			 outputVec)   // resulting vector according to gaussian distr.
+
+				PolarGaussianOnDisk(input,  // input (RND in interval [0-1)x[0-1))
+					sigma,  // standard deviation of gaussian distribution
+					radius, // standard deviation of gaussian distribution
+					s);     // result
+				samples[i] = s;
+			}
+		}
+		break;
+	default:
+		break;
+	}
+}
+
+
+static void CreateNoiseTex2D(int w, int h)
+{
+	//GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
+	float *randomNormals = new float[w * h * 3];
+
+	for (int i = 0; i < w * h * 3; i += 3)
+	{
+		// create random samples on a circle
+		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 + 0] = cos(theta);
+		randomNormals[i + 1] = sin(theta);
+		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, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
+
+	glBindTexture(GL_TEXTURE_2D, 0);
+	glDisable(GL_TEXTURE_2D);
+
+	delete [] randomNormals;
+
+	cout << "created noise texture" << endl;
+
+	PrintGLerror("noisetexture");
+}
+
+
+SsaoShader::SsaoShader(int w, int h, Camera *cam, float scaleFactor):
+mWidth(w), mHeight(h), 
+mCamera(cam),
+mScaleFactor(scaleFactor),
+mUseTemporalCoherence(true),
+mRegenerateSamples(true),
+mSamplingMethod(POISSON),
+mShadingMethod(DEFAULT),
+mFboIndex(0)
+{
+	// create noise texture for ssao
+	CreateNoiseTex2D(w, h);
+
+	///////////
+	//-- 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);
+
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+}
+
+
+SsaoShader::~SsaoShader() 
+{
+	if (sCgSsaoProgram)	cgDestroyProgram(sCgSsaoProgram);
+	if (sCgDeferredProgram)	cgDestroyProgram(sCgDeferredProgram);
+	if (sCgSsaoProgram)	cgDestroyProgram(sCgSsaoProgram);
+	if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
+	if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
+
+	//DEL_PTR(mNewFbo);
+	//DEL_PTR(mOldFbo);
+	DEL_PTR(mFbo);
+
+	glDeleteTextures(1, &noiseTex);
+}
+
+
+void SsaoShader::SetUseTemporalCoherence(bool temporal)
+{
+	mUseTemporalCoherence = temporal;
+}
+
+
+void SsaoShader::Init(CGcontext context)
+{	
+	sCgDeferredProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/deferred.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
+								NULL);
+
+	if (sCgDeferredProgram != NULL)
+	{
+		cgGLLoadProgram(sCgDeferredProgram);
+
+		// we need size of texture for scaling
+		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
+		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
+		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 
+	}
+	else
+		cerr << "deferred program failed to load" << endl;
+
+
+	///////////////
+
+	sCgSsaoProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/ssao.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
+								NULL);
+
+	if (sCgSsaoProgram != NULL)
+	{
+		cgGLLoadProgram(sCgSsaoProgram);
+
+		// we need size of texture for scaling
+		sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions");  
+		sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors");  
+		sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals");  
+		sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
+		sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
+		
+		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
+		sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
+		sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
+
+		sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");  
+		
+		cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
+		sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
+	}
+	else
+		cerr << "ssao program failed to load" << endl;
+
+	sCgGiProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/globillum.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
+								NULL);
+
+	if (sCgGiProgram != NULL)
+	{
+		cgGLLoadProgram(sCgGiProgram);
+
+		// we need size of texture for scaling
+		sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions");  
+		sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");  
+		sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");  
+		sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
+		sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier");
+		
+		sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
+		sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
+		sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor");
+
+		sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
+		
+		sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");  
+		sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");  
+
+		cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
+	}
+	else
+		cerr << "globillum program failed to load" << endl;
+
+	sCgCombinedIllumProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/globillum.cg", 
+								RenderState::sCgFragmentProfile,
+								"combine",
+								NULL);
+
+	if (sCgCombinedIllumProgram != NULL)
+	{
+		cgGLLoadProgram(sCgCombinedIllumProgram);
+
+		sColorsTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "colors");  
+		sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
+		sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
+	}
+	else
+		cerr << "combined illum program failed to load" << endl;
+
+
+	sCgCombinedSsaoProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/ssao.cg", 
+								RenderState::sCgFragmentProfile,
+								"combine",
+								NULL);
+
+	if (sCgCombinedSsaoProgram != NULL)
+	{
+		cgGLLoadProgram(sCgCombinedSsaoProgram);
+
+		sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors");  
+		sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
+	}
+	else
+		cerr << "combied illum program failed to load" << endl;
+
+	
+	sCgAntiAliasingProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/antialiasing.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
+								NULL);
+
+	if (sCgAntiAliasingProgram != NULL)
+	{
+		cgGLLoadProgram(sCgAntiAliasingProgram);
+
+		sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");  
+		sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
+	}
+	else
+		cerr << "antialiasing program failed to load" << endl;
+
+	sCgDeferredShadowProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/deferred.cg", 
+								RenderState::sCgFragmentProfile,
+								"main_shadow",
+								NULL);
+
+	if (sCgDeferredShadowProgram != NULL)
+	{
+		cgGLLoadProgram(sCgDeferredShadowProgram);
+
+		// we need size of texture for scaling
+		sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");  
+		sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");  
+		sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
+
+		sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");  
+		sMaxDepthShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
+		sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
+		sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
+	}
+	else
+		cerr << "deferred program failed to load" << endl;
+	PrintGLerror("init");
+}
+
+
+void SsaoShader::Render(FrameBufferObject *fbo, 
+						const Matrix4x4 &oldProjViewMatrix,
+						float expFactor,
+						ShadowMap *shadowMap)
+{
+	
+	// switch roles of old and new fbo
+	// the algorihm uses two input fbos, where the one
+	// contais the color buffer from the last frame, 
+	// the other one will be written
+	//swap(mNewFbo, mOldFbo);	
+	mFboIndex = 2 - mFboIndex;
+	FrameBufferObject::Release();
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	glDisable(GL_ALPHA_TEST);
+	glDisable(GL_TEXTURE_2D);
+	glDisable(GL_LIGHTING);
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mWidth, mHeight);
+
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+	glLoadIdentity();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	glLoadIdentity();
+
+	const float offs = 0.5f;
+	glOrtho(-offs, offs, -offs, offs, 0, 1);
+
+	if (shadowMap)
+		FirstPassShadow(fbo, shadowMap);
+	else
+		FirstPass(fbo);
+	
+	switch (mShadingMethod)
+	{
+	case SSAO:
+		ComputeSsao(fbo, expFactor, oldProjViewMatrix);
+		CombineSsao(fbo);
+		break;
+	case GI:
+		ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
+		CombineIllum(fbo);
+		break;
+	default: // DEFAULT
+		// do nothing: standard deferred shading
+		break;
+	}
+
+	AntiAliasing(fbo);
+
+	glEnable(GL_LIGHTING);
+	glDisable(GL_TEXTURE_2D);
+
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPopMatrix();
+
+	glPopAttrib();
+
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+}
+
+
+void SsaoShader::ComputeSsao(FrameBufferObject *fbo, 
+							 float expFactor,
+							 const Matrix4x4 &oldProjViewMatrix
+							 )
+{
+	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
+
+	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+
+	if (1)
+	{
+		// generate mip map levels for position texture
+		glBindTexture(GL_TEXTURE_2D, positionsTex);
+		glGenerateMipmapEXT(GL_TEXTURE_2D);
+	}
+
+
+	// read the second buffer, write to the first buffer
+	//mNewFbo->Bind();
+	mFbo->Bind();
+	glDrawBuffers(1, mymrt + mFboIndex);
+
+
+	//GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
+	GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(sCgSsaoProgram);
+
+	cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
+	cgGLEnableTextureParameter(sPositionsTexParam);
+
+	cgGLSetTextureParameter(sColorsTexParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexParam);
+
+	cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
+	cgGLEnableTextureParameter(sNormalsTexParam);
+
+	cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
+	cgGLEnableTextureParameter(sNoiseTexParam);
+
+	cgGLSetTextureParameter(sOldTexParam, oldTex);
+	cgGLEnableTextureParameter(sOldTexParam);
+	
+	cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
+	
+
+	if (mUseTemporalCoherence || mRegenerateSamples)
+	{
+		mRegenerateSamples = false;
+		cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
+
+		// 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(mSamplingMethod); 
+		cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
+	}
+
+	cgGLSetParameter1f(sExpFactorParam, mUseTemporalCoherence ? expFactor : 1.0f);
+
+	Vector3 tl, tr, bl, br;
+	ComputeViewVectors(tl, tr, bl, br);
+
+	glBegin(GL_QUADS);
+
+	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
+	//const float new_offs = 0.55f;
+	const float new_offs = 0.5f;
+	
+	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
+	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
+	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
+	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexParam);
+	cgGLDisableTextureParameter(sPositionsTexParam);
+	cgGLDisableTextureParameter(sNormalsTexParam);
+	cgGLDisableTextureParameter(sNoiseTexParam);
+	cgGLDisableTextureParameter(sOldTexParam);
+
+
+	FrameBufferObject::Release();
+
+	PrintGLerror("ssao first pass");
+}
+
+
+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);
+
+	bl = Normalize(nbl - fbl);
+	br = Normalize(nbr - fbr);
+	tl = Normalize(ntl - ftl);
+	tr = Normalize(ntr - ftr);
+}
+
+
+
+static void SetVertex(float x, float y, float x_offs, float y_offs)
+{
+	glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
+	glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
+	glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
+	glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
+	glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
+
+	glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
+	glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
+
+	glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
+}
+
+
+void SsaoShader::AntiAliasing(FrameBufferObject *fbo)
+{
+	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+	
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(sCgAntiAliasingProgram);
+	
+	cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
+
+	cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
+	cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
+
+	glColor3f(1.0f, 1.0f, 1.0f);
+
+	float offs2 = 0.5f;
+
+	glBegin(GL_QUADS);
+
+	// the neighbouring texels
+	float x_offs = 1.0f / mWidth;
+	float y_offs = 1.0f / mHeight;
+
+	SetVertex(0, 0, x_offs, y_offs);
+	SetVertex(1, 0, x_offs, y_offs);
+	SetVertex(1, 1, x_offs, y_offs);
+	SetVertex(0, 1, x_offs, y_offs);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
+	cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
+
+	PrintGLerror("antialiasing");
+}
+
+
+void SsaoShader::FirstPass(FrameBufferObject *fbo)
+{
+	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+
+	fbo->Bind();
+
+	colorBufferIdx = 3;
+	glDrawBuffers(1, mymrt + colorBufferIdx);
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	cgGLBindProgram(sCgDeferredProgram);
+
+	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");
+}
+
+
+void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo, 
+								  float expFactor,
+								  const Matrix4x4 &oldProjViewMatrix
+								  )
+{
+	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
+
+	//GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
+	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+
+	if (1)
+	{
+		// generate mip map levels for position texture
+		glBindTexture(GL_TEXTURE_2D, positionsTex);
+		glGenerateMipmapEXT(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
+	//mNewFbo->Bind();
+	mFbo->Bind();
+
+	glDrawBuffers(2, mymrt + mFboIndex);
+
+	//GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
+	//GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
+	
+	GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
+	GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(sCgGiProgram);
+
+	cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
+	cgGLEnableTextureParameter(sPositionsTexGiParam);
+
+	cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexGiParam);
+
+	cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
+	cgGLEnableTextureParameter(sNormalsTexGiParam);
+
+	cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
+	cgGLEnableTextureParameter(sNoiseTexGiParam);
+
+	cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
+	cgGLEnableTextureParameter(sOldSsaoTexGiParam);
+
+	cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
+	cgGLEnableTextureParameter(sOldIllumTexGiParam);
+
+	cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
+
+
+	if (mUseTemporalCoherence || mRegenerateSamples)
+	{
+		mRegenerateSamples = false;
+		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(mSamplingMethod); 
+		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
+	}
+
+	cgGLSetParameter1f(sExpFactorParam, mUseTemporalCoherence ? expFactor : 1.0f);
+
+	Vector3 tl, tr, bl, br;
+	ComputeViewVectors(tl, tr, bl, br);
+
+	glBegin(GL_QUADS);
+
+	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
+	//const float new_offs = 0.55f;
+	const float new_offs = 0.5f;
+		
+	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
+	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
+	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
+	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexGiParam);
+	cgGLDisableTextureParameter(sPositionsTexGiParam);
+	cgGLDisableTextureParameter(sNormalsTexGiParam);
+	cgGLDisableTextureParameter(sNoiseTexGiParam);
+	cgGLDisableTextureParameter(sOldSsaoTexGiParam);
+	cgGLDisableTextureParameter(sOldIllumTexGiParam);
+
+	FrameBufferObject::Release();
+
+	PrintGLerror("globillum first pass");
+}
+
+
+void SsaoShader::CombineIllum(FrameBufferObject *fbo)
+{
+	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
+
+	//GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
+	//GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
+	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
+	GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
+
+
+	fbo->Bind();
+
+	// overwrite old color texture
+	colorBufferIdx = 0;
+	glDrawBuffers(1, mymrt + colorBufferIdx);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	cgGLBindProgram(sCgCombinedIllumProgram);
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+
+	cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
+
+	cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
+	cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
+
+	cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
+	cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
+	
+	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(sColorsTexCombinedIllumParam);
+	cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
+	cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
+
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+
+	FrameBufferObject::Release();
+
+	PrintGLerror("combine");
+}
+
+
+void SsaoShader::CombineSsao(FrameBufferObject *fbo)
+{
+	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
+	//GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
+	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
+
+	fbo->Bind();
+
+	// overwrite old color texture
+	colorBufferIdx = 0;
+	glDrawBuffers(1, mymrt + colorBufferIdx);
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	cgGLBindProgram(sCgCombinedSsaoProgram);
+
+	cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
+
+	cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
+	cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
+
+	
+	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(sColorsTexCombinedSsaoParam);
+	cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
+
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+
+	FrameBufferObject::Release();
+
+	PrintGLerror("combine ssao");
+}
+
+
+void SsaoShader::FirstPassShadow(FrameBufferObject *fbo, ShadowMap *shadowMap)
+{
+	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
+
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+	GLuint shadowTex = shadowMap->GetShadowTexture();
+
+	Matrix4x4 shadowMatrix;
+	shadowMap->GetTextureMatrix(shadowMatrix);
+
+	fbo->Bind();
+
+	colorBufferIdx = 3;
+	glDrawBuffers(1, mymrt + colorBufferIdx);
+
+	
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLBindProgram(sCgDeferredShadowProgram);
+
+	cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexShadowParam);
+
+	cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
+	cgGLEnableTextureParameter(sPositionsTexShadowParam);
+
+	cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
+	cgGLEnableTextureParameter(sNormalsTexShadowParam);
+	
+	cgGLSetTextureParameter(sShadowMapParam, shadowTex);
+	cgGLEnableTextureParameter(sShadowMapParam);
+
+	cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor);
+	cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize());
+	 
+	cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
+
+	glColor3f(1.0f, 1.0f, 1.0f);
+	
+	glBegin(GL_QUADS);
+
+	float offs2 = 0.5f;
+
+	glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
+	glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
+	glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
+	glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexShadowParam);
+	cgGLDisableTextureParameter(sPositionsTexShadowParam);
+	cgGLDisableTextureParameter(sNormalsTexShadowParam);
+	cgGLDisableTextureParameter(sShadowMapParam);
+
+	FrameBufferObject::Release();
+
+	PrintGLerror("deferred shading + shadows");
+}
+
+
+void SsaoShader::SetSamplingMethod(SAMPLING_METHOD s) 
+{ 
+	if (s != mSamplingMethod)
+	{
+		mSamplingMethod = s; 
+		mRegenerateSamples = true;
+	}
+}
+
+} // namespace
Index: P/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2894)
+++ 	(revision )
@@ -1,906 +1,0 @@
-#include "SsaoShader.h"
-#include "FrameBufferObject.h"
-#include "RenderState.h"
-#include "SampleGenerator.h"
-#include "Vector3.h"
-#include "Camera.h"
-#include "shaderenv.h"
-#include "RndGauss.h"
-#include "Halton.h"
-
-
-using namespace std;
-
-static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
-
-
-namespace CHCDemoEngine
-{
-
-static CGprogram sCgSsaoProgram = NULL;
-static CGprogram sCgGiProgram = NULL;
-
-static CGprogram sCgDeferredProgram = NULL;
-static CGprogram sCgAntiAliasingProgram = NULL;
-
-static CGparameter sColorsTexCombineParam;
-static CGparameter sSsaoTexCombineParam;
-
-static CGparameter sColorsTexDeferredParam;
-static CGparameter sPositionsTexDeferredParam;
-static CGparameter sNormalsTexDeferredParam;
-
-
-static CGprogram sCgCombinedSsaoProgram = NULL;
-static CGprogram sCgCombinedIllumProgram = NULL;
-
-///////////////////////////////////////7
-
-
-static CGparameter sColorsTexParam;
-static CGparameter sPositionsTexParam;
-static CGparameter sNormalsTexParam;
-
-static CGparameter sOldModelViewProjMatrixParam;
-static CGparameter sMaxDepthParam;
-static CGparameter sSamplesParam; 
-static CGparameter sOldTexParam;
-static CGparameter sNoiseTexParam;
-static CGparameter sNoiseMultiplierParam;
-static CGparameter sExpFactorParam;
-
-
-
-///////////////////////////////////////
-
-
-static CGparameter sColorsTexGiParam;
-static CGparameter sPositionsTexGiParam;
-static CGparameter sNormalsTexGiParam;
-
-
-static CGparameter sOldModelViewProjMatrixGiParam;
-static CGparameter sMaxDepthGiParam;
-static CGparameter sSamplesGiParam; 
-static CGparameter sOldSsaoTexGiParam;
-static CGparameter sOldIllumTexGiParam;
-static CGparameter sNoiseTexGiParam;
-static CGparameter sNoiseMultiplierGiParam;
-static CGparameter sExpFactorGiParam;
-
-
-static CGparameter sColorsTexCombinedIllumParam;
-static CGparameter sSsaoTexCombinedIllumParam;
-static CGparameter sIllumTexCombinedIllumParam;
-
-static CGparameter sColorsTexCombinedSsaoParam;
-static CGparameter sSsaoTexCombinedSsaoParam;
-
-
-////////////
-
-static CGparameter sColorsTexAntiAliasingParam;
-static CGparameter sNormalsTexAntiAliasingParam;
-
-static GLuint noiseTex = 0;
-
-// ssao random spherical samples
-static Sample2 samples[NUM_SAMPLES];
-
-
-static void PrintGLerror(char *msg)
-{
-	GLenum errCode;
-	const GLubyte *errStr;
-	
-	if ((errCode = glGetError()) != GL_NO_ERROR) 
-	{
-		errStr = gluErrorString(errCode);
-		fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
-	}
-}
-
-
-/** Generate poisson disc distributed sample points on the unit disc
-*/
-static void GenerateSamples(int sampling)
-{
-	switch (sampling)
-	{
-	case SsaoShader::POISSON:
-		{
-			static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
-			poisson.Generate((Sample2 *)samples);
-		}
-		break;
-	case SsaoShader::GAUSS:
-		{
-			const float radius = 1.0f;
-			const float sigma = 0.25f;//ComputeSigmaFromRadius(1.0f);
-
-			static HaltonSequence halton;
-
-			for (int i = 0; i < NUM_SAMPLES; ++ i)
-			{
-				Sample2 s;
-				Vector3 input;
-
-				halton.GetNext(3, (float *)&input.x);
-
-				//GaussianOn2D(cinput, // input (RND in interval [0-1)x[0-1))
-				//	         sigma,            // standard deviation of gaussian distribution
-				//			 outputVec)   // resulting vector according to gaussian distr.
-
-				PolarGaussianOnDisk(input,  // input (RND in interval [0-1)x[0-1))
-					sigma,  // standard deviation of gaussian distribution
-					radius, // standard deviation of gaussian distribution
-					s);     // result
-				samples[i] = s;
-			}
-		}
-		break;
-	default:
-		break;
-	}
-}
-
-
-static void CreateNoiseTex2D(int w, int h)
-{
-	//GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
-	float *randomNormals = new float[w * h * 3];
-
-	for (int i = 0; i < w * h * 3; i += 3)
-	{
-		// create random samples on a circle
-		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 + 0] = cos(theta);
-		randomNormals[i + 1] = sin(theta);
-		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, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
-
-	glBindTexture(GL_TEXTURE_2D, 0);
-	glDisable(GL_TEXTURE_2D);
-
-	delete [] randomNormals;
-
-	cout << "created noise texture" << endl;
-
-	PrintGLerror("noisetexture");
-}
-
-
-SsaoShader::SsaoShader(int w, int h, Camera *cam, float scaleFactor):
-mWidth(w), mHeight(h), 
-mCamera(cam),
-mScaleFactor(scaleFactor),
-mUseTemporalCoherence(true),
-mUseGlobIllum(false),
-mSampling(POISSON),
-mFboIndex(0)
-{
-	// create noise texture for ssao
-	CreateNoiseTex2D(w, h);
-
-	///////////
-	//-- 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);
-
-	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-}
-
-
-SsaoShader::~SsaoShader() 
-{
-	if (sCgSsaoProgram)	cgDestroyProgram(sCgSsaoProgram);
-	if (sCgDeferredProgram)	cgDestroyProgram(sCgDeferredProgram);
-	if (sCgSsaoProgram)	cgDestroyProgram(sCgSsaoProgram);
-	if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
-	if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
-
-	//DEL_PTR(mNewFbo);
-	//DEL_PTR(mOldFbo);
-	DEL_PTR(mFbo);
-
-	glDeleteTextures(1, &noiseTex);
-}
-
-
-void SsaoShader::SetUseGlobIllum(bool useGlobIllum)
-{
-	mUseGlobIllum = useGlobIllum;
-}
-
-
-void SsaoShader::SetUseTemporalCoherence(bool temporal)
-{
-	mUseTemporalCoherence = temporal;
-}
-
-
-void SsaoShader::Init(CGcontext context)
-{	
-	sCgDeferredProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/deferred.cg", 
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgDeferredProgram != NULL)
-	{
-		cgGLLoadProgram(sCgDeferredProgram);
-
-		// we need size of texture for scaling
-		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
-		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
-		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 
-	}
-	else
-		cerr << "deferred program failed to load" << endl;
-
-
-	///////////////
-
-	sCgSsaoProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/ssao.cg", 
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgSsaoProgram != NULL)
-	{
-		cgGLLoadProgram(sCgSsaoProgram);
-
-		// we need size of texture for scaling
-		sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions");  
-		sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors");  
-		sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals");  
-		sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
-		sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
-		
-		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
-		sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
-		sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
-
-		sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");  
-		
-		cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
-
-		// generate samples for ssao kernel
-		//GenerateSamples(mSampling); 
-
-		sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
-		//cgSetArraySize(sSamplesParam, NUM_SAMPLES);
-		//cgCompileProgram(sCgSsaoProgram);
-		
-		//cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
-	}
-	else
-		cerr << "ssao program failed to load" << endl;
-
-	sCgGiProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/globillum.cg", 
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgGiProgram != NULL)
-	{
-		cgGLLoadProgram(sCgGiProgram);
-
-		// we need size of texture for scaling
-		sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions");  
-		sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");  
-		sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");  
-		sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
-		sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier");
-		
-		sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
-		sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
-		sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor");
-
-		sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
-		
-		sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");  
-		sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");  
-
-		// generate samples for ssao kernel
-		//GenerateSamples(); 
-		//cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
-
-		cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
-	}
-	else
-		cerr << "globillum program failed to load" << endl;
-
-	sCgCombinedIllumProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/globillum.cg", 
-								RenderState::sCgFragmentProfile,
-								"combine",
-								NULL);
-
-	if (sCgCombinedIllumProgram != NULL)
-	{
-		cgGLLoadProgram(sCgCombinedIllumProgram);
-
-		sColorsTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "colors");  
-		sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
-		sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
-	}
-	else
-		cerr << "combined illum program failed to load" << endl;
-
-
-	sCgCombinedSsaoProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/ssao.cg", 
-								RenderState::sCgFragmentProfile,
-								"combine",
-								NULL);
-
-	if (sCgCombinedSsaoProgram != NULL)
-	{
-		cgGLLoadProgram(sCgCombinedSsaoProgram);
-
-		sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors");  
-		sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
-	}
-	else
-		cerr << "combied illum program failed to load" << endl;
-
-	
-	sCgAntiAliasingProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/antialiasing.cg", 
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgAntiAliasingProgram != NULL)
-	{
-		cgGLLoadProgram(sCgAntiAliasingProgram);
-
-		sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");  
-		sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
-	}
-	else
-		cerr << "antialiasing program failed to load" << endl;
-
-	PrintGLerror("init");
-}
-
-
-void SsaoShader::Render(FrameBufferObject *fbo, 
-						const Matrix4x4 &oldProjViewMatrix,
-						float expFactor)
-{
-	
-	// switch roles of old and new fbo
-	// the algorihm uses two input fbos, where the one
-	// contais the color buffer from the last frame, 
-	// the other one will be written
-	//swap(mNewFbo, mOldFbo);	
-	mFboIndex = 2 - mFboIndex;
-	FrameBufferObject::Release();
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
-	glDisable(GL_ALPHA_TEST);
-	glDisable(GL_TEXTURE_2D);
-	glDisable(GL_LIGHTING);
-
-	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mWidth, mHeight);
-
-	glMatrixMode(GL_PROJECTION);
-	glPushMatrix();
-	glLoadIdentity();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPushMatrix();
-	glLoadIdentity();
-
-	const float offs = 0.5f;
-	glOrtho(-offs, offs, -offs, offs, 0, 1);
-
-	FirstPass(fbo);
-	
-	if (!mUseGlobIllum)
-	{
-		ComputeSsao(fbo, expFactor, oldProjViewMatrix);
-		CombineSsao(fbo);
-	}
-	else
-	{
-		ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
-		CombineIllum(fbo);
-	}
-
-	AntiAliasing(fbo);
-
-	glEnable(GL_LIGHTING);
-	glDisable(GL_TEXTURE_2D);
-
-	glMatrixMode(GL_PROJECTION);
-	glPopMatrix();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPopMatrix();
-
-	glPopAttrib();
-
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-}
-
-
-void SsaoShader::ComputeSsao(FrameBufferObject *fbo, 
-							 float expFactor,
-							 const Matrix4x4 &oldProjViewMatrix
-							 )
-{
-	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
-
-	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
-	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
-
-	if (1)
-	{
-		// generate mip map levels for position texture
-		glBindTexture(GL_TEXTURE_2D, positionsTex);
-		glGenerateMipmapEXT(GL_TEXTURE_2D);
-	}
-
-
-	// read the second buffer, write to the first buffer
-	//mNewFbo->Bind();
-	mFbo->Bind();
-	glDrawBuffers(1, mymrt + mFboIndex);
-
-
-	//GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
-	GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgSsaoProgram);
-
-	cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
-	cgGLEnableTextureParameter(sPositionsTexParam);
-
-	cgGLSetTextureParameter(sColorsTexParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexParam);
-
-	cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
-	cgGLEnableTextureParameter(sNormalsTexParam);
-
-	cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
-	cgGLEnableTextureParameter(sNoiseTexParam);
-
-	cgGLSetTextureParameter(sOldTexParam, oldTex);
-	cgGLEnableTextureParameter(sOldTexParam);
-	
-	cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
-	
-
-	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(mSampling); 
-		cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
-	}
-	else
-	{
-			cgGLSetParameter1f(sExpFactorParam, 1.0f);
-	}
-
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
-
-	glBegin(GL_QUADS);
-
-	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
-	//const float new_offs = 0.55f;
-	const float new_offs = 0.5f;
-	
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexParam);
-	cgGLDisableTextureParameter(sPositionsTexParam);
-	cgGLDisableTextureParameter(sNormalsTexParam);
-	cgGLDisableTextureParameter(sNoiseTexParam);
-	cgGLDisableTextureParameter(sOldTexParam);
-
-
-	FrameBufferObject::Release();
-
-	PrintGLerror("ssao first pass");
-}
-
-
-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);
-
-	bl = Normalize(nbl - fbl);
-	br = Normalize(nbr - fbr);
-	tl = Normalize(ntl - ftl);
-	tr = Normalize(ntr - ftr);
-}
-
-
-
-static void SetVertex(float x, float y, float x_offs, float y_offs)
-{
-	glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
-	glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
-	glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
-	glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
-	glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
-
-	glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
-	glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
-
-	glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
-}
-
-
-void SsaoShader::AntiAliasing(FrameBufferObject *fbo)
-{
-	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
-	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
-	
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgAntiAliasingProgram);
-	
-	cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
-
-	cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
-	cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
-
-	glColor3f(1.0f, 1.0f, 1.0f);
-
-	float offs2 = 0.5f;
-
-	glBegin(GL_QUADS);
-
-	// the neighbouring texels
-	float x_offs = 1.0f / mWidth;
-	float y_offs = 1.0f / mHeight;
-
-	SetVertex(0, 0, x_offs, y_offs);
-	SetVertex(1, 0, x_offs, y_offs);
-	SetVertex(1, 1, x_offs, y_offs);
-	SetVertex(0, 1, x_offs, y_offs);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
-	cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
-
-	PrintGLerror("antialiasing");
-}
-
-
-void SsaoShader::FirstPass(FrameBufferObject *fbo)
-{
-	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
-	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
-
-	fbo->Bind();
-
-	glDrawBuffers(1, mymrt + 3);
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
-	cgGLBindProgram(sCgDeferredProgram);
-
-	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");
-}
-
-
-void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo, 
-								  float expFactor,
-								  const Matrix4x4 &oldProjViewMatrix
-								  )
-{
-	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
-
-	//GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
-	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
-	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
-
-	if (1)
-	{
-		// generate mip map levels for position texture
-		glBindTexture(GL_TEXTURE_2D, positionsTex);
-		glGenerateMipmapEXT(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
-	//mNewFbo->Bind();
-	mFbo->Bind();
-
-	glDrawBuffers(2, mymrt + mFboIndex);
-
-	//GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
-	//GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
-	
-	GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
-	GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgGiProgram);
-
-	cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
-	cgGLEnableTextureParameter(sPositionsTexGiParam);
-
-	cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexGiParam);
-
-	cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
-	cgGLEnableTextureParameter(sNormalsTexGiParam);
-
-	cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
-	cgGLEnableTextureParameter(sNoiseTexGiParam);
-
-	cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
-	cgGLEnableTextureParameter(sOldSsaoTexGiParam);
-
-	cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
-	cgGLEnableTextureParameter(sOldIllumTexGiParam);
-
-	cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
-
-
-	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(mSampling); 
-		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
-	}
-	else
-	{
-			cgGLSetParameter1f(sExpFactorGiParam, 1.0f);
-	}
-
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
-
-	glBegin(GL_QUADS);
-
-	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
-	//const float new_offs = 0.55f;
-	const float new_offs = 0.5f;
-		
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexGiParam);
-	cgGLDisableTextureParameter(sPositionsTexGiParam);
-	cgGLDisableTextureParameter(sNormalsTexGiParam);
-	cgGLDisableTextureParameter(sNoiseTexGiParam);
-	cgGLDisableTextureParameter(sOldSsaoTexGiParam);
-	cgGLDisableTextureParameter(sOldIllumTexGiParam);
-
-	FrameBufferObject::Release();
-
-	PrintGLerror("globillum first pass");
-}
-
-
-void SsaoShader::CombineIllum(FrameBufferObject *fbo)
-{
-	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
-
-	//GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
-	//GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
-	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
-	GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
-
-
-	fbo->Bind();
-
-	// overwrite old color texture
-	glDrawBuffers(1, mymrt);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
-	cgGLBindProgram(sCgCombinedIllumProgram);
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	
-
-	cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
-
-	cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
-	cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
-
-	cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
-	cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
-	
-	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(sColorsTexCombinedIllumParam);
-	cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
-	cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
-
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-
-	FrameBufferObject::Release();
-
-	PrintGLerror("combine");
-}
-
-
-void SsaoShader::CombineSsao(FrameBufferObject *fbo)
-{
-	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
-	//GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
-	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
-
-	fbo->Bind();
-
-	// write into old color texture (not needed anymore)
-	glDrawBuffers(1, mymrt);
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
-	cgGLBindProgram(sCgCombinedSsaoProgram);
-
-	cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
-
-	cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
-	cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
-
-	
-	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(sColorsTexCombinedSsaoParam);
-	cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
-
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-
-	FrameBufferObject::Release();
-
-	PrintGLerror("combine ssao");
-}
-
-
-
-} // namespace
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2887)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2895)
@@ -216,12 +216,4 @@
 			</File>
 			<File
-				RelativePath=".\src\DeferredShader.cpp"
-				>
-			</File>
-			<File
-				RelativePath=".\src\DeferredShader.h"
-				>
-			</File>
-			<File
 				RelativePath=".\src\Environment.cpp"
 				>
@@ -321,12 +313,4 @@
 			<File
 				RelativePath=".\src\ShadowMapping.h"
-				>
-			</File>
-			<File
-				RelativePath=".\src\SsaoShader.cpp"
-				>
-			</File>
-			<File
-				RelativePath=".\src\SsaoShader.h"
 				>
 			</File>
@@ -544,4 +528,8 @@
 				<File
 					RelativePath=".\src\Camera.h"
+					>
+				</File>
+				<File
+					RelativePath=".\src\DeferredRenderer.cpp"
 					>
 				</File>
