Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 510)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 512)
@@ -62,5 +62,5 @@
 			<Tool
 				Name="VCCLCompilerTool"
-				AdditionalIncludeDirectories="..\support;..\support\devil\include;..\support\zlib\include;..\include;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\src"
+				AdditionalIncludeDirectories="..\support;..\support\devil\include;..\support\zlib\include;..\include;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\src;&quot;$(CG_INC_PATH)&quot;"
 				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;"
 				RuntimeLibrary="2"
@@ -76,6 +76,6 @@
 			<Tool
 				Name="VCLinkerTool"
-				AdditionalDependencies="xerces-c_2.lib zdll.lib zziplib.lib devil.lib qtmain.lib QtOpenGL4.lib QtUiTools.lib QtCore4.lib QtDesigner4.lib QtGui4.lib QtAssistantClient.lib QtTest4.lib Qt3Supportd4.lib QAxContainer.lib QAxServerd.lib QtXml4.lib glut32.lib OpenGL32.Lib glu32.lib QAxServer.lib QtDesignerComponents4.lib"
-				AdditionalLibraryDirectories="..\support\xercesc\lib\;..\support\zlib\lib\;..\support\devil\lib;&quot;$(QTDIR)\lib&quot;;..\include;..\src\GL"/>
+				AdditionalDependencies="xerces-c_2.lib zdll.lib zziplib.lib devil.lib qtmain.lib QtOpenGL4.lib QtCore4.lib QtGui4.lib Qt3Supportd4.lib QAxContainer.lib glut32.lib OpenGL32.Lib glu32.lib cg.lib cgGL.lib"
+				AdditionalLibraryDirectories="..\support\xercesc\lib\;..\support\zlib\lib\;..\support\devil\lib;&quot;$(QTDIR)\lib&quot;;..\include;..\src\GL;&quot;$(CG_LIB_PATH)&quot;"/>
 			<Tool
 				Name="VCMIDLTool"/>
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 511)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 512)
@@ -5,5 +5,5 @@
 
 void
