Index: trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 494)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 496)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.0) on: út 3. I 23:44:18 2006
+# Generated by qmake (2.00a) (Qt 4.1.0) on: st 4. I 15:01:55 2006
 # Project:  preprocessor.pro
 # Template: app
@@ -63,45 +63,45 @@
 	$(MAKE) -f $(MAKEFILE).Debug uninstall
 
-Makefile: preprocessor.pro  C:/Qt/4.1.0/mkspecs/win32-msvc.net\qmake.conf C:/Qt/4.1.0/mkspecs/qconfig.pri \
-		C:\Qt\4.1.0\mkspecs\features\qt_config.prf \
-		C:\Qt\4.1.0\mkspecs\features\exclusive_builds.prf \
-		C:\Qt\4.1.0\mkspecs\features\default_pre.prf \
-		C:\Qt\4.1.0\mkspecs\features\win32\default_pre.prf \
-		C:\Qt\4.1.0\mkspecs\features\release.prf \
-		C:\Qt\4.1.0\mkspecs\features\debug_and_release.prf \
-		C:\Qt\4.1.0\mkspecs\features\default_post.prf \
-		C:\Qt\4.1.0\mkspecs\features\qt.prf \
-		C:\Qt\4.1.0\mkspecs\features\win32\opengl.prf \
-		C:\Qt\4.1.0\mkspecs\features\moc.prf \
-		C:\Qt\4.1.0\mkspecs\features\win32\thread.prf \
-		C:\Qt\4.1.0\mkspecs\features\warn_off.prf \
-		C:\Qt\4.1.0\mkspecs\features\win32\console.prf \
-		C:\Qt\4.1.0\mkspecs\features\win32\rtti.prf \
-		C:\Qt\4.1.0\mkspecs\features\win32\exceptions.prf \
-		C:\Qt\4.1.0\mkspecs\features\win32\stl.prf \
-		C:\Qt\4.1.0\mkspecs\features\shared.prf \
-		C:\Qt\4.1.0\mkspecs\features\resources.prf \
-		C:\Qt\4.1.0\mkspecs\features\uic.prf
+Makefile: preprocessor.pro  D:/Qt/4.1.0/mkspecs/win32-msvc.net\qmake.conf D:/Qt/4.1.0/mkspecs/qconfig.pri \
+		D:\Qt\4.1.0\mkspecs\features\qt_config.prf \
+		D:\Qt\4.1.0\mkspecs\features\exclusive_builds.prf \
+		D:\Qt\4.1.0\mkspecs\features\default_pre.prf \
+		D:\Qt\4.1.0\mkspecs\features\win32\default_pre.prf \
+		D:\Qt\4.1.0\mkspecs\features\release.prf \
+		D:\Qt\4.1.0\mkspecs\features\debug_and_release.prf \
+		D:\Qt\4.1.0\mkspecs\features\default_post.prf \
+		D:\Qt\4.1.0\mkspecs\features\qt.prf \
+		D:\Qt\4.1.0\mkspecs\features\win32\opengl.prf \
+		D:\Qt\4.1.0\mkspecs\features\moc.prf \
+		D:\Qt\4.1.0\mkspecs\features\win32\thread.prf \
+		D:\Qt\4.1.0\mkspecs\features\warn_off.prf \
+		D:\Qt\4.1.0\mkspecs\features\win32\console.prf \
+		D:\Qt\4.1.0\mkspecs\features\win32\rtti.prf \
+		D:\Qt\4.1.0\mkspecs\features\win32\exceptions.prf \
+		D:\Qt\4.1.0\mkspecs\features\win32\stl.prf \
+		D:\Qt\4.1.0\mkspecs\features\shared.prf \
+		D:\Qt\4.1.0\mkspecs\features\resources.prf \
+		D:\Qt\4.1.0\mkspecs\features\uic.prf
 	$(QMAKE) -win32 -o Makefile preprocessor.pro
