Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 3054)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 3065)
@@ -56,11 +56,7 @@
 	*/
 	Technique();
-
-	//Technique(const Technique &tech);
 	/** Sets ambient and diffuse color to color
 	*/
 	Technique(const RgbaColor &color);
-
-	~Technique();
 	/** Renders this technique.
 	*/
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3064)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3065)
@@ -73,57 +73,34 @@
 
 	Bvh *bvh = new Bvh(entities);
-	bvh->mStaticRoot = LoadNextNode(stream, NULL);
+
+	BvhNode *root = LoadNextNode(stream, NULL);
 
 #if 1
-	bvh->mRoot = bvh->mStaticRoot;
-	bvh->mNumNodes = 1;
+
+	bvh->mRoot = root;
+
 #else
+	// copy root and set new one for dynamic objects
+	BvhInterior *newRoot = new BvhInterior(NULL);
+	
+	newRoot->mBox = root->mBox;
+	newRoot->mFirst = root->mFirst;
+	newRoot->mLast = root->mLast;
 
-	// we are setting a new root node 
-	// and adds one level of indirection to the bvh 
-	// It allows us to use dynamic objects
+	// create 'dynamic' leaf which basically is a container
+	// for all dynamic objects
+	BvhLeaf *dynamicLeaf = new BvhLeaf(newRoot);
+	dynamicLeaf->mBox = root->mBox;
 
-	// 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
+	newRoot->mFront = root;
+	root->mParent = newRoot;
 
-	// create new root
-	BvhInterior *newRoot = new BvhInterior(NULL);
+	newRoot->mBack = dynamicLeaf;
+
 	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(newRoot);
-	
-	dynamicRoot->mFirst = 0;
-	dynamicRoot->mLast = 0;
-	dynamicRoot->mBox = bvh->mStaticRoot->mBox;
-	dynamicRoot->mArea = dynamicRoot->mBox.SurfaceArea();
-
-	bvh->mDynamicRoot = dynamicRoot;
-	newRoot->mBack = dynamicRoot;
-
-	bvh->mNumNodes = 1;
-
 #endif
 
-	tQueue.push(bvh->mStaticRoot);
+	tQueue.push(bvh->mRoot);
+	bvh->mNumNodes = 1;
 
 	while(!tQueue.empty())
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3057)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3065)
@@ -86,6 +86,34 @@
 
 
-// q matt: is this function really necessary or would it be better
-// to just create a query material and use the setstate function?
+static Technique GetDummyTechnique()
+{
+	Technique tech;
+	tech.Init();
+
+	tech.SetColorWriteEnabled(true);
+	tech.SetLightingEnabled(true);
+	tech.SetDepthWriteEnabled(false);
+	//tech.SetCullFaceEnabled(false);
+
+	return tech;
+}
+
+/** A special technique for occlusion queries. This material
+	has color write, depth write, and lighting turned off
+*/
+static Technique GetQueryTechnique()
+{
+	Technique tech;
+	tech.Init();
+
+	tech.SetColorWriteEnabled(false);
+	tech.SetLightingEnabled(false);
+	tech.SetDepthWriteEnabled(false);
+	//tech.SetCullFaceEnabled(false);
+
+	return tech;
+}
+
+
 bool RenderState::SetMode(Mode mode)
 {
@@ -101,5 +129,10 @@
 		/// the query box material
 		static Technique queryTech = GetQueryTechnique();
-		SetState(&queryTech);
+		static Technique dummyTech = GetDummyTechnique();
+
+		//if ((float)rand() / RAND_MAX < 0.2)
+		//	SetState(&dummyTech);
+		//else 
+			SetState(&queryTech);
 	}
 
@@ -286,18 +319,4 @@
 
 
-Technique RenderState::GetQueryTechnique()
-{
-	Technique tech;
-
-	tech.Init();
-
-	tech.SetColorWriteEnabled(false);
-	tech.SetLightingEnabled(false);
-	tech.SetDepthWriteEnabled(false);
-
-	return tech;
-}
-
-
 void RenderState::SetUseAlphaToCoverage(bool useAlphaToCoverage)
 {
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 3064)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 3065)
@@ -62,30 +62,29 @@
 	*/
 	BvhNode(BvhNode *parent);
+	/** Virtual destructor doing nothing.
+	*/
+	virtual ~BvhNode();
+	/** Reset the status of this node.
+	*/
+	virtual void ResetVisibility();
 	/** Returns true if this node is a leaf.
 	*/
