Index: GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp	(revision 2643)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp	(revision 2670)
@@ -13,4 +13,5 @@
 #define USE_MULTIPLE_OBJECTS  0
 using namespace std;
+
 
 namespace GtpVisibilityPreprocessor {
@@ -155,5 +156,4 @@
 
 
-
 float ObjectPvs::EvalPvsCost() const
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.cpp	(revision 2660)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.cpp	(revision 2670)
@@ -15,5 +15,6 @@
 #endif
 
-namespace GtpVisibilityPreprocessor {
+namespace GtpVisibilityPreprocessor 
+{
 
 
@@ -24,8 +25,11 @@
 int Intersectable::sReservedMailboxes2 = 1;
 
+
 // This is the result of computing intersection
 // static data member
-  ALIGN16  IntersectionStr
-  SimpleRay::IntersectionRes[32];
+ALIGN16  IntersectionStr
+SimpleRay::IntersectionRes[32];
+
+
 
 Intersectable::Intersectable(): 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2669)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2670)
@@ -123,4 +123,5 @@
 }
 
+
 // reimplemented here so that we can snap the error windows
 float QtGlRendererBuffer::GetPixelError(int &pvsSize)
@@ -128,8 +129,13 @@
 	MakeCurrent();
 	
-	cout << "stencil: " << format().stencil() << endl;
-	cout << "depth: " << format().depth() << endl;
-	cout << "rgba: " << format().rgba() << endl;
-	cout << "double: " << format().doubleBuffer() << endl;
+	if (0)
+	{
+		cout << "stencil: " << format().stencil() << endl;
+		cout << "depth: " << format().depth() << endl;
+		cout << "rgba: " << format().rgba() << endl;
+		cout << "double: " << format().doubleBuffer() << endl;
+	}
+
+	++ mCurrentFrame;
 
 	float pErrorPixels = -1.0f;
@@ -144,11 +150,8 @@
 		return -1.0f;
 
-	KdNode::NewMail2();
-	Intersectable::NewMail();
-
-	ObjectPvs pvs;
-
 	bool evaluateFilter;
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluateFilter", evaluateFilter);
+
+	ObjectPvs pvs;
 
 	if (!evaluateFilter) 
@@ -169,11 +172,10 @@
 	if (mSnapErrorFrames)
 	{
-
 		GLfloat light_ambient[] = {0.3, 0.3, 0.3, 1.0};
 		GLfloat light_diffuse[] = {0.6, 0.6, 0.6, 1.0};
 		GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
 
-		GLfloat light_position[] =  //{278.0f, 548.8f,279.0f, 1.0f};
-		{0.f,0.f,0.f, 1.0f};
+		//GLfloat light_position[] =  {278.0f, 548.8f,279.0f, 1.0f};
+		GLfloat light_position[] =  {0.f,0.f,0.f, 1.0f};
 
 		glEnable(GL_LIGHT0); 
@@ -208,5 +210,12 @@
 	glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); 
 
-	
+	KdNode::NewMail2();
+	Intersectable::NewMail();
+
+KdNode::NewMail2();
+	Intersectable::NewMail();
+	KdNode::NewMail2();
+	Intersectable::NewMail();
+
 	// render pvs once
 	RenderPvs(pvs);
@@ -228,81 +237,64 @@
 	query->BeginQuery();
 
+	++ mCurrentFrame;
+
 	RenderScene();
 
 	query->EndQuery();
 	glDisable(GL_STENCIL_TEST); 
-
-
-	// reenable other state
-	int wait = 0;
-	while (0 && !query->ResultAvailable()) 
-		wait ++;
 	
 	pixelCount = query->GetQueryResult();
 