-C:/Qt/4.1.0/mkspecs/qconfig.pri:
-C:\Qt\4.1.0\mkspecs\features\qt_config.prf:
-C:\Qt\4.1.0\mkspecs\features\exclusive_builds.prf:
-C:\Qt\4.1.0\mkspecs\features\default_pre.prf:
-C:\Qt\4.1.0\mkspecs\features\win32\default_pre.prf:
-C:\Qt\4.1.0\mkspecs\features\release.prf:
-C:\Qt\4.1.0\mkspecs\features\debug_and_release.prf:
-C:\Qt\4.1.0\mkspecs\features\default_post.prf:
-C:\Qt\4.1.0\mkspecs\features\qt.prf:
-C:\Qt\4.1.0\mkspecs\features\win32\opengl.prf:
-C:\Qt\4.1.0\mkspecs\features\moc.prf:
-C:\Qt\4.1.0\mkspecs\features\win32\thread.prf:
-C:\Qt\4.1.0\mkspecs\features\warn_off.prf:
-C:\Qt\4.1.0\mkspecs\features\win32\console.prf:
-C:\Qt\4.1.0\mkspecs\features\win32\rtti.prf:
-C:\Qt\4.1.0\mkspecs\features\win32\exceptions.prf:
-C:\Qt\4.1.0\mkspecs\features\win32\stl.prf:
-C:\Qt\4.1.0\mkspecs\features\shared.prf:
-C:\Qt\4.1.0\mkspecs\features\resources.prf:
-C:\Qt\4.1.0\mkspecs\features\uic.prf:
+D:/Qt/4.1.0/mkspecs/qconfig.pri:
+D:\Qt\4.1.0\mkspecs\features\qt_config.prf:
+D:\Qt\4.1.0\mkspecs\features\exclusive_builds.prf:
+D:\Qt\4.1.0\mkspecs\features\default_pre.prf:
+D:\Qt\4.1.0\mkspecs\features\win32\default_pre.prf:
+D:\Qt\4.1.0\mkspecs\features\release.prf:
+D:\Qt\4.1.0\mkspecs\features\debug_and_release.prf:
+D:\Qt\4.1.0\mkspecs\features\default_post.prf:
+D:\Qt\4.1.0\mkspecs\features\qt.prf:
+D:\Qt\4.1.0\mkspecs\features\win32\opengl.prf:
+D:\Qt\4.1.0\mkspecs\features\moc.prf:
+D:\Qt\4.1.0\mkspecs\features\win32\thread.prf:
+D:\Qt\4.1.0\mkspecs\features\warn_off.prf:
+D:\Qt\4.1.0\mkspecs\features\win32\console.prf:
+D:\Qt\4.1.0\mkspecs\features\win32\rtti.prf:
+D:\Qt\4.1.0\mkspecs\features\win32\exceptions.prf:
+D:\Qt\4.1.0\mkspecs\features\win32\stl.prf:
+D:\Qt\4.1.0\mkspecs\features\shared.prf:
+D:\Qt\4.1.0\mkspecs\features\resources.prf:
+D:\Qt\4.1.0\mkspecs\features\uic.prf:
 qmake: qmake_all FORCE
 	@$(QMAKE) -win32 -o Makefile preprocessor.pro
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 496)
@@ -11,4 +11,5 @@
 #include "VspKdTree.h"
 #include "RenderSimulator.h"
+#include "GlRenderer.h"
 
 Preprocessor *preprocessor;
@@ -22,4 +23,7 @@
 {
   environment->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
+  // renderer will be constructed when the scene graph and viewcell manager will be known
+  renderer = NULL;
+  
 }
 
@@ -27,9 +31,9 @@
 Preprocessor::~Preprocessor()
 {
-	DEL_PTR(mViewCellsManager);
-	DEL_PTR(mBspTree);
-	DEL_PTR(mKdTree);
-	DEL_PTR(mVspKdTree);
-	DEL_PTR(mVspBspTree);
+  DEL_PTR(mViewCellsManager);
+  DEL_PTR(mBspTree);
+  DEL_PTR(mKdTree);
+  DEL_PTR(mVspKdTree);
+  DEL_PTR(mVspBspTree);
 }
 
@@ -256,4 +260,8 @@
 	}
 
+
+	if (mUseGlRenderer)
+	  renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager);
+	
 	return true;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 496)
