Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2921)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2922)
@@ -189,5 +189,5 @@
 	//-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix
 
-	const float n = 10;//ComputeN(extremalPoints);
+	const float n = ComputeN(extremalPoints);
 
 	cout << "n: " << n << endl;
@@ -198,24 +198,34 @@
 	const Vector3 lsNear = lightProjView * nearPt;
 
-	//c start has the x and y coordinate of e, the z coord of B.min()
-	const Vector3 startPt = Vector3::ZERO();//Vector3(lsNear.x, lsNear.y, extremalPoints.Max().z);
+	//c start 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, extremalPoints.Max().z);
+
+	cout << "mx: " <<  extremalPoints.Max() << endl;
+	cout << "mn: " << extremalPoints.Min() << endl;
 
 	// the new projection center
 	Vector3	projCenter = startPt + Vector3::UNIT_Z() * n;
 
+	cout <<"start: " << startPt << " " << projCenter << endl;
+
 	//construct a translation that moves to the projection center
 	const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter);
 
 	// light space y size
-	const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 
-	cout << "d: " << d << endl;
+	const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);
+
+	const float dy = fabs(extremalPoints.Max()[1] - extremalPoints.Min()[1]);
+	const float dx = fabs(extremalPoints.Max()[0] - extremalPoints.Min()[0]);
+
+	cout << "d: " << d << " dy: " << dy << " dx: " << dx << endl;
+
 	matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
 
-	cout << "lispsm\n" << matLispSM << endl;
+	//cout << "lispsm\n" << matLispSM << endl;
 
 	matLispSM = projectionCenter * matLispSM;
 
 	cout << "center\n" << projectionCenter << endl;
-	cout << "new\n" << matLispSM << endl;
+	//cout << "new\n" << matLispSM << endl;
 
 	// transform into OpenGL right handed system
@@ -230,6 +240,6 @@
 Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
 {
-	Vector3 nearest;
-	float minDist = 1e20f;
+	Vector3 nearest = Vector3::ZERO();
+	float minDist = 1e25f;
 
 	Vector3 camPos = mCamera->GetPosition();
@@ -279,5 +289,5 @@
 
 
-static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &lightView,
+static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &m,
 										 const VertexArray &pts)
 {
@@ -290,5 +300,5 @@
 	{
 		Vector3 pt = *it;
-		pt = lightView * pt;
+		pt = m * pt;
 
 		extremalPoints.Include(pt);
@@ -301,10 +311,8 @@
 bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj)
 {
-	DEL_PTR(polyhedron);
-
-
 	///////////////////
 	//-- First step: calc frustum clipped by scene box
 
+	DEL_PTR(polyhedron);
 	polyhedron = CalcClippedFrustum(mSceneBox);
 
@@ -329,4 +337,15 @@
 	lightProj = IdentityMatrix(); 
 
+	// switch coordinate system to that used in the lispsm algorithm for calculations
+	Matrix4x4 transform2LispSM = ZeroMatrix();
+
+	transform2LispSM.x[0][0] =  1.0f;
+	transform2LispSM.x[1][2] = -1.0f; // y => -z
+	transform2LispSM.x[2][1] =  1.0f;  // z => y
+	transform2LispSM.x[3][3] =  1.0f;
+
+	//switch to the lightspace used in the article
+	lightProj = lightProj * transform2LispSM;
+
 	const Vector3 projViewDir = GetProjViewDir(lightProj, frustumPoints);
 
@@ -334,10 +353,12 @@
 
 	//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
+	//rotate the lightspace so that the projected light view always points upwards
+	//calculate a frame matrix that uses the projViewDir[lightspace] as up vector
 	//look(from position, into the direction of the projected direction, with unchanged up-vector)
 	Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
 	lightProj = frame * lightProj;
 
+	cout << "here9\n" << lightProj << endl;
+
 	const Matrix4x4 matLispSM = 
 		CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
@@ -345,15 +366,26 @@
 	lightProj = matLispSM * lightProj;
 
+	// change back to GL coordinate system
+	Matrix4x4 transformToGL = ZeroMatrix();
+	
+	transformToGL.x[0][0] =  1.0f;
+	transformToGL.x[1][2] =  1.0f; // z => y
+	transformToGL.x[2][1] = -1.0f; // y => -z
+	transformToGL.x[3][3] =  1.0f;
+
+	lightProj = lightProj * transformToGL;
+	//cout << "here4 \n" << lightProj << endl;
+
 	AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
 
-	//cout << "max: " << lightPts.Max() << endl;
-	//cout << "min: " << lightPts.Min() << endl;
+	//cout << "ma2: " << lightPts.Max() << endl;
+	//cout << "mi2: " << lightPts.Min() << endl;
 
 	// focus projection matrix on the extremal points => scale to unit cube
-	lightProj *= GetFittingProjectionMatrix(lightPts);
-	//lightProj *= GetFittingProjectionMatrix(extremalPoints);
-
-	cout << "max: " << lightProj * lightPts.Max() << endl;
-	cout << "min: " << lightProj * lightPts.Min() << endl;
+	Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(lightPts);
+	lightProj *= scaleTranslate;
+
+	cout << "max: " << lightProj * extremalPoints.Max() << endl;
+	cout << "min: " << lightProj * extremalPoints.Min() << endl;
 
 	// we have to flip the signs in order to tranform to opengl right handed system
@@ -413,6 +445,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);
 
 	mShadowCam->SetDirection(dir);
@@ -426,5 +458,5 @@
 	//mCamera->GetModelViewMatrix(camView);
 
-	pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
+	pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.1f);
 	mShadowCam->SetPosition(pos);
 
