Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h	(revision 2942)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h	(revision 2944)
@@ -52,4 +52,9 @@
 
 	float Det3x3() const;
+
+	/** multiply with homogen vector form, where the 4 coordinat is in h, 
+		the resulting homogen coordinate returned in w
+	*/
+	Vector3 Transform(float &w, const Vector3 &v, float h) const;
 
 
@@ -160,5 +165,4 @@
 Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far);
 Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up);
-Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up);
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.cpp	(revision 2913)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.cpp	(revision 2944)
@@ -9,5 +9,6 @@
 {
     Vector3 v1 = a - b, v2 = c - b;
-    mNormal = Normalize(CrossProd(v2, v1));
+
+	mNormal = Normalize(CrossProd(v2, v1));
     mD = -DotProd(b, mNormal);
 }
@@ -17,4 +18,5 @@
 mNormal(normal)
 {
+	//mNormal = Normalize(normal)
     mD = -DotProd(normal, point);
 }
@@ -29,5 +31,5 @@
 		    sy(a.mNormal.y, b.mNormal.y, c.mNormal.y),
 		    sz(a.mNormal.z, b.mNormal.z, c.mNormal.z),
-		    sd(a.mD, b.mD, c.mD);
+		    sd(a.mD,        b.mD,        c.mD);
 
 	Matrix4x4 md(a.mNormal, b.mNormal, c.mNormal), mx, my, mz;
@@ -42,7 +44,5 @@
 		return false;
 
-	result.SetValue(mx.Det3x3()/det,
-		my.Det3x3()/det,
-		mz.Det3x3()/det);
+	result.SetValue(mx.Det3x3() / det, my.Det3x3() / det, mz.Det3x3() / det);
 
 	return true;
@@ -107,3 +107,31 @@
 
 
+void Plane3::Transform(const Matrix4x4 &m)
+{
+	float w;
+
+	const Matrix4x4 invTranspose = Transpose(Invert(m));
+
+	const Vector3 n = invTranspose.Transform(w, mNormal, -mD);
+
+	const float mag = Magnitude(n);
+
+	mNormal = n / mag;
+	// our d is -d of homogen equation
+	mD = -w / mag;
+
+	// find point on plane
+	/*const Vector3 pt = mNormal * -mD;
+
+	// transform point
+	const Vector3 newPt = m * pt;
+	// transform normal
+	const Vector3 newNormal = TransformNormal(m, newPt);
+
+	const float mag = Magnitude(newNormal);
+
+	return Plane3(newNormal / mag,  newPt / mag);*/
 }
+
+
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2943)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2944)
@@ -13,4 +13,16 @@
 };
 
+#define NUM_SAMPLES 16
+
+
+float2 myreflect(float2 pt, float2 n)
+{
+	// distance to plane
+	float d = dot(n, pt);
+	// reflect around plane
+	float2 rpt = pt - d * 2.0f * n;
+
+	return rpt;
+}
 
 
@@ -73,4 +85,48 @@
 }
 
+
+float CalcShadowTerm(fragment IN,
+					 uniform sampler2D shadowMap,
+					 uniform float w,
+					 uniform float2 lightSpacePos,
+					 float depth,
+					 uniform float2 samples[NUM_SAMPLES],
+					 uniform sampler2D noiseTexture
+					 )
+{
+	float s;
+
+	float total_d = 0.0;
+	//float total_d = tex2D(shadowMap, lightSpacePos).x;
+
+	for (int i = 0; i < NUM_SAMPLES; ++ i) 
+	{
+		const float2 offset = samples[i];
+
+#if 1
+		////////////////////
+		//-- add random noise: reflect around random normal vector (warning: slow!)
+
+		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
+		const float2 offsetTransformed = myreflect(offset, mynoise);
+#else
+		const float2 offsetTransformed = offset;
+#endif
+		// weight with projected coordinate to reach similar kernel size for near and far
+		float2 texcoord = lightSpacePos + offsetTransformed * w;
+
+		float shadowDepth = tex2D(shadowMap, texcoord).x;
+
+		total_d += step(shadowDepth, depth);
+		//total_d += length(offsetTransformed);
+	}
+
+	total_d /= (float)NUM_SAMPLES;
+
+	return total_d;
+}
+
+
+#if 0
 /** The mrt shader for standard rendering
 */