@@ -449,9 +449,10 @@
   // compute rendering error
   if (renderer) {
-	emit EvalPvsStat();
-	QMutex mutex;
-	mutex.lock();
-	renderer->mRenderingFinished.wait(&mutex);
-	mutex.unlock();
+	//	emit EvalPvsStat();
+	//	QMutex mutex;
+	//	mutex.lock();
+	//	renderer->mRenderingFinished.wait(&mutex);
+	//	mutex.unlock();
+	renderer->EvalPvsStat();
 	mStats <<
 	  "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
@@ -465,5 +466,5 @@
 {
   
-  connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) );
+  //  connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) );
   
   cout<<"Rss Preprocessor started\n"<<flush;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 494)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 496)
@@ -55,4 +55,18 @@
 		  exit(1);
 		}
+
+  QApplication *app = NULL;
+  
+  if (p->mUseGlRenderer) {
+	// create a qt application first (must be created before any opengl widget...
+	app = new QApplication(argc, argv);
+	
+	if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
+	  QMessageBox::information(0, "OpenGL pbuffers", 
+							   "This system does not support OpenGL/pbuffers.",
+							   QMessageBox::Ok);
+	  return -1;
+	}
+  }
   
   preprocessor = p;
@@ -79,22 +93,12 @@
 
   if (p->mUseGlRenderer) {
-	QApplication a(argc, argv);
 	
-	if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
-	  QMessageBox::information(0, "OpenGL pbuffers", 
-							   "This system does not support OpenGL/pbuffers.",
-							   QMessageBox::Ok);
-	  return -1;
-	}
-	
-	renderer = new GlRenderer(p->mSceneGraph, p->mViewCellsManager);
+	rendererWidget = new GlRendererWidget(p->mSceneGraph, p->mViewCellsManager);
 	//  renderer->resize(640, 480);
-	renderer->resize(1024, 768);
-	renderer->show();
-	
-	
+	rendererWidget->resize(640, 480);
+	rendererWidget->show();
+
 	pt->start(QThread::LowPriority);
 	
-	return a.exec();
   } else {
 	// just call the mail method -> will be executed in the main thread
@@ -105,4 +109,6 @@
   //  DEL_PTR(p);
   //  DEL_PTR(environment);
+  if (app)
+	return app->exec();
 
   return 0;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 496)
@@ -162,6 +162,6 @@
 	#type kdTree
 	#type vspKdTree
-	# type bspTree
-	type vspBspTree
+	 type bspTree
+	# type vspBspTree
 	
 	#type sceneDependent
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 496)
@@ -20,4 +20,5 @@
 struct VssRayContainer;
 
+class GlRendererBuffer;
 
 /** Namespace for the external visibility preprocessor
@@ -136,4 +137,6 @@
   RenderSimulator *mRenderSimulator;
 
+  GlRendererBuffer *renderer;
+  
   signals:
   void EvalPvsStat();
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 494)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 496)
@@ -38,7 +38,13 @@
 };
 
-class GlRenderer : public QGLWidget
+
+/** Class encapsulating gl rendering for the scene.
+	There is no gl context binding so the binding is performed in the
+	derived classes
+*/
+
+class GlRenderer
 {
-  Q_OBJECT
+
 public:
   SceneGraph *mSceneGraph;
@@ -50,29 +56,20 @@
 
   int timerId;
-  Vector3 mousePoint;
   bool mUseFalseColors;
 
   HaltonSequence halton;
   
-  bool mRender;
   int mFrame;
-
-  int mPvsStatFrames;
-  vector<float> mPvsErrorBuffer;
-  
-  PvsRenderStatistics mPvsStat;
   
   QWaitCondition mRenderingFinished;
-
-  QGLPixelBuffer *pbuffer;
   
   
   GlRenderer(SceneGraph *sceneGraph,
-		   ViewCellsManager *viewcells);
-
+			 ViewCellsManager *viewcells);
+  
   ~GlRenderer();
 
+
   void SetupFalseColor(const int id);
-  
   void RenderIntersectable(Intersectable *);
   void RenderMeshInstance(MeshInstance *mi);
@@ -81,10 +78,83 @@
   void SetupCamera();
   void RandomViewPoint();
+  
+  void
+  RenderScene();
 
