Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h	(revision 3240)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h	(revision 3241)
@@ -3,11 +3,13 @@
 
 
-#include "SimpleVec.h"
+#include "Vector3.h"
+#include "Triangle3.h"
 #include <string>
 #include <vector>
 #include "AxisAlignedBox3.h"
 
+	
 
-typedef std::vector<SimpleVec> VertexArray;
+typedef std::vector<CHCDemoEngine::Vector3> VertexArray;
 typedef std::pair<float, float> TexCoord;
 
@@ -15,4 +17,17 @@
 
 struct BvhInterior;
+
+
+
+struct Geometry
+{
+	CHCDemoEngine::Vector3 *mVertices;
+	CHCDemoEngine::Vector3 *mNormals;
+	TexCoord *mTexCoords;
+
+	int mVertexCount;
+	int mTexcoordCount;
+};
+
 
 struct BvhNode
@@ -36,6 +51,5 @@
 	int Count() const { return last - first + 1; }
 
-	SimpleVec bmin;
-	SimpleVec bmax;
+	CHCDemoEngine::AxisAlignedBox3 box;
 
 	BvhInterior *parent;
@@ -54,8 +68,7 @@
 struct BvhLeaf: public BvhNode
 {
-	//Color color;
-	int texture;
+	Geometry *geometry;
 
-	BvhLeaf(): BvhNode(), texture(0) {}
+	BvhLeaf(): BvhNode(), geometry(NULL) {}
 };
 
@@ -79,13 +92,4 @@
 protected:
 
-	struct Geometry
-	{
-		SimpleVec *mVertices;
-		SimpleVec *mNormals;
-		TexCoord *mTexCoords;
-
-		int mVertexCount;
-		int mTexcoordCount;
-	};
 
 	void LoadShape(const VertexArray &vertices,
@@ -136,4 +140,7 @@
 	void WriteNextNode(ogzstream &stream, BvhNode *parent);
 
+	void UpdateLeafBox(BvhLeaf *leaf);
+
+	void UpdateNodeBox(BvhNode *node);
 
 
@@ -146,5 +153,5 @@
 	std::vector<int> mGlobalTriangleIds;
 
-	std::vector<BvhNode *> mBvhNodes;
+	std::vector<BvhLeaf *> mBvhLeaves;
 
 	BvhNode *mRoot;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp	(revision 3239)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp	(revision 3241)
@@ -1,5 +1,5 @@
 #include "VisibilitySolutionConverter.h"
-#include "SimpleTri.h"
-#include "SimpleVec.h"
+#include "Triangle3.h"
+#include "Vector3.h"
 #include "gzstream.h"
 #include <iostream>
@@ -133,9 +133,10 @@
 			{
 				// no face normals? => create normals
-				const SimpleTri tri(vertices[indices[idx1]],
-					                vertices[indices[idx2]], 
-									vertices[indices[idx3]]);
-
-				const SimpleVec n = tri.GetNormal();
+				const CHCDemoEngine::Triangle3 
+					tri(vertices[indices[idx1]],
+					    vertices[indices[idx2]], 
+						vertices[indices[idx3]]);
+
+				const CHCDemoEngine::Vector3 n = tri.GetNormal();
 
 				faceNormals.push_back(n);
@@ -182,6 +183,6 @@
 
 	// convert the triangles to geometry
-	geom->mVertices = new SimpleVec[numElements];
-	geom->mNormals = new SimpleVec[numElements];
+	geom->mVertices = new CHCDemoEngine::Vector3[numElements];
+	geom->mNormals = new CHCDemoEngine::Vector3[numElements];
 	geom->mTexCoords = new TexCoord[numElements];
 
@@ -244,4 +245,9 @@
 	}
 
+	// update bvh bounding boxes with loaded geometry
+	UpdateNodeBox(mRoot);
+
+	cout << "writing scene" << endl;
+
 	if (!WriteScene(sceneOutputFilename))
 	{
@@ -249,4 +255,6 @@
 		return false;
 	}
+
+	cout << "writing bvh" << endl;
 
 	if (!WriteBvh(bvhOutputFilename))
@@ -315,5 +323,5 @@
 				case 'n' :
 					sscanf(str + 2, "%f %f %f", &x, &y, &z);
-					tempNormals.push_back(SimpleVec(x, y, z));
+					tempNormals.push_back(CHCDemoEngine::Vector3(x, y, z));
 					break;
 				case 't':
@@ -325,5 +333,5 @@
 					//const float scale = 5e-3f;
 					const float scale = 0.1f;
-					tempVertices.push_back(SimpleVec(x * scale, y * scale, z * scale));
+					tempVertices.push_back(CHCDemoEngine::Vector3(x * scale, y * scale, z * scale));
 					//cout <<"v " << x << " " << y << " "<< z << " ";
 				}
