Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3199)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3202)
@@ -10,6 +10,6 @@
 //#define NUM_SAMPLES 24
 
-//#define SAMPLE_INTENSITY 0.5f
-#define SAMPLE_INTENSITY 1.0f
+//#define SAMPLE_INTENSITY 0.1f
+#define SAMPLE_INTENSITY 2.0f
 
 #define SAMPLE_RADIUS 8e-1f
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h	(revision 3070)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h	(revision 3202)
@@ -17,4 +17,6 @@
 
 
+/** A very simple ray structure.
+*/
 struct SimpleRay
 {
@@ -22,7 +24,4 @@
 
 	SimpleRay(const Vector3 &o, const Vector3 &d): mOrigin(o), mDirection(d) {}
-
-	Vector3 mOrigin;
-	Vector3 mDirection;
 
 	Vector3 Extrap(const float t) const 
@@ -30,4 +29,11 @@
 		return mOrigin + mDirection * t;
 	}
+
+
+	//////////////7
+	
+	Vector3 mOrigin;
+	Vector3 mDirection;
+
 };
 
@@ -45,6 +51,11 @@
 	//-- Constructors.
 
+	/** Default constructor creating an existing box.
+		The user has to call Initialize once in order
+		to be able to use the box.
+	*/
 	AxisAlignedBox3();
-
+	/** Constructor taking the minimum and maximum as parameters.
+	*/
 	AxisAlignedBox3(const Vector3 &nMin, const Vector3 &nMax);
 	/** initialization to the non existing bounding box
@@ -57,11 +68,15 @@
 	*/
 	Vector3 Diagonal() const;
-
+	/** Computes the mid point in the given axis.
+	*/
 	float Center(const int axis) const;
-
+	/** The vertex of the box with the smallest values in all dimensions.
+	*/
 	float Min(const int axis) const;
-
+	/** The vertex of the box with the largest values in all dimensions.
+	*/
 	float Max(const int axis) const;
-
+	/** The extent of the box in the given axis.
+	*/
 	float Size(const int axis) const;
 	/** Returns axis where box has largest extent.
@@ -71,27 +86,42 @@
 	*/
 	const Vector3& Min() const;
+	/** See Min
+	*/
 	const Vector3& Max() const;
-
+	/** Enlarges box in all dimensions by adding with the given value.
+	*/
 	void Enlarge(const Vector3 &v);
-
+	/** If the box is degenerate, this function enlarges the box
+		to the minimum valid size.
+	*/
 	void EnlargeToMinSize();
-
+	/** Sets a new minimum of the box.
+	*/
 	void SetMin(const Vector3 &v);
-
+	/** Sets a new maximum of the box.
+	*/
 	void SetMax(const Vector3 &v);
-
+	/** Sets the minimum for one dimension.
+	*/
 	void SetMin(int axis, const float value);
-
+	/** Sets the maximum for one dimension.
+	*/
 	void SetMax(int axis, const float value);
 	/** Decrease box by given splitting plane
 	*/
 	void Reduce(int axis, int right, float value); 
-
+	/** Returns true if the box intersects the line given by the end
+		vertices.
+	*/
 	bool Intersects(const Vector3 &lStart, const Vector3 &lEnd) const;
-
-	// the size of the box along all the axes
+	/** The size of the box along all the axes
+	*/
 	Vector3 Size() const;
-	float Radius() const { return 0.5f * Magnitude(Size()); }
-	float SqrRadius() const { return 0.5f * SqrMagnitude(Size()); }
+	/** The diagonal radius of the box.
+	*/
+	float Radius() const;
+	/** The square radius.
+	*/
+	float SqrRadius() const;
 	/** Return whether the box is unbounded.  Unbounded boxes appear
 		when unbounded objects such as quadric surfaces are included.
@@ -99,5 +129,6 @@
 	bool Unbounded() const;
 
-	// Expand the axis-aligned box to include the given object.
+	/** Expand the axis-aligned box to include the given object.
+	*/
 	void Include(const Vector3 &newpt);
 	void Include(const AxisAlignedBox3 &bbox);
@@ -111,5 +142,4 @@
 	*/
 	bool Includes(const AxisAlignedBox3 &b) const;
-
 	/** Returns true if this point is inside box.
 	*/
@@ -160,21 +190,29 @@
 				 ID_Top = 5};
 
-	// Writes a brief description of the object, indenting by the given
-	// number of spaces first.
+	/** Writes a brief description of the object, indenting by the given
+	    number of spaces first.
+	*/
 	virtual void Describe(std::ostream& app, int ind) const;
 
-	// For edge .. number <0..11> returns two incident vertices
+	/** For edge .. number <0..11> returns two incident vertices
+	*/
 	void GetEdge(int edge, Vector3 *a, Vector3 *b) const;
