Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 2897)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 2947)
@@ -51,5 +51,6 @@
 mUseTightBounds(false), 
 mShowBounds(false),
-mRenderQueue(NULL)
+mRenderQueue(NULL),
+mMaxVisibleDistance(.0f)
 {
 }
@@ -64,5 +65,5 @@
 void RenderTraverser::EnqueueNode(BvhNode *node)
 {
-	mBvh->CalcDistance(node);
+	mBvh->UpdateMinDistance(node);
 	mDistanceQueue.push(node);
 }
@@ -78,5 +79,5 @@
 
 		if (mShowBounds)
-			mBvh->RenderBoundsForViz(node, mRenderState, mUseTightBounds);		
+			mBvh->RenderBoundsForViz(node, mRenderState, mUseTightBounds);
 	}
 	else 
@@ -93,4 +94,5 @@
 void RenderTraverser::RenderNode(BvhNode *node)
 {
+	// test if node was already rendered in this frame
 	if (node->GetLastRenderedFrame() != mFrameId)
 	{
@@ -120,5 +122,11 @@
 			if (mUseDepthPass)
 				mVisibleObjects.push_back(ent);
-		}		
+		}
+
+		// store the max distance in the scene for later use
+		float maxDist = mBvh->CalcMaxDistance(node);
+
+		if (maxDist > mMaxVisibleDistance)
+			mMaxVisibleDistance = maxDist;
 	}
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h	(revision 2929)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h	(revision 2947)
@@ -190,5 +190,10 @@
 	    is closest to it (this vertex is unequivocally identified by the direction of the vector)
 	*/
-	float GetMinVisibleDistance(const Plane3 &near) const;
+	float GetMinDistance(const Plane3 &near) const;
+	/** Returns the distance between the plane given by 'vecNearplaneNormal' and the vertex that
+	    is farthest to it (this vertex is unequivocally identified by the direction of the vector)
+	*/
+	float GetMaxDistance(const Plane3 &near) const;
+	
 	/** Returns cross section of the plane as a polygon.
 	*/
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h	(revision 2936)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h	(revision 2947)
@@ -80,11 +80,10 @@
 	void CalcFrustum(Frustum &frustum);
 	/** Computes the extremal points of this frustum.
+		If farthestVisibleDistance is nearer than the far plane, 
+		it is used to define the far plane instead.
 	*/
 	void ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
-		               Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const;
-	/** Computes the extremal points of the base frustum.
-	*/
-	void ComputeBasePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
-						   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const;
+		               Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
+					   float farthestVisibleDistance = 1e25f) const;
 	/** Returns the near plane.
 	*/
@@ -111,7 +110,7 @@
 	*/
 	void SetDirection(const Vector3 &direction);
-	/** Returns frustum as polyhedron.
+	/** Returns frustum as polyhedron. 
 	*/
-	Polyhedron *Camera::ComputeFrustum() const;
+	Polyhedron *ComputeFrustum(float farthestVisibleDistance = 1e25f) const;
 
 protected:
@@ -124,5 +123,6 @@
 							   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
 							   const Vector3 &view, const Vector3 &right, const Vector3 &up,
-							   const Vector3 &pos) const;
+							   const Vector3 &pos,
+							   float farthestVisibleDistance = 1e25f) const;
 
 	////////////////
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2897)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2947)
@@ -85,4 +85,8 @@
 	bool IsVirtualLeaf() const { return mIsVirtualLeaf; }
 
+	/** Returns the stored distance to the near plane.
+	*/
+	float GetDistance() const  { return mDistance; }
+
 
 	////////////////
@@ -405,5 +409,5 @@
 	bool operator() (BvhNode *v1, BvhNode *v2) const
     {
-		return (v1->mDistance > v2->mDistance);
+		return (v1->GetDistance() > v2->GetDistance());
     }
 };
@@ -520,11 +524,12 @@
 	*/
 	void InitFrame(Camera *camera);
-	/** This gives the orthogonal distance from the viewpoint to the nearest bounding box vertex
-		note that negative values can appear because culling is done only afterwards
-	*/
-	void CalcDistance(BvhNode *node) const;
-	/** Returns the stored distance.
-	*/
-	float GetDistance(BvhNode *node) const  { return node->mDistance; }
+	/** Stores the orthogonal distance from the viewpoint 
+		to the nearest bounding box vertex with the node. 
+		Note that negative values can appear because culling is done only afterwards
+	*/
+	void UpdateMinDistance(BvhNode *node) const;
+	/** Returns the maximum distance from the near plane to this node.
+	*/
+	float CalcMaxDistance(BvhNode *node) const;
 	/** Pulls up the last visited classification in the bvh.
 	*/
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2944)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2947)
@@ -57,5 +57,4 @@
 
 	void RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView);