-  void initializeGL();
+  void
+  SetupProjection(const int w, const int h);
+
+  
+  
+  float
+  GetPixelError();
+
+  void InitGL();
+
+  virtual int GetWidth() const = 0;
+  virtual int GetHeight() const = 0;
+};
+
+
+class GlRendererBuffer : public QGLPixelBuffer, public GlRenderer
+{
+
+public:
+  int mPvsStatFrames;
+  vector<float> mPvsErrorBuffer;
+  
+  PvsRenderStatistics mPvsStat;
+
+
+  GlRendererBuffer(const int w,
+				   const int h,
+				   SceneGraph *sceneGraph,
+				   ViewCellsManager *viewcells):
+	QGLPixelBuffer(QSize(w, h)), GlRenderer(sceneGraph, viewcells) {
+
+	mPvsStatFrames = 10000;
+	mPvsErrorBuffer.resize(mPvsStatFrames);
+	ClearErrorBuffer();
+
+	makeCurrent();
+	InitGL();
+	
+  }
+  
+  void
+  EvalPvsStat();
+
+  void
+  ClearErrorBuffer();
+
+  
+
+  virtual int GetWidth() const { return width(); }
+  virtual int GetHeight() const { return height(); }
+
+};
+
+
+
+class GlRendererWidget : public QGLWidget, public GlRenderer
+{
+  Q_OBJECT
+public:
+
+  GlRendererWidget(SceneGraph *sceneGraph,
+				   ViewCellsManager *viewcells,
+				   QWidget * parent = 0, const QGLWidget * shareWidget = 0, Qt::WFlags f = 0
+				   ):
+	GlRenderer(sceneGraph, viewcells), QGLWidget(parent, shareWidget, f)
+  {}
+
+  Vector3 mousePoint;
+  
+  void initializeGL() {
+	InitGL();
+  }
   void resizeGL(int w, int h);
   void paintGL();
   void timerEvent(QTimerEvent *) {
-	if (mRender) 
 	  update();
   }
@@ -94,27 +164,14 @@
   void mouseMoveEvent(QMouseEvent *);
 
-  void
-  ClearErrorBuffer();
+  float
+  RenderErrors();
 
-  void
-  RenderScene();
-
-  void
-  SetupProjection(const int w, const int h);
-
- public slots:
- void
- EvalPvsStat();
-  
-  float Render();
-
-  float
-  GetPixelError();
-
+  virtual int GetWidth() const { return width(); }
+  virtual int GetHeight() const { return height(); }
 
 };
 
 
-extern GlRenderer *renderer;
+extern GlRendererWidget *rendererWidget;
 
 #endif
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 494)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 496)
@@ -8,5 +8,5 @@
 #include <GL/glext.h>
 
-GlRenderer *renderer = NULL;
+GlRendererWidget *rendererWidget = NULL;
 
 #ifdef _WIN32
@@ -18,6 +18,5 @@
 
 GlRenderer::GlRenderer(SceneGraph *sceneGraph,
-				   ViewCellsManager *viewCellsManager):
-  QGLWidget(),
+					   ViewCellsManager *viewCellsManager):
   mSceneGraph(sceneGraph),
   mViewCellsManager(viewCellsManager)
@@ -26,18 +25,10 @@
   mViewPoint = mSceneGraph->GetBox().Center();
   mViewDirection = Vector3(0,0,1);
-  mRender = true;
-
   //  timerId = startTimer(10);
   mFrame = 0;
-  mPvsStatFrames = 10000;
-  mPvsErrorBuffer.resize(mPvsStatFrames);
-  ClearErrorBuffer();
-  pbuffer = NULL;
 }
 
 GlRenderer::~GlRenderer()
 {
-  if (pbuffer)
-	delete pbuffer;
 }
 
@@ -101,5 +92,5 @@
 	
 void
-GlRenderer::initializeGL()
+GlRenderer::InitGL()
 {
   glMatrixMode(GL_PROJECTION);
@@ -124,17 +115,4 @@
 }
 
-void
-GlRenderer::resizeGL(int w, int h)
-{
-  SetupProjection(w, h);
-
-  if (pbuffer == NULL || pbuffer->size().width() != w || pbuffer->size().height() != h) {
-	if (pbuffer)
-	  delete pbuffer;
-	pbuffer = new QGLPixelBuffer(QSize(w,h));
-  }
-  
-  updateGL();
-}
 
 
@@ -182,60 +160,89 @@
 }
 
