Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3065)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3068)
@@ -76,32 +76,61 @@
 	BvhNode *root = LoadNextNode(stream, NULL);
 
-#if 1
-
 	bvh->mRoot = root;
 
+#if 0
+
+	//bvh->mRoot = bvh->mStaticRoot;
+	bvh->mNumNodes = 1;
+
 #else
-	// copy root and set new one for dynamic objects
-	BvhInterior *newRoot = new BvhInterior(NULL);
+	// we are setting a new root node 
+	// and adds one level of indirection to the bvh 
+	// It allows us to use dynamic objects
+
+	// the new bvh has two main branches
+	// a static branch (the old root), and adynamic branch 
+	// we create a 'dynamic' leaf which basically is a container
+	// for all dynamic objects underneath
+
+	// the bounding boxes of the dynamic tree must be updated
+	// once each frame in order to be able to incorporate
+	// the movements of the objects within
+
+	// create new root
+	/*BvhInterior *newRoot = new BvhInterior(NULL);
+	bvh->mRoot = newRoot;
+
+	// the separation is a purely logical one
+	// the bounding boxes of the child nodes are 
+	// identical to those of the root node
+
+	newRoot->mBox = bvh->mStaticRoot->mBox;
+	newRoot->mArea = newRoot->mBox.SurfaceArea();
+
+	newRoot->mFirst = bvh->mStaticRoot->mFirst;
+	newRoot->mLast = bvh->mStaticRoot->mLast;
+
+	// add static root on left subtree
+	newRoot->mFront = bvh->mStaticRoot;
+	bvh->mStaticRoot->mParent = newRoot;
+	*/
+	// create and add dynamic root
+	BvhLeaf *dynamicRoot = new BvhLeaf(NULL);
 	
-	newRoot->mBox = root->mBox;
-	newRoot->mFirst = root->mFirst;
-	newRoot->mLast = root->mLast;
+	dynamicRoot->mFirst = 0;
+	dynamicRoot->mLast = 0;
 
-	// create 'dynamic' leaf which basically is a container
-	// for all dynamic objects
-	BvhLeaf *dynamicLeaf = new BvhLeaf(newRoot);
-	dynamicLeaf->mBox = root->mBox;
+	//dynamicRoot->mBox = bvh->mStaticRoot->mBox;
+	dynamicRoot->mBox = bvh->mRoot->mBox;
+	dynamicRoot->mArea = dynamicRoot->mBox.SurfaceArea();
 
-	newRoot->mFront = root;
-	root->mParent = newRoot;
+	bvh->mDynamicRoot = dynamicRoot;
+	//newRoot->mBack = dynamicRoot;
 
-	newRoot->mBack = dynamicLeaf;
-
-	bvh->mRoot = newRoot;
+	bvh->mNumNodes = 1;
 #endif
 
 	tQueue.push(bvh->mRoot);
-	bvh->mNumNodes = 1;
-
+	
 	while(!tQueue.empty())
 	{
@@ -139,9 +168,10 @@
 	//-- post process nodes
 
+	/// 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
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3066)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3068)
@@ -17,5 +17,5 @@
 
 RenderState::RenderState():
-mRenderTechnique(FORWARD),
+mRenderTechnique(0),
 mUseAlphaToCoverage(true),
 mLockCullFaceEnabled(false),
@@ -320,5 +320,5 @@
 
 
-void RenderState::SetRenderTechnique(RenderTechnique t) 
+void RenderState::SetRenderTechnique(int t) 
 { 
 	mRenderTechnique = t; 
@@ -326,5 +326,5 @@
 
 
-RenderState::RenderTechnique RenderState::GetRenderTechnique() const 
+int RenderState::GetRenderTechnique() const 
 {
 	return mRenderTechnique; 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3062)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3068)
@@ -30,11 +30,7 @@
 public:
 	/** Constructor for a deferred shader taking the requested output image size,
-		the current camera,	and a scaling factor.
-			
-		The parameter scaleFactor must be reciprocal value of the 
-		scale factor used for creating the world space position texture. It is used recover the 
-		exact scene size that was scaled in order to improve floating point precision.
+		the current camera;
 	*/