-
 
 	static void DrawPolys();
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2911)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2947)
@@ -389,7 +389,13 @@
 
 
-void Bvh::CalcDistance(BvhNode *node) const
-{
-	node->mDistance = node->GetBox().GetMinVisibleDistance(sNearPlane);
+void Bvh::UpdateMinDistance(BvhNode *node) const
+{
+	node->mDistance = node->GetBox().GetMinDistance(sNearPlane);
+}
+
+
+float Bvh::CalcMaxDistance(BvhNode *node) const
+{
+	return node->GetBox().GetMaxDistance(sNearPlane);
 }
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2945)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2947)
@@ -141,9 +141,8 @@
 {
 	mFbo = new FrameBufferObject(size, size, FrameBufferObject::DEPTH_32, true);
-	// the diffuse color buffer
+	// need a color buffer to keep opengl happy
 	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	//mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-
-	mShadowCam = new Camera(mSize, mSize);//mSceneBox.Size().x * 0.5f, mSceneBox.Size().y * 0.5f);
+
+	mShadowCam = new Camera(mSize, mSize);
 	mShadowCam->SetOrtho(true);
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.h	(revision 2897)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.h	(revision 2947)
@@ -133,4 +133,8 @@
 	*/
 	Camera *GetCamera() const { return mCamera; }
+	/** Returns the maximal visible distance encountered in the scene. 
+	    Useful for e.g., shadow map focussing
+	*/
+	float GetMaxVisibleDistance() const { return mMaxVisibleDistance; }
 
 
@@ -187,7 +191,9 @@
 	SceneEntityContainer mVisibleObjects;
 
+	/// the maximal visible distance in the scene
+	float mMaxVisibleDistance;
 
 	/////////////////
-	//-- algorithm parametes
+	//-- algorithm parameters
 
 	int mVisibilityThreshold;
@@ -213,5 +219,5 @@
 inline bool RenderTraverser::IntersectsNearPlane(BvhNode *node) const
 { 
-	return mBvh->GetDistance(node) < mCamera->GetNear();
+	return node->GetDistance() < mCamera->GetNear();
 }
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 2944)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 2947)
@@ -1038,9 +1038,18 @@
 
 
-float AxisAlignedBox3::GetMinVisibleDistance(const Plane3 &near) const
+float AxisAlignedBox3::GetMinDistance(const Plane3 &near) const
 {
 	return	near.mNormal.x * ((near.mNormal.x > 0.0f) ? mMin.x : mMax.x) +
 			near.mNormal.y * ((near.mNormal.y > 0.0f) ? mMin.y : mMax.y) +
 			near.mNormal.z * ((near.mNormal.z > 0.0f) ? mMin.z : mMax.z) +
+			near.mD;
+}
+
+
+float AxisAlignedBox3::GetMaxDistance(const Plane3 &near) const
+{
+	return	near.mNormal.x * ((near.mNormal.x <= 0.0f) ? mMin.x : mMax.x) +
+			near.mNormal.y * ((near.mNormal.y <= 0.0f) ? mMin.y : mMax.y) +
+			near.mNormal.z * ((near.mNormal.z <= 0.0f) ? mMin.z : mMax.z) +
 			near.mD;
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2944)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2947)
@@ -137,8 +137,9 @@
 								   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
 								   const Vector3 &view, const Vector3 &right, const Vector3 &up,
-								   const Vector3 &pos) const
+								   const Vector3 &pos,
+								   float farthestVisibleDistance) const
 {
 	float z_near = mNear;
-	float z_far = mFar;
+	float z_far = min(mFar, farthestVisibleDistance);
 
 	float fov = mFovy;
@@ -177,18 +178,11 @@
 
 void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
-						   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const
+						   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
+						   float farthestVisibleDistance) const
 {
 	ComputePointsInternal(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr, 
-						  GetDirection(), GetRightVector(), GetUpVector(), GetPosition());
-}
-
-
-void Camera::ComputeBasePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
-							   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const
-{
-	ComputePointsInternal(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr, 
-		GetBaseDirection(), GetBaseRightVector(), GetBaseUpVector(), Vector3(0, 0, 0));
-}
-
+						  GetDirection(), GetRightVector(), GetUpVector(), GetPosition(),
+						  farthestVisibleDistance);
+}
 
 
@@ -334,5 +328,5 @@
 
 
-Polyhedron *Camera::ComputeFrustum() const
+Polyhedron *Camera::ComputeFrustum(float farthestVisibleDistance) const
 {
 	Vector3 ftl, ftr, fbl, fbr;
@@ -341,5 +335,5 @@
 	VertexArray sides[6];
 
-	ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
+	ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr, farthestVisibleDistance);
 
 	for (int i = 0; i < 6; ++ i)