-
-	// Compute the coordinates of one vertex of the box for 0/1 in each axis
-	// 0 .. smaller coordinates, 1 .. large coordinates
+	/** Compute the coordinates of one vertex of the box for 0/1 in each axis
+	    0 .. smaller coordinates, 1 .. large coordinates
+	*/
 	Vector3 GetVertex(int xAxis, int yAxis, int zAxis) const;
-
-	// Compute the vertex for number N=<0..7>, N = 4*x + 2*y + z, where
-	// x,y,z are either 0 or 1; (0 .. lower coordinate, 1 .. large coordinate)
-	// (xmin,ymin, zmin) .. N = 0, (xmax, ymax, zmax) .. N= 7
+	/** Compute the vertex for number N=<0..7>, N = 4*x + 2*y + z, where
+	    x,y,z are either 0 or 1; (0 .. lower coordinate, 1 .. large coordinate)
+	   (xmin,ymin, zmin) .. N = 0, (xmax, ymax, zmax) .. N= 7
+    */
 	void GetVertex(int N, Vector3 &vertex) const;
-
+	/** Convencience method that returns a vertex internally using the 
+		above routine.
+	*/
 	Vector3 GetVertex(int N) const;
+	/** Uses different vertex order: vertices are chosen so that they
+		only differ by one value (min or max) in the x, y, z triple
+	*/
+	void GetVertex2(int N, Vector3 &vertex) const;
 	/** get the extent of face.
 	*/
@@ -322,4 +360,16 @@
 // --------------------------------------------------------------------------
 // Implementation of inline (member) functions
+
+inline float AxisAlignedBox3::Radius() const 
+{ 
+	return 0.5f * Magnitude(Size()); 
+}
+
+
+inline float AxisAlignedBox3::SqrRadius() const 
+{ 
+	return 0.5f * SqrMagnitude(Size()); 
+}
+
 
 inline bool
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3201)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3202)
@@ -61,5 +61,5 @@
 
 
-const static int sNumIndicesPerBox = 20;
+const static int NUM_INDICES_PER_BOX = 20;
 
 /*	Order of vertices
@@ -548,4 +548,16 @@
 	else
 	{
+		BvhNodeContainer::const_iterator nit, nit_end = nodes.end();
+
+		/*for (nit = nodes.begin(); nit != nit_end; ++ nit)
+		{
+			BvhNode *node = *nit;
+
+			for (int i = 0; i < node->mNumTestNodes; ++ i)
+			{
+				RenderBoundingBoxImmediate(mTestNodes[node->mTestNodesIdx + i]->GetBox());
+			}
+		}*/
+
 		renderedBoxes = PrepareBoundsWithDrawArrays(nodes);
 		RenderBoundsWithDrawArrays(renderedBoxes, state);