-	DeferredRenderer(int w, int h, PerspectiveCamera *cam, float scaleFactor);
+	DeferredRenderer(int w, int h, PerspectiveCamera *cam);
 	/** The algorithm renders the scene given an fbo consists of 1 color buffer, 
 		1 position buffer, and 1 normal buffer.
@@ -59,10 +55,12 @@
 	enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT};
 	enum SHADING_METHOD {DEFAULT, SSAO, GI};
-
+	/** Set the samplig method for the indirect illumination
+	*/
 	void SetSamplingMethod(SAMPLING_METHOD s);
-
+	/** Set the shading method (SSAO, SSAO + color bleeding
+	*/
 	void SetShadingMethod(SHADING_METHOD s);
 
-	// hack: store the color buffer idx for the first flipflip-mrt here
+	// hack: store the color buffer idx for the currently used flip flop-MRT here
 	// TODO matt: make this less hacky
 	static int colorBufferIdx;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3066)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3068)
@@ -419,5 +419,5 @@
 	if (!mDynamicEntities.empty())
 	{
-		//UpdateDynamicBranch();
+		UpdateDynamicBranch();
 	}
 }
@@ -427,13 +427,7 @@
 {
 	// q: should we use distance to center rather than the distance to the near plane?
-	
-	// because otherwise problems with big object penetrating the near plane 
-	// (e.g., big objects in the distance which are then always visible)
-	// especially annoying is this problem when using the frustum 
-	// fitting on the visible objects for shadow mapping
-	// on the other hand, distance to near plane can also be used 
-	// for near plane intersection
-	node->mDistance = sNearPlane.Distance(node->GetBox().Center());
-	//node->mDistance = node->GetBox().GetMinDistance(sNearPlane);
+	// distance to near plane can also be used for checking near plane intersection
+	//node->mDistance = sNearPlane.Distance(node->GetBox().Center());
+	node->mDistance = node->GetBox().GetMinDistance(sNearPlane);
 }
 
@@ -441,6 +435,9 @@
 float Bvh::CalcMaxDistance(BvhNode *node) const
 {
+#if 1
 	return node->GetBox().GetMaxDistance(sNearPlane);
 
+#else
+	// use bounding boxes of geometry to determine max dist
 	float maxDist = .0f;
 
@@ -457,4 +454,5 @@
 
 	return maxDist;
+#endif
 }
 
@@ -932,8 +930,6 @@
 
 	mNumVirtualNodes = 0;
-
 	// assign new virtual leaves based on specified #triangles per leaf
 	std::stack<BvhNode *> nodeStack;
-
 	nodeStack.push(mRoot);
 
@@ -1273,20 +1269,22 @@
 void Bvh::UpdateDynamicBranch()
 {
-	BvhNode *dynamicRoot = static_cast<BvhInterior *>(mRoot)->mBack;
-
 	// delete old branch
-	if (!dynamicRoot->IsLeaf())
+	if (!mDynamicRoot->IsLeaf())
 	{
 		cout << "deleting old branch" << endl;
 
-		DEL_PTR(dynamicRoot);
-
-		dynamicRoot = new BvhLeaf(mRoot);
-		dynamicRoot->mBox = mRoot->mBox;
+		DEL_PTR(mDynamicRoot);
+
+		mDynamicRoot = new BvhLeaf(mRoot);
+		mDynamicRoot->mBox = mRoot->mBox;
+
+		mDynamicRoot->mFirst = 0;
+		mDynamicRoot->mLast = 0;
+		mDynamicRoot->mArea = mDynamicRoot->mBox.SurfaceArea();
 	}
 
 	cout << "updating dynamic branch" << endl;
 
-	dynamicRoot = SubdivideLeaf(static_cast<BvhLeaf *>(dynamicRoot), 0, mDynamicEntities);
+	mDynamicRoot = SubdivideLeaf(static_cast<BvhLeaf *>(mDynamicRoot), 0, mDynamicEntities);
 
 	cout << "finished updating dynamic branch" << endl;
@@ -1302,11 +1300,17 @@
 bool Bvh::IntersectsNearPlane(BvhNode *node) const
 { 
-	float distanceToNearPlane = node->GetBox().GetMinDistance(sNearPlane);
+	// note: we have problems with large scale object penetrating the near plane 
+	// (e.g., objects in the distance which are always handled to be visible)
+	// 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
+	
 	// we stored the near plane distance => we can use it also here
-	//float distanceToNearPlane = node->GetDistance();
-	
-	return distanceToNearPlane < sNear;
-}
-
-
-}
+	float distanceToNearPlane = node->GetDistance();
+	//float distanceToNearPlane = node->GetBox().GetMinDistance(sNearPlane);
+	
+	return (distanceToNearPlane < sNear);
+}
+
+
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3066)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3068)
@@ -23,12 +23,4 @@
 		QUERY,
 		RENDER
