Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.h	(revision 2913)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.h	(revision 2929)
@@ -73,4 +73,8 @@
 	inline friend std::ostream &operator<<(std::ostream &s, const Polyhedron &a);
 
+	/** Computes polyhedron from a couple of intersecting planes and a bounding box.
+	*/
+	static Polyhedron *CreatePolyhedron(const std::vector<Plane3> &planes, const AxisAlignedBox3 &box);
+
 protected:
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h	(revision 2913)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h	(revision 2929)
@@ -206,7 +206,11 @@
 	*/
 	Polyhedron *CalcIntersection(const Polyhedron &polyhedron) const;
-
-
+	/** Tests for intersection of the ray with the box.
+	*/
 	bool Intersects(const SimpleRay &ray, float &tnear, float &tfar) const;
+	/** Extracts the box sides as polygons.
+	*/
+	void ExtractPolygons(PolygonContainer &polys) const;
+
 
 	////////////
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 2913)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 2929)
@@ -13,4 +13,6 @@
 static const float epsilon = 1e-6f;
 
+
+
 /**********************************************************/
 /*             class Polyhedron Implementation             */
@@ -42,4 +44,45 @@
 	for (it = rhs.mPolygons.begin(); it != it_end; ++ it)
 		Add(new Polygon3(*(*it)));
+}
+
+
+Polyhedron *Polyhedron::CreatePolyhedron(const vector<Plane3> &planes, 
+										 const AxisAlignedBox3 &sceneBox)
+{
+	// add the bounding box
+	PolygonContainer polygons;
+	sceneBox.ExtractPolygons(polygons);
+
+	Polyhedron *oldPolyhedron = new Polyhedron(polygons);
+
+	if (!oldPolyhedron->Valid()) cerr << "******************* not valid!! ************* " << endl;
+
+	//oldPolyhedron->mBox = sceneBox;
+
+	vector<Plane3>::const_iterator it, it_end = planes.end();
+
+	Polyhedron *newPolyhedron = NULL;
+	
+	// intersect bounding box with planes
+	for (it = planes.begin(); it != it_end; ++ it)
+	{	
+		Plane3 plane = *it;
+		
+		newPolyhedron = oldPolyhedron->CalcIntersection(plane);
+	
+		if (!newPolyhedron)
+		{
+			cerr << "polyhedron not valid or NULL!" << endl; 
+			return NULL;	
+		}
+
+		// hack: set bounding box temporarily to scene bounding box
+		//newPolyhedron->mBox = sceneBox;
+
+		DEL_PTR(oldPolyhedron);
+		oldPolyhedron = newPolyhedron;
+	}
+
+	return newPolyhedron;
 }
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp	(revision 2913)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp	(revision 2929)
@@ -150,4 +150,5 @@
 	glDisable(GL_LIGHTING);
 	glDisable(GL_DEPTH_TEST);
+	glDepthMask(GL_FALSE);
 
 	RenderFrustum();
@@ -171,5 +172,7 @@
 
 	//RenderBoxForViz(mBvh->GetBox());
-	
+
+	glDepthMask(GL_TRUE);
+
 	glPopAttrib();
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2928)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2929)
@@ -84,4 +84,5 @@
 	Vector3 GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const;
 
+	static void DrawPoly(Polyhedron *poly, const Vector3 &color);
 
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2928)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2929)
@@ -19,4 +19,5 @@
 
 static Polyhedron *polyhedron = NULL;
