Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2938)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2939)
@@ -60,4 +60,7 @@
 	}
 
+	// first create near plane because of precision issues
+	clipPlanes.push_back(frustum.mClipPlanes[4]);
+
 	clipPlanes.push_back(frustum.mClipPlanes[0]);
 	clipPlanes.push_back(frustum.mClipPlanes[1]);
@@ -65,6 +68,4 @@
 	clipPlanes.push_back(frustum.mClipPlanes[3]);
 	clipPlanes.push_back(frustum.mClipPlanes[5]);
-
-	clipPlanes.push_back(frustum.mClipPlanes[4]);
 
 	return Polyhedron::CreatePolyhedron(clipPlanes, sceneBox);
@@ -226,5 +227,5 @@
 										 )
 {
-	//return IdentityMatrix();
+	return IdentityMatrix();
 
 	AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
@@ -235,6 +236,6 @@
 
 	//const float n = 1e2f;
-	const float n = 1e2f;
-	//const float n = ComputeN(bounds_ls) * 100;
+	const float n = 1e6f;
+	//const float n = ComputeN(bounds_ls);
 
 	cout << "n: " << n << endl;
@@ -245,5 +246,5 @@
 	const Vector3 lsNear = lightSpace * nearPt;
 
-	//c start has the x and y coordinate of e, the z coord of the near plane of the light volume
+	// the start point has the x and y coordinate of e, the z coord of the near plane of the light volume
 	const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
 	//const Vector3 schas = lightSpace * mCamera->GetPosition();
@@ -278,5 +279,5 @@
 	Matrix4x4 matLispSM;
 	
-	matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d * 1e10f);
+	matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
 
 	//cout << "lispsm\n" << matLispSM << endl;
@@ -405,5 +406,6 @@
 
 	VertexArray frustumPoints;
-	IncludeLightVolume(*polyhedron, frustumPoints, mShadowCam->GetDirection(), mSceneBox);
+	//Vector3 lightDir = Vector3(0, 0, -1);
+	IncludeLightVolume(*polyhedron, frustumPoints, mLight->GetDirection(), mSceneBox);
 
 
@@ -445,9 +447,4 @@
 	lightProj *= frame;
 
-	cout << "here9\n" << lightProj << endl;
-
-	//Matrix4x4 refl2 = ScaleMatrix(-1, -1, 1); 
-	//lightProj = refl2 * lightProj;
-
 	const Matrix4x4 matLispSM = 
 		CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
@@ -464,20 +461,21 @@
 
 	lightProj *= transformToGL;
-	//cout << "here4 \n" << lightProj << endl;
 
 	AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
 
-	//cout << "max2: " << lightPts.Max() << endl;
-	//cout << "min2: " << lightPts.Min() << endl;
-
-	AxisAlignedBox3 dummyBox = GetExtremalPoints(IdentityMatrix(), frustumPoints);
-
+	cout << "max: " << lightPts.Max() << " min: " << lightPts.Min() << endl;
 	// focus projection matrix on the extremal points => scale to unit cube
-	//Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(extremalPoints);
 	Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(lightPts);
-	lightProj = scaleTranslate * lightProj;
-
-	cout << "max: " << lightView * lightProj * dummyBox.Max() << endl;
-	cout << "min: " << lightView * lightProj * dummyBox.Min() << endl;
+
+	cout << "scaleTranslate:\n" << scaleTranslate << endl;
+
+	lightProj = lightProj * scaleTranslate;
+
+	Matrix4x4 mymat = lightView * lightProj;
+
+	AxisAlignedBox3 lightPtsNew = GetExtremalPoints(mymat, frustumPoints);
+
+	cout << "newmax: " << lightPtsNew.Max() << endl;
+	cout << "newmin: " << lightPtsNew.Min() << endl;
 
 	// we have to flip the signs in order to tranform to opengl right handed system
@@ -554,5 +552,5 @@
 		frustumPoints.push_back(v);
 		
-		// hack: get point surely outside of box
+		// hack: start at point which is guaranteed to be outside of box
 		v -= Magnitude(mSceneBox.Diagonal()) * lightDir;
 
@@ -575,6 +573,6 @@
 	const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
 	
-	//const Vector3 dir = mLight->GetDirection();
-	const Vector3 dir(0, 0, -1);
+	const Vector3 dir = mLight->GetDirection();
+	//const Vector3 dir(0, 0, -1);
 
 	// set position so that we can see the whole scene
@@ -627,5 +625,5 @@
 
 	mLightProjView = lightView * lightProj;
-;
+
 	cout << "here3" << endl;
 	DEL_PTR(lightPoly);
@@ -690,7 +688,8 @@
 void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView)
 {
-	//const Vector3 dir = mLight->GetDirection();
-	const Vector3 dir(0, 0, -1);
-
+	const Vector3 dir = mLight->GetDirection();
+	//const Vector3 dir(0, 0, -1);
+
+	cout << "dir: " << dir << endl;
 	mShadowCam->SetDirection(dir);
 
@@ -735,4 +734,8 @@
 	lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
 
+	Frustum frustum(mLightProjView);
+	cout << "near: " << Normalize(frustum.mClipPlanes[4].mNormal) << endl;
+	cout << "far: " << Normalize(frustum.mClipPlanes[5].mNormal) << endl;
+
 	glMatrixMode(GL_MODELVIEW);
 	glPushMatrix();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2936)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2939)
@@ -70,5 +70,5 @@
 int renderMode = RenderTraverser::CHCPLUSPLUS;
 // eye near plane distance
-float nearDist = 0.1f;
+float nearDist = 0.5f;
 float farDist = 1e6f;
 /// the field of view
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2936)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2939)
@@ -569,5 +569,7 @@
 	ret.y = v.x * M.x[0][1] + v.y * M.x[1][1] + v.z * M.x[2][1] + M.x[3][1];
 	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 = M.x[0][3] + M.x[1][3] + M.x[2][3] + M.x[3][3];
+	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)
 		ret /= denom;
@@ -677,4 +679,34 @@
 	return m;
 }
+/*
+
+template<class REAL>
+Matrix4<REAL>& scaleTranslateToFit(Matrix4<REAL>& output, const Vector3<REAL>& vMin, const Vector3<REAL>& vMax) {
+	const REAL diffX = vMax.x()-vMin.x();
+	output[ 0] = 2/diffX;
+	output[ 4] = 0;
+	output[ 8] = 0;
+	output[12] = -(vMax.x()+vMin.x())/diffX;
+
+	const REAL diffY = vMax.y()-vMin.y();
+	output[ 1] = 0;
+	output[ 5] = 2/diffY;
+	output[ 9] = 0;
+	output[13] = -(vMax.y()+vMin.y())/diffY;
+
+	const REAL diffZ = vMax.z()-vMin.z();
+	output[ 2] = 0;
+	output[ 6] = 0;
+	output[10] = 2/diffZ;
+	output[14] = -(vMax.z()+vMin.z())/diffZ;
+
+	output[ 3] = 0;
+	output[ 7] = 0;
+	output[11] = 0;
+	output[15] = 1;
+	return output;
+}
+*/
+
 
 