-	};
-
-	/// the used render technique
-	enum RenderTechnique
-	{
-		FORWARD,
-		DEFERRED,
-		DEPTH_PASS
 	};
 
@@ -59,10 +51,11 @@
 	*/
 	inline int GetCurrentVboId() const { return mCurrentVboId; }
-	/** Sets the render technique (foreward, depth pass, deferred)
+	/** Sets the index of the render technique that is used to render a material. 
+		Initially we use foreward = 0, deferred = 1, depth pass = 1
 	*/
-	void SetRenderTechnique(RenderTechnique t);
+	void SetRenderTechnique(int t);
 	/** See Set
 	*/
-	RenderTechnique GetRenderTechnique() const;
+	int GetRenderTechnique() const;
 	/** If alpha to coverage is instead of alpha testing
 	*/
@@ -87,5 +80,5 @@
 	int mCurrentVboId;
 	/// the current render technique
-	RenderTechnique mRenderTechnique;
+	int mRenderTechnique;
 
 	bool mUseAlphaToCoverage;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3065)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3068)
@@ -96,5 +96,7 @@
 void Material::Render(RenderState *state)
 {
-	mTechniques[state->GetRenderTechnique()]->Render(state);
+	// set technique if available
+	int idx = min((int)mTechniques.size() - 1 , state->GetRenderTechnique());
+	mTechniques[idx]->Render(state);
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 3067)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 3068)
@@ -596,6 +596,5 @@
 	glViewport(0, 0, mSize, mSize);
 
-	glDisable(GL_LIGHTING);
-	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+	// turn off colors + lighting (should be handled by the render state)
 	glShadeModel(GL_FLAT);
 
@@ -607,8 +606,5 @@
 
 	glPopAttrib();
-	
 	glShadeModel(GL_SMOOTH);
-	glEnable(GL_LIGHTING);
-	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 
 #if 0
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3067)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3068)
@@ -62,11 +62,10 @@
 
 
+/// the environment for the program parameter
 static Environment env;
 
-#define MAX_DEPTH_CONST 10.0f
-
 
 GLuint fontTex;
-/// fbo
+/// the fbo used for MRT
 FrameBufferObject *fbo = NULL;
 /// the renderable scene geometry
@@ -91,21 +90,26 @@
 int renderMode = RenderTraverser::CHCPLUSPLUS;
 /// eye near plane distance
-float nearDist = 0.2f;
+const float nearDist = 0.2f;
 /// eye far plane distance
 float farDist = 1e6f;
 /// the field of view
-float fov = 50.0f;
-/// the pixel threshold where a node is still considered invisible
-int threshold;
-
-int assumedVisibleFrames = 10;
-int maxBatchSize = 50;
-int trianglesPerVirtualLeaf = INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES;
+const float fov = 50.0f;
+
 SceneQuery *sceneQuery = NULL;
 RenderQueue *renderQueue = NULL;
-// traverses and renders the hierarchy
+/// traverses and renders the hierarchy
 RenderTraverser *shadowTraverser = NULL;