-	//virtual bool IsPseudoLeaf() = 0;
-	/** Virtual destructor doing nothing.
-	*/
-	virtual ~BvhNode();
+	virtual bool IsLeaf() const = 0;
 	/** Depth of this node in the tree.
 	*/
-	inline int GetDepth() const {return mDepth;}
+	inline int GetDepth() const;
 	/** Returns parent of this bvh node, NULL if it is root.
 	*/
-	inline BvhNode *GetParent() {return mParent;}
+	inline BvhNode *GetParent();
 	/** Number of leaves in the subtree induced by this node.
 	*/
-	inline int GetNumLeaves() const {return mNumLeaves;}
-	/** Reset the status of this node.
-	*/
-	virtual void ResetVisibility();
-
-	virtual bool IsLeaf() const = 0;
-
-	bool IsVirtualLeaf() const { return mIsVirtualLeaf; }
-
+	inline int GetNumLeaves() const;
+	/** Returns true if this node is a "virtual" leaf,
+		i.e., the node is handled as a leaf during traversal.
+	*/
+	inline bool IsVirtualLeaf() const;
 	/** Returns the stored distance to the near plane.
 	*/
-	float GetDistance() const  { return mDistance; }
+	inline float GetDistance() const;
 
 
@@ -131,11 +130,11 @@
 	/** Returns the bounding box of this node.
 	*/
-	inline const AxisAlignedBox3 &GetBox() { return mBox; }
+	inline const AxisAlignedBox3 &GetBox() const;
 	/** Return index of this node.
 	*/
-	inline int GetId() const { return mId; }
+	inline int GetId() const;
 	/** See get
 	*/
-	inline void SetId(int id) { mId = id; }
+	inline void SetId(int id);
 
 
@@ -160,5 +159,5 @@
 		order to not break temporal coherency.
 	*/
-	static void SetCurrentState(int _state) { sCurrentState = _state; }
+	inline static void SetCurrentState(int _state) { sCurrentState = _state; }
 
 
@@ -174,5 +173,5 @@
 
 	//////////////
-	//-- members that define the current state. There is one state for each camera
+	//-- members definig the current visibility state (one state per camera)
 
 	/// the currently used state
@@ -197,26 +196,4 @@
 	/// #leaves under this node
 	int mNumLeaves;
-
-
-	////////////
-	//-- rendering related options
-	
-	
-	// Indices to first and last triangle in the triangle array
-	// assumes the triangle are placed in continuous chunk of memory
-	// however this need not be a global array!
-	
-	/// the index of the first triangle
-	int mFirst;
-	/// the index of the last triangle
-	int mLast;
-
-	/// the bounding boxes of these nodes are queries instead of the current node
-	int mTestNodesIdx;
-	/// the number of tighter bounding volumes used for this node
-	int mNumTestNodes;
-	/// used for efficient element array rendering
-	int mIndicesPtr;
-
 	/// Area of this node
 	float mArea;
@@ -236,4 +213,28 @@
 	*/
 	bool mIsMaxDepthForVirtualLeaf;
+
+
+	////////////
+	//-- rendering related options
+	
+	// Indices to first and last triangle in the triangle array
+	// assumes the triangle are placed in continuous chunk of memory
+	// however this need not be a global array!
+	
+	/// the index of the first triangle
+	int mFirst;
+	/// the index of the last triangle
+	int mLast;
+
+
+	////////////////
+	//-- rendering related options
+
+	/// the bounding boxes of the test nodes are queries instead of the actual node
+	int mTestNodesIdx;
+	/// the number of these test nodes
+	int mNumTestNodes;
+	/// used for efficient element array rendering
+	int mIndicesPtr;
 };
 
@@ -357,5 +358,50 @@
 }
 
-
+int BvhNode::GetDepth() const 
+{ 
+	return mDepth; 
+}
+
+
+BvhNode *BvhNode::GetParent() 
+{ 
+	return mParent; 
+}
+
+
+int BvhNode::GetNumLeaves() const
+{
+	return mNumLeaves;
+}
+
+
+bool BvhNode::IsVirtualLeaf() const
+{ 
+	return mIsVirtualLeaf; 
+}
+
+
+float BvhNode::GetDistance() const
+{
+	return mDistance; 
+}
+
+	
+const AxisAlignedBox3 &BvhNode::GetBox() const
+{
+	return mBox; 
+}
+
+
+int BvhNode::GetId() const 
+{
+	return mId; 
+}
+
+
+void BvhNode::SetId(int id) 
+{
+	mId = id; 
+}
 
 
@@ -383,4 +429,5 @@
 	virtual ~BvhInterior();
 
