Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1951)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1953)
@@ -5,4 +5,8 @@
 #include "Preprocessor.h"
 #include "GlRenderer.h"
+// the devil library
+#include <IL/il.h>
+#include <IL/ilu.h>
+#include <IL/ilut.h>
 
 #include <Cg/cg.h>
@@ -25,6 +29,21 @@
 static int texWidth = 256;
 static int texHeight = 256;
-static Vector3 scaleFactor = Vector3(2000.0f, 2000.0f, 2000.0f);
-static Vector3 translFactor = Vector3(1000.0f, 1000.0f, 1000.0f);
+
+
+static void InitDevIl()
+{
+	ilInit();
+	ILuint ImageName;
+	ilGenImages(1, &ImageName);
+	ilBindImage(ImageName);
+	ilEnable(IL_FILE_OVERWRITE);
+
+	//  ilRegisterFormat(IL_RGBA);
+	//  ilRegisterType(IL_FLOAT);
+
+	//  ilEnable(IL_ORIGIN_SET);
+	//  ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
+}
+
 
 static void cgErrorCallback()
@@ -69,4 +88,18 @@
 }
 
+
+void SetFrustum(int sizeX, int sizeY, float nearPlane, float farPlane)
+{
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+
+	glOrtho(-sizeX / 2, sizeX / 2, 
+			-sizeY / 2, sizeY / 2, 
+			nearPlane, farPlane);
+
+	/*glOrtho(0, sizeX, 
+		    0, sizeY , 
+			nearPlane, farPlane);*/
+}
 
 void Display()
@@ -98,8 +131,12 @@
 		cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
 		return;
+	case '3':
+		//globalLinesRenderer->ApplyDepthPeeling(Beam(), 0);
+		globalLinesRenderer->GrabDepthBuffer(globalLinesRenderer->mNewDepthBuffer, globalLinesRenderer->mNewTexture);
 	default:
 		return;
 	}
 }
+
 
 /*GlobalLinesRenderer::GlobalLinesRenderer(RenderTexture *buffer1, 
@@ -107,13 +144,15 @@
 										 Preprocessor *preprocessor,
 										 GlRenderer *renderer)
-: mNewBuffer(buffer1), mOldBuffer(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
+: mNewTexture(buffer1), mOldTexture(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
 mRenderer(renderer)
 {
 }
 */
+
+
 GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor, 
 										 GlRenderer *renderer): 
-mNewBuffer(NULL), 
-mOldBuffer(NULL), 
+mNewTexture(NULL), 
+mOldTexture(NULL), 
 mMaxDepth(0), 
 mRenderer(renderer), 
