Index: GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 1328)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 1958)
@@ -53,4 +53,5 @@
 	void SetRoot(SceneGraphNode *sgNnode);
 
+
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h	(revision 1883)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h	(revision 1958)
@@ -184,6 +184,6 @@
   // Viewer is the coordinates of viewer location, UpL is the vector.
   friend void ViewVectors(const Vector3 &DirAt, const Vector3 &Viewer,
-	                  const Vector3 &UpL, Vector3 &ViewV,
-			  Vector3 &ViewU, Vector3 &ViewN );
+						  const Vector3 &UpL, Vector3 &ViewV,
+						  Vector3 &ViewU, Vector3 &ViewN );
 
   // Given the intersection point `P', you have available normal `N'
@@ -262,4 +262,13 @@
 
 
+inline void
+Vector3::RightHandedBase(Vector3& U, Vector3& V) const
+{
+	// HACK
+	V = ArbitraryNormal(*this);
+	U = CrossProd(V, *this);
+}
+
+
 inline Vector3
 ToGlobalFrame(const Vector3 &loc,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 1344)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 1958)
@@ -181,3 +181,4 @@
 }
 
+
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1940)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1958)
@@ -1294,3 +1294,28 @@
 }
 
-}
+
+Intersectable *Preprocessor::GetObjectById(const int id)
+{
+#if 0
+	// create a dummy mesh instance to be able to use stl
+	MeshInstance object(NULL);
+	object.SetId(id);
+
+	ObjectContainer::const_iterator oit =
+		lower_bound(mObjects.begin(), mObjects.end(), &object, ilt);
+				
+	// objects sorted by id
+	if ((oit != mObjects.end()) && ((*oit)->GetId() == object.GetId()))
+	{
+		return (*oit);
+	}
+	else
+	{
+		return NULL;
+	}
+#else
+	return mObjects[id];
+#endif
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1949)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1958)
@@ -717,10 +717,4 @@
 			</File>
 			<File
-				RelativePath=".\QtPreprocessorThread.cpp">
-			</File>
-			<File
-				RelativePath=".\QtPreprocessorThread.h">
-			</File>
-			<File
 				RelativePath="..\src\Ray.cpp">
 			</File>
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1942)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1958)
@@ -147,7 +147,6 @@
 						  const bool pruneInvalidRays = true);
 