+
 protected:
 
@@ -409,5 +456,5 @@
 	a lower distance has a higher value in the queue
 */
-class mygreaterdistance
+class GtDistance
 {
 public:
@@ -418,5 +465,5 @@
 };
 
-typedef std::priority_queue<BvhNode *, std::vector<BvhNode *>, mygreaterdistance> TraversalQueue;
+typedef std::priority_queue<BvhNode *, std::vector<BvhNode *>, GtDistance> TraversalQueue;
 
 
@@ -464,23 +511,30 @@
 	/** Returns number of bvh nodes.
 	*/
-	inline int GetNumNodes() const { return mNumNodes; }
+	inline int GetNumNodes() const;
 	/** Returns number of bvh leaves.
 	*/
-	inline int GetNumLeaves() const { return mNumNodes / 2 + 1;}
+	inline int GetNumLeaves() const;
 	/** Returns number of 'virtual' nodes in the hierarchy, i.e.
 		the number of nodes actually used for traversal.
 	*/
-	int GetNumVirtualNodes() const  { return mNumVirtualNodes; }
+	inline int GetNumVirtualNodes() const;
 	/** Returns number of bvh leaves.
 	*/
-	inline int GetNumVirtualLeaves() const { return mNumVirtualNodes / 2 + 1;}
+	inline int GetNumVirtualLeaves() const;
 	/** Returns root node of the bvh.
 	*/
-	BvhNode *GetRoot() { return mRoot; }
-	
+	inline BvhNode *GetRoot();
+	/** Returns the bounding box of this bvh.
+	*/
+	inline const AxisAlignedBox3 &GetBox() const;
+	/** Returns true if the current bvh node intersects the near plane.
+	*/
+	bool IntersectsNearPlane(BvhNode *node) const; 
+
+
 	///////////////
 	//-- functions collecting nodes based on some criteria
 
-	/** Collect all nodes higher or equal to a given depth.
+	/** Collect all child nodes in a given depth from the specified node.
 	*/
 	void CollectNodes(BvhNode *node, BvhNodeContainer &nodes, int depth);
@@ -489,5 +543,5 @@
 	*/
 	void CollectLeaves(BvhNode *node, BvhLeafContainer &leaves);
-	/** Collect only the virtual leaves (can be anywhere in the Žhierarchy).
+	/** Collect only the virtual leaves (can be anywhere in the hierarchy).
 	*/
 	void CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves);
@@ -512,7 +566,5 @@
 	*/
 	int RenderBounds(const BvhNodeContainer &nodes, RenderState *state, bool useTightBounds);
-	/** Returns the bounding box of this bvh.
-	*/
-	inline const AxisAlignedBox3 &GetBox() { return mBox; }
+	
 
 
@@ -656,5 +708,6 @@
 
 	/////////////////////////////
-	// functions that are required to build the dynamic part of the hierarchy
+	//-- functions used to construct the dynamic part of the hierarchy
+
 	int SortTriangles(BvhLeaf *leaf, 
 		              int axis, 
@@ -686,5 +739,5 @@
 	BvhNode *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;
@@ -741,4 +794,46 @@
 };
 
+
+
+/////////////////
+//-- public inline functions
+
+
+int Bvh::GetNumNodes() const
+{
+	return mNumNodes; 
+}
+
+
+int Bvh::GetNumLeaves() const 
+{ 
+	return mNumNodes / 2 + 1;
+}
+
+
+int Bvh::GetNumVirtualNodes() const  
+{ 
+	return mNumVirtualNodes; 
+}
+
+
+int Bvh::GetNumVirtualLeaves() const
+{
+	return mNumVirtualNodes / 2 + 1;
+}
+
+
+BvhNode *Bvh::GetRoot()
+{ 
+	return mRoot; 
+}
+
+
+const AxisAlignedBox3 &Bvh::GetBox() const 
+{ 
+	return mBox; 
+}
+
+
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3064)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3065)
@@ -27,12 +27,10 @@
 #endif
 