+	pErrorPixels = ((float)pixelCount) / (GetWidth() * GetHeight());
+
 	if (pixelCount > 0) 
 	{
 		cout << "vc id: " << viewcell->GetId() << " pvs: " << pvsSize << " pc: " << pixelCount << endl;
-	}
-
-	pErrorPixels = ((float)pixelCount) / (GetWidth() * GetHeight());
-
-
-	if (mSnapErrorFrames && (pixelCount > 0)) 
-	{
-		glReadBuffer(GL_BACK);
-		//glReadBuffer(GL_FRONT);
-
-		char filename[256];
-		sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels);
-		QImage im = toImage();
-		string str = mSnapPrefix + filename;
-		QString qstr(str.c_str());
-
-		im.save(qstr, "PNG");
-		if (0) { //0 && mFrame == 1543) {
-			int x,y;
-			int lastIndex = -1;
-			for (y=0; y < im.height(); y++)
-				for (x=0; x < im.width(); x++) {
-					QRgb p = im.pixel(x,y);
-					int index = qRed(p) + (qGreen(p)<<8) + (qBlue(p)<<16);
-					if (qGreen(p) != 255 && index!=0) {
-						if (index != lastIndex) {
-							//				Debug<<"ei="<<index<<" ";
-							lastIndex = index;
-						}
-					}
-				}
-		}
-		
-
-#if 0
-		mUseFalseColors = false;
-
-		glPushAttrib(GL_CURRENT_BIT);
-		glColor3f(0, 1, 0);
-		
-		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-		//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-
-		KdNode::NewMail2();
-		Intersectable::NewMail();
-
-		RenderPvs(pvs);
-
-		glFinish();
-
-		mUseForcedColors = false;
-
-		im = toImage();
-		sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels);
-		str = mSnapPrefix + filename;
-		qstr = str.c_str();
-		im.save(qstr, "PNG");
-		
-		glPopAttrib();
-#endif
+	
+		if (mSnapErrorFrames) 
+		{
+			glReadBuffer(GL_BACK);
+			//glReadBuffer(GL_FRONT);
+
+			//////////////
+			//-- output error visualization
+
+			char filename[256];
+			sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels);
+			QImage im = toImage();
+			string str = mSnapPrefix + filename;
+			QString qstr(str.c_str());
+
+			im.save(qstr, "PNG");
+
+
+			///////////
+			//-- output computed pvs
+
+			mUseFalseColors = false;
+
+			glPushAttrib(GL_CURRENT_BIT);
+			glColor3f(0, 1, 0);
+
+			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+
+			KdNode::NewMail2();
+			Intersectable::NewMail();
+
+			++ mCurrentFrame;
+			// render pvs once
+			RenderPvs(pvs);
+
+			mUseForcedColors = false;
+
+			im = toImage();
+			sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels);
+			str = mSnapPrefix + filename;
+			qstr = str.c_str();
+			im.save(qstr, "PNG");
+
+			glPopAttrib();
+		}
 	}
 
@@ -497,11 +489,10 @@
 
 
-void QtGlRendererWidget::PreparePvs2(const ObjectPvs &pvs)
+void QtGlRendererWidget::PreparePvs(const ObjectPvs &pvs)
 {
 	int indexBufferSize = 0;
 	
-	// super hack: mal not working with multiple threads
+	// hack: mail not working with multiple threads
 	KdNode::NewMail2();
-	//Intersectable::NewMail();
 
 	mPvsSize = pvs.GetSize();
@@ -546,7 +537,5 @@
 
 
-
-
-void QtGlRendererWidget::RenderPvs()
+void QtGlRendererWidget::VisualizePvs()
 {
 	if (mUseVbos) 
@@ -600,9 +589,8 @@
 #else
 	
-		PreparePvs2(viewcell->GetPvs());
+		PreparePvs(viewcell->GetPvs());
 		emit PvsUpdated();
 
 #endif
-
 
 		// Render PVS
@@ -662,5 +650,5 @@
 
 	// Render PVS
-	RenderPvs();
+	VisualizePvs();
 
 	glEnable(GL_STENCIL_TEST); 
@@ -696,4 +684,5 @@
 	int pixelCount = query->GetQueryResult();
 	pErrorPixels = ((float)pixelCount) / (GetWidth() * GetHeight());
+	
 	if (0) cout << "error pixels=" << pixelCount << endl;
 
@@ -826,6 +815,6 @@
 }
 
-void
-QtGlRendererWidget::paintGL()
+
+void QtGlRendererWidget::paintGL()
 {
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -843,5 +832,5 @@
 		{
 			glColor3f(0.6f, 0.6f, 0.6f);
-			RenderPvs();
+			VisualizePvs();
 		}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2664)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2670)
@@ -269,5 +269,5 @@
 	void keyPressEvent(QKeyEvent * e);
 
-	void RenderPvs();
+	void VisualizePvs();
 
 	float RenderErrors();
@@ -544,12 +544,14 @@
 	}
 
-	void PreparePvs2(const ObjectPvs &pvs);
-
+
+	/** Prepare pvs for rendering.
+	*/
+	virtual void PreparePvs(const ObjectPvs &pvs);
+	/** Internal method rendering the pvs if it has been properly prepared.
+	*/
 	void _RenderPvs();
 	/** Render pvs using false colors.
 	*/
 	void _RenderColoredPvs();