-  /** Compute pixel error of the current PVS solution by sampling given number of viewpoints */
-  virtual void
-  ComputeRenderError();
+	/** Compute pixel error of the current PVS solution by sampling given number of viewpoints */
+	virtual void ComputeRenderError();
 
 	/** Returns a view cells manager of the given name.
@@ -172,4 +171,6 @@
 	PreprocessorThread *GetThread() const;
 
+	
+	Intersectable *GetObjectById(const int id);
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1954)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1958)
@@ -106,5 +106,6 @@
 {
 	//globalLinesRenderer->DrawGeometry();
-	globalLinesRenderer->CastGlobalLines(Beam(), 0);
+	//globalLinesRenderer->CastGlobalLines(Beam(), 0);
+	globalLinesRenderer->DisplayBuffer();
 	PrintGLerror("display");
 
@@ -124,14 +125,36 @@
 	{
 	case '2':
-		++ globalLinesRenderer->mMaxDepth;
-		cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
-		return;
+		{
+			VssRayContainer rays;
+
+			++ globalLinesRenderer->mMaxDepth;
+			globalLinesRenderer->ApplyDepthPeeling(rays, Beam(), 0);
+
+			cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
+			return;
+		}
 	case '1':
-		-- globalLinesRenderer->mMaxDepth;
-		cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
-		return;
+		{
+			VssRayContainer rays;
+
+			-- globalLinesRenderer->mMaxDepth;
+			cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
+			
+			globalLinesRenderer->ApplyDepthPeeling(rays, Beam(), 0);
+			return;
+		}
 	case '3':
-		//globalLinesRenderer->ApplyDepthPeeling(Beam(), 0);
-		globalLinesRenderer->GrabDepthBuffer(globalLinesRenderer->mNewDepthBuffer, globalLinesRenderer->mNewTexture);
+		{
+			//globalLinesRenderer->ApplyDepthPeeling(Beam(), 0);
+			globalLinesRenderer->ExportDepthBuffer();
+			return;
+		}
+	case '4':
+		{
+			VssRayContainer rays;
+
+			globalLinesRenderer->ApplyDepthPeeling(rays, Beam(), 0);
+			return;
+		}
 	default:
 		return;
@@ -162,4 +185,36 @@
 
 
+void GlobalLinesRenderer::DisplayBuffer()
+{
+	//mNewTexture->Bind();
+	mNewTexture->BindDepth();
+	mNewTexture->EnableTextureTarget();
+	
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+	if (mNewTexture->IsRectangleTexture())
+	{
+		glBegin(GL_QUADS);
+		glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
+		glTexCoord2f(mNewTexture->GetWidth(), 0); glVertex3f( 1, -1, -0.5f);
+		glTexCoord2f(mNewTexture->GetWidth(), mNewTexture->GetHeight()); glVertex3f( 1,  1, -0.5f);
+		glTexCoord2f(0, mNewTexture->GetHeight()); glVertex3f(-1, 1, -0.5f);
+		glEnd();
+	}
+	else
+	{
+		glBegin(GL_QUADS);
+		glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
+		glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
+		glTexCoord2f(1, 1); glVertex3f( 1,  1, -0.5f);
+		glTexCoord2f(0, 1); glVertex3f(-1,  1, -0.5f);
+		glEnd();
+	}  
+
+	mNewTexture->DisableTextureTarget();
+	PrintGLerror("displaytexture");
+}
+
+
 GlobalLinesRenderer::~GlobalLinesRenderer()
 {
@@ -168,4 +223,11 @@
 	if (sCgContext)
 		cgDestroyContext(sCgContext);
+
+	// init the receiving buffers
+	delete mNewDepthBuffer;
+	delete mOldDepthBuffer;
+	
+	delete mNewItemBuffer;
+	delete mOldItemBuffer;
 }
 
@@ -173,6 +235,8 @@
 void GlobalLinesRenderer::CastGlobalLines(Beam &beam, const int samples)
 {
+	VssRayContainer rays;
+
 	// bind pixel shader implementing the front depth buffer functionality
-	ApplyDepthPeeling(beam, samples);
+	ApplyDepthPeeling(rays, beam, samples);
 }
 
@@ -192,6 +256,4 @@
 	{ 
 		//glLoadIdentity();
-			
-		//mRenderer->mUseFalseColors = true;
 		ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
 
@@ -215,4 +277,20 @@
 
 
+void GlobalLinesRenderer::ComputeLookAt(const float alpha, 
+										const float beta,
+										Vector3 &eye, 
+										Vector3 &up, 
+										Vector3 left)
+{
+	//float x = cos(alpha);
+	//float y = sin(alpha);
+	eye.x = sin(alpha) * cos(beta);
+	eye.y = sin(alpha) * sin(beta);
+	eye.z = cos(beta);
+
+	eye.RightHandedBase(up, left);
+}
+
+
 void GlobalLinesRenderer::InitGl()
 {
@@ -241,21 +319,36 @@
 	glLoadIdentity();
 
-	// init the receiving buffers
+	// initialise the receiving buffers
 	mNewDepthBuffer = new float[texWidth * texHeight];
 	mOldDepthBuffer = new float[texWidth * texHeight];
 	
+	for (int i = 0; i < texWidth * texHeight; ++ i)
+	{
+		mNewDepthBuffer[i] = 1;
+		mOldDepthBuffer[i] = 1;
+	}
+
 	mNewItemBuffer = new int[texWidth * texHeight *4];
 	mOldItemBuffer = new int[texWidth * texHeight *4];
 	
+	for (int i = 0; i < texWidth * texHeight * 4; ++ i)
+	{
+		mNewItemBuffer[i] = 0;
+		mOldItemBuffer[i] = 0;
+	}
+
 	AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
 	
-	Vector3 midPoint = bbox.Center();
+	mViewPoint = bbox.Center();
 	
 	const float sceneSize = Magnitude(bbox.Diagonal());
-	Vector3 viewPoint = midPoint + Vector3(0.5 * sceneSize, 0, 0);
-	
-	cout << "mid point: " << midPoint << endl;
+	Vector3 viewPoint = mViewPoint + Vector3(0.5 * sceneSize, 0, 0);
+	
+	cout << "mid point: " << mViewPoint << endl;
 	cout << "view point: " << viewPoint << endl;
 	cout << "scene: " << bbox << endl;
+
+	mEyeVec = Normalize(viewPoint - mViewPoint);
+	mEyeVec.RightHandedBase(mUpVec, mLeftVec);
 
 	/*gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, 
@@ -331,5 +424,6 @@
 		SetFrustum(sceneSize, sceneSize, 1, sceneSize);
 
-		glClearColor(0.1, 0.7, 0.2, 1);
+		// for item buffer: white means no color
+		glClearColor(1, 1, 1, 1);
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -343,5 +437,5 @@
 		glLoadIdentity();
 		gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, 
-				  midPoint.x, midPoint.y, midPoint.z, 
+				  mViewPoint.x, mViewPoint.y, mViewPoint.z, 
 				  0, 1, 0);
 
@@ -350,8 +444,9 @@
 	mNewTexture->EndCapture();
 
-	// setup the rendering context for the RenderTexture
+	// setup the rendering context for the other depth buffer
 	mOldTexture->BeginCapture();
 	{
-		glClearColor(0.1, 0.7, 0.2, 1);
+		// for item buffer: white means no color
+		glClearColor(1, 1, 1, 1);
        
 		//Reshape(texWidth, texHeight);
@@ -369,5 +464,5 @@
 		
 		gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, 
-				  midPoint.x, midPoint.y, midPoint.z, 
+				  mViewPoint.x, mViewPoint.y, mViewPoint.z, 
 				  0, 1, 0);
 
@@ -406,18 +501,39 @@
 
 			float newDummy2 = x - texWidth / 2;
-			float newDummy3 = y -  texHeight / 2;
+			float newDummy3 = y - texHeight / 2;
 
 			float oldDummy2 = newDummy2;
 			float oldDummy3 = newDummy3;
 
-			const Vector3 newPt = newDepth * mEyeVec + newDummy1 * mLeftVec + newDummy2 * mUpVec;
-			const Vector3 oldPt = oldDepth * mEyeVec + oldDummy1 * mLeftVec + oldDummy2 * mUpVec;
-
-			Intersectable *termObj1 = GetObject(mNewItemBuffer[index]);
-			Intersectable *termObj2 = GetObject(mOldItemBuffer[index]);
-
+			const Vector3 newPt = mViewPoint + newDepth * mEyeVec + newDummy1 * mLeftVec + newDummy2 * mUpVec;
+			const Vector3 oldPt = mViewPoint + oldDepth * mEyeVec + oldDummy1 * mLeftVec + oldDummy2 * mUpVec;
+
+			//cout << "viewpoint: " << mViewPoint << " new: " << newPt << " old: " << newPt << endl;
+			
+			int r = mNewItemBuffer[index];
+			int g = mNewItemBuffer[index + 1];
+			int b = mNewItemBuffer[index + 2];
+			
+			Intersectable *termObj1 = NULL;
+			Intersectable *termObj2 = NULL;
+
+			// 3 times 255 means no valid object
+			if (!((r == 255) && (g == 255) && (b == 255)))
+			{
+				const int id = mRenderer->GetId(r, g, b);
+				cout << id << " ";
+				//termObj1 = mPreprocessor->GetObjectById(id);
+			}
+
+			/*r = mOldItemBuffer[index];
+			g = mOldItemBuffer[index + 1];
+			b = mOldItemBuffer[index + 2];
+
+			if (!((r == 255) && (g == 255) && (b == 255)))
+				Intersectable *termObj2 = mPreprocessor->GetObjectById(mRenderer->GetId(r, g, b));
+*/
 			// create rays in both directions