-MyBeam::Construct(const  AxisAlignedBox3 &box,
+Beam::Construct(const  AxisAlignedBox3 &box,
 				const  AxisAlignedBox3 &dBox)
 {
@@ -53,5 +53,5 @@
 
 int
-MyBeam::ComputeIntersection(const AxisAlignedBox3 &box)
+Beam::ComputeIntersection(const AxisAlignedBox3 &box)
 {
   int i;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 511)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 512)
@@ -420,5 +420,5 @@
   HaltonSequence halton;
 
-  MyBeam mBeam;
+  Beam mBeam;
   
   RssTreeLeaf(RssTreeInterior *p,
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Beam.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Beam.h	(revision 511)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Beam.h	(revision 512)
@@ -10,5 +10,5 @@
 class Intersectable;
 
-class MyBeam {
+class Beam {
   
 public:			
@@ -40,5 +40,5 @@
   bool SetValid() { return mFlags |= VALID; }
 
-  MyBeam():mFlags(STORE_KD_NODES+STORE_OBJECTS), mKdNodes(0), mObjects(0)
+  Beam():mFlags(STORE_KD_NODES+STORE_OBJECTS), mKdNodes(0), mObjects(0)
   {
   }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 511)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 512)
@@ -968,5 +968,5 @@
 int
 KdTree::CastBeam(
-				 MyBeam &beam
+				 Beam &beam
 				 )
 {
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h	(revision 511)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h	(revision 512)
@@ -16,5 +16,5 @@
 class Intersectable;
 //class KdViewCell;
-class MyBeam;
+class Beam;
 
 // --------------------------------------------------------------
@@ -289,5 +289,5 @@
   int
   CastBeam(
-		   MyBeam &beam
+		   Beam &beam
 		   );
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 511)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 512)
@@ -17,5 +17,5 @@
 class Intersectable;
 class Material;
-class MyBeam;
+class Beam;
 
 struct PvsRenderStatistics {
@@ -159,5 +159,5 @@
   void SampleBeamContributions(
 							   Intersectable *sourceObject,
-							   MyBeam &beam,
+							   Beam &beam,
 							   const int samples,
 							   BeamSampleStatistics &stat
@@ -167,5 +167,5 @@
   SampleViewpointContributions(
 							   Intersectable *sourceObject,
-							   MyBeam &beam,
+							   Beam &beam,
 							   const int desiredSamples,
 							   BeamSampleStatistics &stat
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 511)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 512)
@@ -6,6 +6,19 @@
 #include "Viewcell.h"
 #include "Beam.h"
-
 #include <GL/glext.h>
+#include <Cg/cg.h>
+#include <Cg/cgGL.h>
+
+static CGcontext sCgContext = NULL;
+static CGprogram sCgFragmentProgram = NULL;
+
+static CGparameter sCgKdParam = NULL;
+static CGparameter sCgModelViewProjParam = NULL;
+
+
+static CGprofile sCgFragmentProfile = CG_PROFILE_FP30;
+
+GLuint depthMap;
+const int depthMapSize = 512;
 
 GlRendererWidget *rendererWidget = NULL;
@@ -119,18 +132,23 @@
 	wglGetProcAddress("glGetOcclusionQueryuivNV");
 
-  // second depth buffer texture
-  GLuint shadowMap;
-  const int shadowMapSize=512;
- 
-
-  glGenTextures(1, &shadowMap);
-  glBindTexture(GL_TEXTURE_2D, shadowMap);
-  
-  glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapSize, 
-	  shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
+  // initialise second depth buffer texture
+  glGenTextures(1, &depthMap);
+  glBindTexture(GL_TEXTURE_2D, depthMap);
+  
+  glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize, 
+			   depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+
+  // cg initialization
+  sCgContext = cgCreateContext();
+
+  sCgFragmentProgram = cgCreateProgramFromFile(sCgContext, 		
+											   CG_SOURCE, "dual_depth.cg",
+											   sCgFragmentProfile,
+											   NULL, 
+											   NULL);
 }
 
@@ -547,101 +565,113 @@
 
 
-void
-GlRendererBuffer::SampleBeamContributions(
-										  Intersectable *sourceObject,
-										  MyBeam &beam,
-										  const int desiredSamples,
-										  BeamSampleStatistics &stat
-										  )
-{
-  // assumes that the beam is constructed and contains kd-tree nodes
-  // and viewcells which it intersects
-
-
-  
-  
-  // Get the number of viewpoints to be sampled
-  // Now it is a sqrt but in general a wiser decision could be made.
-  // The less viewpoints the better for rendering performance, since less passes
-  // over the beam is needed.
-  // The viewpoints could actually be generated outside of the bounding box which
-  // would distribute the 'efective viewpoints' of the object surface and thus
-  // with a few viewpoints better sample the vipoint space....
-  
-  int viewPointSamples = sqrt((float)desiredSamples);
-
-  // the number of direction samples per pass is given by the number of viewpoints
-  int directionalSamples = desiredSamples/viewPointSamples;
-  int i;
-  for (i=0; i < viewPointSamples; i++) {
-	Vector3 viewPoint = beam.mBox.GetRandomPoint();
-	// perhaps the viewpoint should be shifted back a little bit so that it always lies
-	// inside the source object
-	// 'ideally' the viewpoints would be distributed on the soureObject surface, but this
-	// would require more complicated sampling (perhaps hierarchical rejection sampling of
-	// the object surface is an option here - only the mesh faces which are inside the box
-	// are considered as candidates) 
-	SampleViewpointContributions(
-								  sourceObject,
-								  beam,
-								  directionalSamples,
-								  stat
-								  );
-  }
-
-
-  // note:
-  // this routine would be called only if the number of desired samples is sufficiently
-  // large - for other rss tree cells the cpu based sampling is perhaps more efficient
-  // distributing the work between cpu and gpu would also allow us to place more sophisticated
-  // sample distributions (silhouette ones) using the cpu and the jittered once on the GPU
-  // in order that thios scheme is working well the gpu render buffer should run in a separate
-  // thread than the cpu sampler, which would not be such a big problem....
-}
-
-
-void
-GlRendererBuffer::SampleViewpointContributions(
-											   Intersectable *sourceObject,
-											   MyBeam &beam,
+void GlRendererBuffer::SampleBeamContributions(Intersectable *sourceObject,
+											   Beam &beam,
 											   const int desiredSamples,
-											   BeamSampleStatistics &stat
-											   )
-{
-  // 1. setup the view port to match the desired samples
-  // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox
-  
-  // 3. reset z-buffer to 0 and render the source object for the beam using
-  //    glDepthFunc(GL_GREATER)
-  //    and glCullFace(Enabled) and glFrontFace(GL_CW)
-  //    remember the ray origin depth buffer somewhere?
-
-  // 4. set back to normal rendering and clear the ray termination depth buffer
-  // 5. render all objects inside the beam using id based false color
-  //    (objects can be compiled to a gl list now so that subsequent rendering for
-  //     this beam is fast - the same hold for step 3)
-
-
-  // 6. Now we have a two depth buffers defining the ray origin and termination
-  //    only rays which have non-zero entry in the origin buffer are valid since
-  //    they realy start on the object surface (this can also be tagged by setting a
-  //    stencil buffer bit at step 3)
-
-  // 7. Use occlusion queries for all viewcell meshes associated with the beam ->
-  // a fragment passes if the corresponding stencil fragment is set and its depth is
-  // between origin and termination buffer
-
-  // 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
-  // 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)
-  // the subdivision of the tree would only be driven by the statistics (the glrender could
-  // evaluate the contribution entropy for example)
-  // However might be an option to extract/store only those the rays which made a contribution
-  // (new viewcell has been discovered) or relative contribution greater than a threashold...
-       
-  
-}
+											   BeamSampleStatistics &stat)
+{
+	// TODO: should be out of here
+	cgGLBindProgram(sCgFragmentProgram);
+	cgGLEnableProfile(sCgFragmentProfile);
+
+	// assumes that the beam is constructed and contains kd-tree nodes
+	// and viewcells which it intersects
+	
+
+  
+  
+	// Get the number of viewpoints to be sampled
+	// Now it is a sqrt but in general a wiser decision could be made.
+	// The less viewpoints the better for rendering performance, since less passes
+	// over the beam is needed.
+	// The viewpoints could actually be generated outside of the bounding box which
+	// would distribute the 'efective viewpoints' of the object surface and thus
+	// with a few viewpoints better sample the vipoint space....
+  
+	int viewPointSamples = sqrt((float)desiredSamples);
+
+	// the number of direction samples per pass is given by the number of viewpoints
+	int directionalSamples = desiredSamples/viewPointSamples;
+	
+	for (int i = 0; i < viewPointSamples; ++ i) 
+	{
+		Vector3 viewPoint = beam.mBox.GetRandomPoint();
+		
+		// perhaps the viewpoint should be shifted back a little bit so that it always lies
+		// inside the source object
+		// 'ideally' the viewpoints would be distributed on the soureObject surface, but this
+        // would require more complicated sampling (perhaps hierarchical rejection sampling of
+		// the object surface is an option here - only the mesh faces which are inside the box
+		// are considered as candidates) 
+		
+		SampleViewpointContributions(sourceObject,
+									 beam,
+									 directionalSamples,
+									 stat);
+	}
+
+
+	// note:
+	// this routine would be called only if the number of desired samples is sufficiently
+	// large - for other rss tree cells the cpu based sampling is perhaps more efficient
+	// distributing the work between cpu and gpu would also allow us to place more sophisticated
+	// sample distributions (silhouette ones) using the cpu and the jittered once on the GPU
+	// in order that thios scheme is working well the gpu render buffer should run in a separate
+	// thread than the cpu sampler, which would not be such a big problem....
+
+	cgGLDisableProfile(sCgFragmentProfile);
+}
+
+
+void GlRendererBuffer::SampleViewpointContributions(Intersectable *sourceObject,
+													Beam &beam,
+													const int desiredSamples,
+                                                    BeamSampleStatistics &stat)
+{
+	// 1. setup the view port to match the desired samples
+	// 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox
+  
+	// 3. reset z-buffer to 0 and render the source object for the beam using
+	//    glDepthFunc(GL_GREATER)
+	//    and glCullFace(Enabled) and glFrontFace(GL_CW)
+	//    save result to depth buffer?
+	
+	// bind ray origin depth buffer
+	glClear(GL_DEPTH);
+	glBindTexture(GL_TEXTURE_2D, depthMap);
+    glEnable(GL_TEXTURE_2D);
+
+	// compare with second depth buffer: should be done in pixel shader
+	/*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_GREATER);
+	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
+	*/
+
+
+	// 4. set back to normal rendering and clear the ray termination depth buffer
+	// 5. render all objects inside the beam using id based false color
+	//    (objects can be compiled to a gl list now so that subsequent rendering for
+	//     this beam is fast - the same hold for step 3)
+
+
+	// 6. Now we have a two depth buffers defining the ray origin and termination
+	//    only rays which have non-zero entry in the origin buffer are valid since
+	//    they realy start on the object surface (this can also be tagged by setting a
+	//    stencil buffer bit at step 3)
+
+	// 7. Use occlusion queries for all viewcell meshes associated with the beam ->
+	// a fragment passes if the corresponding stencil fragment is set and its depth is
+	// between origin and termination buffer
+
+	// 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
+	// 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)
+	// the subdivision of the tree would only be driven by the statistics (the glrender could
+	// evaluate the contribution entropy for example)
+	// However might be an option to extract/store only those the rays which made a contribution
+	// (new viewcell has been discovered) or relative contribution greater than a threashold ... 
+
+}