-void
-GlRenderer::paintGL()
-{
-  if (mRender) {
-	Render();
-	mFrame++;
-  }
-}
-
-void
-GlRenderer::ClearErrorBuffer()
-{
-  for (int i=0; i < mPvsStatFrames; i++) {
-	mPvsErrorBuffer[i] = 1.0f;
-  }
-}
-
-void
-GlRenderer::EvalPvsStat()
-{
-  mPvsStat.Reset();
-  halton.Reset();
-
-  pbuffer->makeCurrent();
-  initializeGL();
-  SetupProjection(pbuffer->size().width(), pbuffer->size().height());
-  
-  for (int i=0; i < mPvsStatFrames; i++) {
-	float err;
-	RandomViewPoint();
-	if (mPvsErrorBuffer[i] > 0.0f) {
-	  mPvsErrorBuffer[i] = GetPixelError();
-	  cout<<"("<<i<<","<<mPvsErrorBuffer[i]<<")";
-	  //	  swapBuffers();
+
+float
+GlRenderer::GetPixelError()
+{
+  float pErrorPixels = -1.0f;
+
+  glReadBuffer(GL_BACK);
+  
+  mUseFalseColors = true;
+
+  SetupCamera();
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+  glEnable( GL_CULL_FACE );
+  
+  RenderScene();
+
+  // now check whether any backfacing polygon would pass the depth test
+  static int query = -1;
+  if (query == -1)
+	glGenOcclusionQueriesNV(1, (unsigned int *)&query);
+  
+  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+  glDepthMask(GL_FALSE);
+  glDisable( GL_CULL_FACE );
+  
+  glBeginOcclusionQueryNV(query);
+  
+  RenderScene();
+  
+  glEndOcclusionQueryNV();
+
+  // at this point, if possible, go and do some other computation
+  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+  glDepthMask(GL_TRUE);
+  glEnable( GL_CULL_FACE );
+  
+  unsigned int pixelCount;
+  // reenable other state
+  glGetOcclusionQueryuivNV(query,
+						   GL_PIXEL_COUNT_NV,
+						   &pixelCount);
+
+  if (pixelCount > 0)
+	return -1.0f; // backfacing polygon found -> not a valid viewspace sample
+
+  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+  
+  if (viewcell) {
+	SetupCamera();
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	// Render PVS
+	std::map<Intersectable *,
+	  PvsData<Intersectable *>,
+	  LtSample<Intersectable *> >::const_iterator it = viewcell->GetPvs().mEntries.begin();
+	
+	for (; it != viewcell->GetPvs().mEntries.end(); ++ it) {
+	  Intersectable *object = (*it).first;
+	  RenderIntersectable(object);
 	}
-	
-	err = mPvsErrorBuffer[i];
-	
-	if (err >= 0.0f) {
-	  if (err > mPvsStat.maxError)
-		mPvsStat.maxError = err;
-	  mPvsStat.sumError += err;
-	  if (err == 0.0f)
-		mPvsStat.errorFreeFrames++;
-	  mPvsStat.frames++;
-	}
-  }
-  
-  pbuffer->doneCurrent();
-
-  cout<<endl<<flush;
-  mRenderingFinished.wakeAll();
-}
+
+	glBeginOcclusionQueryNV(query);
+
+	SetupCamera();
+
+	RenderScene();
+	
+	glEndOcclusionQueryNV();
+	
+
+	unsigned int pixelCount;
+	// reenable other state
+	glGetOcclusionQueryuivNV(query,
+							 GL_PIXEL_COUNT_NV,
+							 &pixelCount);
+	
+	pErrorPixels = (100.f*pixelCount)/(GetWidth()*GetHeight());
+  }
+  
+  return pErrorPixels;
+}
+
 
 float
-GlRenderer::Render()
+GlRendererWidget::RenderErrors()
 {
   float pErrorPixels = -1.0f;
@@ -346,4 +353,5 @@
   return pErrorPixels;
 }
+
 
 void
@@ -363,88 +371,57 @@
 }
 