-			vssRays.push_back(new VssRay(oldPt, newPt, NULL, termObj1));
-			vssRays.push_back(new VssRay(newPt, oldPt, NULL, termObj2));
+			//vssRays.push_back(new VssRay(oldPt, newPt, NULL, termObj1));
+			//vssRays.push_back(new VssRay(newPt, oldPt, NULL, termObj2));
 		}
 	}
@@ -446,13 +562,14 @@
 void GlobalLinesRenderer::GrabItemBuffer(int *data, RenderTexture *rt)
 {
-	rt->BindDepth();
+	rt->Bind();
 	rt->EnableTextureTarget();
-	cout << "depth: " << mNewTexture->GetDepthBits() << endl;
-
-	const int texFormat = GL_DEPTH_COMPONENT;
+	//cout << "depth: " << mNewTexture->GetDepthBits() << endl;
+
+	const int texFormat = GL_RGBA;
 	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
 
 	mNewTexture->DisableTextureTarget();
 }
+
 
 void GlobalLinesRenderer::ExportDepthBuffer()
@@ -493,5 +610,40 @@
 
 
-void GlobalLinesRenderer::ApplyDepthPeeling(Beam &beam, const int samples)
+/*
+void GlobalLinesRenderer::ExportItemBuffer()
+{
+	mNewTexture->BindDepth();
+	mNewTexture->EnableTextureTarget();
+	cout << "depth: " << mNewTexture->GetDepthBits() << endl;
+
+	const bool components = 1;//mNewTexture->GetDepthBits() / 8;
+
+	float *data = new float[texWidth * texHeight * components];
+	//const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
+	const int texFormat = GL_DEPTH_COMPONENT;
+	//const int texFormat = GL_RGBA;
+	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
+
+	string filename("depth.tga");
+	ilRegisterType(IL_FLOAT);
+
+	const int depth = 1;
+	const int bpp = components;
+
+	ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data);
+	ilSaveImage((char *const)filename.c_str());
+
+	cout << "finished" << endl;
+	delete data;
+	cout << "data deleted" << endl;
+	mNewTexture->DisableTextureTarget();
+	PrintGLerror("grab texture");
+}
+*/
+
+
+void GlobalLinesRenderer::ApplyDepthPeeling(VssRayContainer &rays,
+											Beam &beam, 
+											const int samples)
 {
 	mNewTexture->BeginCapture();
@@ -504,6 +656,8 @@
 	
 	PrintGLerror("firstpass");
-
 	if (mNewTexture->IsRectangleTexture()) cout << "rect" << endl;
+
+	// process the buffers for the first layer
+	ProcessDepthBuffer(rays);
 
 	for(int i = 0; i < mMaxDepth; ++ i) 
@@ -520,5 +674,4 @@
 
 			cgGLBindProgram(sCgDepthPeelingProgram);
-		//	cgGLBindProgram(sCgPassThroughProgram);
 			cgGLEnableProfile(sCgFragmentProfile);
 			cgGLSetTextureParameter(sTextureParam, mOldTexture->GetDepthTextureID());
@@ -532,4 +685,8 @@
 			cgGLDisableTextureParameter(sTextureParam);
 			cgGLDisableProfile(sCgFragmentProfile);
+
+			// process the buffers for following layer
+			ProcessDepthBuffer(rays);
+
 		}
 		mNewTexture->EndCapture();
