Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 3071)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 3072)
@@ -157,8 +157,5 @@
 	// add root node to queue
 	EnqueueNode(mBvh->GetRoot());
-	// add dynamic root
-	if (mBvh->GetDynamicRoot())
-		EnqueueNode(mBvh->GetDynamicRoot());
-
+	
 
 	///////////
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.cpp	(revision 3071)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.cpp	(revision 3072)
@@ -5,4 +5,6 @@
 #include "SceneEntity.h"
 #include "Transform3.h"
+#include "glInterface.h"
+
 
 
@@ -26,5 +28,7 @@
 	if (mMaterial) 
 		mMaterial->Render(state);
-
+	//mDepthWriteEnabled = true;
+glDepthMask(GL_TRUE);	glDepthFunc(GL_LESS);
+glEnable(GL_DEPTH_TEST);
 	mGeometry->Render(state);
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3070)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3072)
@@ -63,5 +63,6 @@
 
 Bvh *BvhLoader::Load(const string &filename,  
-					 const SceneEntityContainer &entities)
+					 const SceneEntityContainer &staticEntities,
+					 const SceneEntityContainer &dynamicEntities)
 {
 	queue<BvhNode *> tQueue;
@@ -72,13 +73,12 @@
 	cout << "loading bvh" << endl;
 
-	Bvh *bvh = new Bvh(entities);
+	Bvh *bvh = new Bvh(staticEntities, dynamicEntities);
 
 	BvhNode *root = LoadNextNode(stream, NULL);
 
-	bvh->mRoot = root;
-	//bvh->mRoot = bvh->mStaticRoot;
-	bvh->mNumNodes = 1;
+	bvh->mStaticRoot = root;
+	bvh->mNumNodes = 3;
 
-	tQueue.push(bvh->mRoot);
+	tQueue.push(root);
 	
 	while(!tQueue.empty())
@@ -108,8 +108,8 @@
 
 
-	bvh->mBox = bvh->mRoot->GetBox();
+	////////////
+	//-- create dynamic part of the hierarchy
 
-	cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl;
-	cout << "scene box: " << bvh->mBox << endl;
+	bvh->CreateDynamicBranch();
 
 	
@@ -119,17 +119,8 @@
 	/// this function must be called once after creation
 	bvh->PostProcess();
-	
-	// set virtual leaves for specified number of triangles
-	bvh->SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES);
-	/// update the numleaves parameter of the bvh nodes
-	bvh->UpdateNumLeaves(bvh->mRoot);
-	// compute unique ids
-	bvh->ComputeIds();
-	// specify bounds for occlusion tests
-	bvh->RecomputeBounds();
 
-	// compute and print stats
-	bvh->ComputeBvhStats(bvh->mRoot, bvh->mBvhStats);
-	bvh->PrintBvhStats(bvh->mBvhStats);
+
+	cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl;
+	cout << "scene box: " << bvh->mBox << endl;
 
 	return bvh;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 3070)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 3072)
@@ -137,5 +137,5 @@
 	*/
 	inline void SetId(int id);
-
+	
 
 	//////////
@@ -648,7 +648,4 @@
 	void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds);
 
-	void AddDynamicObjects(const SceneEntityContainer &entities);
-
-
 
 protected:
@@ -660,6 +657,8 @@
 	Bvh();
 	/** Protected constructor taking scene geometry into account
-	*/
-	const Bvh(const SceneEntityContainer &entities);
+		Sets the static and dynamic objects for the hierarchy.
+	*/
+	const Bvh(const SceneEntityContainer &staticEntities, 
+		      const SceneEntityContainer &dynamicEntities);
 	/** Called by the constructor. Initializes important members.
 	*/
@@ -740,4 +739,9 @@
 	inline int GetNumStaticNodes() const { return mNumNodes; }
 
+	void ComputeMaxDepthForVirtualLeaves();
+
+	void CreateRoot();
+
+
 	////////////////////////
 	