+#define INVALID_TEST ((unsigned int)-1)
+
+using namespace std;
 
 namespace CHCDemoEngine
 {
-
-#define INVALID_TEST ((unsigned int)-1)
-
-using namespace std;
-
 
 int BvhNode::sCurrentState = 0;
@@ -76,4 +74,5 @@
 
 static Plane3 sNearPlane;
+static float sNear;
 static Frustum sFrustum;
 
@@ -147,7 +146,7 @@
 
 
-/***********************************************************/
-/*                class Bvh implementation                 */
-/***********************************************************/
+/**********************************************************/
+/*                class Bvh implementation                */
+/**********************************************************/
 
 
@@ -183,5 +182,4 @@
 
 	if (mRoot) delete mRoot;
-
 	// delete vbo
 	glDeleteBuffersARB(1, &mVboId);
@@ -191,5 +189,5 @@
 void Bvh::Init()
 {
-	mStaticRoot = NULL;
+	//mStaticRoot = NULL;
 	mDynamicRoot = NULL;
 	mRoot = NULL;
@@ -202,5 +200,5 @@
 	
 	// nodes are tested using the subnodes from 3 levels below
-	mMaxDepthForTestingChildren = 3;
+	mMaxDepthForTestingChildren = 0;//3;
 	//mMaxDepthForTestingChildren = 4;
 
@@ -311,5 +309,5 @@
 		tStack.pop();
 
-		// found depth => take this node
+		// found node in specified depth => take this node
 		if ((d == depth) || (node->IsLeaf()))
 		{
@@ -338,6 +336,9 @@
 	// do the test only if necessary
 	if (!(node->mPlaneMask[BvhNode::sCurrentState] & (1 << i)))
+	{
 		return true;
-			
+	}
+
+
 	////////
 	//-- test the n-vertex
@@ -375,5 +376,5 @@
 
 	////////
-	//-- apply frustum culling for the planes [mPreferredPlane - 5]
+	//-- apply frustum culling for the planes with index mPreferredPlane to 6
 
 	for (int i = node->mPreferredPlane[BvhNode::sCurrentState]; i < 6; ++ i)
@@ -382,5 +383,5 @@
 
 	//////////
-	//-- do the view frustum culling for the planes [0 - m_iPreferredPlane)
+	//-- apply frustum culling for the planes with index 0 to mPreferredPlane
 
 	for (int i = 0; i < node->mPreferredPlane[BvhNode::sCurrentState]; ++ i)
@@ -413,4 +414,5 @@
 	// store near plane
 	sNearPlane = Plane3(cam->GetDirection(), cam->GetPosition());
+	sNear = cam->GetNear();
 
 	// rebuild dynamic part of the hierarchy
@@ -424,12 +426,14 @@
 void Bvh::UpdateDistance(BvhNode *node) const
 {
-	// use distance to center rather than the distance to the near plane because
-	// otherwise problems with big object penetrating the near plane 
+	// 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
-
-	//node->mDistance = node->GetBox()GetMinDistance(sNearPlane);
+	// 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);
 }
 
@@ -458,17 +462,8 @@
 void Bvh::RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds)
 {
-	// if not using tight bounds, rendering boxes in immediate mode
-	// is preferable to vertex arrays (less setup time)
-	if (!useTightBounds)
-	{
-		RenderBoundingBoxImmediate(node->GetBox());
-	}
-	else
-	{
-		// hack: use dummy wrapper in order to use function
-		static BvhNodeContainer dummy(1);
-		dummy[0] = node;
-		RenderBounds(dummy, state, useTightBounds);
-	}
+	// hack: use dummy contayiner as wrapper in order to use multibox function
+	static BvhNodeContainer dummy(1);
+	dummy[0] = node;
+	RenderBounds(dummy, state, useTightBounds);
 }
 
@@ -490,9 +485,9 @@
 			RenderBoundingBoxImmediate((*nit)->GetBox());
 		}
-cout<<"y";
+
 		renderedBoxes = (int)nodes.size();
 	}
 	else