@@ -537,34 +694,6 @@
 
 	PrintGLerror("endpeeling");
-
-	//mNewTexture->Bind();
-	mNewTexture->BindDepth();
-	mNewTexture->EnableTextureTarget();
-	
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	
-	if (mNewTexture->IsRectangleTexture())
-	{
-		glBegin(GL_QUADS);
-		glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
-		glTexCoord2f(mNewTexture->GetWidth(), 0); glVertex3f( 1, -1, -0.5f);
-		glTexCoord2f(mNewTexture->GetWidth(), mNewTexture->GetHeight()); glVertex3f( 1,  1, -0.5f);
-		glTexCoord2f(0, mNewTexture->GetHeight()); glVertex3f(-1, 1, -0.5f);
-		glEnd();
-	}
-	else
-	{
-		glBegin(GL_QUADS);
-		glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
-		glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
-		glTexCoord2f(1, 1); glVertex3f( 1,  1, -0.5f);
-		glTexCoord2f(0, 1); glVertex3f(-1,  1, -0.5f);
-		glEnd();
-	}  
-
-	mNewTexture->DisableTextureTarget();
-	PrintGLerror("displaytexture");
-}
-
-
-}
+}
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h	(revision 1954)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h	(revision 1958)
@@ -51,7 +51,8 @@
 void GrabDepthBuffer(float *data, RenderTexture *rt);
 void GrabItemBuffer(int *data, RenderTexture *rt);
-void ApplyDepthPeeling(Beam &beam, const int samples);
+void ApplyDepthPeeling(VssRayContainer &rays, Beam &beam, const int samples);
 void ExportDepthBuffer();
 void ProcessDepthBuffer(VssRayContainer &vssRays);
+void DisplayBuffer();
 
 	int mMaxDepth;
@@ -62,4 +63,6 @@
 	float mNear;
 
+	Vector3 mViewPoint;
+
 	float *mNewDepthBuffer;
 	float *mOldDepthBuffer;
@@ -68,7 +71,11 @@
 RenderTexture *mNewTexture;
 	RenderTexture *mOldTexture;
+
+	void ComputeLookAt(const float alpha, 
+					   const float beta,
+					   Vector3 &eye, 
+					   Vector3 &up, 
+					   Vector3 left);
 protected:
-
-	void ProcessDepthBuffer();
 
 	void SwitchRenderTextures();