@@ -745,7 +749,7 @@
 	AxisAlignedBox3 mBox;
 	/// the root of the hierarchy
-	BvhNode *mRoot;
+	BvhInterior *mRoot;
 	/// the root of static part of the the hierarchy
-	//BvhNode *mStaticRoot;
+	BvhNode *mStaticRoot;
 	/// the root of dynamic part of the the hierarchy
 	BvhNode *mDynamicRoot;
@@ -754,4 +758,8 @@
 	/// #of entities
 	size_t mGeometrySize;
+	/// #of static entities
+	size_t mStaticGeometrySize;
+	/// #of dynamic entities
+	size_t mDynamicGeometrySize;
 
 
@@ -799,5 +807,5 @@
 	int mMaxDepthForDynamicBranch;
 
-	SceneEntityContainer mDynamicEntities;
+	//SceneEntityContainer mDynamicEntities;
 };
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h	(revision 3071)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h	(revision 3072)
@@ -2,7 +2,4 @@
 #define __SHAPE_H
 
-#include "glInterface.h"
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
 #include "common.h"
 #include "Vector3.h"
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h	(revision 2795)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h	(revision 3072)
@@ -17,6 +17,11 @@
 {
 public:
-
-	Bvh *Load(const std::string &filename, const SceneEntityContainer &entities);
+	/** Creates and loads bvh with static and dynamic objects.
+		The static part of the hierarchy is loaded from the disc,
+		the dynamic part is created and changed on the fly.
+	*/
+	Bvh *Load(const std::string &filename, 
+			  const SceneEntityContainer &staticEntities,
+			  const SceneEntityContainer &dynamicEntities);
 
 protected:
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3071)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3072)
@@ -30,4 +30,5 @@
 
 using namespace std;
+
 
 namespace CHCDemoEngine
@@ -80,6 +81,6 @@
 static int sClipPlaneAABBVertexIndices[12];
 
-
 #define ALIGN_INDICES
+
 
 BvhNode::BvhNode(BvhNode *parent): 