-float
-GlRenderer::GetPixelError()
-{
-  float pErrorPixels = -1.0f;
-
-  glReadBuffer(GL_BACK);
-  
-  mUseFalseColors = true;
-
-  SetupCamera();
-  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-  glEnable( GL_CULL_FACE );
-  
-  RenderScene();
-
-  // now check whether any backfacing polygon would pass the depth test
-  static int query = -1;
-  if (query == -1)
-	glGenOcclusionQueriesNV(1, (unsigned int *)&query);
-  
-  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-  glDepthMask(GL_FALSE);
-  glDisable( GL_CULL_FACE );
-  
-  glBeginOcclusionQueryNV(query);
-  
-  RenderScene();
-  
-  glEndOcclusionQueryNV();
-
-  // at this point, if possible, go and do some other computation
-  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-  glDepthMask(GL_TRUE);
-  glEnable( GL_CULL_FACE );
-  
-  unsigned int pixelCount;
-  // reenable other state
-  glGetOcclusionQueryuivNV(query,
-						   GL_PIXEL_COUNT_NV,
-						   &pixelCount);
-
-  if (pixelCount > 0)
-	return -1.0f; // backfacing polygon found -> not a valid viewspace sample
-
-  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
-  
-  if (viewcell) {
-	SetupCamera();
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	// Render PVS
-	std::map<Intersectable *,
-	  PvsData<Intersectable *>,
-	  LtSample<Intersectable *> >::const_iterator it = viewcell->GetPvs().mEntries.begin();
-	
-	for (; it != viewcell->GetPvs().mEntries.end(); ++ it) {
-	  Intersectable *object = (*it).first;
-	  RenderIntersectable(object);
+
+void
+GlRendererBuffer::ClearErrorBuffer()
+{
+  for (int i=0; i < mPvsStatFrames; i++) {
+	mPvsErrorBuffer[i] = 1.0f;
+  }
+}
+
+
+void
+GlRendererBuffer::EvalPvsStat()
+{
+  mPvsStat.Reset();
+  halton.Reset();
+
+  makeCurrent();
+
+  SetupProjection(GetWidth(), GetHeight());
+  
+  for (int i=0; i < mPvsStatFrames; i++) {
+	float err;
+	RandomViewPoint();
+	if (mPvsErrorBuffer[i] > 0.0f) {
+	  mPvsErrorBuffer[i] = GetPixelError();
+	  cout<<"("<<i<<","<<mPvsErrorBuffer[i]<<")";
+	  //	  swapBuffers();
 	}
-
-	glBeginOcclusionQueryNV(query);
-
-	SetupCamera();
-
-	RenderScene();
-	
-	glEndOcclusionQueryNV();
-	
-
-	unsigned int pixelCount;
-	// reenable other state
-	glGetOcclusionQueryuivNV(query,
-							 GL_PIXEL_COUNT_NV,
-							 &pixelCount);
-	
-	pErrorPixels = (100.f*pixelCount)/(height()*width());
-  }
-  
-  return pErrorPixels;
-}
-
-
-void
-GlRenderer::mousePressEvent(QMouseEvent *e)
+	
+	err = mPvsErrorBuffer[i];
+	
+	if (err >= 0.0f) {
+	  if (err > mPvsStat.maxError)
+		mPvsStat.maxError = err;
+	  mPvsStat.sumError += err;
+	  if (err == 0.0f)
+		mPvsStat.errorFreeFrames++;
+	  mPvsStat.frames++;
+	}
+  }
+  
+  doneCurrent();
+
+  cout<<endl<<flush;
+  mRenderingFinished.wakeAll();
+}
+
+
+
+
+
+void
+GlRendererWidget::mousePressEvent(QMouseEvent *e)
 {
   int x = e->pos().x();
@@ -457,5 +434,5 @@
 
 void
-GlRenderer::mouseMoveEvent(QMouseEvent *e)
+GlRendererWidget::mouseMoveEvent(QMouseEvent *e)
 {
   float MOVE_SENSITIVITY = Magnitude(mSceneGraph->GetBox().Diagonal())*1e-3;
@@ -485,7 +462,21 @@
 
 void
-GlRenderer::mouseReleaseEvent(QMouseEvent *)
-{
-
-
-}
+GlRendererWidget::mouseReleaseEvent(QMouseEvent *)
+{
+
+
+}
+
+void
+GlRendererWidget::resizeGL(int w, int h)
+{
+  SetupProjection(w, h);
+  updateGL();
+}
+
+void
+GlRendererWidget::paintGL()
+{
+  RenderErrors();
+  mFrame++;
+}