@@ -114,12 +170,12 @@
 	
 	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;
+	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;
 
 	
@@ -156,22 +212,53 @@
 	return OUT;
 }
-
-
-/** The mrt shader for standard rendering
-*/
-pixel main_shadow_debug(fragment IN, 
-						uniform sampler2D shadowMap,
-						uniform sampler2D colorTex,
-						uniform float4x4 shadowMatrix)
+#else
+
+pixel main_shadow(fragment IN, 
+				  uniform sampler2D colors,
+				  uniform sampler2D positions,
+				  uniform sampler2D normals,		   
+				  uniform sampler2D shadowMap,
+				  uniform float4x4 shadowMatrix,
+				  uniform float maxDepth,
+				  uniform float sampleWidth,
+				  uniform sampler2D noiseTexture,
+				  uniform float2 samples[NUM_SAMPLES]
+				  )
 {
 	pixel OUT;
-#if 0
-	// visualize depth
-	OUT.color = tex2Dlod(colorTex, float4(IN.texCoord.xy, 0, 0));
-#else
-	float4 col;
-	col = tex2Dlod(shadowMap, float4(IN.texCoord.xy, 0, 0));
-	OUT.color = float4(col.z / col.w, 0, 0, 1);
+
+	float4 norm = tex2D(normals, IN.texCoord.xy);
+	float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
+	float4 position = tex2D(positions, IN.texCoord.xy);
+
+
+	// an ambient color term
+	float amb = norm.w;
+
+	float3 normal = normalize(norm.xyz);
+	float4 col = shade(IN, color, position, normal, amb);
+	
+	position *= maxDepth;
+	position.w = 1.0f;
+	
+	float4 lightSpacePos = mul(shadowMatrix, position);
+	lightSpacePos /= lightSpacePos.w;
+
+	OUT.color = col;
+
+	float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
+
+	if (amb < 0.9f) // hack: prevent shadowing the sky	
+	{
+		// base lighting
+		const float baseLighting = 0.3f;
+		OUT.color *= baseLighting + (1.0f - baseLighting) * (1.0f - shadowTerm);
+	}
+	
+	// also write out depth component
+	OUT.color.w = color.w;
+
+	return OUT;
+}
+
 #endif
-	return OUT;
-}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 2943)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 2944)
@@ -71,5 +71,5 @@
 		if (!newPolyhedron)
 		{
-			cerr << "polyhedron not valid or NULL!" << endl; 
+			//cerr << "polyhedron not valid or NULL!" << endl; 
 			return NULL;	
 		}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.h	(revision 2911)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.h	(revision 2944)
@@ -8,5 +8,5 @@
 {
 
-/** Class describing a 3D Plane.
+/** Class describing a plane in 3D space.
 */
 class Plane3 
@@ -17,5 +17,6 @@
 
 	Plane3(const Vector3 &a, const Vector3 &b, const Vector3 &c);
-
+	/** Note: we assume that the normal vector is already normalized.
+	*/
 	Plane3(const Vector3 &normal, const Vector3 &point);
 
@@ -66,4 +67,8 @@
 		return *(&mNormal.x + inx);
 	}
+	/** Transforms the plane  with the matrix m.
+	*/
+	void Transform(const Matrix4x4 &m);
+
 
 	///////////
@@ -82,4 +87,6 @@
 
 
+//protected:
+
 	///////////////
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2943)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2944)
@@ -83,4 +83,8 @@
 	float ComputeN(const AxisAlignedBox3 &extremalPoints) const;
 
+	float ComputeNOpt(const Matrix4x4 &lightSpace, 
+		              const AxisAlignedBox3 &extremalPoints,
+					  const VertexArray &body) const;
+
 	Vector3 GetNearCameraPointE(const VertexArray &pts) const;
 