@@ -388,5 +396,5 @@
 					sscanf(str + 1, "%f %f %f", &x, &y, &z);
 					const float scale = 0.1f;
-					vertices.push_back(SimpleVec(x * scale, y * scale, z * scale));
+					vertices.push_back(CHCDemoEngine::Vector3(x * scale, y * scale, z * scale));
 					break;
 				}
@@ -406,9 +414,9 @@
 	{
 		// no face normals? => create normals
-		const SimpleTri tri(vertices[i + 0],
-    	                    vertices[i + 1], 
-							vertices[i + 2]);
-
-		const SimpleVec n = tri.GetNormal();
+		const CHCDemoEngine::Triangle3 tri(vertices[i + 0],
+			                               vertices[i + 1],
+										   vertices[i + 2]);
+
+		const CHCDemoEngine::Vector3 n = tri.GetNormal();
 
 		normals.push_back(n);
@@ -433,6 +441,6 @@
 	str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
   
-	str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount);
-	str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount);
+	str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(CHCDemoEngine::Vector3) * vertexCount);
+	str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(CHCDemoEngine::Vector3) * vertexCount);
 
 	int texCoordCount = 0;//geom->mTexcoordCount;
@@ -463,5 +471,5 @@
 	if (hasMaterial)
 	{
-		SimpleVec ambient, diffuse, spec, emm;
+		CHCDemoEngine::Vector3 ambient, diffuse, spec, emm;
 
 		ambient.x = ambient.y = ambient.z = 0.2f;
@@ -472,12 +480,12 @@
 		diffuse.z = RandomColor(0.5f);
 
-		spec.x    = spec.y    = spec.z    =  .0f;
+		spec.x = spec.y = spec.z = .0f;
 		emm = spec;
 
 		// only write rgb part of the material
-		str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
-		str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
-		str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
-		str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
+		str.write(reinterpret_cast<char *>(&ambient), sizeof(CHCDemoEngine::Vector3));
+		str.write(reinterpret_cast<char *>(&diffuse), sizeof(CHCDemoEngine::Vector3));
+		str.write(reinterpret_cast<char *>(&spec), sizeof(CHCDemoEngine::Vector3));
+		str.write(reinterpret_cast<char *>(&emm), sizeof(CHCDemoEngine::Vector3));
 	}
 }
@@ -550,14 +558,15 @@
 	vector<TexCoord> _texCoords;
 