-	{cout<<"x";
+	{
 		renderedBoxes = PrepareBoundsWithDrawArrays(nodes);
 		RenderBoundsWithDrawArrays(renderedBoxes, state);
@@ -529,5 +524,4 @@
 	{
 		BvhNode *node = *nit;
-
         const int numIndices = node->mNumTestNodes * sNumIndicesPerBox;
 		
@@ -569,5 +563,5 @@
 	// collect bvh nodes
 	BvhNodeContainer nodes;
-	CollectNodes(mStaticRoot, nodes);
+	CollectNodes(mRoot, nodes);
 
 	cout << "creating new indices" << endl;
@@ -587,5 +581,4 @@
 	}
 
-
 	cout << "creating global indices buffer" << endl;
 
@@ -607,8 +600,7 @@
 		// resize array
 		node->mIndicesPtr = mCurrentIndicesPtr;
-
 		int numIndices = 0;
 
-		// the bounding box of the test nodes are rendered instead of the root node
+		// the bounding boxes of the test nodes are rendered instead of the node itself
 		// => store their indices
 		for (int i = 0; i < node->mNumTestNodes; ++ i, numIndices += sNumIndicesPerBox)
@@ -657,5 +649,5 @@
 
 	nodes.reserve(GetNumStaticNodes());
-	CollectNodes(mStaticRoot, nodes);
+	CollectNodes(mRoot, nodes);
 
 	const unsigned int bufferSize = 8 * (int)nodes.size();
@@ -717,5 +709,5 @@
 	// collect all nodes
 	BvhNodeContainer nodes;
-	CollectNodes(mStaticRoot, nodes);
+	CollectNodes(mRoot, nodes);
 
 	cout << "recomputing bounds, children will be tested in depth " << mMaxDepthForTestingChildren << endl;
@@ -930,5 +922,5 @@
 	// first invalidate old virtual leaves
 	BvhNodeContainer leaves;
-	CollectVirtualLeaves(mStaticRoot, leaves);
+	CollectVirtualLeaves(mRoot, leaves);
 
 	BvhNodeContainer::const_iterator bit, bit_end = leaves.end();
@@ -998,5 +990,5 @@
 
 	std::stack<BvhNode *> nodeStack;
-	nodeStack.push(mStaticRoot);
+	nodeStack.push(mRoot);
 
 	while (!nodeStack.empty()) 
@@ -1039,35 +1031,33 @@
 	const Vector3 u = box.Max();
 
+	///////////
+	//-- render AABB as triangle strips
+
 	glBegin(GL_TRIANGLE_STRIP);
-	{
-		///////////
-		//-- render AABB as triangle strips
-
-		// render first half of AABB
-		glVertex3f(l.x, l.y, u.z);
-		glVertex3f(u.x, l.y, u.z);
-		glVertex3f(l.x, u.y, u.z);
-		glVertex3f(u.x, u.y, u.z);
-		glVertex3f(l.x, u.y, l.z); 
-		glVertex3f(u.x, u.y, l.z);
-		glVertex3f(l.x, l.y, l.z); 
-		glVertex3f(u.x, l.y, l.z);
-
-		glPrimitiveRestartNV();
-
-		// render second half of AABB
-		glVertex3f(l.x, u.y, u.z); 
-		glVertex3f(l.x, u.y, l.z);
-		glVertex3f(l.x, l.y, u.z);
-		glVertex3f(l.x, l.y, l.z);
-		glVertex3f(u.x, l.y, u.z); 
-		glVertex3f(u.x, l.y, l.z);
-		glVertex3f(u.x, u.y, u.z); 
-		glVertex3f(u.x, u.y, l.z);
-	}
+
+	// render first half of AABB
+	glVertex3f(l.x, l.y, u.z);
+	glVertex3f(u.x, l.y, u.z);
+	glVertex3f(l.x, u.y, u.z);
+	glVertex3f(u.x, u.y, u.z);
+	glVertex3f(l.x, u.y, l.z); 
+	glVertex3f(u.x, u.y, l.z);
+	glVertex3f(l.x, l.y, l.z); 
+	glVertex3f(u.x, l.y, l.z);
+
+	glPrimitiveRestartNV();
+
+	// render second half of AABB
+	glVertex3f(l.x, u.y, u.z); 
+	glVertex3f(l.x, u.y, l.z);
+	glVertex3f(l.x, l.y, u.z);
+	glVertex3f(l.x, l.y, l.z);
+	glVertex3f(u.x, l.y, u.z); 
+	glVertex3f(u.x, l.y, l.z);
+	glVertex3f(u.x, u.y, u.z); 
+	glVertex3f(u.x, u.y, l.z);
+
 	glEnd();
 }
-
-
 
 
@@ -1123,10 +1113,10 @@
 							 bool useTightBounds)
 {
-	glDisable(GL_TEXTURE_2D);
-	glDisable(GL_LIGHTING);
 	glColor3f(1, 1, 1);
 
 	// hack: for deferred shading we have to define a material
 	static Technique boxMat;
+	boxMat.SetLightingEnabled(false);
+
 	boxMat.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
 
@@ -1135,5 +1125,8 @@
 	if (!useTightBounds)
 	{
-		RenderBoxForViz(node->GetBox());
+		//RenderBoxForViz(node->GetBox());
+		glPolygonMode(GL_FRONT, GL_LINE);
+		RenderBoundingBoxImmediate(node->GetBox());
+		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 	}
 	else
@@ -1144,7 +1137,4 @@
 		}
 	}
