Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h	(revision 2932)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h	(revision 2936)
@@ -25,5 +25,6 @@
 	Frustum(const Matrix4x4 &trafo);
 
-	enum { RIGHT_PLANE, LEFT_PLANE, BOTTOM_PLANE, TOP_PLANE, FAR_PLANE, NEAR_PLANE};
+	enum { LEFT_PLANE, RIGHT_PLANE, BOTTOM_PLANE, TOP_PLANE, NEAR_PLANE, FAR_PLANE};
+
 	/** Resizes the current frustum so that it fully encloses the given polyhedron
 	*/
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 2931)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 2936)
@@ -275,13 +275,9 @@
 	}
 
-	// add the new polygon to the polyhedron
-	clippedPolyhedron->Add(planePoly);
-
-
 
 	/////////////
 	//-- clip polyhedron: plane splits all polygons
 
-	for (int i = 0; i < (int)mPolygons.size(); ++ i)
+	for (size_t i = 0; i < mPolygons.size(); ++ i)
 	{
 		Polygon3 *poly = mPolygons[i];
@@ -320,4 +316,7 @@
 		}
 	}
+
+	// we can now savely add the new polygon
+	clippedPolyhedron->Add(planePoly);
 
 	return clippedPolyhedron;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2932)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2936)
@@ -87,5 +87,5 @@
 	Vector3 GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const;
 
-	static void DrawPoly(Polyhedron *poly, const Vector3 &color);
+	static void DrawPoly(Polyhedron *poly, const Vector3 &color, const Vector3 &color2);
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2935)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2936)
@@ -40,5 +40,6 @@
 
 
-static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix, const AxisAlignedBox3 &sceneBox)
+static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix, 
+									const AxisAlignedBox3 &sceneBox)
 {
 	Frustum frustum(lightMatrix);
@@ -57,13 +58,12 @@
 		frustum.mClipPlanes[i].mD *= invLength;
 		frustum.mClipPlanes[i].mNormal *= invLength;
-
-		//clipPlanes.push_back(frustum.mClipPlanes[i]);
-	}
-
-	clipPlanes.push_back(frustum.mClipPlanes[5]);
+	}
+
 	clipPlanes.push_back(frustum.mClipPlanes[0]);
 	clipPlanes.push_back(frustum.mClipPlanes[1]);
 	clipPlanes.push_back(frustum.mClipPlanes[2]);
 	clipPlanes.push_back(frustum.mClipPlanes[3]);
+	clipPlanes.push_back(frustum.mClipPlanes[5]);
+
 	clipPlanes.push_back(frustum.mClipPlanes[4]);
 
@@ -158,5 +158,5 @@
 
 
-void ShadowMap::DrawPoly(Polyhedron *poly, const Vector3 &color)
+void ShadowMap::DrawPoly(Polyhedron *poly, const Vector3 &color, const Vector3 &color2)
 {
 	if (!poly) return;
@@ -166,4 +166,10 @@
 		glColor3f(color.x, color.y, color.z);
 
+		if (i == poly->NumPolygons() - 1) // hack: different color for near
+		{
+			//glLineWidth(2);
+			glColor3f(color2.x, color2.y, color2.z);
+		
+		}
 		glBegin(GL_LINE_LOOP);
 
@@ -177,4 +183,6 @@
 
 		glEnd();
+		
+		//if (i == poly->NumPolygons() - 1) glLineWidth(1);
 	}
 }
@@ -183,10 +191,11 @@
 void ShadowMap::DrawPolys()
 {
-	DrawPoly(lightPoly, Vector3(1, 0, 1));
-	DrawPoly(polyhedron, Vector3(0, 1, 0));
+	DrawPoly(lightPoly, Vector3(1, 0, 1), Vector3(1, 1, 1));
+	DrawPoly(polyhedron, Vector3(0, 1, 0), Vector3(0, 1, 1));
 
 	glPointSize(10.0f);
 
 	Vector3 pt = Vector3::ZERO();
+	//Vector3 pt = dummyPt;
 
 	Matrix4x4 myMat = Invert(dummyMat);
@@ -225,5 +234,5 @@
 	//-- first find the free parameter values n, and P (the projection center), and the projection depth
 
-	const float n = 1e1f;
+	const float n = 1e2f;
 	//const float n = 1e6f;
 	//const float n = ComputeN(bounds_ls) * 100;
@@ -237,6 +246,7 @@
 
 	//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, bounds_ls.Max().z);
 	const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
+	//const Vector3 schas = lightSpace * mCamera->GetPosition();
+	//const Vector3 startPt = Vector3(schas.x, schas.y, schas.y);//bounds_ls.Max().z);
 
 	cout << "mx: " <<  bounds_ls.Max() << endl;
@@ -244,8 +254,9 @@
 
 	// the new projection center
-	Vector3	projCenter = startPt + Vector3::UNIT_Z() * n;
+	const Vector3 projCenter = startPt + Vector3::UNIT_Z() * n;
 
 	cout <<"start: " << startPt << " " << projCenter << " " << Distance(lightSpace * mCamera->GetPosition(), startPt) << endl;
-//dummyPt = -projCenter;
+
+	dummyPt = startPt;
 
 	//construct a translation that moves to the projection center
@@ -416,9 +427,9 @@
 	transform2LispSM.x[3][3] =  1.0f;
 
+
 	//switch to the lightspace used in the article
-	lightProj = lightProj * transform2LispSM;
+	lightProj *= transform2LispSM;
 
 	const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints);
-
 
 	cout << "projViewDir: " << projViewDir << " orig " << mCamera->GetDirection() << endl;
@@ -428,16 +439,19 @@
 	//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)
