Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2648)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2669)
@@ -11,4 +11,6 @@
 class Exporter;
 class VssRay;
+class TriangleIntersectable;
+class KdIntersectable;
 
 /** View space partition statistics.
@@ -234,5 +236,10 @@
 	*/
 	void ProcessViewCell();
-
+	/** Use this for qt visualization.
+	*/
+	void UpdateStatsForVisualization(KdIntersectable *kdInt);
+	/** Keep count of new objects for stats.
+	*/
+	void CountObject(Intersectable *triObj);
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/OcclusionQuery.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/OcclusionQuery.cpp	(revision 2663)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/OcclusionQuery.cpp	(revision 2669)
@@ -79,5 +79,5 @@
 #ifdef _WIN32
 		// VH
-		cout << "issuing arb query" << endl;
+		//cout << "issuing arb query" << endl;
 		glBeginQueryARB(GL_SAMPLES_PASSED_ARB, mId);
 #endif
@@ -100,5 +100,5 @@
 #ifdef _WIN32
 		// VH
-		cout << "ending query" << endl;
+		//cout << "ending query" << endl;
 		glEndQueryARB(GL_SAMPLES_PASSED_ARB);
 #endif
@@ -134,6 +134,6 @@
 	{
 #ifdef _WIN32
-	  // VH
-	  glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_AVAILABLE_NV, &available);
+		// VH
+		glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_AVAILABLE_NV, &available);
 #endif
 	}
@@ -180,5 +180,5 @@
 			// VH
 			glGenQueriesARB(n, (unsigned int *)newQueries);
-			cout << "creating " << n << " arb queries" <<  endl;
+			//cout << "creating " << n << " arb queries" <<  endl;
 #endif
 		}
@@ -188,5 +188,5 @@
 			// VH		  
 			glGenOcclusionQueriesNV(n, (unsigned int *)newQueries);
-			cout << "creating " << n << " nv queries" <<  endl;
+			//cout << "creating " << n << " nv queries" <<  endl;
 #endif		  
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2667)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2669)
@@ -31,6 +31,6 @@
 #include <QVBoxLayout>
 
-namespace GtpVisibilityPreprocessor {
-
+namespace GtpVisibilityPreprocessor 
+{
 
   
@@ -41,6 +41,4 @@
 static CGprofile sCgFragmentProfile;
 
-
-//static vector<OcclusionQuery *> sQueries;
 
 QtGlRendererWidget *rendererWidget = NULL;
@@ -88,5 +86,10 @@
 									   ViewCellsManager *viewcells,
 									   KdTree *tree):
-QGLPixelBuffer(QSize(w, h), QGLFormat(QGL::StencilBuffer | QGL::DirectRendering | QGL::DepthBuffer | QGL::Rgba)),
+QGLPixelBuffer(QSize(w, h), 
+			   QGLFormat(QGL::StencilBuffer | 
+						 QGL::DepthBuffer | 
+						 QGL::DoubleBuffer |
+						 QGL::Rgba
+						 )),
 GlRendererBuffer(sceneGraph, viewcells, tree)
 {
@@ -125,8 +128,9 @@
 	MakeCurrent();
 	
-/*
-SetupCamera();
-RenderScene();
-*/
+	cout << "stencil: " << format().stencil() << endl;
+	cout << "depth: " << format().depth() << endl;
+	cout << "rgba: " << format().rgba() << endl;
+	cout << "double: " << format().doubleBuffer() << endl;
+
 	float pErrorPixels = -1.0f;
 
@@ -134,9 +138,12 @@
 	unsigned int pixelCount = 0;
 
-
+	
 	ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
 
 	if (viewcell == NULL)
 		return -1.0f;
+
+	KdNode::NewMail2();
+	Intersectable::NewMail();
 
 	ObjectPvs pvs;
@@ -150,38 +157,71 @@
 		mViewCellsManager->ApplyFilter2(viewcell, false, mViewCellsManager->GetFilterWidth(), pvs);
 
-
-	mUseForcedColors = true;
-
-	SetupCamera();
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-	glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
-	glColor3f(0,1,0);
-	glDepthFunc(GL_LESS );
-	glEnable(GL_DEPTH_TEST);
-
-	glDisable(GL_STENCIL_TEST);
-
 	pvsSize = pvs.GetSize();
-
-	Intersectable::NewMail();
 	
 	if (pvsSize == 0)
 		return 0.0f;
 
-	RenderPvs(pvs);
-
-	//glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
+	mUseForcedColors = true;
+
+	SetupCamera();
+
+	// use shading
+	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};
+
+		glEnable(GL_LIGHT0); 
+
+		// a light
+		glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
+		glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
+		glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
+
+		glEnable(GL_LIGHTING);
+		glLightfv (GL_LIGHT0, GL_POSITION, light_position);
+
+		glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
+		glEnable(GL_COLOR_MATERIAL);
+
+		glShadeModel(GL_SMOOTH);
+	}
+
+	glDisable(GL_ALPHA_TEST);
+		
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+
+	//glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE);
+	glColor3f(0, 1, 0);
+
+	glDepthFunc(GL_LESS);
+	glDepthMask(GL_TRUE);
+	glEnable(GL_DEPTH_TEST);
 
 	glStencilFunc(GL_EQUAL, 0x0, 0x1);
 	glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); 
