Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 752)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 811)
@@ -9,4 +9,5 @@
 #include "Environment.h"
 
+//#define GL_GLEXT_PROTOTYPES
 #include <GL/glext.h>
 #include <Cg/cg.h>
@@ -34,4 +35,9 @@
 PFNGLENDOCCLUSIONQUERYNVPROC glEndOcclusionQueryNV;
 PFNGLGETOCCLUSIONQUERYUIVNVPROC glGetOcclusionQueryuivNV;
+
+//PFNGLGENQUERIESARBPROC glGenQueriesARB;
+//PFNGLBEGINQUERYARBPROC glBeginQueryARB;
+//PFNGLENDQUERYARBPROC glEndQueryARB;
+//PFNGLGETQUERYUIVARBPROC glGetQueryuivARB;
 #endif
 
@@ -57,4 +63,7 @@
   mSnapPrefix = "snap/";
   mUseForcedColors = false;
+
+  mUseGlLists = true;
+  
 }
 
@@ -140,5 +149,4 @@
 {
   // swap bits of the color
-  
   glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
 }
@@ -203,4 +211,16 @@
 
 
+#if 0
+  glGenQueriesARB = (PFNGLGENDQUERIESARBPROC)
+	wglGetProcAddress("glGenQueriesARB");
+  glBeginQueryARB = (PFNGLBEGINQUERYARBPROC)
+	wglGetProcAddress("glBeginQueryARB");
+  glEndQueryARB = (PFNGLENDQUERYARBPROC)
+	wglGetProcAddress("glEndQueryARB");
+  glGetQueryuivARB = (PFNGLGETQUERYUIVARBPROC)
+	wglGetProcAddress("glGetQueryuivARB");
+
+#endif
+  
 
 #if 0
@@ -391,10 +411,10 @@
 
 void
-GlRenderer::SetupProjection(const int w, const int h)
+GlRenderer::SetupProjection(const int w, const int h, const float angle)
 {
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
-  gluPerspective(70.0, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
+  gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
   glMatrixMode(GL_MODELVIEW);
 }
@@ -404,5 +424,9 @@
 {
   Vector3 target = mViewPoint + mViewDirection;
+
   Vector3 up(0,1,0);
+  
+  if (abs(DotProd(mViewDirection, up)) > 0.99f)
+	up = Vector3(1, 0, 0);
   
   glLoadIdentity();
@@ -411,4 +435,177 @@
 			up.x, up.y, up.z);
 }