@@ -91,4 +95,11 @@
 	*/
 	void _Render(RenderTraverser *renderer);
+
+	Vector3 GetLightSpaceZ0(const Matrix4x4 &lightSpace, 
+		                    const Vector3 &e, 
+							const float maxZ, 
+							const Vector3 &eyeDir) const;
+
+
 
 	//////////////
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2930)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2944)
@@ -77,11 +77,8 @@
 
 	void AntiAliasing(FrameBufferObject *fbo);
-
 	/** Helper method that computes the view vectors in the corners of the current view frustum.
 	*/
 	void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br);
 
-
-	void ShadowDebug(FrameBufferObject *fbo, ShadowMap *shadowMap);
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2943)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2944)
@@ -187,7 +187,75 @@
 
 
+// z0 is the point that lies on the parallel plane to the near plane through e (A)
+//and on the near plane of the C frustum (the plane z = bZmax) and on the line x = e.x
+Vector3 ShadowMap::GetLightSpaceZ0(const Matrix4x4 &lightSpace, 
+								   const Vector3 &e, 
+								   const float maxZ, 
+								   const Vector3 &eyeDir) const 
+{
+	// to calculate the parallel plane to the near plane through e we 
+	// calculate the plane A with the three points
+	Plane3 planeA(e, eyeDir);
+
+	planeA.Transform(lightSpace);
+	
+	// get the parameters of A from the plane equation n dot d = 0
+	const float d = planeA.mD;
+	const Vector3 n = planeA.mNormal;
+	
+	// transform to light space
+	const Vector3 e_ls = lightSpace * e;
+
+	Vector3 z0;
+
+	z0.x = e_ls.x;
+	z0.y = (d - n.z * maxZ - n.x * e_ls.x) / n.y;
+	z0.z = maxZ;
+
+	return z0;
+	//return V3(e_ls.x(),(d-n.z()*b_lsZmax-n.x()*e_ls.x())/n.y(),b_lsZmax);
+}
+
+
+float ShadowMap::ComputeNOpt(const Matrix4x4 &lightSpace, 
+							 const AxisAlignedBox3 &extremalPoints,
+							 const VertexArray &body) const
+{
+	const Vector3 nearPt = GetNearCameraPointE(body);
+	const Vector3 eyeDir = mCamera->GetDirection();
+
+	Matrix4x4 eyeView;
+	mCamera->GetModelViewMatrix(eyeView);
+
+	const Matrix4x4 invLightSpace = Invert(lightSpace);
+
+	const Vector3 z0_ls = GetLightSpaceZ0(lightSpace, nearPt, extremalPoints.Max().z, eyeDir);
+	const Vector3 z1_ls = Vector3(z0_ls.x, z0_ls.y, extremalPoints.Min().z);
+	
+	// transform back to world space
+	const Vector3 z0_ws = invLightSpace * z0_ls;
+	const Vector3 z1_ws = invLightSpace * z1_ls;
+
+	// transform to eye space
+	const Vector3 z0_es = eyeView * z0_ws;
+	const Vector3 z1_es = eyeView * z1_ws;
+
+	const float z0 = z0_es.z;
+	const float z1 = z1_es.z;
+
+	cout << "z0 ls: " << z0_ls << " z1 ls: " << z1_ls << endl;
+	cout << "z0: " << z0_es << " z1: " << z1_es << endl;
+
+	const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 
+
+	const float n = d / (sqrt(z1 / z0) - 1.0f);
+
+	return n;
+}
+
+
 float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const 
 {
-	const float n = mCamera->GetNear();
+	const float nearPlane = mCamera->GetNear();
 	
 	const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 
@@ -196,5 +264,8 @@
 	const float sinGamma = sin(fabs(acos(dotProd)));
 
-	return (n + sqrt(n * (n + d * sinGamma))) /  sinGamma;
+	// test for values close to zero
+	if (sinGamma < 1e-6f) return 1e6f;
+	
+	return (nearPlane + sqrt(nearPlane * (nearPlane + d * sinGamma))) /  sinGamma;
 }
 