@@ -152,13 +191,6 @@
 	glPushMatrix();
 	{ 
-		glLoadIdentity();
-		//translFactor = Vector3(-1, -1, -1);
-		
-		glScalef(scaleFactor.x, scaleFactor.y, scaleFactor.z);
-		glTranslatef(translFactor.x, translFactor.y, translFactor.z);
-
-		//cout << "scale: " << scaleFactor << endl;
-		//cout << "transl: " << translFactor << endl;
-
+		//glLoadIdentity();
+			
 		//mRenderer->mUseFalseColors = true;
 		ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
@@ -177,7 +209,7 @@
 void GlobalLinesRenderer::SwitchRenderTextures()
 {
-	RenderTexture *buffer = mOldBuffer;
-	mOldBuffer = mNewBuffer;
-	mNewBuffer = buffer;
+	RenderTexture *buffer = mOldTexture;
+	mOldTexture = mNewTexture;
+	mNewTexture = buffer;
 }
 
@@ -185,4 +217,6 @@
 void GlobalLinesRenderer::InitGl()
 {
+	InitDevIl();
+
 	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
 	glutInitWindowPosition(50, 50);
@@ -209,14 +243,9 @@
 	AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
 	
-	Vector3 center = bbox.Center();
-	translFactor = -center;
-	//translFactor.z = 0;
-	//translFactor = Vector3(-1500, -1500, -1500);
-	float diag = 2.0f / Magnitude(bbox.Diagonal());
-	scaleFactor = Vector3(diag, diag, diag);
- 
-	Vector3 midPoint = Vector3(0, 0, 0);
-	Vector3 viewPoint = Vector3(0, 0, 500);
-
+	Vector3 midPoint = bbox.Center();
+	
+	const float sceneSize = Magnitude(bbox.Diagonal());
+	Vector3 viewPoint = midPoint + Vector3(0.5 * sceneSize, 0, 0);
+	
 	cout << "mid point: " << midPoint << endl;
 	cout << "view point: " << viewPoint << endl;
@@ -232,5 +261,4 @@
 	glEnable(GL_CULL_FACE);
 	glDisable(GL_LIGHTING);
-	//glEnable(GL_COLOR_MATERIAL);
 	glDisable(GL_COLOR_MATERIAL);
 	glEnable(GL_DEPTH_TEST); 
@@ -241,11 +269,11 @@
 	// Note that RT_COPY_TO_TEXTURE is required for depth textures on ATI hardware
 	
-	mNewBuffer = new RenderTexture(texWidth, texHeight, true, true);
-	//mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
-	mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
-	
-	mOldBuffer = new RenderTexture(texWidth, texHeight, true, true);
-	//mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
-	mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
+	mNewTexture = new RenderTexture(texWidth, texHeight, true, true);
+	//mNewTexture->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
+	mNewTexture->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
+	
+	mOldTexture = new RenderTexture(texWidth, texHeight, true, true);
+	//mOldTexture ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
+	mOldTexture ->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
 
 	// Setup Cg
@@ -264,5 +292,5 @@
 		cgCreateProgramFromFile(sCgContext, 
 								CG_SOURCE,
-								mNewBuffer->IsRectangleTexture() ? 
+								mNewTexture->IsRectangleTexture() ? 
 								"../src/depth_peelingRect.cg" : "../src/depth_peeling2d.cg", 
 								GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
@@ -290,7 +318,10 @@
 
     // setup the rendering context for the RenderTexture
-	mNewBuffer->BeginCapture();
-	{
-		Reshape(texWidth, texHeight);
+	mNewTexture->BeginCapture();
+	{
+		//Reshape(texWidth, texHeight);
+		glViewport(0, 0, texWidth, texHeight);
+		SetFrustum(sceneSize, sceneSize, 1, sceneSize);
+
 		glClearColor(0.1, 0.7, 0.2, 1);
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -307,11 +338,18 @@
 				  midPoint.x, midPoint.y, midPoint.z, 
 				  0, 1, 0);
-	}
-	mNewBuffer->EndCapture();
+
+		//SetFrustum(sceneSize, sceneSize, 1, sceneSize);
+	}
+	mNewTexture->EndCapture();
 
 	// setup the rendering context for the RenderTexture
-	mOldBuffer->BeginCapture();
-	{
-		Reshape(texWidth, texHeight);
+	mOldTexture->BeginCapture();
+	{
+		glClearColor(0.1, 0.7, 0.2, 1);
+       
+		//Reshape(texWidth, texHeight);
+		glViewport(0, 0, texWidth, texHeight);
+		SetFrustum(sceneSize, sceneSize, 1, sceneSize);
+
 		glMatrixMode(GL_MODELVIEW);
 		glLoadIdentity();
@@ -327,7 +365,7 @@
 				  0, 1, 0);
 
-		glClearColor(0.6, 0.2, 0.2, 1);
-	}
-	mOldBuffer->EndCapture();
+		//SetFrustum(sceneSize, sceneSize, 1, sceneSize);
+	}
+	mOldTexture->EndCapture();
 
 	PrintGLerror("init");
@@ -335,6 +373,46 @@
 
 
-void GlobalLinesRenderer::ProcessDepthBuffer()
-{
+Intersectable *GetObject(const int index)
+{
+	return NULL;
+}
+
+
+void GlobalLinesRenderer::ProcessDepthBuffer(VssRayContainer &vssRays)
+{
+	GrabDepthBuffer(mNewDepthBuffer, mNewTexture);
+	GrabDepthBuffer(mOldDepthBuffer, mOldTexture);
+
+	GrabItemBuffer(mNewItemBuffer, mNewTexture);
+	GrabItemBuffer(mOldItemBuffer, mOldTexture);
+
+	for (int y = 0; y < texHeight; ++ y)
+	{
+		for (int x = 0; x < texWidth; ++ x)
+		{
+			const int index = x + texWidth * y;
+			const float newDepth = mNewDepthBuffer[index];
+			const float oldDepth = mOldDepthBuffer[index];
+
+			float newDummy1 = mNear + (mFar - mNear) * newDepth;
+			float oldDummy1 = mNear + (mFar - mNear) * oldDepth;
+
+			float newDummy2 = x - texWidth / 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]);
+
+			// create rays in both directions
+			vssRays.push_back(new VssRay(oldPt, newPt, NULL, termObj1));
+			vssRays.push_back(new VssRay(newPt, oldPt, NULL, termObj2));
+		}
+	}
 }
 