-
-	glEnable(GL_LIGHTING);
-	glEnable(GL_TEXTURE_2D);
 }
 
@@ -1297,3 +1287,13 @@
 
 
-}
+bool Bvh::IntersectsNearPlane(BvhNode *node) const
+{ 
+	float distanceToNearPlane = node->GetBox().GetMinDistance(sNearPlane);
+	// we stored the near plane distance => we can use it also here
+	//float distanceToNearPlane = node->GetDistance();
+	
+	return distanceToNearPlane < sNear;
+}
+
+
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3061)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3065)
@@ -73,9 +73,4 @@
 protected:
 
-	/** A special technique for occlusion queries. This material
-		has color write, depth write, and lighting turned off
-	*/
-	static Technique GetQueryTechnique();
-
 
 	//////////////////
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCTraverser.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCTraverser.cpp	(revision 2818)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCTraverser.cpp	(revision 3065)
@@ -71,5 +71,5 @@
 				// for near plane intersecting bounding box possible 
 				// wrong results => skip occlusion query
-				if (IntersectsNearPlane(node))
+				if (mBvh->IntersectsNearPlane(node))
 				{
 					// update node's visited flag
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/StopAndWaitTraverser.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/StopAndWaitTraverser.cpp	(revision 2802)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/StopAndWaitTraverser.cpp	(revision 3065)
@@ -25,5 +25,5 @@
 		{
 			// if intersects near plane assume visible and don't issue test
-			if (IntersectsNearPlane(node))
+			if (mBvh->IntersectsNearPlane(node))
 			{
 				TraverseNode(node);
@@ -33,5 +33,5 @@
 				OcclusionQuery *query = IssueOcclusionQuery(node);
 
-				const bool visible = query->GetQueryResult() > mVisibilityThreshold;
+				const bool visible = (query->GetQueryResult() > mVisibilityThreshold);
 			
 				if (visible)
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3058)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3065)
@@ -36,5 +36,5 @@
 	/// if depth write should be enabled
 	mDepthWriteEnabled = true;
-
+	/// the render queue bucket this technique is assigned to
 	mRenderQueueBucket = NULL;
 }
@@ -60,33 +60,4 @@
 }
 
-/*
-Technique::Technique(const Technique &tech)
-{
-	mAmbientColor = tech.mAmbientColor;
-	mDiffuseColor = tech.mDiffuseColor;
-	mSpecularColor = tech.mSpecularColor;
-	mEmmisiveColor = tech.mEmmisiveColor;
-
-	mVertexProgram = tech.mVertexProgram;
-	mFragmentProgram = tech.mFragmentProgram;
-
-	mAlphaTestEnabled = tech.mAlphaTestEnabled;
-	mCullFaceEnabled = tech.mCullFaceEnabled;
-
-	mTexture = tech.mTexture;
-
-	mVertexProgramParameters = tech.mVertexProgramParameters;
-	mFragmentProgramParameters = tech.mFragmentProgramParameters;
-
-	mColorWriteEnabled = tech.mColorWriteEnabled;
-	mLightingEnabled = tech.mLightingEnabled;
-	mDepthWriteEnabled = tech.mDepthWriteEnabled;
-}*/
-
-
-Technique::~Technique()
-{
-}
-
 
 void Technique::Render(RenderState *state)
