Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1944)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1944)
@@ -0,0 +1,394 @@
+#include "glInterface.h"
+#include "GlobalLinesRenderer.h"
+#include "common.h"
+#include "RenderTexture.h"
+#include "Preprocessor.h"
+#include "GlRenderer.h"
+
+#include <Cg/cg.h>
+#include <Cg/cgGL.h>
+
+//#include <QtOpenGL>
+
+namespace GtpVisibilityPreprocessor {
+
+
+static CGcontext sCgContext = NULL;
+static CGprogram sCgDepthPeelingProgram = NULL;
+static CGprogram sCgPassThroughProgram = NULL;
+
+static CGprofile sCgFragmentProfile;
+static CGparameter sTextureParam;
+
+GlobalLinesRenderer *globalLinesRenderer = NULL;
+
+
+static void cgErrorCallback()
+{
+	CGerror lastError = cgGetError();
+
+	if(lastError)
+	{
+		printf("%s\n\n", cgGetErrorString(lastError));
+		printf("%s\n", cgGetLastListing(sCgContext));
+		printf("Cg error, exiting...\n");
+
+		exit(0);
+	}
+}
+
+static void PrintGLerror(char *msg)
+{
+	GLenum errCode;
+	const GLubyte *errStr;
+	
+	if ((errCode = glGetError()) != GL_NO_ERROR) 
+	{
+		errStr = gluErrorString(errCode);
+		fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
+	}
+}
+
+
+void Reshape(int w, int h)
+{
+	if (h == 0) h = 1;
+
+	glViewport(0, 0, w, h);
+
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+
+	gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 3, 5000.0);
+	//gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 10.0);
+}
+
+
+void Display()
+{
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	
+	//globalLinesRenderer->DrawGeometry();
+	
+	globalLinesRenderer->CastGlobalLines(Beam(), 0);
+
+	PrintGLerror("display");
+
+	glPopMatrix();
+	glutSwapBuffers();
+}
+
+
+void Idle()
+{
+	glutPostRedisplay();
+}
+
+
+void Keyboard(unsigned char key, int x, int y)
+{
+	switch(key)
+	{
+	case ' ':
+		return;
+	default:
+		return;
+	}
+}
+
+/*GlobalLinesRenderer::GlobalLinesRenderer(RenderTexture *buffer1, 
+										 RenderTexture *buffer2, 
+										 Preprocessor *preprocessor,
+										 GlRenderer *renderer)
+: mNewBuffer(buffer1), mOldBuffer(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
+mRenderer(renderer)
+{
+}
+*/
+GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor, GlRenderer *renderer): 
+mNewBuffer(NULL), 
+mOldBuffer(NULL), 
+mMaxDepth(100), 
+mRenderer(renderer), 
+mPreprocessor(preprocessor)
+{
+}
+
+
+GlobalLinesRenderer::~GlobalLinesRenderer()
+{
+	if (sCgDepthPeelingProgram)
+		cgDestroyProgram(sCgDepthPeelingProgram);
+	if (sCgContext)
+		cgDestroyContext(sCgContext);
+}
+
+ 
+void GlobalLinesRenderer::CastGlobalLines(Beam &beam, const int samples)
+{
+	// bind pixel shader implementing the front depth buffer functionality
+	//cgGLBindProgram(sCgDepthPeelingProgram);
+	//cgGLEnableProfile(sCgFragmentProfile);
+
+	ApplyDepthPeeling(beam, samples);
+}
+
+
+void GlobalLinesRenderer::RenderObject(Intersectable *obj)
+{
+	mRenderer->RenderIntersectable(obj);
+}
+
+
+void GlobalLinesRenderer::DrawGeometry()
+{ //glutWireTorus(0.25, 1, 32, 64);
+	mRenderer->mUseFalseColors = true;
+	ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
+
+	float r = Random(1.0f);
+	float g = Random(1.0f);
+	float b = Random(1.0f);
+
+	Intersectable::NewMail();
+
+	//cout << r << " " << g << " " << b << " ";
+	for (oit = mPreprocessor->mObjects.begin(); oit != oit_end; ++ oit)
+	{
+		glColor3f(r, g, b);
+		RenderObject(*oit);
+	}
+}
+
+
+void GlobalLinesRenderer::SwitchRenderTextures()
+{
+	RenderTexture *buffer = mOldBuffer;
+	mOldBuffer = mNewBuffer;
+	mNewBuffer = buffer;
+}
+
+
+void GlobalLinesRenderer::InitGl()
+{
+	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
+	glutInitWindowPosition(50, 50);
+	glutInitWindowSize(512, 512);
+	glutCreateWindow("TestRenderDepthTexture");  
+
+	int err = glewInit();
+	if (GLEW_OK != err)
+	{
+		// problem: glewInit failed, something is seriously wrong
+		fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
+		exit(-1);
+	}  
+	
+	glutKeyboardFunc(Keyboard);
+	glutDisplayFunc(Display);
+	glutIdleFunc(Idle);
+	glutReshapeFunc(Reshape);
+
+	Reshape(512, 512);
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+
+	AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
+	const Vector3 midPoint = bbox.Center();
+	const Vector3 viewPoint = midPoint + Vector3(-800, 0, 0);
+
+	cout << "midPoint: " << midPoint << endl;
+	cout << "viewPoint: " << viewPoint << endl;
+	cout << "scene: " << bbox << endl;
+
+	/*gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, 
+			  midPoint.x, midPoint.y, midPoint.z, 
+			  0, 1, 0);
+*/
+	gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
+
+	glDisable(GL_CULL_FACE);
+	glDisable(GL_LIGHTING);
+	//glEnable(GL_COLOR_MATERIAL);
+	glDisable(GL_COLOR_MATERIAL);
+	glEnable(GL_DEPTH_TEST); 
+	glClearColor(0.1, 0.2, 0.3, 1);
+
+    int texWidth = 256, texHeight = 256;
+	
+	// A square, mipmapped, anisotropically filtered 8-bit RGBA texture with
+	// depth and stencil.
+	// 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);
+
+	mOldBuffer = new RenderTexture(texWidth, texHeight, true, true);
+	mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
+
+	// Setup Cg
+	cgSetErrorCallback(cgErrorCallback);
+
+	// Create cgContext.
+	sCgContext = cgCreateContext();
+
+	// get the best profile for this hardware
+	sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
+	
+	//assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
+	cgGLSetOptimalOptions(sCgFragmentProfile);
+
+	sCgDepthPeelingProgram = 
+		cgCreateProgramFromFile(sCgContext, 
+								CG_SOURCE,
+								"../src/depth_peeling.cg", 
+								GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
+								NULL,
+								NULL);
+
+	if(sCgDepthPeelingProgram != NULL)
+	{
+		cgGLLoadProgram(sCgDepthPeelingProgram);
+		sTextureParam = cgGetNamedParameter(sCgDepthPeelingProgram, "depthTex");  
+	}
+
+	sCgPassThroughProgram = 
+		cgCreateProgramFromFile(sCgContext, 
+								CG_SOURCE,
+								"../src/passthrough.cg", 
+								GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
+								NULL,
+								NULL);
+
+	if(sCgPassThroughProgram != NULL)
+	{
+		cgGLLoadProgram(sCgPassThroughProgram);
+	}
+
+
+    // setup the rendering context for the RenderTexture
+	mNewBuffer->BeginCapture();
+	{
+		Reshape(texWidth, texHeight);
+		glClearColor(0.1, 0.7, 0.2, 1);
+		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+		glMatrixMode(GL_MODELVIEW);
+		glLoadIdentity();
+		gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, 
+				  midPoint.x, midPoint.y, midPoint.z, 
+				  0, 1, 0);
+//gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
+		//glDisable(GL_DEPTH_TEST); 
+		glEnable(GL_DEPTH_TEST); 
+	}
+	mNewBuffer->EndCapture();
+
+	// setup the rendering context for the RenderTexture
+	mOldBuffer->BeginCapture();
+	{
+		Reshape(texWidth, texHeight);
+		glMatrixMode(GL_MODELVIEW);
+		glLoadIdentity();
+		gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, 
+				  midPoint.x, midPoint.y, midPoint.z, 
+				  0, 1, 0);
+
+		//glDisable(GL_DEPTH_TEST); 
+		glEnable(GL_DEPTH_TEST); 
+		glClearColor(0.6, 0.2, 0.2, 1);
+	}
+	mOldBuffer->EndCapture();
+
+	PrintGLerror("init");
+}
+
+
+void GlobalLinesRenderer::ProcessDepthBuffer()
+{
+}
+
+
+void GlobalLinesRenderer::Run()
+{
+	glutMainLoop();
+}
+
+
+void GlobalLinesRenderer::ApplyDepthPeeling(Beam &beam, const int samples)
+{
+	//if (mNewBuffer->IsDoubleBuffered()) 
+	//	glDrawBuffer(GL_BACK);
+
+	mNewBuffer->BeginCapture();
+	{
+		//cgGLBindProgram(sCgPassThroughProgram);
+		//cgGLEnableProfile(sCgFragmentProfile);
+		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+		glMatrixMode(GL_MODELVIEW);
+		glPushMatrix();
+		{ //glutSolidTorus(0.25, 1, 32, 64);
+			DrawGeometry();
+		}
+		glPopMatrix();
+	}
+	mNewBuffer->EndCapture();
+	
+	//mNewBuffer->Bind();
+	mNewBuffer->BindDepth();
+	mNewBuffer->EnableTextureTarget();
+
+
+	//glBindTexture(texture_target,texID);
+	//const int texID = mNewBuffer->GetTextureTarget();
+	//glGetTexImage(texture_target,0,texture_format,GL_FLOAT,data);
+/*
+	for(int l = 0; l < mMaxDepth; ++ l) 
+	{
+		// Peel another layer
+		SwitchRenderTextures(); // switch pointer between rendertextures
+
+		mNewBuffer->BeginCapture();
+		{
+			//if (mNewBuffer->IsDoubleBuffered()) 
+			//	glDrawBuffer(GL_BACK);
+
+			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
+
+			cgGLBindProgram(sCgDepthPeelingProgram);
+			cgGLEnableProfile(sCgFragmentProfile);
+			cgGLSetTextureParameter(sTextureParam, mOldBuffer->GetDepthTextureID());
+			cgGLEnableTextureParameter(sTextureParam);
+
+			//glColor3f(0,0.0,0.0);
+			DrawGeometry();
+			cgGLDisableTextureParameter(sTextureParam);
+		}
+		mNewBuffer->EndCapture();
+	}
+*/
+
+	PrintGLerror("display 2");
+	glColor3f(1,1,1);
+	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();
+	PrintGLerror("display 1");
+
+    if (mNewBuffer->IsFloatTexture())
+	{
+		// cgGLDisableTextureParameter(textureParam);
+		// cgGLDisableProfile(theProfile);
+	}
+	else 
+	{
+		mNewBuffer->DisableTextureTarget();
+	}
+}
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 1942)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 1944)
@@ -109,4 +109,5 @@
 {
   Triangle3 &t = object->GetItem();
+  
   glBegin(GL_TRIANGLES);
   glVertex3f(t.mVertices[0].x, t.mVertices[0].y, t.mVertices[0].z);
@@ -126,6 +127,5 @@
   if (mUseFalseColors)
 	SetupFalseColor(object->mId);
-  glColor3f(1, 1,1);
-
+  
   switch (object->Type()) {
   case Intersectable::MESH_INSTANCE:
@@ -253,6 +253,6 @@
 GlRenderer::SetupFalseColor(const int id)
 {
-  // swap bits of the color
-  glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
+	// swap bits of the color
+	glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
 }
 
@@ -276,5 +276,5 @@
 
   if (!mUseFalseColors && !mUseForcedColors)
-	SetupMaterial(mesh->mMaterial);
+	  SetupMaterial(mesh->mMaterial);
   
   for (i=0; i < mesh->mFaces.size(); i++) {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 1940)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 1944)
@@ -178,5 +178,5 @@
 	  Vector3 mDirection;
   };
-
+friend class GlobalLinesRenderer;
 protected:
 
