Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 535)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 538)
@@ -21,4 +21,5 @@
 
 GlRendererWidget *rendererWidget = NULL;
+GlDebuggerWidget *debuggerWidget = NULL;
 
 
@@ -446,9 +447,8 @@
 {
   static int glList = -1;
-  if (glList != -1) {
-	glCallList(glList);
-  } else {
+  if (glList == -1)
+  {
 	glList = glGenLists(1);
-	glNewList(glList, GL_COMPILE_AND_EXECUTE);
+	glNewList(glList, GL_COMPILE);
 	ObjectContainer::const_iterator oi = mObjects.begin();
 	for (; oi != mObjects.end(); oi++)
@@ -456,4 +456,7 @@
 	glEndList();
   }
+  
+  glCallList(glList);
+  
   return true;
 }
@@ -565,6 +568,7 @@
 GlRendererWidget::paintGL()
 {
-  RenderErrors();
-  mFrame++;
+	RenderErrors();
+	
+	mFrame++;
 }
 
@@ -780,5 +784,5 @@
 	GenQueries((int)beam.mViewCells.size());
 
-	// now check whether any backfacing polygon would pass the depth test
+	// now check whether any backfacing polygon would pass the depth test?
 	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 	glDepthMask(GL_FALSE);
@@ -799,13 +803,24 @@
 
 	// 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);
+
+	queryIdx = 0;
+	unsigned int pixelCount;
 
 	// 8. The number of visible pixels is the number of sample rays which see the source
-	//    object from the corresponding viewcell -> rember these values for later update
-	//   of the viewcell pvs - or update immediatelly?
-
-	//  In general it is not neccessary to rember to extract all the rays cast. I hope it
+	//    object from the corresponding viewcell -> remember these values for later update
+	//   of the viewcell pvs - or update immediately?
+
+	for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit)
+	{
+		// fetch queries
+		glGetOcclusionQueryuivNV(sQueries[queryIdx ++],
+								 GL_PIXEL_COUNT_NV,
+								 &pixelCount);
+
+		Debug << "visble pixels: " << pixelCount << endl;
+	}
+	
+
+	//  In general it is not neccessary to remember to extract all the rays cast. I hope it
 	// would be sufficient to gain only the intergral statistics about the new contributions
 	// and so the rss tree would actually store no new rays (only the initial ones)
@@ -815,4 +830,9 @@
 	// (new viewcell has been discovered) or relative contribution greater than a threashold ... 
 
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	glDepthMask(GL_TRUE);
+	glEnable(GL_CULL_FACE);
+
+
 	cgGLDisableProfile(sCgFragmentProfile);
 	glDisable(GL_TEXTURE_2D);
@@ -838,4 +858,5 @@
 }
 
+
 void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint, 
 												 const Beam &beam, 
@@ -855,3 +876,115 @@
 			  viewPoint.x, viewPoint.y, viewPoint.z, 
 			  up.x, up.y, up.z);
-}			  
+}		
+
+  
+ 
+
+/***************************************************************/
+/*             GlDebuggerWidget implementation				   */
+/***************************************************************/
+
+
+GlDebuggerWidget::GlDebuggerWidget(QWidget *parent, GlRendererBuffer *buf)
+      : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
+{
+	// create the pbuffer
+    //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
+    timerId = startTimer(20);
+    setWindowTitle(("OpenGL pbuffers"));
+}
+
+
+GlDebuggerWidget::~GlDebuggerWidget()
+{
+	mRenderBuffer->releaseFromDynamicTexture();
+	glDeleteTextures(1, &dynamicTexture);
+	
+	DEL_PTR(mRenderBuffer);
+}
+
+
+void GlDebuggerWidget::initializeGL()
+{
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+
+	glFrustum(-1, 1, -1, 1, 10, 100);
+	glTranslatef(-0.5f, -0.5f, -0.5f);
+	glTranslatef(0.0f, 0.0f, -15.0f);
+	glMatrixMode(GL_MODELVIEW);
+
+	glEnable(GL_CULL_FACE);
+	initCommon();
+	initPbuffer();
+
+}
+
+
+void GlDebuggerWidget::resizeGL(int w, int h)
+{
+	glViewport(0, 0, w, h);
+}
+
+
+void GlDebuggerWidget::paintGL()
+{
+	// draw a spinning cube into the pbuffer..
+	mRenderBuffer->makeCurrent();
+	//mRenderBuffer->SampleBeamContributions();
+	glFlush();
+
+    // rendering directly to a texture is not supported on X11, unfortunately
+    mRenderBuffer->updateDynamicTexture(dynamicTexture);
+   
+    // ..and use the pbuffer contents as a texture when rendering the
+    // background and the bouncing cubes
+    makeCurrent();
+    glBindTexture(GL_TEXTURE_2D, dynamicTexture);
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+    // draw the background
+    glMatrixMode(GL_MODELVIEW);
+    glPushMatrix();
+    glLoadIdentity();
+    glMatrixMode(GL_PROJECTION);
+    glPushMatrix();
+    glLoadIdentity();
+
+  	glPopMatrix();
+	glMatrixMode(GL_MODELVIEW);
+	glPopMatrix();
+}
+
+
+void GlDebuggerWidget::initPbuffer()
+{
+	// set up the pbuffer context
+    mRenderBuffer->makeCurrent();
+	initCommon();
+
+	glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+	glOrtho(-1, 1, -1, 1, -99, 99);
+	glTranslatef(-0.5f, -0.5f, 0.0f);
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+
+	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+
+	// generate a texture that has the same size/format as the pbuffer
+    dynamicTexture = mRenderBuffer->generateDynamicTexture();
+
+	// bind the dynamic texture to the pbuffer - this is a no-op under X11
+	mRenderBuffer->bindToDynamicTexture(dynamicTexture);
+	makeCurrent();
+}
+
+void GlDebuggerWidget::initCommon()
+{
+	glEnable(GL_TEXTURE_2D);
+	glEnable(GL_DEPTH_TEST);
+
+	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+}