+static Polyhedron *lightPoly = NULL;
 
 
@@ -125,23 +126,19 @@
 
 
-void ShadowMap::DrawPolys()
-{
-	if (!polyhedron) return;
-
-	for (size_t i = 0; i < polyhedron->NumPolygons(); ++ i)
-	{
-		float r = (float)i / polyhedron->NumPolygons();
-		float g = 1;
-		float b = 1;
-
-		glColor3f(r, g, b);
+void ShadowMap::DrawPoly(Polyhedron *poly, const Vector3 &color)
+{
+	if (!poly) return;
+
+	for (size_t i = 0; i < poly->NumPolygons(); ++ i)
+	{
+		glColor3f(color.x, color.y, color.z);
 
 		glBegin(GL_LINE_LOOP);
 
-		Polygon3 *poly = polyhedron->GetPolygons()[i];
-
-		for (size_t j = 0; j < poly->mVertices.size(); ++ j)
+		Polygon3 *p = poly->GetPolygons()[i];
+
+		for (size_t j = 0; j < p->mVertices.size(); ++ j)
 		{
-			Vector3 v = poly->mVertices[j];
+			Vector3 v = p->mVertices[j];
 			glVertex3d(v.x, v.y, v.z);
 		}
@@ -152,36 +149,8 @@
 
 
-void ShadowMap::IncludeLightVolume(const Polyhedron &polyhedron, 
-								   VertexArray &frustumPoints, 
-								   const Vector3 lightDir,
-								   const AxisAlignedBox3 &sceneBox
-								   ) 
-{
-	// we don't need closed form anymore => just store vertices
-	VertexArray vertices;
-	polyhedron.CollectVertices(vertices);
-
-	// we 'look' at each point and calculate intersections of rays with scene bounding box
-	VertexArray::const_iterator it, it_end = vertices.end();
-
-	for (it = vertices.begin(); it != it_end; ++ it)
-	{
-		Vector3 v  = *it;
-
-		frustumPoints.push_back(v);
-		
-		// hack: get point surely outside of box
-		v -= Magnitude(mSceneBox.Diagonal()) * lightDir;
-
-		SimpleRay ray(v, lightDir);
-
-		float tNear, tFar;
-
-		if (sceneBox.Intersects(ray, tNear, tFar))
-		{
-			Vector3 newpt = ray.Extrap(tNear);
-			frustumPoints.push_back(newpt);			
-		}
-	}
+void ShadowMap::DrawPolys()
+{
+	DrawPoly(lightPoly, Vector3(1, 0, 1));
+	DrawPoly(polyhedron, Vector3(0, 1, 0));
 }
 
@@ -214,6 +183,6 @@
 
 	//const float n = 1e6f;
-	//const float n = 1e1f;
-	const float n = ComputeN(bounds_ls) * 100;
+	const float n = 1e3f;
+	//const float n = ComputeN(bounds_ls) * 100;
 
 	cout << "n: " << n << endl;
@@ -566,4 +535,22 @@
 	mLightProjView = lightView * lightProj;
 
+	Frustum frustum(mLightProjView);
+	//Frustum frustum(projView);
+
+	DEL_PTR(lightPoly);
+
+	vector<Plane3> clipPlanes;
+
+	for (int i = 0; i < 6; ++ i)
+	{
+		frustum.mClipPlanes[i].mNormal *= -1;
+		frustum.mClipPlanes[i].mD *= -1;
+
+		clipPlanes.push_back(frustum.mClipPlanes[i]);
+	}
+
+	DEL_PTR(lightPoly);
+	lightPoly = Polyhedron::CreatePolyhedron(clipPlanes, mSceneBox);
+
 	//cout << "new:\n" << lightProj << endl;
 
@@ -638,3 +625,39 @@
 }
 
+
+void ShadowMap::IncludeLightVolume(const Polyhedron &polyhedron, 
+								   VertexArray &frustumPoints, 
+								   const Vector3 lightDir,
+								   const AxisAlignedBox3 &sceneBox
+								   ) 
+{
+	// we don't need closed form anymore => just store vertices
+	VertexArray vertices;
+	polyhedron.CollectVertices(vertices);
+
+	// we 'look' at each point and calculate intersections of rays with scene bounding box
+	VertexArray::const_iterator it, it_end = vertices.end();
+
+	for (it = vertices.begin(); it != it_end; ++ it)
+	{
+		Vector3 v  = *it;
+
+		frustumPoints.push_back(v);
+		
+		// hack: get point surely outside of box
+		v -= Magnitude(mSceneBox.Diagonal()) * lightDir;
+
+		SimpleRay ray(v, lightDir);
+
+		float tNear, tFar;
+
+		if (sceneBox.Intersects(ray, tNear, tFar))
+		{
+			Vector3 newpt = ray.Extrap(tNear);
+			frustumPoints.push_back(newpt);			
+		}
+	}
+}
+
+
 } // namespace
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2928)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2929)
@@ -1560,5 +1560,6 @@
 	//camera->SetFar(1000);
 
-	const float offs = box.Size().x * 0.3f;
+	//const float offs = box.Size().x * 0.3f;
+	const float offs = box.Size().x * 0.4f;
 	
 	Vector3 vizpos = Vector3(box.Min().x, box.Min().y  - box.Size().y * 0.35f, box.Min().z + box.Size().z * 50);
@@ -1582,6 +1583,4 @@
 	visCamera->SetupCameraView();
 
-	//Matrix4x4 rotX = RotationXMatrix(camera->GetYaw());
-	//glMultMatrixf((float *)rotX.x);
 	Matrix4x4 rotZ = RotationZMatrix(-camera->GetPitch());
 	glMultMatrixf((float *)rotZ.x);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 2913)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 2929)