@@ -346,7 +424,69 @@
 
 
+void GlobalLinesRenderer::GrabDepthBuffer(float *data, RenderTexture *rt)
+{
+	rt->BindDepth();
+	rt->EnableTextureTarget();
+	cout << "depth: " << mNewTexture->GetDepthBits() << endl;
+
+	const int texFormat = GL_DEPTH_COMPONENT;
+	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
+
+	mNewTexture->DisableTextureTarget();
+}
+
+
+void GlobalLinesRenderer::GrabItemBuffer(float *data, RenderTexture *rt)
+{
+	rt->BindDepth();
+	rt->EnableTextureTarget();
+	cout << "depth: " << mNewTexture->GetDepthBits() << endl;
+
+	const int texFormat = GL_DEPTH_COMPONENT;
+	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
+
+	mNewTexture->DisableTextureTarget();
+}
+
+void GlobalLinesRenderer::ExportDepthBuffer()
+{
+	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);
+
+	/*for (int i = texWidth * 123; i < texWidth * 124; ++ i)
+	{
+		cout << data[i] << " ";
+	}
+	cout << "Saving image " << texWidth << " " << texHeight << endl;
+*/
+	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(Beam &beam, const int samples)
 {
-	mNewBuffer->BeginCapture();
+	mNewTexture->BeginCapture();
 	{
 		//cgGLBindProgram(sCgPassThroughProgram);
@@ -354,17 +494,9 @@
 		DrawGeometry();
 	}
-	mNewBuffer->EndCapture();
-	
-	//mNewBuffer->BindDepth();
-	/*float *data = new float[texWidth * texHeight];
-	//const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
-	const int texFormat = GL_LUMINANCE;
-	glGetTexImage(mNewBuffer->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
-	
-	delete [] data;
-*/
+	mNewTexture->EndCapture();
+	
 	PrintGLerror("firstpass");
 
-	if (mNewBuffer->IsRectangleTexture()) cout << "rect" << endl;
+	if (mNewTexture->IsRectangleTexture()) cout << "rect" << endl;
 
 	for(int i = 0; i < mMaxDepth; ++ i) 
@@ -373,7 +505,7 @@
 		SwitchRenderTextures(); // switch pointer between rendertextures
 
-		mNewBuffer->BeginCapture();
+		mNewTexture->BeginCapture();
 		{
-			//if (mNewBuffer->IsDoubleBuffered()) 
+			//if (mNewTexture->IsDoubleBuffered()) 
 			//	glDrawBuffer(GL_BACK);
 
@@ -383,5 +515,5 @@
 		//	cgGLBindProgram(sCgPassThroughProgram);
 			cgGLEnableProfile(sCgFragmentProfile);
-			cgGLSetTextureParameter(sTextureParam, mOldBuffer->GetDepthTextureID());
+			cgGLSetTextureParameter(sTextureParam, mOldTexture->GetDepthTextureID());
 			cgGLEnableTextureParameter(sTextureParam);
 
@@ -394,22 +526,22 @@
 			cgGLDisableProfile(sCgFragmentProfile);
 		}
-		mNewBuffer->EndCapture();
+		mNewTexture->EndCapture();
 	}
 
 	PrintGLerror("endpeeling");
 
-	//mNewBuffer->Bind();
-	mNewBuffer->BindDepth();
-	mNewBuffer->EnableTextureTarget();
+	//mNewTexture->Bind();
+	mNewTexture->BindDepth();
+	mNewTexture->EnableTextureTarget();
 	
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	
-	if (mNewBuffer->IsRectangleTexture())
+	if (mNewTexture->IsRectangleTexture())
 	{
 		glBegin(GL_QUADS);
 		glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
-		glTexCoord2f(mNewBuffer->GetWidth(), 0); glVertex3f( 1, -1, -0.5f);
-		glTexCoord2f(mNewBuffer->GetWidth(), mNewBuffer->GetHeight()); glVertex3f( 1,  1, -0.5f);
-		glTexCoord2f(0, mNewBuffer->GetHeight()); 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();
 	}
@@ -424,5 +556,5 @@
 	}  
 
-	mNewBuffer->DisableTextureTarget();
+	mNewTexture->DisableTextureTarget();
 	PrintGLerror("displaytexture");
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h	(revision 1949)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h	(revision 1953)
@@ -4,4 +4,6 @@
 #include "common.h"
 #include "Containers.h"
+#include "Vector3.h"
+
 
 class RenderTexture;
@@ -41,16 +43,32 @@
 
 	void RenderObject(Intersectable *obj);
-void DrawGeometry();
+
+	
+	/** Draws the geometry for one pass.
+	*/
+	void DrawGeometry();
 void Run();
-int mMaxDepth;
+void GrabDepthBuffer(float *data, RenderTexture *rt);
+void GrabItemBuffer(float *data, RenderTexture *rt);
+void ApplyDepthPeeling(Beam &beam, const int samples);
+void ExportDepthBuffer();
+void ProcessDepthBuffer(VssRayContainer &vssRays);
+
+	int mMaxDepth;
+	Vector3 mEyeVec;
+	Vector3 mLeftVec;
+	Vector3 mUpVec;
+	float mFar;
+	float mNear;
+
+	float *mNewDepthBuffer;
+	float *mOldDepthBuffer;
+	float *mNewItemBuffer;
+	float *mOldItemBuffer;
+RenderTexture *mNewTexture;
+	RenderTexture *mOldTexture;
 protected:
 
 	void ProcessDepthBuffer();
-
-	/** Draws the geometry for one pass.
-	*/
-	
-
-	void ApplyDepthPeeling(Beam &beam, const int samples);
 
 	void SwitchRenderTextures();
@@ -59,6 +77,5 @@
 						 const Beam &beam);
 
-	RenderTexture *mNewBuffer;
-	RenderTexture *mOldBuffer;
+	
 
 	Preprocessor *mPreprocessor;