@@ -205,6 +276,4 @@
 										 )
 {
-	//return IdentityMatrix();
-
 	AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
 
@@ -213,7 +282,11 @@
 	//-- first find the free parameter values n, and P (the projection center), and the projection depth
 
-	const float n = ComputeN(bounds_ls);
-
-	//cout << "n: " << n << endl;
+	//const float n = ComputeN(bounds_ls);
+	const float n = ComputeNOpt(lightSpace, extremalPoints, body);
+
+	cout << "n: " << n << endl;
+
+	if (n >= 1e6f) // light direction nearly parallel to view => switch to uniform
+		return IdentityMatrix();
 
 	const Vector3 nearPt = GetNearCameraPointE(body);
@@ -358,5 +431,4 @@
 
 	VertexArray frustumPoints;
-	//Vector3 lightDir = Vector3(0, 0, -1);
 	IncludeLightVolume(*polyhedron, frustumPoints, mLight->GetDirection(), mSceneBox);
 
@@ -392,5 +464,5 @@
 	//look(from position, into the direction of the projected direction, with unchanged up-vector)
 	//const Matrix4x4 frame = MyLookAt2(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
-	const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
+	const Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
 
 	lightProj *= frame;
@@ -600,5 +672,5 @@
 
 	Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
-	Matrix4x4 lightView = MyLookAt(mShadowCam->GetPosition(), dir, upVec);
+	Matrix4x4 lightView = LookAt(mShadowCam->GetPosition(), dir, upVec);
 
 	mShadowCam->mViewOrientation = lightView;
@@ -606,5 +678,5 @@
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-	glPolygonOffset(1.0f, 2000.0f);
+	glPolygonOffset(5.0f, 100.0f);
 	glEnable(GL_POLYGON_OFFSET_FILL);
 	
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2943)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2944)
@@ -450,5 +450,5 @@
 	sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
 
-	//Vector3 lightDir = Normalize(Vector3(0, 1, -1));
+	//Vector3 lightDir = Normalize(Vector3(0.2, -0.8, -1));
 	Vector3 lightDir = Normalize(-Vector3(0.8f, -1.0f, 0.7f));
 	
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2942)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2944)
@@ -570,5 +570,4 @@
 	ret.z = v.x * M.x[0][2] + v.y * M.x[1][2] + v.z * M.x[2][2] + M.x[3][2];
 	denom = v.x * M.x[0][3] + v.y * M.x[1][3] + v.z * M.x[2][3] + M.x[3][3];
-	//denom = M.x[0][3] + M.x[1][3] + M.x[2][3] + M.x[3][3];
 
 	if (denom != 1.0)
@@ -576,4 +575,19 @@
 	return ret;
 }
+
+
+Vector3 Matrix4x4::Transform(float &w, const Vector3 &v, float h) const
+{
+	Vector3 ret;
+	float denom;
+
+	ret.x = v.x * x[0][0] + v.y * x[1][0] + v.z * x[2][0] + h * x[3][0];
+	ret.y = v.x * x[0][1] + v.y * x[1][1] + v.z * x[2][1] + h * x[3][1];
+	ret.z = v.x * x[0][2] + v.y * x[1][2] + v.z * x[2][2] + h * x[3][2];
+	w     = v.x * x[0][3] + v.y * x[1][3] + v.z * x[2][3] + h * x[3][3];
+
+	return ret;
+}
+
 
 // Apply the rotation portion of a matrix to a vector.
@@ -754,22 +768,4 @@
 	nUp = Normalize(CrossProd(nRight, nDir));
 