-
+/// the skylight + skydome model
 SkyPreetham *preetham = NULL;
+
+
+
+/// the technique used for rendering
+enum RenderTechnique
+{
+	FORWARD,
+	DEFERRED,
+	DEPTH_PASS
+};
 
 
@@ -170,5 +174,4 @@
 bool descendKeyPressed = false;
 bool ascendKeyPressed = false;
-
 bool altKeyPressed = false;
 
@@ -207,6 +210,19 @@
 
 
-#define CAMERA_PASS 0
-#define LIGHT_PASS 1
+//////////////
+//-- algorithm parameters
+
+/// the pixel threshold where a node is still considered invisible 
+/// (should be zero for conservative visibility)
+int threshold;
+int assumedVisibleFrames = 10;
+int maxBatchSize = 50;
+int trianglesPerVirtualLeaf = INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES;
+
+//////////////
+
+enum {CAMERA_PASS = 0, LIGHT_PASS = 1};
+
+
 
 //DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::SAMPLING_POISSON;
@@ -495,5 +511,5 @@
 	
 	/// forward rendering is the default
-	state.SetRenderTechnique(RenderState::FORWARD);
+	state.SetRenderTechnique(FORWARD);
 
 	// frame time is restarted every frame
@@ -814,11 +830,5 @@
 	// multisampling does not work with deferred shading
 	glDisable(GL_MULTISAMPLE_ARB);
-
-	state.SetRenderTechnique(RenderState::DEFERRED);
-
-	shaderManager->EnableVertexProfile();
-	shaderManager->EnableFragmentProfile();
-
-	const Vector3 pos = camera->GetPosition();
+	state.SetRenderTechnique(DEFERRED);
 
 
@@ -887,5 +897,5 @@
 	int oldRenderMethod = renderMethod;
 	// for rendering the light view, we use forward rendering
-	if (renderLightView) renderMethod = RenderState::FORWARD;
+	if (renderLightView) renderMethod = FORWARD;
 
 	/// enable vbo vertex array
@@ -899,5 +909,5 @@
 		glEnable(GL_MULTISAMPLE_ARB);
 		
-		state.SetRenderTechnique(RenderState::FORWARD);
+		state.SetRenderTechnique(FORWARD);
 		glEnable(GL_LIGHTING);
 
@@ -913,5 +923,5 @@
 		state.SetUseAlphaToCoverage(false);
 
-		state.SetRenderTechnique(RenderState::DEPTH_PASS);
+		state.SetRenderTechnique(DEPTH_PASS);
 
 		if (!fbo) InitFBO(); fbo->Bind();
@@ -921,8 +931,7 @@
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-		// the scene is rendered withouth any shading	
+		// the scene is rendered withouth any shading 
+		// (should be handled by render state)
 		glShadeModel(GL_FLAT);
-		glDisable(GL_LIGHTING);
-		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
 
@@ -933,13 +942,12 @@
 		glEnable(GL_MULTISAMPLE_ARB);
 		
-		state.SetRenderTechnique(RenderState::DEPTH_PASS);
+		state.SetRenderTechnique(DEPTH_PASS);
 
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-		// the scene is rendered withouth any shading	
+		// the scene is rendered withouth any shading 
+		// (should be handled by render state)
 		glShadeModel(GL_FLAT);
-		glDisable(GL_LIGHTING);
-		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-
+		
 
 		break;
@@ -1014,5 +1022,5 @@
 
 		if (!ssaoShader) ssaoShader = 
-			new DeferredRenderer(texWidth, texHeight, camera, farDist / MAX_DEPTH_CONST);
+			new DeferredRenderer(texWidth, texHeight, camera);
 		
 		DeferredRenderer::SHADING_METHOD shadingMethod;
@@ -1038,5 +1046,5 @@
 
 
-	state.SetRenderTechnique(RenderState::FORWARD);
+	state.SetRenderTechnique(FORWARD);
 	state.Reset();
 