-	for (size_t i = 0; i < mBvhNodes.size(); ++ i)
-	{
-		BvhNode *node = mBvhNodes[i];
-
-		//for (size_t j = 0; j < node->mTriangleIds.size(); ++ j)
+	mGeometry.reserve(mBvhLeaves.size());
+
+	mNumShapes = (int)mBvhLeaves.size();
+
+	for (size_t i = 0; i < mBvhLeaves.size(); ++ i)
+	{
+		BvhLeaf *node = mBvhLeaves[i];
+
 		for (int j = node->first; j <= node->last; ++ j)
 		{
 			const int idx = 3 * mGlobalTriangleIds[j];
-
-			//if (idx >= maxVertices * 3) continue;
 
 			for (int k = 0; k < 3; ++ k)
@@ -569,9 +578,7 @@
 		}
 
-		if (!_vertices.empty())
-		{
-			++ mNumShapes;
-			LoadShape(_vertices, _normals, _texCoords);
-		}
+		LoadShape(_vertices, _normals, _texCoords);
+		
+		node->geometry = mGeometry.back();
 
 		_vertices.clear();
@@ -610,5 +617,5 @@
 	}
 
-	//if (mRoot) delete mRoot;
+	const size_t numNodes = buffer[5];
 
 	mNumNodes = 0; // this was set to 1 in constructor!
@@ -616,5 +623,5 @@
 	mRoot = LoadNode(fr, 0);
 
-	if (mNumNodes != buffer[5]) 
+	if (mNumNodes != numNodes) 
 	{
 		cerr << "Warning: Loaded " << mNumNodes <<
@@ -625,4 +632,18 @@
 	
 	return true;
+}
+
+
+void VisibilitySolutionConverter::UpdateLeafBox(BvhLeaf *leaf)
+{
+	leaf->box.Initialize();
+
+	Geometry *geom = leaf->geometry;
+
+	for (size_t i = 0; i < geom->mVertexCount; ++ i)
+	{
+		CHCDemoEngine::Vector3 v = geom->mVertices[i];
+		leaf->box.Include(v);
+	}
 }
 
@@ -655,7 +676,4 @@
 		interior->front = front;
 		interior->back = back;
-
-		//interior->bmin = Minimize(front->bmin, back->bmin);
-		//interior->bmax = Maximize(front->bmax, back->bmax);
 	
 		return (BvhNode *)interior;
@@ -664,5 +682,4 @@
 	{
 		// leaf
-		//Debug << "Info: Loading leaf (id: " << numberOfNodes << ", depth: " << depth << ")" << endl;
 		BvhLeaf *leaf = new BvhLeaf();
 
@@ -673,7 +690,6 @@
 
 		leaf->depth = depth;
-		//UpdateLeafBox(leaf);
-
-		mBvhNodes.push_back(leaf);
+
+		mBvhLeaves.push_back(leaf);
 	
 		return (BvhNode *)leaf;
@@ -765,5 +781,5 @@
 	vertices.reserve(numTriangles * 3);
 	cout << "loading " << numTriangles * 3 << " vertices (" 
-		 << numTriangles * 3 * sizeof(SimpleVec) /	(1024 * 1024) << " MB)" << endl;
+		<< numTriangles * 3 * sizeof(CHCDemoEngine::Vector3) / (1024 * 1024) << " MB)" << endl;
 
 	int i = 0;
@@ -771,7 +787,6 @@
 	while (1)
 	{
-		SimpleVec v;
-		
-		inStream.read(reinterpret_cast<char *>(&v), sizeof(SimpleVec));
+		CHCDemoEngine::Vector3 v;
+		inStream.read(reinterpret_cast<char *>(&v), sizeof(CHCDemoEngine::Vector3));
 
 		// end of file reached
@@ -810,6 +825,6 @@
 	for (it = vertices.begin(); it != it_end; ++ it)
 	{
-		SimpleVec v = *it;
-		ofile.write(reinterpret_cast<char *>(&v), sizeof(SimpleVec));
+		CHCDemoEngine::Vector3 v = *it;
+		ofile.write(reinterpret_cast<char *>(&v), sizeof(CHCDemoEngine::Vector3));
 	}
 
@@ -832,15 +847,31 @@
 		nodeType = TYPE_INTERIOR;
 		
-	cerr << "error: wrong node type: " << nodeType << endl;
-		
 	stream.write(reinterpret_cast<char *>(&nodeType), sizeof(int));
 
-	SimpleVec bMin, bMax;
+	CHCDemoEngine::Vector3 bMin = node->box.Min();
+	CHCDemoEngine::Vector3 bMax = node->box.Max();
 
 	stream.write(reinterpret_cast<char *>(&(node->first)), sizeof(int));
 	stream.write(reinterpret_cast<char *>(&(node->last)), sizeof(int));
 
-	stream.write(reinterpret_cast<char *>(&node->bmin), sizeof(SimpleVec));
-	stream.write(reinterpret_cast<char *>(&node->bmax), sizeof(SimpleVec));
+	stream.write(reinterpret_cast<char *>(&bMin), sizeof(CHCDemoEngine::Vector3));
+	stream.write(reinterpret_cast<char *>(&bMin), sizeof(CHCDemoEngine::Vector3));
+}
+
+
+void VisibilitySolutionConverter::UpdateNodeBox(BvhNode *node)
+{
+	if (!node->IsLeaf())
+	{
+		BvhInterior *interior = (BvhInterior *)node;
+		node->box = Union(interior->front->box, interior->back->box);
+
+		UpdateNodeBox(interior->front);
+		UpdateNodeBox(interior->back);
+	}
+	else
+	{
+		UpdateLeafBox((BvhLeaf *)node);
+	}
 }
 