@@ -154,9 +155,19 @@
 inline AxisAlignedBox3 ComputeBoundingBox(SceneEntity **entities, int numEntities)
 {
-	AxisAlignedBox3 box = entities[0]->GetWorldBoundingBox();
-
-	for (int i = 1; i < numEntities; ++ i)
-	{
-		box.Include(entities[i]->GetWorldBoundingBox());
+	AxisAlignedBox3 box;
+	
+	if (!numEntities)
+	{
+		// no box=> just initialize
+		box.Initialize();
+	}
+	else
+	{
+		box = entities[0]->GetWorldBoundingBox();
+
+		for (int i = 1; i < numEntities; ++ i)
+		{
+			box.Include(entities[i]->GetWorldBoundingBox());
+		}
 	}
 
@@ -171,17 +182,23 @@
 
 
-Bvh::Bvh(const SceneEntityContainer &entities)
+Bvh::Bvh(const SceneEntityContainer &staticEntities, 
+		 const SceneEntityContainer &dynamicEntities)
 {
 	Init();
 
-	mGeometrySize = entities.size();
+	mGeometrySize = staticEntities.size() + dynamicEntities.size();
 	mGeometry = new SceneEntity*[mGeometrySize];
 
-	SceneEntityContainer::const_iterator it, it_end = entities.end();
-
-	int i = 0;
-	for (it = entities.begin(); it != it_end; ++ it, ++ i)
-	{
-		mGeometry[i] = (*it);
+	mStaticGeometrySize = staticEntities.size();
+	mDynamicGeometrySize = dynamicEntities.size();
+
+	for (size_t i = 0; i < mStaticGeometrySize; ++ i)
+	{
+		mGeometry[i] = staticEntities[i];
+	}
+
+	for (size_t i = 0; i < mDynamicGeometrySize; ++ i)
+	{
+		mGeometry[mStaticGeometrySize + i] = dynamicEntities[i];
 	}
 }
@@ -430,10 +447,8 @@
 	sNear = cam->GetNear();
 
-	// rebuild dynamic part of the hierarchy
-	if (!mDynamicEntities.empty())
-	{
-		if (!mDynamicRoot)
-			CreateDynamicBranch();
-		
+	// update dynamic part of the hierarchy
+	//if (!mDynamicEntities.empty())
+	if (mDynamicGeometrySize)
+	{
 		UpdateDynamicBranch(mDynamicRoot);
 	}
@@ -647,5 +662,5 @@
 	CollectNodes(mRoot, nodes);
 
-	// assign unique ids to all nodes of the hierarchy
+	// assign ids to all nodes of the "regular" hierarchy
 	int i = 0;
 	BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
@@ -788,5 +803,5 @@
 	node->mTestNodesIdx = (int)mTestNodes.size();
 
-	// use the found nodes as nodes during the occlusion tests
+	// use the collected nodes as proxy for the occlusion tests
 	for (cit = children.begin(); cit != children.end(); ++ cit)
 	{
@@ -992,9 +1007,7 @@
 
 
-void Bvh::PostProcess() 
-{
-	// this function must be called once after hierarchy creation
-
-	// We initialize the virtual leaves
+void Bvh::ComputeMaxDepthForVirtualLeaves()
+{
+	// We initialize the maximal depth for virtual leaves
 	// of this bvh, i.e., the nodes that are used as
 	// leaves of the hierarchy during traversal.
@@ -1043,4 +1056,41 @@
 
 
+// this function must be called once after hierarchy creation
+void Bvh::PostProcess() 
+{
+	CreateRoot();
+
+	ComputeIds();
+	ComputeMaxDepthForVirtualLeaves();
+	// set virtual leaves for specified number of triangles
+	SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES);
+	/// for each node compute the number of leaves under this node
+	UpdateNumLeaves(mRoot);
+	// specify bounds for occlusion tests
+	RecomputeBounds();
+
+	// compute and print stats
+	ComputeBvhStats(mRoot, mBvhStats);
+	PrintBvhStats(mBvhStats);
+
+	if (mDynamicGeometrySize)
+	{
+		BvhStats bvhStats;
+		ComputeBvhStats(mDynamicRoot, bvhStats);
+
+		cout << "\n=========== Dynamic BVH statistics: =========" << endl;
+		cout << "leaves = " << bvhStats.mLeaves << endl;
+		cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl;
+		cout << "leafNodesSA = " << bvhStats.mLeafSA << endl;
+		cout << "interiorNodesVolume = " << bvhStats.mInteriorVol  << endl;
+		cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl;
+
+		cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl;
+		cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl;
+		cout << "=============================================" << endl << endl;
+	}
+}
+
+
 void Bvh::RenderBoundingBoxImmediate(const AxisAlignedBox3 &box)
 {
@@ -1219,5 +1269,5 @@
 		leaf->mIsVirtualLeaf = true;
 		leaf->mIsMaxDepthForVirtualLeaf = true;
-		cout << "leaf contructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl;
+		//cout << "leaf constructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl;
 		return leaf;
 	}
@@ -1225,5 +1275,4 @@
 	//const int axis = (parentAxis + 1) % 3;
 	const int axis = leaf->mBox.MajorAxis();
-	
 
 	const int scale = 20;
@@ -1248,5 +1297,6 @@
 	mNumNodes += 2;
 	BvhInterior *parent = new BvhInterior(leaf->GetParent());
-	BvhLeaf *front = new BvhLeaf(parent);
+	parent->mFirst = leaf->mFirst;
+	parent->mLast = leaf->mLast;
 	
 	parent->mAxis = axis;
@@ -1254,8 +1304,9 @@
 	parent->mDepth = leaf->mDepth;
 
+	BvhLeaf *front = new BvhLeaf(parent);
+
 	parent->mBack = leaf;
 	parent->mFront = front;
-	//parent->mPosition = pos;
-	
+		
 	// now assign the triangles to the subnodes
 	front->mFirst = split + 1;
@@ -1267,7 +1318,4 @@
 	leaf->mParent = parent;
 	
-	// reset box
-	//leaf->mBox.Initialize();
-
 	front->mBox = ComputeBoundingBox(mGeometry + front->mFirst, front->CountPrimitives());
 	leaf->mBox = ComputeBoundingBox(mGeometry + leaf->mFirst, leaf->CountPrimitives());
@@ -1329,15 +1377,9 @@
 
 	BvhLeaf *l = new BvhLeaf(mRoot);
-
-	l->mBox.Initialize();
-
-	SceneEntityContainer::const_iterator sit, sit_end = mDynamicEntities.end();
-
-	for (sit = mDynamicEntities.begin(); sit != sit_end; ++ sit)
-	{
-		l->mBox.Include((*sit)->GetWorldBoundingBox());
-	}
-
-	l->mFirst = (int)(mGeometrySize - mDynamicEntities.size());
+	mDynamicRoot = l;
+
+	l->mBox = ComputeBoundingBox(mGeometry + mStaticGeometrySize, (int)mDynamicGeometrySize);
+
+	l->mFirst = (int)mStaticGeometrySize;
 	l->mLast = (int)mGeometrySize - 1;
 	l->mArea = l->mBox.SurfaceArea();
@@ -1345,44 +1387,6 @@
 	cout << "updating dynamic branch " << l->mFirst << " " << l->mLast << endl;
 
-	mDynamicRoot = SubdivideLeaf(l, 0);
-
-	BvhStats bvhStats;
-	ComputeBvhStats(mDynamicRoot, bvhStats);
-
-	cout << "\n=========== Dynamic BVH statistics: =========" << endl;
-	cout << "leaves = " << bvhStats.mLeaves << endl;
-	cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl;
-	cout << "leafNodesSA = " << bvhStats.mLeafSA << endl;
-	cout << "interiorNodesVolume = " << bvhStats.mInteriorVol  << endl;
-	cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl;
-
-	cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl;
-	cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl;
-	cout << "=============================================" << endl << endl;
-}
-
-
-void Bvh::AddDynamicObjects(const SceneEntityContainer &entities)
-{
-	// copy old entities
-	SceneEntity **newGeom = new SceneEntity*[mGeometrySize + (int)entities.size()];
-
-	memcpy(newGeom, mGeometry, mGeometrySize * sizeof(SceneEntity *));
-
-	delete [] mGeometry;
-	mGeometry = newGeom;
-
-	// now add new entities
-	SceneEntityContainer::const_iterator it, it_end = entities.end();
-
-	size_t i = mGeometrySize;
-	for (it = entities.begin(); it != it_end; ++ it, ++ i)
-	{
-		mGeometry[i] = (*it);
-		mDynamicEntities.push_back(*it);
-	}
-
-
-	mGeometrySize += entities.size();
+	if (mDynamicGeometrySize)
+		mDynamicRoot = SubdivideLeaf(l, 0);
 }
 
@@ -1394,5 +1398,5 @@
 	// especially annoying is this problem when using the frustum 
 	// fitting on the visible objects for shadow mapping
-	// but don't see how to solve this issue without using much costlier calculations
+	// but don't see how to solve this issue without using costly calculations
 	
 	// we stored the near plane distance => we can use it also here
@@ -1404,3 +1408,31 @@
 
 
-}
+void Bvh::CreateRoot()
+{
+	// create new root
+	mRoot = new BvhInterior(NULL);
+	
+	// the separation is a purely logical one
+	// the bounding boxes of the child nodes are 
+	// identical to those of the root node
+
+	mRoot->mBox = mStaticRoot->mBox;
+	mRoot->mBox.Include(mDynamicRoot->mBox);
+
+	mRoot->mArea = mRoot->mBox.SurfaceArea();
+
+	mRoot->mFirst = 0;
+	mRoot->mLast = (int)mGeometrySize - 1;
+
+	cout<<"f: " << mRoot->mFirst<< " l: " <<mRoot->mLast << endl;
+	// add static root on left subtree
+	mRoot->mFront = mStaticRoot;
+	mStaticRoot->mParent = mRoot;
+
+	// add dynamic root on left subtree
+	mRoot->mBack = mDynamicRoot;
+	mDynamicRoot->mParent = mRoot;
+}
+
+
+}
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3071)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3072)
@@ -184,6 +184,6 @@
 
 bool useOptimization = false;
-bool useTightBounds = true;
-//bool useTightBounds = false;
+//bool useTightBounds = true;
+bool useTightBounds = false;
 bool useRenderQueue = true;
 bool useMultiQueries = true;
@@ -453,11 +453,40 @@
 	LoadModel("city.dem", sceneEntities);
 
-
+/*
+	//////////
+	//-- load some dynamic stuff
+
+	LoadModel("hbuddha.dem", dynamicObjects);
+	buddha = dynamicObjects.back();
+	
+	const Vector3 sceneCenter(470.398f, 240.364f, 182.5f);
+	Matrix4x4 transl = TranslationMatrix(sceneCenter);
+	buddha->GetTransform()->SetMatrix(transl);
+
+	for (int i = 0; i < 10; ++ i)
+	{
+		SceneEntity *ent = new SceneEntity(*buddha);
+		resourceManager->AddSceneEntity(ent);
+
+		Vector3 offs = Vector3::ZERO();
+
+		offs.x = RandomValue(.0f, 150.0f);
+		offs.y = RandomValue(.0f, 150.0f);
+
+		transl = TranslationMatrix(sceneCenter + offs);
+		Transform3 *transform = resourceManager->CreateTransform(transl);
+
+		ent->SetTransform(transform);
+		dynamicObjects.push_back(ent);
+	}
+
+*/
 	///////////
 	//-- load the associated static bvh
 
 	const string bvh_filename = string(model_path + "city.bvh");
+
 	BvhLoader bvhLoader;
-	bvh = bvhLoader.Load(bvh_filename, sceneEntities);
+	bvh = bvhLoader.Load(bvh_filename, sceneEntities, dynamicObjects);
 
 	if (!bvh)
@@ -494,30 +523,4 @@
 	// the bird-eye visualization
 	visualization = new Visualization(bvh, camera, NULL, &state);
-
-	LoadModel("hbuddha.dem", dynamicObjects);
-	buddha = dynamicObjects.back();
-	
-	const Vector3 sceneCenter(470.398f, 240.364f, 182.5f);
-	Matrix4x4 transl = TranslationMatrix(sceneCenter);
-	buddha->GetTransform()->SetMatrix(transl);
-
-	for (int i = 0; i < 10; ++ i)
-	{
-		SceneEntity *ent = new SceneEntity(*buddha);
-		resourceManager->AddSceneEntity(ent);
-
-		Vector3 offs = Vector3::ZERO();
-
-		offs.x = RandomValue(.0f, 150.0f);
-		offs.y = RandomValue(.0f, 150.0f);
-
-		transl = TranslationMatrix(sceneCenter + offs);
-		Transform3 *transform = resourceManager->CreateTransform(transl);
-
-		ent->SetTransform(transform);
-		dynamicObjects.push_back(ent);
-	}
-	
-	bvh->AddDynamicObjects(dynamicObjects);
 
 	// this function assign the render queue bucket ids of the materials in beforehand
@@ -1003,7 +1006,9 @@
 	}
 	else
-	{
+	{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 		// actually render the scene geometry using the specified algorithm
 		traverser->RenderScene();
+		//for (int i = 0; i < sceneEntities.size(); ++ i)
+		//	sceneEntities[i]->Render(&state);
 	}
 