@@ -103,6 +74,5 @@
 void Technique::SetFragmentProgram(ShaderProgram *p) 
 { 
-	mFragmentProgram = p; 
-
+	mFragmentProgram = p;
 	mFragmentProgramParameters.Reset();
 	mFragmentProgramParameters.SetProgram(p);
@@ -113,5 +83,4 @@
 { 
 	mVertexProgram = p; 
-
 	mVertexProgramParameters.Reset();
 	mVertexProgramParameters.SetProgram(p);
@@ -119,7 +88,8 @@
 
 
-/***********************************************/
-/*        class Material implementation        */
-/***********************************************/
+
+/***************************************************/
+/*          class Material implementation          */
+/***************************************************/
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3064)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3065)
@@ -178,6 +178,6 @@
 
 bool useOptimization = false;
-//bool useTightBounds = true;
-bool useTightBounds = false;
+bool useTightBounds = true;
+//bool useTightBounds = false;
 bool useRenderQueue = true;
 bool useMultiQueries = true;
@@ -1765,5 +1765,4 @@
 
 		glEnable(GL_TEXTURE_2D);
-
 		myfont.Begin();
 
@@ -1771,11 +1770,11 @@
 		{
 			glColor3f(0.0f, 1.0f, 0.0f);
-
 			int i = 0;
 
-			static char *renderMethodStr[] = {"forward", "depth pass + forward", "deferred shading", "depth pass + deferred"};
+			static char *renderMethodStr[] = 
+				{"forward", "depth pass + forward", "deferred shading", "depth pass + deferred"};
 	
 			sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d", 
-							useMultiQueries, useTightBounds, useRenderQueue);
+					useMultiQueries, useTightBounds, useRenderQueue);
 
 			sprintf(msg[i ++], "render technique: %s, SSAO: %d", renderMethodStr[renderMethod], useAdvancedShading);
@@ -1831,5 +1830,5 @@
 		
 		if (!showAlgorithmTime)
-				sprintf(msg[7], "%s:  %6.1f fps", alg_str[renderMode], fps);
+			sprintf(msg[7], "%s:  %6.1f fps", alg_str[renderMode], fps);
 		else
 			sprintf(msg[7], "%s:  %6.1f ms", alg_str[renderMode], rTime);
@@ -1850,5 +1849,5 @@
 void RenderSky()
 {
-	buddha->Render(&state);
+	//buddha->Render(&state);
 
 	if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCPlusPlusTraverser.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCPlusPlusTraverser.cpp	(revision 3038)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCPlusPlusTraverser.cpp	(revision 3065)
@@ -123,5 +123,5 @@
 				// for near plane intersecting bounding box possible 
 				// wrong results => skip occlusion query
-				if (IntersectsNearPlane(node))
+				if (mBvh->IntersectsNearPlane(node))
 				{
 					// update node's visited flag
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.h	(revision 2951)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.h	(revision 3065)
@@ -154,7 +154,4 @@
 	*/
 	void IssueOcclusionQuery(const OcclusionQuery &query);
-	/** Retunrs true if the current bvh node intersects the near plane.
-	*/
-	inline bool IntersectsNearPlane(BvhNode *node) const; 
 	/** Enqueues a bvh node for distance traversal
 	*/
@@ -216,14 +213,7 @@
 
 
-inline bool RenderTraverser::IntersectsNearPlane(BvhNode *node) const
-{ 
-	return node->GetDistance() < mCamera->GetNear();
 }
 
 
 
-}
-
-
-
 #endif // RENDERTRAVERSER_H
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 3021)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 3065)
@@ -56,5 +56,5 @@
 {
 	return (mMin == Vector3(-MAXFLOAT)) || 
-		(mMax == Vector3(-MAXFLOAT));
+		   (mMax == Vector3(-MAXFLOAT));
 }
 
@@ -216,10 +216,10 @@
 AxisAlignedBox3::IsInside(const Vector3 &v) const
 {
-	return ! ((v.x < mMin.x) ||
-		(v.x > mMax.x) ||
-		(v.y < mMin.y) ||
-		(v.y > mMax.y) ||
-		(v.z < mMin.z) ||
-		(v.z > mMax.z));
+	return !((v.x < mMin.x) ||
+		    (v.x > mMax.x) ||
+			(v.y < mMin.y) ||
+			(v.y > mMax.y) ||
+			(v.z < mMin.z) ||
+			(v.z > mMax.z));
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3064)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3065)
@@ -4,5 +4,6 @@
 assumedVisibleFrames=10
 maxBatchSize=50
-trianglesPerVirtualLeaf=300
+#trianglesPerVirtualLeaf=300
+trianglesPerVirtualLeaf=30000
 keyForwardMotion=20.0f
 keyRotation=1.5f