+
+
+void
+GlRendererBuffer::EvalRenderCostSample(
+									   RenderCostSample &sample
+									   )
+{
+  mViewCellsManager->GetViewPoint(mViewPoint);
+  cout<<mViewPoint<<endl;
+  
+  // take a render cost sample by rendering a cube
+  Vector3 directions[6];
+  directions[0] = Vector3(1,0,0);
+  directions[1] = Vector3(0,1,0);
+  directions[2] = Vector3(0,0,1);
+  directions[3] = Vector3(-1,0,0);
+  directions[4] = Vector3(0,-1,0);
+  directions[5] = Vector3(0,0,-1);
+
+  sample.mVisibleObjects = 0;
+
+  int i, j;
+  
+  // reset object visibility
+  for (i=0; i < mObjects.size(); i++) {
+	mObjects[i]->mCounter = 0;
+  }
+  mFrame++;
+  for (i=0; i < 6; i++) {
+	mViewDirection = directions[i];
+	SetupCamera();
+	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+	glDepthFunc( GL_LESS );
+
+	mUseFalseColors = true;
+
+	RenderScene();
+
+	if (1) {
+	  char filename[256];
+	  sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i);
+	  QImage im = toImage();
+	  im.save(filename, "PNG");
+	}
+	
+	bool useItemBuffer = false;
+	
+	if (useItemBuffer) {
+	  // read back the texture
+	  glReadPixels(0, 0,
+				   GetWidth(), GetHeight(),
+				   GL_RGBA,
+				   GL_UNSIGNED_BYTE,
+				   mPixelBuffer);
+	  
+	  int x, y;
+	  unsigned int *p = mPixelBuffer;
+	  for (y = 0; y < GetHeight(); y++)
+		for (x = 0; x < GetWidth(); x++, p++) {
+		  unsigned int id = (*p) & 0xFFFFFF;
+		  if (id != 0xFFFFFF)
+			mObjects[id]->mCounter++;
+		}
+	  
+	} else {
+	
+	  //	  glDepthFunc( GL_LEQUAL );
+	  glDepthFunc( GL_LEQUAL );
+	  
+	  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+	  glDepthMask(GL_FALSE);
+	  
+	  // now issue queries for all objects
+	  for (j = 0; j < mObjects.size(); j++) {
+		int q;
+		for (q = 0; j + q < mObjects.size() && q < mOcclusionQueries.size(); q++) {
+		  glBeginOcclusionQueryNV(mOcclusionQueries[q]);
+		  RenderIntersectable(mObjects[j+q]);
+		  glEndOcclusionQueryNV();
+		}
+		
+		
+		// collect results of the queries
+		for (q = 0; j + q < mObjects.size() && q < mOcclusionQueries.size(); q++) {
+		  unsigned int pixelCount;
+		  // reenable other state
+
+#if 1
+		  do {
+			glGetOcclusionQueryuivNV(mOcclusionQueries[q],
+									 GL_PIXEL_COUNT_AVAILABLE_NV,
+									 &pixelCount);
+			if (!pixelCount)
+			  cout<<"W";
+		  } while(!pixelCount);
+#endif
+
+		  glGetOcclusionQueryuivNV(mOcclusionQueries[q],
+								   GL_PIXEL_COUNT_NV,
+								   &pixelCount);
+		  //		if (pixelCount)
+		  //		  cout<<"q="<<mOcclusionQueries[q]<<" pc="<<pixelCount<<" ";
+		  mObjects[j+q]->mCounter += pixelCount;
+		}
+		j += q;
+	  }
+	  
+	  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	  glDepthMask(GL_TRUE);
+	}
+	  
+	
+  }  
+  // now evaluate the statistics over that sample
+  // currently only the number of visible objects is taken into account
+  sample.Reset();
+  for (j = 0; j < mObjects.size(); j++) {
+	if (mObjects[j]->mCounter) {
+	  sample.mVisibleObjects++;
+	  sample.mVisiblePixels += mObjects[j]->mCounter;
+	}
+  }
+  cout<<"RS="<<sample.mVisibleObjects<<" ";
+}
+
+void
+GlRendererBuffer::SampleRenderCost(
+								   const int n,
+								   vector<RenderCostSample> &samples
+								   )
+{
+  makeCurrent();
+
+  if (mPixelBuffer == NULL)
+	mPixelBuffer = new unsigned int[GetWidth()*GetHeight()];
+  
+  SetupProjection(GetHeight(), GetHeight(), 90.0f);
+
+  samples.resize(n);
+  halton.Reset();
+  
+  int i;
+  int numQ = 500;
+  
+  if (mOcclusionQueries.size() < numQ) {
+	cout<<"allocating occ queries..."<<endl;
+	
+	numQ;
+	unsigned int *queries = new unsigned int[numQ];
+	for (i=0; i < numQ; i++)
+	  queries[i] = 111;
+	
+	glGenOcclusionQueriesNV(numQ, queries);
+	
+	mOcclusionQueries.resize(numQ);
+	
+	for (i=0; i < numQ; i++)
+	  mOcclusionQueries[i] = queries[i];
+	
+	delete queries;
+  }
+  
+  for (i=0; i < n; i++)
+	EvalRenderCostSample(
+						 samples[i]
+						 );
+
+  doneCurrent();
+
+}
+  
 
 void
@@ -455,11 +652,15 @@
 QGLPixelBuffer(QSize(w, h)), GlRenderer(sceneGraph, viewcells, tree) {
  
- environment->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames);
-   mPvsErrorBuffer.resize(mPvsStatFrames);
-	 ClearErrorBuffer();
-	   
-	   InitGL();
-		 
-		 }
+  environment->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames);
+  mPvsErrorBuffer.resize(mPvsStatFrames);
+  ClearErrorBuffer();
+
+  mPixelBuffer = NULL;
+  
+  makeCurrent();
+  InitGL();
+  doneCurrent();
+  
+}
 
 float
@@ -822,4 +1023,11 @@
 }
 
+void
+GlRenderer::_RenderScene()
+{
+  ObjectContainer::const_iterator oi = mObjects.begin();
+  for (; oi != mObjects.end(); oi++)
+	RenderIntersectable(*oi);
+}
 
 bool
@@ -827,15 +1035,15 @@
 {
   static int glList = -1;
-  if (glList == -1)
-  {
-	glList = glGenLists(1);
-	glNewList(glList, GL_COMPILE);
-	ObjectContainer::const_iterator oi = mObjects.begin();
-	for (; oi != mObjects.end(); oi++)
-	  RenderIntersectable(*oi);
-	glEndList();
-  }
-  
-  glCallList(glList);
+  if (mUseGlLists) {
+	if (glList == -1) {
+	  glList = glGenLists(1);
+	  glNewList(glList, GL_COMPILE);
+	  _RenderScene();
+	  glEndList();
+	}
+	glCallList(glList);
+  } else
+	_RenderScene();
+	
   
   return true;
@@ -1714,4 +1922,5 @@
  makeCurrent(); 
  GlRenderer::InitGL();
+
 #if 1
 	// initialise dual depth buffer textures
