Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h	(revision 2916)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h	(revision 2920)
@@ -81,4 +81,5 @@
 	}
  
+	
 	/** Returns the axis, where the std::vector has the largest value.
 	*/
@@ -142,4 +143,9 @@
 	Vector3& operator/= (float A);
 
+	static inline Vector3 UNIT_X() { return Vector3(1, 0, 0); }
+	static inline Vector3 UNIT_Y() { return Vector3(0, 1, 0); }
+	static inline Vector3 UNIT_Z() { return Vector3(0, 0, 1); }
+	static inline Vector3 ZERO() { return Vector3(0, 0, 0); }
+
 
 	//////////
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2917)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2920)
@@ -65,5 +65,8 @@
 	
 	bool CalcLightProjection(Matrix4x4 &lightProj);
-	Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView, const AxisAlignedBox3 &bounds);
+
+	Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView, 
+		                          const AxisAlignedBox3 &bounds, 
+								  const VertexArray &pts);
 
 	void IncludeLightVolume(const Polyhedron &polyhedron, 
@@ -72,5 +75,11 @@
 							const AxisAlignedBox3 &sceneBox);
 
-	float ComputeN() const;
+	float ComputeN(const AxisAlignedBox3 &extremalPoints) const;
+
+	Vector3 GetNearCameraPointE(const VertexArray &pts) const;
+
+	Vector3 GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const;
+
+
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2919)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2920)
@@ -166,10 +166,9 @@
 
 
-float ShadowMap::ComputeN() const 
-{
-	const float n = mShadowCam->GetNear();
-	const float f = mShadowCam->GetFar();
-
-	const float d = fabs(f - n);
+float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const 
+{
+	const float n = mCamera->GetNear();
+	
+	const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 
 	
 	const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection());
@@ -180,16 +179,19 @@
 
 
-Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView, const AxisAlignedBox3 &extremalPoints)
-{
-	Matrix4x4 lispMtx;
+Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView, 
+										 const AxisAlignedBox3 &extremalPoints,
+										 const VertexArray &pts
+										 )
+{
+	Matrix4x4 matLispSM;
 
 	///////////////
 	//-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix
 
-	const float n = 1e20f;//ComputeN();
+	const float n = 10;//ComputeN(extremalPoints);
 
 	cout << "n: " << n << endl;
 
-	const Vector3 nearPt = mShadowCam->GetNear() * mShadowCam->GetDirection() + mShadowCam->GetPosition();
+	const Vector3 nearPt = GetNearCameraPointE(pts);
 
 	//get the coordinates of the near camera point in light space
@@ -197,9 +199,8 @@
 
 	//c start has the x and y coordinate of e, the z coord of B.min()
-	const Vector3 startPt = Vector3(lsNear.x, lsNear.y, extremalPoints.Min().y);
+	const Vector3 startPt = Vector3::ZERO();//Vector3(lsNear.x, lsNear.y, extremalPoints.Max().z);
 
 	// the new projection center
-	const Vector3 unit_y = Vector3(0, 1, 0);
-	Vector3	projCenter = startPt - unit_y * n;
+	Vector3	projCenter = startPt + Vector3::UNIT_Z() * n;
 
 	//construct a translation that moves to the projection center
@@ -208,33 +209,91 @@
 	// light space y size
 	const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 
-
-	lispMtx = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
-
-	cout << "lispsm\n" << lispMtx << endl;
-
-	lispMtx *= projectionCenter;
+	cout << "d: " << d << endl;
+	matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
+
+	cout << "lispsm\n" << matLispSM << endl;
+
+	matLispSM = projectionCenter * matLispSM;
+
+	cout << "center\n" << projectionCenter << endl;
+	cout << "new\n" << matLispSM << endl;
 
 	// transform into OpenGL right handed system
-	Matrix4x4 scale = ScaleMatrix(1.0f, 1.0f, -1.0f);
-
-	lispMtx *= scale;
-	
-	return IdentityMatrix();
-}
-
-
-Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace) const 
+	Matrix4x4 refl = ScaleMatrix(1.0f, 1.0f, -1.0f);
+	matLispSM *= refl;
+	//matLispSM = IdentityMatrix();
+
+	return matLispSM;
+}
+
+
+Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
+{
+	Vector3 nearest;
+	float minDist = 1e20f;
+
+	Vector3 camPos = mCamera->GetPosition();
+
+	/*Matrix4x4 inverseCamView;
+	mCamera->GetModelViewMatrix(invervseCamView);
+	inverseCamView.Invert();*/
+
+	VertexArray::const_iterator it, it_end = pts.end();
+
+	for (it = pts.begin(); it != it_end; ++ it)
+	{
+		Vector3 pt = *it;
+
+		const float dist = SqrDistance(pt, camPos);
+
+		if (dist < minDist)
+		{
+			minDist = dist;
+			nearest = pt;
+		}
+	}
+
+	return nearest;
+}
+
+
+Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const 
 {
 	//get the point in the LVS volume that is nearest to the camera
-	const V3 e = getNearCameraPointE(fs);
+	const Vector3 e = GetNearCameraPointE(pts);
+
 	//construct edge to transform into light-space
-	const V3 b = e+fs.getEyeDir();
+	const Vector3 b = e + mCamera->GetDirection();
 	//transform to light-space
-	const V3 e_lp = lightSpace.mulHomogenPoint(e);
-	const V3 b_lp = lightSpace.mulHomogenPoint(b);
-	V3 projDir(b_lp-e_lp);
+	const Vector3 e_lp = lightSpace * e;
+	const Vector3 b_lp = lightSpace * b;
+
+	Vector3 projDir(b_lp - e_lp);
+
 	//project the view direction into the shadow map plane
-	projDir.y() = 0.0;
-	return projDir;
+	projDir.y = 0.0;
+	//projDir.z = 0.0;
+
+	return Normalize(projDir);
+}
+
+
+static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &lightView,
+										 const VertexArray &pts)
+{
+	AxisAlignedBox3 extremalPoints;
+	extremalPoints.Initialize();
+
+	VertexArray::const_iterator it, it_end = pts.end();
+		
+	for (it = pts.begin(); it != it_end; ++ it)
+	{
+		Vector3 pt = *it;
+		pt = lightView * pt;
+
+		extremalPoints.Include(pt);
+	}
+
+	return extremalPoints;
 }
 