-	Matrix4x4 m(nRight, nUp, -nDir);
-
-	m.x[3][0] = -DotProd(nRight, pos);
-	m.x[3][1] = -DotProd(nUp, pos);
-	m.x[3][2] = DotProd(nDir, pos);
-
-	return m;
-}
-
-
-Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up) 
-{
-	const Vector3 nDir = Normalize(dir);
-	Vector3 nUp = Normalize(up);
-
-	Vector3 nRight = Normalize(CrossProd(nDir, nUp));
-	nUp = Normalize(CrossProd(nRight, nDir));
-
 	Matrix4x4 m(nRight.x, nRight.y, nRight.z, 0,
  		        nUp.x,    nUp.y,    nUp.z,    0,
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2932)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2944)
@@ -22,5 +22,4 @@
 static CGprogram sCgAntiAliasingProgram = NULL;
 static CGprogram sCgDeferredShadowProgram = NULL;
-static CGprogram sCgShadowDebugProgram = NULL;
 
 static CGparameter sColorsTexCombineParam;
@@ -31,13 +30,8 @@
 static CGparameter sNormalsTexDeferredParam;
 
-static CGparameter sShadowMapDebugParam;  
-static CGparameter sShadowMatrixDebugParam;
-static CGparameter sColorsTexDebugParam;
-
-
 static CGprogram sCgCombinedSsaoProgram = NULL;
 static CGprogram sCgCombinedIllumProgram = NULL;
 
-///////////////////////////////////////7
+///////////////////////////////////////
 
 
@@ -95,4 +89,8 @@
 static CGparameter sSampleWidthParam;
 
+static CGparameter sNoiseTexShadowParam;
+static CGparameter sSamplesShadowParam; 
+
+
 //#define USE_3D_SSAO
 
@@ -102,12 +100,9 @@
 // ssao random spherical samples
 #ifdef USE_3D_SSAO
-
 static Sample2 samples3[NUM_SAMPLES];
-
 #else
-
 static Sample2 samples2[NUM_SAMPLES];
-
 #endif
+Sample2 pcfSamples[16];
 
 static int colorBufferIdx = 0;
@@ -281,5 +276,5 @@
 		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
 		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
-		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 
+		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
 	}
 	else
@@ -316,5 +311,4 @@
 
 		sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");  
-		
 		sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
 	}
@@ -338,5 +332,4 @@
 		sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");  
 		sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");  
-		sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
 		
 		sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
@@ -344,4 +337,5 @@
 		sTemporalCoherenceGiParam = cgGetNamedParameter(sCgGiProgram, "temporalCoherence");
 
+		sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
 		sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
 		
@@ -430,27 +424,15 @@
 		sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
 		sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
+
+		sNoiseTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "noiseTexture");
+		sSamplesShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "samples");
+
+		PoissonDiscSampleGenerator2 poisson(16, 1.0f);
+		poisson.Generate((float *)pcfSamples);
+
+		cgGLSetParameterArray2f(sSamplesShadowParam, 0, 16, (const float *)pcfSamples);
 	}
 	else
 		cerr << "deferred program failed to load" << endl;
-
-	sCgShadowDebugProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/deferred.cg", 
-								RenderState::sCgFragmentProfile,
-								"main_shadow_debug",
-								NULL);
-
-	if (sCgShadowDebugProgram != NULL)
-	{
-		cgGLLoadProgram(sCgShadowDebugProgram);
-
-		// we need size of texture for scaling
-		sShadowMapDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMap");  
-		sShadowMatrixDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMatrix");
-		sColorsTexDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "colorTex");  
-	}
-	else
-		cerr << "shadow debug program failed to load" << endl;
 
 	PrintGLerror("init");
@@ -494,32 +476,26 @@
 	glLoadIdentity();
 
-	if (shadowMap && (mShadingMethod == GI))
-	{
-		ShadowDebug(fbo, shadowMap);
-	}
+	if (shadowMap)
+		FirstPassShadow(fbo, shadowMap);
 	else