@@ -1855,5 +1863,5 @@
 
 	if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
-		state.SetRenderTechnique(RenderState::DEFERRED);
+		state.SetRenderTechnique(DEFERRED);
 
 	const bool useToneMapping = 
@@ -1874,37 +1882,37 @@
 		if (showShadowMap && !renderLightView)
 		{
-			state.Reset();
-
-			float minVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance());
-			RenderShadowMap(minVisibleDist);
+			// usethe maximal visible distance to focus shadow map
+			float maxVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance());
+			RenderShadowMap(maxVisibleDist);
 		}
 
-		glViewport(0, 0, texWidth, texHeight);
-		// rese the state before the final visible objects pass
-		state.Reset();
+		//glViewport(0, 0, texWidth, texHeight);
+		// initialize deferred rendering
 		InitDeferredRendering();
 	}
 	else
 	{
-		state.SetRenderTechnique(RenderState::FORWARD);
-		// rese the state before the final visible objects pass
-		state.Reset();
-	}
+		state.SetRenderTechnique(FORWARD);
+	}
+
+	/////////////////
+	//-- reset gl state before the final visible objects pass
+
+	state.Reset();
 
 	glEnableClientState(GL_NORMAL_ARRAY);
-
+	/// switch back to smooth shading
 	glShadeModel(GL_SMOOTH);
-	// draw all objects that have exactly the same depth as the current sample
-	glDepthFunc(GL_LEQUAL);
-
-	//glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	/// reset alpha to coverage flag
 	state.SetUseAlphaToCoverage(true);
 	// clear color
 	glClear(GL_COLOR_BUFFER_BIT);
 	
+	// draw only objects having exactly the same depth as the current sample
+	glDepthFunc(GL_EQUAL);
 
 	//cout << "visible: " << (int)traverser->GetVisibleObjects().size() << endl;
 
-	SceneEntityContainer::const_iterator sit, 
+	SceneEntityContainer::const_iterator sit,
 		sit_end = traverser->GetVisibleObjects().end();
 
@@ -1913,7 +1921,8 @@
 		renderQueue->Enqueue(*sit);
 	}
-
+	/// now render out everything in one giant pass
 	renderQueue->Apply();
 
+	// switch back to standard depth func
 	glDepthFunc(GL_LESS);
 	state.Reset();
@@ -1941,15 +1950,12 @@
 void RenderShadowMap(float newfar)
 {
-	shaderManager->DisableVertexProfile();
-	shaderManager->DisableFragmentProfile();
-
 	glDisableClientState(GL_NORMAL_ARRAY);
-
-	state.SetRenderTechnique(RenderState::DEPTH_PASS);
+	state.SetRenderTechnique(DEPTH_PASS);
 	
 	// hack: disable cull face because of alpha textured balconies
 	glDisable(GL_CULL_FACE);
 	state.LockCullFaceEnabled(true);
-	/// don't use alphatocoverage
+
+	/// don't use alpha to coverage for the depth map (problems with fbo rendering)
 	state.SetUseAlphaToCoverage(false);
 
@@ -1974,4 +1980,5 @@
 }
 
+
 /** Toach each material once in order to preload the render queue 
 	bucket id of each material
@@ -1981,5 +1988,5 @@
 	for (int i = 0; i < 3; ++ i)
 	{
-		state.SetRenderTechnique((RenderState::RenderTechnique)i);
+		state.SetRenderTechnique(i);
 
 		// fill all shapes into the render queue	once so we can establish the buckets
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3063)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3068)
@@ -222,14 +222,11 @@
 	glDrawBuffers(1, mrt + index);
 
-	//glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-	glEnd();
-
 	FrameBufferObject::Release();
 }
 
 
-DeferredRenderer::DeferredRenderer(int w, int h, PerspectiveCamera *cam, float scaleFactor):
+DeferredRenderer::DeferredRenderer(int w, int h, PerspectiveCamera *cam):
 mWidth(w), mHeight(h), 
 mCamera(cam),