@@ -262,44 +321,43 @@
 	//-- transform points from world view to light view and calculate extremal points
 
-	AxisAlignedBox3 extremalPoints;
-	extremalPoints.Initialize();
-
 	Matrix4x4 lightView;
 	mShadowCam->GetModelViewMatrix(lightView);
 
-	VertexArray::const_iterator it, it_end = frustumPoints.end();
-		
-	for (it = frustumPoints.begin(); it != it_end; ++ it)
-	{
-		Vector3 pt = *it;
-		pt = lightView * pt;
-
-		extremalPoints.Include(pt);
-	}
-
-	lightProj = IdentityMatrix(); // we use directional lights
-
-	Matrix4x4 shadowView;
-	mShadowCam->GetModelViewMatrix(shadowView);
-
-	Vector3 projViewDir = 
+	const AxisAlignedBox3 extremalPoints = GetExtremalPoints(lightView, frustumPoints);
+
+	// we use directional lights, so the projection can be set to identity
+	lightProj = IdentityMatrix(); 
+
+	const Vector3 projViewDir = GetProjViewDir(lightProj, frustumPoints);
+
+	cout << "projViewDir: " << projViewDir << endl;
+
 	//do Light Space Perspective shadow mapping
 	//rotate the lightspace so that the proj light view always points upwards
 	//calculate a frame matrix that uses the projViewDir[light-space] as up vector
 	//look(from position, into the direction of the projected direction, with unchanged up-vector)
-	Matrix4x4 frame = LookAt(Vector3(0), projViewDir, Vector3(0, 0, 1));
-	lightProj *= frame;
+	Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
+	//lightProj = frame * lightProj;
 
 	const Matrix4x4 matLispSM = 
-		CalcLispSMTransform(shadowView * lightProj, extremalPoints);
-
-	lightProj *= matLispSM;
+		CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
+
+	lightProj = matLispSM * lightProj;
+
+	AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
+
+	//cout << "max: " << lightPts.Max() << endl;
+	//cout << "min: " << lightPts.Min() << endl;
 
 	// focus projection matrix on the extremal points => scale to unit cube
-	lightProj *= GetFittingProjectionMatrix(extremalPoints);
-
-	// we have to flip the signs in order tp opengl style projection matrix
-	Matrix4x4 scale = ScaleMatrix(-1, -1, -1); 
-	lightProj *= scale;
+	lightProj *= GetFittingProjectionMatrix(lightPts);
+	//lightProj *= GetFittingProjectionMatrix(extremalPoints);
+
+	cout << "max: " << lightProj * lightPts.Max() << endl;
+	cout << "min: " << lightProj * lightPts.Min() << endl;
+
+	// we have to flip the signs in order to tranform to opengl right handed system
+	Matrix4x4 refl = ScaleMatrix(1, 1, -1); 
+	lightProj *= refl;
 	
 	return true;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2917)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2920)
@@ -680,5 +680,7 @@
 
 //output is initialized with the same result as glFrustum
-Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far)
+Matrix4x4 GetFrustum(float left, float right, 
+					 float bottom, float top, 
+					 float near, float far)
 {
 	Matrix4x4 m;