@@ -1344,4 +1344,55 @@
 
 
-}
-
+void AxisAlignedBox3::ExtractPolygons(PolygonContainer &polys) const
+{
+	Polygon3 *face1 = new Polygon3();
+	polys.push_back(face1);   
+
+    face1->mVertices.push_back(Vector3(mMin.x, mMin.y, mMax.z));
+	face1->mVertices.push_back(Vector3(mMin.x, mMax.y, mMax.z));
+	face1->mVertices.push_back(Vector3(mMin.x, mMax.y ,mMin.z));
+	face1->mVertices.push_back(Vector3(mMin.x, mMin.y, mMin.z));
+
+	Polygon3 *face2 = new Polygon3();  
+	polys.push_back(face2);
+    
+    face2->mVertices.push_back(Vector3(mMax.x, mMin.y, mMin.z));
+    face2->mVertices.push_back(Vector3(mMax.x, mMax.y, mMin.z));
+    face2->mVertices.push_back(Vector3(mMax.x, mMax.y, mMax.z));
+    face2->mVertices.push_back(Vector3(mMax.x, mMin.y, mMax.z));
+  
+	Polygon3 *face3 = new Polygon3();  
+	polys.push_back(face3);
+
+    face3->mVertices.push_back(Vector3(mMax.x, mMin.y ,mMin.z));
+	face3->mVertices.push_back(Vector3(mMax.x, mMin.y, mMax.z));
+	face3->mVertices.push_back(Vector3(mMin.x, mMin.y, mMax.z));
+	face3->mVertices.push_back(Vector3(mMin.x, mMin.y, mMin.z));
+
+	Polygon3 *face4 = new Polygon3();  
+	polys.push_back(face4);
+
+	face4->mVertices.push_back(Vector3(mMin.x, mMax.y, mMin.z));
+	face4->mVertices.push_back(Vector3(mMin.x, mMax.y, mMax.z));
+	face4->mVertices.push_back(Vector3(mMax.x, mMax.y, mMax.z));
+	face4->mVertices.push_back(Vector3(mMax.x, mMax.y, mMin.z));
+    
+	Polygon3 *face5 = new Polygon3();  
+	polys.push_back(face5);
+
+	face5->mVertices.push_back(Vector3(mMin.x, mMax.y, mMin.z));
+    face5->mVertices.push_back(Vector3(mMax.x, mMax.y, mMin.z));
+    face5->mVertices.push_back(Vector3(mMax.x, mMin.y, mMin.z));
+	face5->mVertices.push_back(Vector3(mMin.x, mMin.y, mMin.z));
+
+	Polygon3 *face6 = new Polygon3();  
+	polys.push_back(face6);  
+  
+    face6->mVertices.push_back(Vector3(mMin.x, mMin.y, mMax.z));
+    face6->mVertices.push_back(Vector3(mMax.x, mMin.y, mMax.z));
+    face6->mVertices.push_back(Vector3(mMax.x, mMax.y, mMax.z));
+    face6->mVertices.push_back(Vector3(mMin.x, mMax.y, mMax.z));
+}
+
+}
+