-	const Matrix4x4 frame = MyLookAt2(Vector3::ZERO(), -projViewDir, -Vector3::UNIT_Y());
-	//const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Z());
+	const Matrix4x4 frame = MyLookAt2(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
+	//const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
 
 	cout << "frame\n " << frame << endl;
-	lightProj = lightProj * frame;
+	lightProj *= frame;
 
 	cout << "here9\n" << lightProj << endl;
+
+	Matrix4x4 refl2 = ScaleMatrix(-1, -1, 1); 
+	//lightProj = refl2 * lightProj;
 
 	const Matrix4x4 matLispSM = 
 		CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
 
-	lightProj = lightProj * matLispSM;
+	lightProj *= matLispSM;
 
 	// change back to GL coordinate system
@@ -449,18 +463,21 @@
 	transformToGL.x[3][3] =  1.0f;
 
-	lightProj = lightProj * transformToGL;
+	lightProj *= transformToGL;
 	//cout << "here4 \n" << lightProj << endl;
 
 	AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
 
-	//cout << "ma2: " << lightPts.Max() << endl;
-	//cout << "mi2: " << lightPts.Min() << endl;
+	//cout << "max2: " << lightPts.Max() << endl;
+	//cout << "min2: " << lightPts.Min() << endl;
+
+	AxisAlignedBox3 dummyBox = GetExtremalPoints(IdentityMatrix(), frustumPoints);
 
 	// focus projection matrix on the extremal points => scale to unit cube
+	//Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(extremalPoints);
 	Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(lightPts);
 	lightProj *= scaleTranslate;
 
-	cout << "max: " << lightProj * extremalPoints.Max() << endl;
-	cout << "min: " << lightProj * extremalPoints.Min() << endl;
+	cout << "max: " << lightView * lightProj * dummyBox.Max() << endl;
+	cout << "min: " << lightView * lightProj * dummyBox.Min() << endl;
 
 	// we have to flip the signs in order to tranform to opengl right handed system
@@ -569,4 +586,8 @@
 	Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
 	Matrix4x4 lightView = MyLookAt2(mShadowCam->GetPosition(), -dir, upVec);
+	//Matrix4x4 lightView = MyLookAt(mShadowCam->GetPosition(), -dir, mCamera->GetDirection());
+
+	Matrix4x4 refl = ScaleMatrix(-1, 1, 1); 
+	lightView = lightView * refl;
 
 	mShadowCam->mViewOrientation = lightView;
@@ -682,4 +703,8 @@
 	Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
 	Matrix4x4 lightView = MyLookAt2(mShadowCam->GetPosition(), -dir, upVec);
+	//Matrix4x4 lightView = MyLookAt(mShadowCam->GetPosition(), -dir, mCamera->GetDirection());
+
+	//Matrix4x4 refl = ScaleMatrix(-1, 1, 1); 
+	//lightView = lightView * refl;
 
 	mShadowCam->mViewOrientation = lightView;
@@ -727,21 +752,17 @@
 	glDisable(GL_POLYGON_OFFSET_FILL);
 
-	glPushAttrib(GL_CURRENT_BIT);
 	glDisable(GL_LIGHTING);
 	glDisable(GL_DEPTH_TEST);
-	glDepthMask(GL_FALSE);
-
+
+	glLineWidth(2);
 	Polyhedron *hpoly = CreatePolyhedron(projView, mSceneBox);
-	//Polyhedron *hpoly = CalcClippedFrustum(mSceneBox);
-	
-	DrawPoly(hpoly, Vector3(1, 1, 1));
+	DrawPoly(hpoly, Vector3(1, 1, 1), Vector3(0, 1, 1));
 
 	DEL_PTR(hpoly);
 
-	glEnable(GL_CULL_FACE);
-
+	//glEnable(GL_CULL_FACE);
+
+	glEnable(GL_LIGHTING);
 	glEnable(GL_DEPTH_TEST);
-	glDepthMask(GL_TRUE);
-	glPopAttrib();
 
 	glMatrixMode(GL_MODELVIEW);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2932)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2936)
@@ -1035,5 +1035,5 @@
 	// this would conveniently solves some issues (e.g, skys without shadows)
 
-	RenderSky();
+	//RenderSky();
 	
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2934)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2936)
@@ -763,14 +763,7 @@
 	Matrix4x4 m(nRight, nUp, nDir);
 
-	/*m.x[3][0] = -DotProd(nRight, position);
-	m.x[3][1] = -DotProd(nUp, position);
-	m.x[3][2] = -DotProd(nDir, position);*/
-
-	Vector3 position = m * -pos;
-	
-	// note: left handed system => we go into positive z
-	m.x[3][0] = position.x;
-	m.x[3][1] = position.y;
-	m.x[3][2] = position.z;
+	m.x[3][0] = -DotProd(nRight, pos);
+	m.x[3][1] = -DotProd(nUp, pos);
+	m.x[3][2] = -DotProd(nDir, pos);
 
 	return m;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2932)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2936)
@@ -279,6 +279,8 @@
 		mClipPlanes[Frustum::RIGHT_PLANE][i] = trafo.x[i][3] - trafo.x[i][0];
 		mClipPlanes[Frustum::LEFT_PLANE][i] = trafo.x[i][3] + trafo.x[i][0]; 
+		
 		mClipPlanes[Frustum::BOTTOM_PLANE][i] = trafo.x[i][3] + trafo.x[i][1];
 		mClipPlanes[Frustum::TOP_PLANE][i] = trafo.x[i][3] - trafo.x[i][1]; 
+
 		mClipPlanes[Frustum::FAR_PLANE][i] = trafo.x[i][3] - trafo.x[i][2]; 
 		mClipPlanes[Frustum::NEAR_PLANE][i] = trafo.x[i][3] + trafo.x[i][2];