-	{
-		if (shadowMap)
-			FirstPassShadow(fbo, shadowMap);
-		else
-			FirstPass(fbo);
-
-		switch (mShadingMethod)
-		{
-		case SSAO:
-			ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix);
-			CombineSsao(fbo);
-			break;
-		case GI:
-			ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix);
-			CombineIllum(fbo);
-			break;
-		default: // DEFAULT
-			// do nothing: standard deferred shading
-			break;
-		}
-
-		AntiAliasing(fbo);
-	}
+		FirstPass(fbo);
+
+	switch (mShadingMethod)
+	{
+	case SSAO:
+		ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix);
+		CombineSsao(fbo);
+		break;
+	case GI:
+		ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix);
+		CombineIllum(fbo);
+		break;
+	default: // DEFAULT
+		// do nothing: standard deferred shading
+		break;
+	}
+
+	AntiAliasing(fbo);
+
 	glEnable(GL_LIGHTING);
 	glDisable(GL_TEXTURE_2D);
@@ -1010,7 +986,12 @@
 
 	cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor);
-	cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize());
+	//cgGLSetParameter1f(sSampleWidthParam, 10.0f / shadowMap->GetSize());
+	cgGLSetParameter1f(sSampleWidthParam, 2.5f / shadowMap->GetSize());
 	 
 	cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
+
+	cgGLSetTextureParameter(sNoiseTexShadowParam, noiseTex);
+	cgGLEnableTextureParameter(sNoiseTexShadowParam);
+
 
 	glColor3f(1.0f, 1.0f, 1.0f);
@@ -1032,50 +1013,9 @@
 	cgGLDisableTextureParameter(sShadowMapParam);
 
+	cgGLDisableTextureParameter(sNoiseTexShadowParam);
+
 	FrameBufferObject::Release();
 
 	PrintGLerror("deferred shading + shadows");
-}
-
-
-void DeferredRenderer::ShadowDebug(FrameBufferObject *fbo, ShadowMap *shadowMap)
-{
-	GLuint colorsTex = shadowMap->GetShadowColorTexture();
-	GLuint shadowTex = shadowMap->GetDepthTexture();
-	
-
-	Matrix4x4 shadowMatrix;
-	shadowMap->GetTextureMatrix(shadowMatrix);
-
-	//glMatrixMode(GL_PROJECTION);glLoadIdentity();
-	//glViewport(0, 0, shadowMap->GetSize(), shadowMap->GetSize());
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	cgGLBindProgram(sCgShadowDebugProgram);
-
-	cgGLSetTextureParameter(sShadowMapDebugParam, shadowTex);
-	cgGLSetTextureParameter(sColorsTexDebugParam, colorsTex);
-
-	cgGLEnableTextureParameter(sShadowMapDebugParam);
-
-	cgGLSetMatrixParameterfc(sShadowMatrixDebugParam, (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(sColorsTexDebugParam);
-	cgGLDisableTextureParameter(sShadowMapDebugParam);
-
-	PrintGLerror("shadows debug");
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 2929)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 2944)
@@ -1223,7 +1223,4 @@
 {
 	Polyhedron *oldPolyhedron = new Polyhedron(polyhedron);
-
-	if (!oldPolyhedron->Valid()) cerr << "******************* not valid!! ************* " << endl;
-	
 	Polyhedron *newPolyhedron = NULL;
 
@@ -1235,6 +1232,5 @@
 		{
 			DEL_PTR(newPolyhedron);
-			cerr << "polyhedron not valid or NULL!" << endl; 
-
+			//cerr << "polyhedron not valid or NULL!" << endl; 
 			return NULL;	
 		}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2942)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2944)
@@ -57,5 +57,5 @@
 
 	//mBaseOrientation = Matrix4x4(right, up, -dir);
-	mBaseOrientation = MyLookAt(Vector3::ZERO(), dir, up);//Matrix4x4(right, up, -dir);
+	mBaseOrientation = LookAt(Vector3::ZERO(), dir, up);//Matrix4x4(right, up, -dir);
 	mViewOrientation = mBaseOrientation;
 }
@@ -108,4 +108,5 @@
 
 	frustum = Frustum(matProjectionView);
+
 
 	////////////
