Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2923)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2924)
@@ -85,4 +85,24 @@
 
 
+static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &m,
+										 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 = m * pt;
+
+		extremalPoints.Include(pt);
+	}
+
+	return extremalPoints;
+}
+
+
 ShadowMap::ShadowMap(Light *light, int size, const AxisAlignedBox3 &sceneBox, Camera *cam):
 mSceneBox(sceneBox), mSize(size), mCamera(cam), mLight(light)
@@ -179,33 +199,38 @@
 
 
-Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView, 
+Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightSpace, 
 										 const AxisAlignedBox3 &extremalPoints,
-										 const VertexArray &pts
+										 const VertexArray &body
 										 )
 {
-	Matrix4x4 matLispSM;
+	AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
+	//return IdentityMatrix();
 
 	///////////////
 	//-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix
 
-	const float n = ComputeN(extremalPoints);
+
+	////////
+	//-- first find the free parameter values n, and P (the projection center), and the projection depth
+
+	const float n = 1e6;//ComputeN(bounds_ls);
 
 	cout << "n: " << n << endl;
 
-	const Vector3 nearPt = GetNearCameraPointE(pts);
-
+	const Vector3 nearPt = GetNearCameraPointE(body);
+	const float dummy = 0;
 	//get the coordinates of the near camera point in light space
-	const Vector3 lsNear = lightProjView * nearPt;
+	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
-	const Vector3 startPt = Vector3(lsNear.x, lsNear.y, extremalPoints.Max().z);
-
-	cout << "mx: " <<  extremalPoints.Max() << endl;
-	cout << "mn: " << extremalPoints.Min() << endl;
+	const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
+
+	cout << "mx: " <<  bounds_ls.Max() << endl;
+	cout << "mn: " << bounds_ls.Min() << endl;
 
 	// the new projection center
 	Vector3	projCenter = startPt + Vector3::UNIT_Z() * n;
 
-	cout <<"start: " << startPt << " " << projCenter << endl;
+	cout <<"start: " << startPt << " " << projCenter << " " << Distance(lightSpace * mCamera->GetPosition(), startPt) << endl;
 
 	//construct a translation that moves to the projection center
@@ -213,18 +238,25 @@
 
 	// light space y size
-	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]);
+	const float d = fabs(bounds_ls.Max()[2] - bounds_ls.Min()[2])+dummy;
+
+	const float dy = fabs(bounds_ls.Max()[1] - bounds_ls.Min()[1]);
+	const float dx = fabs(bounds_ls.Max()[0] - bounds_ls.Min()[0]);
 
 	cout << "d: " << d << " dy: " << dy << " dx: " << dx << endl;
 
-	matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
+	
+
+	////////
+	//-- now apply these values to construct the perspective lispsm matrix
+
+	Matrix4x4 matLispSM;
+	
+	matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n+dummy, n + d);
 
 	//cout << "lispsm\n" << matLispSM << endl;
 
+	// translate to the projection center
 	matLispSM = projectionCenter * matLispSM;
 
-	cout << "center\n" << projectionCenter << endl;
 	//cout << "new\n" << matLispSM << endl;
 
@@ -232,6 +264,5 @@
 	Matrix4x4 refl = ScaleMatrix(1.0f, 1.0f, -1.0f);
 	matLispSM *= refl;
-	matLispSM = IdentityMatrix();
-
+	
 	return matLispSM;
 }
@@ -243,5 +274,5 @@
 	float minDist = 1e25f;
 
-	Vector3 camPos = mCamera->GetPosition();
+	const Vector3 camPos = mCamera->GetPosition();
 
 	/*Matrix4x4 inverseCamView;
@@ -281,29 +312,17 @@
 	Vector3 projDir(b_lp - e_lp);
 
+	Matrix4x4 dummy = lightSpace;
+	Invert(dummy);
+	Vector3 dummyVec = dummy * e_lp;
+	Vector3 dummyVec2 = dummy * b_lp;
+
+	//projDir.z = -projDir.z;
+
+	cout << "dummy: " << Normalize(dummyVec2 - dummyVec) << endl;
 	//project the view direction into the shadow map plane
 	projDir.y = 0.0;
-	//projDir.z = 0.0;
 
 	return Normalize(projDir);
-}
-
-
-static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &m,
-										 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 = m * pt;
-
-		extremalPoints.Include(pt);
-	}
-
-	return extremalPoints;
+	//return projDir;
 }
 
@@ -342,5 +361,5 @@
 	transform2LispSM.x[0][0] =  1.0f;
 	transform2LispSM.x[1][2] = -1.0f; // y => -z
-	transform2LispSM.x[2][1] =  1.0f;  // z => y
+	transform2LispSM.x[2][1] =  1.0f; // z => y
 	transform2LispSM.x[3][3] =  1.0f;
 
@@ -348,7 +367,8 @@
 	lightProj = lightProj * transform2LispSM;
 
-	const Vector3 projViewDir = GetProjViewDir(lightProj, frustumPoints);
-
-	cout << "projViewDir: " << projViewDir << endl;
+	const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints);
+
+
+	cout << "projViewDir: " << projViewDir << " orig " << mCamera->GetDirection() << endl;
 
 	//do Light Space Perspective shadow mapping
@@ -357,5 +377,7 @@
 	//look(from position, into the direction of the projected direction, with unchanged up-vector)
 	Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
-	//lightProj = lightProj * frame;
+
+	cout << "frame\n " << frame << endl;
+	lightProj = lightProj * frame;
 
 	cout << "here9\n" << lightProj << endl;
@@ -364,4 +386,7 @@
 		CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
 
+	Vector3 mydummy = Vector3::UNIT_Z();
+
+	cout << "yoyo: " << Normalize(lightView * lightProj * mydummy) << endl;
 	lightProj = lightProj * matLispSM;
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2920)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2924)
@@ -717,5 +717,4 @@
 {
 	const Vector3 nDir = Normalize(dir);
-	
 	Vector3 nUp = Normalize(up);
 
@@ -723,6 +722,5 @@
 	nUp = Normalize(CrossProd(nRight, nDir));
 
-	Matrix4x4 m;
-	Matrix4x4(nRight, nUp, -nDir);
+	Matrix4x4 m(nRight, nUp, -nDir);
 
 	m.x[3][0] = -DotProd(nRight, pos);