+
+	
+	// render pvs once
+	RenderPvs(pvs);
+
+	//glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
+	//glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+
 	glEnable(GL_STENCIL_TEST); 
-
 	glColor3f(1,0,0);
 
 
-	OcclusionQuery *query = mOcclusionQueries[1];
-
+	// render scene, record differences
+	OcclusionQuery *query = mOcclusionQueries[0];
+
+	KdNode::NewMail2();
 	Intersectable::NewMail();
 
@@ -236,15 +276,25 @@
 				}
 		}
-
+		
+
+#if 0
 		mUseFalseColors = false;
+
 		glPushAttrib(GL_CURRENT_BIT);
-		glColor3f(0,1,0);
+		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);
@@ -252,5 +302,7 @@
 		qstr = str.c_str();
 		im.save(qstr, "PNG");
+		
 		glPopAttrib();
+#endif
 	}
 
@@ -449,4 +501,5 @@
 	int indexBufferSize = 0;
 	
+	// super hack: mal not working with multiple threads
 	KdNode::NewMail2();
 	//Intersectable::NewMail();
@@ -503,6 +556,5 @@
 
 	EnableDrawArrays();
-	//Intersectable::NewMail();
-	
+		
 	if (mDetectEmptyViewSpace) 
 		glEnable(GL_CULL_FACE);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2667)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2669)
@@ -96,19 +96,6 @@
 
 	// for sg snapshot
-	//mViewPoint = Vector3(18.5133, 10.9818, -1032.24);
-	//mViewDirection = Vector3(-0.5373, 0, 0.843391);
-
-	
-	  //	mViewPoint = Vector3(1213.85, 176.988, -437.364);
-	  //	mViewDirection = Vector3(0.433884, 0, -0.900969);
-	
-	//mViewPoint = Vector3(1213.85, 176.988, -437.364);
-	//mViewDirection = Vector3(0.433884, 0, -0.900969);
-
 	mViewPoint = Vector3(32.8596, 9.86079, -1023.79);
 	mViewDirection = Vector3(-0.92196, 0, 0.387286);
-
-	//mViewPoint = Vector3(1099.9,183.0,-387);
-	//mViewDirection = Vector3(-0.6,0,-0.8);
 
 	// inside
@@ -1650,5 +1637,6 @@
 		// drop the first frame (for some reason, the first frame yields wrong pixel error)
 		
-		if ((mFrame > 0) && (err >= 0.0f) && (err < (1.0f - 1e-6f)) && (pvsSize > 0)) 
+		if (//(mFrame > 0) && 
+			(err >= 0.0f) && (err < (1.0f - 1e-6f)) && (pvsSize > 0)) 
 		{
 			if (err > mPvsStat.maxError)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2667)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2669)
@@ -20,5 +20,15 @@
   
 #define GVS_DEBUG 0
-
+#define NOT_ACCOUNTED_OBJECT 0
+#define ACCOUNTED_OBJECT 2
+#define SHOW_QT_VISUALIZATION 0
+
+static const float MIN_DIST = 0.001f;
+
+static ObjectContainer myobjects;
+
+
+/** Visualization structure for the GVS algorithm.
+*/
 struct VizStruct
 {
@@ -28,9 +38,7 @@
 };
 
-
-static const float MIN_DIST = 0.001f;
-
-static ObjectContainer myobjects;
 static vector<VizStruct> vizContainer;
+
+
 
 
@@ -153,9 +161,6 @@
 		//newRay->mFlags |= VssRay::ReverseSample;
 		
