Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3123)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3239)
@@ -84,5 +84,5 @@
 	tQueue.push(root);
 	
-	while(!tQueue.empty())
+	while (!tQueue.empty())
 	{
 		BvhNode *node = tQueue.front();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h	(revision 3238)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h	(revision 3239)
@@ -14,4 +14,5 @@
 class ogzstream;
 
+struct BvhInterior;
 
 struct BvhNode
@@ -35,4 +36,8 @@
 	int Count() const { return last - first + 1; }
 
+	SimpleVec bmin;
+	SimpleVec bmax;
+
+	BvhInterior *parent;
 	std::vector<int> mTriangleIds;
 };
@@ -71,5 +76,4 @@
 		         const std::string &bvhOutputFilename);
 	
-
 
 protected:
@@ -124,6 +128,14 @@
 					float scale);
 
+	bool WriteBinObj(const std::string &filename,
+		             const VertexArray &vertices);
+
 	bool ExportBinObj(const std::string &filename,
-                      const VertexArray &vertices);
+		              const VertexArray &vertices);
+
+	void WriteNextNode(ogzstream &stream, 
+		               BvhNode *parent);
+
+
 
 	//////////////////////////////////
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp	(revision 3238)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp	(revision 3239)
@@ -4,4 +4,5 @@
 #include "gzstream.h"
 #include <iostream>
+#include <queue>
 
 
@@ -15,5 +16,8 @@
 #define BVH_VERSION 2.1
 
-const int maxVertices = 20000000;
+#define TYPE_INTERIOR -2
+#define TYPE_LEAF -3
+
+//const int maxVertices = 20000000;
 
 
@@ -246,9 +250,9 @@
 	}
 
-	/*if (!WriteBvh(bvhOutputFilename))
-	{
-		cerr << "could not write file" << endl;
+	if (!WriteBvh(bvhOutputFilename))
+	{
+		cerr << "could not write bvh!" << endl;
 		return false;
-	}*/
+	}
 
 	return true;
@@ -578,11 +582,4 @@
 
 
-bool VisibilitySolutionConverter::WriteBvh(const string &filename)
-{
-	
-	return true;
-}
-
-
 bool VisibilitySolutionConverter::ReadBvh(FILE *fr) 
 {
@@ -653,10 +650,12 @@
 		back  = LoadNode(fr, depth + 1);
 
-		// front->parent = (BvhNode *)interior;
-		// back->parent = (BvhNode *)interior;
+		front->parent = interior;
+		back->parent = interior;
+
 		interior->front = front;
 		interior->back = back;
 
-		//interior->box = Union(front->box, back->box);
+		//interior->bmin = Minimize(front->bmin, back->bmin);
+		//interior->bmax = Maximize(front->bmax, back->bmax);
 	
 		return (BvhNode *)interior;
@@ -667,8 +666,9 @@
 		//Debug << "Info: Loading leaf (id: " << numberOfNodes << ", depth: " << depth << ")" << endl;
 		BvhLeaf *leaf = new BvhLeaf();
+
 		leaf->first = buffer[0];
 		leaf->last  = buffer[1];
 		leaf->axis  = buffer[2];
-		leaf->id  = buffer[3];
+		leaf->id    = buffer[3];
 
 		leaf->depth = depth;
@@ -676,5 +676,5 @@
 
 		mBvhNodes.push_back(leaf);
-		// leaf->id = numberOfNodes;
+	
 		return (BvhNode *)leaf;
 	}
@@ -820,2 +820,63 @@
 	return true;
 }
+
+
+void VisibilitySolutionConverter::WriteNextNode(ogzstream &stream, 
+												BvhNode *node)
+{
+	int nodeType;
+
+	if (!node->IsLeaf())
+		nodeType = TYPE_LEAF;
+	else 
+		nodeType = TYPE_INTERIOR;
+		
+	cerr << "error: wrong node type: " << nodeType << endl;
+		
+	stream.write(reinterpret_cast<char *>(&nodeType), sizeof(int));
+
+	SimpleVec bMin, bMax;
+
+	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));
+}
+
+
+bool VisibilitySolutionConverter::WriteBvh(const string &filename)
+{
+	std::queue<BvhNode *> tQueue;
+	ogzstream stream(filename.c_str());
+
+	if (!stream.is_open()) return NULL;
+
+	cout << "loading bvh" << endl;
+
+	WriteNextNode(stream, mRoot);
+
+	tQueue.push(mRoot);
+	
+	while (!tQueue.empty())
+	{
+		BvhNode *node = tQueue.front();
+		tQueue.pop();
+
+		if (!node->IsLeaf())
+		{
+			BvhInterior *interior = static_cast<BvhInterior *>(node);
+
+			BvhNode *front = interior->front;
+			BvhNode *back = interior->back;
+
+			WriteNextNode(stream, front);
+			WriteNextNode(stream, back);
+
+			tQueue.push(front);			
+			tQueue.push(back);
+		}
+	}
+
+	return true;
+}