-
-  //	void _UpdatePvsIndices(KdNode *node, int &indexBufferSize);
 
 	float ComputeRenderCost(ViewCell *vc);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp	(revision 2664)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp	(revision 2670)
@@ -124,5 +124,5 @@
 	glEnable(GL_LIGHT1);
 
-	mRenderer->RenderPvs();
+	mRenderer->VisualizePvs();
 	
 	glDisable(GL_LIGHTING);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2669)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2670)
@@ -86,5 +86,5 @@
 mUseVbos(false),
 mComputeGVS(false),
-mCurrentFrame(-1)
+mCurrentFrame(100)
 {
 	mSceneGraph->CollectObjects(mObjects);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2643)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2670)
@@ -69,15 +69,16 @@
   int Leaves() const { return (nodes / 2) + 1; }
 
-  void Reset() {
-    nodes = 0;
-    for (int i=0; i<7; i++)
-      splits[i] = 0;
-    rays = queryDomains = 0;
-    rayRefs = rayRefsNonZeroQuery = objectRefs = 0;
-    zeroQueryNodes = 0;
-    maxDepthNodes = 0;
-    minCostNodes = 0;
-    maxObjectRefs = 0;
-    addedRayRefs = removedRayRefs = 0;
+  void Reset() 
+  {
+	  nodes = 0;
+	  for (int i=0; i<7; i++)
+		  splits[i] = 0;
+	  rays = queryDomains = 0;
+	  rayRefs = rayRefsNonZeroQuery = objectRefs = 0;
+	  zeroQueryNodes = 0;
+	  maxDepthNodes = 0;
+	  minCostNodes = 0;
+	  maxObjectRefs = 0;
+	  addedRayRefs = removedRayRefs = 0;
   }
 
@@ -94,6 +95,9 @@
   
 class KdInterior;
-/** Abstract class for kd-tree node */
-class KdNode {
+
+/** Abstract class for kd-tree node 
+*/
+class KdNode 
+{
 
 public:  
@@ -112,15 +116,21 @@
   bool Mailed() const { return mMailbox == sMailId; }
  
-  static void NewMail(const int reserve = 1) {
-    sMailId += sReservedMailboxes;
-    sReservedMailboxes = reserve;
-  }
+  static void NewMail(const int reserve = 1) 
+  {
+	  sMailId += sReservedMailboxes;
+	  sReservedMailboxes = reserve;
+  }
+
   void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
   bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
 
 
-  static void NewMail2(const int reserve = 1) {
-    sMailId2 += sReservedMailboxes;
-    sReservedMailboxes2 = reserve;
+  ////////////
+  //-- hack!!: second mail for a machine with 2 cores
+
+  static void NewMail2(const int reserve = 1) 
+  {
+	  sMailId2 += sReservedMailboxes;
+	  sReservedMailboxes2 = reserve;
   }
 
@@ -130,4 +140,6 @@
   void Mail2(const int mailbox) { mMailbox2 = sMailId2 + mailbox; }
   bool Mailed2(const int mailbox) const { return mMailbox2 == sMailId2 + mailbox; }
+
+  /////////////////////////////////////
 
 
@@ -154,5 +166,7 @@
 };
 
-/** Implementation of the kd-tree interior node */
+
+/** Implementation of the kd-tree interior node. 
+*/
 class KdInterior : public KdNode {
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2664)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2670)
@@ -172,5 +172,9 @@
   virtual void EvalPvsStat();
 
-  void PreparePvs(const ObjectPvs &pvs);
+  /** Prepare the pvs for rendering.
+  */
+  virtual void PreparePvs(const ObjectPvs &pvs);
+  /** Helper function that recursively prepares the indices for VBO renderin.
+  */
   void _UpdatePvsIndices(KdNode *node, int &indexBufferSize);
 
@@ -207,4 +211,6 @@
 
   Vector3 GetViewPoint() const { return mViewPoint; }
+
+
 public:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2669)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2670)
@@ -1252,8 +1252,10 @@
 void GvsPreprocessor::ComputeViewCell(ViewCell *vc)
 {
+	KdNode::NewMail();
+
 	mCurrentViewCell = vc;
-	KdNode::NewMail();
-
-	long startTime = GetTime();
+	
+	const long startTime = GetTime();
+
 	cout << "\n***********************\n" 
 		<< "computing view cell " << mProcessedViewCells 