@@ -582,8 +594,8 @@
 	{
 		BvhNode *node = *nit;
-        const int numIndices = node->mNumTestNodes * sNumIndicesPerBox;
+        const int numIndices = node->mNumTestNodes * NUM_INDICES_PER_BOX;
 		
 		// copy indices
-		memcpy(mIndices + numNodes * sNumIndicesPerBox, 
+		memcpy(mIndices + numNodes * NUM_INDICES_PER_BOX, 
 			   mTestIndices + node->mIndicesPtr, 
 			   numIndices * sizeof(unsigned int));
@@ -610,6 +622,6 @@
 	}
 
-	// we do use the last or the first index (they are generate and only used to connect strips)
-	int numElements = numNodes * sNumIndicesPerBox - 1;
+	// we don't use the last or the first index (they are generate and only used to connect strips)
+	int numElements = numNodes * NUM_INDICES_PER_BOX - 1;
 	// don't render first degenerate index
 	glDrawElements(GL_TRIANGLE_STRIP, numElements, GL_UNSIGNED_INT, mIndices + 1); 
@@ -621,5 +633,9 @@
 	// collect bvh nodes
 	BvhNodeContainer nodes;
-	CollectNodes(mRoot, nodes);
+	// first collect dynamic nodes so we make sure that they are in the beginning
+	CollectNodes(mDynamicRoot, nodes);
+	// then collect static nodes
+	CollectNodes(mStaticRoot, nodes);
+	//CollectNodes(mRoot, nodes);
 
 	cout << "creating new indices" << endl;
@@ -631,5 +647,5 @@
 	for (lit = nodes.begin(); lit != lit_end; ++ lit)
 	{
-		int offset = (*lit)->mNumTestNodes * sNumIndicesPerBox;
+		int offset = (*lit)->mNumTestNodes * NUM_INDICES_PER_BOX;
 #ifdef ALIGN_INDICES
 		// align with 32 in order to speed up memcopy
@@ -641,9 +657,9 @@
 	cout << "creating global indices buffer" << endl;
 
-	if (mIndices) delete [] mIndices;
+	if (mIndices)     delete [] mIndices;
 	if (mTestIndices) delete [] mTestIndices;
 
 	// global buffer: create it once so we don't have
-	// to allocate memory from the chunks of the node
+	// to allocate memory for each individual chunk of the node
 	mIndices = new unsigned int[numMaxIndices];
 	// create new index buffer for the individual nodes
@@ -662,10 +678,10 @@
 		// 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)
+		for (int i = 0; i < node->mNumTestNodes; ++ i, numIndices += NUM_INDICES_PER_BOX)
 		{
 			BvhNode *testNode = mTestNodes[node->mTestNodesIdx + i];
 
-			// add indices to root node
-			for (int j = 0; j < sNumIndicesPerBox; ++ j)
+			// add vertex indices of boxes to root node
+			for (int j = 0; j < NUM_INDICES_PER_BOX; ++ j)
 			{
 				mTestIndices[mCurrentIndicesPtr + numIndices + j] = sIndices[j] + testNode->GetId() * 8;
@@ -732,6 +748,11 @@
 		BvhNode *node = *lit;
 
+		Vector3 v;
+
 		for (int j = 0; j < 8; ++ j)
-			(static_cast<Vector3 *>(mVertices))[node->GetId() * 8 + j] = node->GetBox().GetVertex(j);
+		{
+			node->GetBox().GetVertex2(j, v);
+			(static_cast<Vector3 *>(mVertices))[node->GetId() * 8 + j] = v;
+		}
 	}
 
@@ -1294,8 +1315,15 @@
 	else
 	{
-		for (int i = 0; i < node->mNumTestNodes; ++ i)
+		glPolygonMode(GL_FRONT, GL_LINE);
+		BvhNodeContainer nodes;
+		nodes.push_back(node);
+		int renderedBoxes = PrepareBoundsWithDrawArrays(nodes);
+		RenderBoundsWithDrawArrays(renderedBoxes, state);
+		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+
+		/*for (int i = 0; i < node->mNumTestNodes; ++ i)
 		{
 			RenderBoxForViz(mTestNodes[node->mTestNodesIdx + i]->GetBox());
-		}
+		}*/
 	}
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 3070)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 3202)
@@ -260,5 +260,26 @@
 // compute the vertex for number N = <0..7>, N = 4.x + 2.y + z, where
 // x,y,z are either 0 or 1; (0 .. min coordinate, 1 .. max coordinate)
-void AxisAlignedBox3::GetVertex(const int N, Vector3 &vertex) const
+void AxisAlignedBox3::GetVertex2(int N, Vector3 &vertex) const
+{
+	switch (N) 
+	{
+	case 0: vertex.SetValue(mMin.x, mMin.y, mMin.z); break;
+	case 1: vertex.SetValue(mMax.x, mMin.y, mMin.z); break;
+	case 2: vertex.SetValue(mMax.x, mMax.y, mMin.z); break;
+	case 3: vertex.SetValue(mMin.x, mMax.y, mMin.z); break;
+	case 4: vertex.SetValue(mMin.x, mMax.y, mMax.z); break;
+	case 5: vertex.SetValue(mMax.x, mMax.y, mMax.z); break;
+	case 6: vertex.SetValue(mMax.x, mMin.y, mMax.z); break;
+	case 7: vertex.SetValue(mMin.x, mMin.y, mMax.z); break;
+	default: 
+		{
+			cerr << "ERROR in AxisAlignedBox3::GetVertex N=" << N <<  "\n";
+		}
+	}
+}
+
+// compute the vertex for number N = <0..7>, N = 4.x + 2.y + z, where
+// x,y,z are either 0 or 1; (0 .. min coordinate, 1 .. max coordinate)
+void AxisAlignedBox3::GetVertex(int N, Vector3 &vertex) const
 {
 	switch (N) 
@@ -844,4 +865,5 @@
 	const float epsAdd = 1e-6f;
 	const float epsMul = 1.000005f;
+	
 	float dir = mMax.x - mMin.x;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 3173)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 3202)
@@ -812,4 +812,8 @@
 			</File>
 			<File
+				RelativePath=".\src\AxisAlignedBox3.h"
+				>
+			</File>
+			<File
 				RelativePath=".\src\Halton.cpp"
 				>
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3201)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3202)
@@ -11,7 +11,7 @@
 maxBatchSize=50
 # triangles per bvh leaf (influences hierarchy depth vs. occlusion power)
-trianglesPerVirtualLeaf=12900
+trianglesPerVirtualLeaf=300
 
-maxDepthForTestingChildren=0
+maxDepthForTestingChildren=3
 
 ################
@@ -85,2 +85,6 @@
 # skylight turbitity
 turbitity=3.0f
+
+#second bad view point
+//camPosition=465.626 248.788 184.978
+//camDirection=0.0592959 0.998021 0.0209425