-		// if ray is not further processed => delete ray
-		if (!HandleRay(newRay))
-		{
-			//delete newRay;
-		}
+		// check if ray is not further processed (ray not deleted because using ray pool)
+		HandleRay(newRay);
 		
 		return 1;
@@ -164,4 +169,29 @@
 	return 0;
 }
+
+
+void GvsPreprocessor::CountObject(Intersectable *triObj)
+{
+	if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) && (triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
+	{
+		++ triObj->mCounter;
+		++ mGenericStats2;
+	}
+}
+
+
+void GvsPreprocessor::UpdateStatsForVisualization(KdIntersectable *kdInt)
+{
+	mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
+
+	// count new objects in pvs due to kd node conversion	
+	myobjects.clear();
+	mKdTree->CollectObjects(kdInt->GetItem(), myobjects);
+
+	ObjectContainer::const_iterator oit, oit_end = myobjects.end();
+
+	for (oit = myobjects.begin(); oit != oit_end; ++ oit)
+		CountObject(*oit);
+}	
 
 
@@ -189,12 +219,13 @@
 		Intersectable *obj = ray.mTerminationObject;
 
-		if (obj->mCounter < 2)
+		// counter < 2 => not accounted for yet
+		if (obj->mCounter < ACCOUNTED_OBJECT)
 		{
-			obj->mCounter += 2;
+			obj->mCounter += ACCOUNTED_OBJECT;
 			mTrianglePvs.push_back(obj);
 		
 			mGenericStats = mTrianglePvs.size();
 
-			// if using kd pvs, exchange the triangle with the node in the pvs
+			// exchange the triangle with the node in the pvs for kd pvs
 			if (mUseKdPvs)
 			{
@@ -203,27 +234,11 @@
 				if (!node->Mailed())
 				{
+					// add to pvs
 					node->Mail();
 					KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(node);
 					mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
-					//mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
-					
-					mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
-					myobjects.clear();
-					mKdTree->CollectObjects(node, myobjects);
-
-					// account for kd object pvs
-					ObjectContainer::const_iterator oit, oit_end = myobjects.end();
-
-					for (oit = myobjects.begin(); oit != oit_end; ++ oit)
-					{
-						TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
-
-						if ((triObj->mCounter != 1) && (triObj->mCounter != 3))
-						{
-							++ triObj->mCounter;
-							++ mGenericStats2;
-						}
-					}
-				}			
+				
+					if (SHOW_QT_VISUALIZATION) UpdateStatsForVisualization(kdInt);	
+				}
 			}
 
@@ -902,7 +917,7 @@
 	ObjectContainer kdobjects;
 	mKdTree->CollectKdObjects(box, kdobjects);
-	//vector<KdLeaf *>::const_iterator lit, lit_end = leaves.end();
-	//for (lit = leaves.begin(); lit != lit_end; ++ lit)
+
 	ObjectContainer::const_iterator oit, oit_end = kdobjects.end();
+
 	for (oit = kdobjects.begin(); oit != oit_end; ++ oit)
 	{
@@ -912,4 +927,5 @@
 		//mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
 		mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
+
 		mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
 
@@ -924,17 +940,12 @@
 			TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
             
-			// account for the overall pvs
-			if ((triObj->mCounter != 1) && (triObj->mCounter != 3))
-			{
-				++ triObj->mCounter;
-				++ mGenericStats2;
-			}
+			CountObject(triObj);
             
 			// the triangle itself intersects
 			if (box.Intersects(triObj->GetItem()))
 			{
-				if ((triObj->mCounter < 2))
+				if ((triObj->mCounter < ACCOUNTED_OBJECT))
 				{
-					triObj->mCounter += 2;
+					triObj->mCounter += ACCOUNTED_OBJECT;
 
 					mTrianglePvs.push_back(triObj);
@@ -957,5 +968,5 @@
 
 		for (oit = mObjects.begin(); oit != oit_end; ++ oit)
-			(*oit)->mCounter = 0;
+			(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
         ComputeViewCell(vc);
@@ -986,5 +997,5 @@
 
 		for (oit = mObjects.begin(); oit != oit_end; ++ oit)
-			(*oit)->mCounter = 0;
+			(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
 		ComputeViewCell(vc);
@@ -1049,5 +1060,5 @@
 
 		// hack: reset counter
-		(*oit)->mCounter = 0;
+		(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
 		if (!bv || bv->Mailed())
@@ -1252,5 +1263,4 @@
 	ProcessViewCell();
 
-	//mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize();
 	mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
 	mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
