Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/scripts/Plugin_VisibilitySceneManager.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/scripts/Plugin_VisibilitySceneManager.vcproj	(revision 1002)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/scripts/Plugin_VisibilitySceneManager.vcproj	(revision 1159)
@@ -109,5 +109,5 @@
 				LinkIncremental="1"
 				SuppressStartupBanner="TRUE"
-				AdditionalLibraryDirectories="&quot;$(OGRE_PATH)\PlugIns\OctreeSceneManager\bin\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\OgreMain\lib\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\Samples\Common\CEGUIRenderer\lib&quot;;&quot;..\..\GtpVisibility\lib\$(ConfigurationName)&quot;;&quot;..\..\lib\$(ConfigurationName)&quot;;&quot;..\lib\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\Dependencies\lib\$(ConfigurationName)&quot;;..\..\..\..\..\..\..\NonGTP\Zlib\lib;&quot;..\..\..\Preprocessing\lib\$(ConfigurationName)&quot;;..\..\..\..\..\..\..\NonGTP\Xerces\xercesc\lib;&quot;$(QTDIR)\lib&quot;;..\..\..\Preprocessing\src\GL;&quot;$(CG_LIB_PATH)&quot;"
+				AdditionalLibraryDirectories="&quot;$(OGRE_PATH)\PlugIns\OctreeSceneManager\bin\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\OgreMain\lib\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\Samples\Common\CEGUIRenderer\lib&quot;;&quot;..\..\GtpVisibility\lib\$(ConfigurationName)&quot;;&quot;..\..\lib\$(ConfigurationName)&quot;;&quot;..\lib\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\Dependencies\lib\$(ConfigurationName)&quot;;&quot;..\..\..\Preprocessing\lib\$(ConfigurationName)&quot;;..\..\..\..\..\..\..\NonGTP\Xerces\xercesc\lib;&quot;$(QTDIR)\lib&quot;;..\..\..\Preprocessing\src\GL;&quot;$(CG_LIB_PATH)&quot;;..\..\..\..\..\..\..\NonGTP\Zlib\lib"
 				ModuleDefinitionFile="..\misc\OgreVisibilitySceneManager.def"
 				GenerateDebugInformation="TRUE"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1155)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1159)
@@ -116,7 +116,7 @@
 		@returns number of sample contributions
 	*/
-	virtual float ComputeSampleContributions(VssRay &ray, 
-											 const bool addRays,
-											 const bool storeViewCells);
+	virtual float ComputeSampleContribution(VssRay &ray, 
+											const bool addRays,
+											const bool storeViewCells);
 
 	virtual void AddSampleContributions(VssRay &ray);
@@ -1012,7 +1012,11 @@
 	void Finalize(ViewCell *viewCell, const bool createMesh);
 
+	/** Stores sample contribution for kd cells or objects.
+	*/
+	virtual float ComputeSampleContribution(VssRay &ray, 
+											const bool addRays,
+											const bool storeViewCells);
 
 protected:
-
 	
 	/** Exports view cell geometry.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1149)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1159)
@@ -1494,4 +1494,9 @@
 		"0");
 
+	RegisterOption("ViewCells.storeKdPvs",
+		optBool, 
+		"view_cells_store_kd_pvs=", 
+		"false");
+	
 
 	/************************************************************************************/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorThread.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorThread.cpp	(revision 1154)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorThread.cpp	(revision 1159)
@@ -1,3 +1,2 @@
-//#include <QApplication>
 #include "PreprocessorThread.h"
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1112)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1159)
@@ -82,5 +82,5 @@
 	VssRay vssRay(ray);
 
-	mViewCellsManager->ComputeSampleContributions(vssRay, true, false);
+	mViewCellsManager->ComputeSampleContribution(vssRay, true, false);
 	
 	return vssRay.mPvsContribution;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1155)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1159)
@@ -20,4 +20,6 @@
 #include "KdIntersectable.h"
 
+// should not count origin object for sampling because it disturbs heuristics
+#define COUNT_ORIGIN_OBJECTS 0
 
 namespace GtpVisibilityPreprocessor {
@@ -103,4 +105,6 @@
 	Environment::GetSingleton()->GetBoolValue("ViewCells.exportPvs", mExportPvs);
 	
+	Environment::GetSingleton()->GetBoolValue("ViewCells.storeKdPvs", mStoreKdPvs);
+
 
 	char buf[100];
@@ -217,5 +221,6 @@
 	Debug << "export pvs for view cells: " << mExportPvs << endl;
 	
-	
+	Debug << "store kd pvs: " << mStoreKdPvs << endl;
+
 	Debug << endl;
 }
@@ -1376,5 +1381,5 @@
   for (it = rays.begin(); it != it_end; ++ it) 
   {
-	  sum += ComputeSampleContributions(*(*it), addRays, storeViewCells);
+	  sum += ComputeSampleContribution(*(*it), addRays, storeViewCells);
   }
 
@@ -1842,7 +1847,7 @@
 
 
-float ViewCellsManager::ComputeSampleContributions(VssRay &ray,
-												   const bool addRays,
-												   const bool storeViewCells)
+float ViewCellsManager::ComputeSampleContribution(VssRay &ray,
+												  const bool addRays,
+												  const bool storeViewCells)
 {
 	ViewCellContainer viewcells;
@@ -1893,5 +1898,5 @@
 			// made in one direction!!!
 			// the other contributions of this sample will be counted for the oposite ray!
-#if 0
+#if COUNT_ORIGIN_OBJECTS
 			if (ray.mOriginObject && 
 				viewcell->GetPvs().GetSampleContribution(ray.mOriginObject,
@@ -1917,21 +1922,14 @@
 				// if ray not outside of view space
 
-				// add kd cell
+				// add new object to the pvs
 				if (ray.mTerminationObject)
 				{
-					if (!mStoreKdPvs)
-					{
-						viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
-					}
-					else
-					{
-						// todo
-						//viewcell->GetPvs().AddSample(ray.mTerminationNode, ray.mPdf);
-					}
-				}
-				
-#if 0
+					viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
+				}				
+#if COUNT_ORIGIN_OBJECTS
 				 if (ray.mOriginObject)
+				 {
 					 viewcell->GetPvs().AddSample(ray.mOriginObject, ray.mPdf);
+				 }
 #endif
 			}
@@ -5714,4 +5712,114 @@
 	
 
+float VspOspViewCellsManager::ComputeSampleContribution(VssRay &ray,
+														const bool addRays,
+														const bool storeViewCells)
+{
+	ViewCellContainer viewcells;
+
+	ray.mPvsContribution = 0;
+	ray.mRelativePvsContribution = 0.0f;
+
+	static Ray hray;
+	hray.Init(ray);
+	//hray.mFlags |= Ray::CULL_BACKFACES;
+	//Ray hray(ray);
+
+	float tmin = 0, tmax = 1.0;
+
+	if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
+		return 0;
+
+	Vector3 origin = hray.Extrap(tmin);
+	Vector3 termination = hray.Extrap(tmax);
+
+	// traverse the view space subdivision
+	CastLineSegment(origin, termination, viewcells);
+
+	if (storeViewCells)
+	{	// copy viewcells memory efficiently
+		ray.mViewCells.reserve(viewcells.size());
+		ray.mViewCells = viewcells;
+	}
+
+	ViewCellContainer::const_iterator it = viewcells.begin();
+
+	for (; it != viewcells.end(); ++ it) 
+	{
+		ViewCell *viewcell = *it;
+
+		if (viewcell->GetValid())
+		{
+			// if ray not outside of view space
+			float contribution;
+
+			if (ray.mTerminationObject) 
+			{
+				if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
+					ray.mPdf, contribution))
+				{
+					++ ray.mPvsContribution;
+				}
+
+				ray.mRelativePvsContribution += contribution;
+			}
+
+			// for directional sampling it is important to count only contributions
+			// made in one direction!!!
+			// the other contributions of this sample will be counted for the oposite ray!
+#if COUNT_ORIGIN_OBJECTS
+			if (ray.mOriginObject && 
+				viewcell->GetPvs().GetSampleContribution(ray.mOriginObject,
+														 ray.mPdf,
+														 contribution))
+			{
+				++ ray.mPvsContribution;
+				ray.mRelativePvsContribution += contribution;
+			}
+#endif
+		}
+	}
+
+	// if addrays is true, sampled entities are stored in the pvs
+	if (addRays)
+	{
+		for (it = viewcells.begin(); it != viewcells.end(); ++ it) 
+		{
+			ViewCell *viewcell = *it;
+            
+			if (viewcell->GetValid())
+			{
+				// if ray not outside of view space
+
+				// add kd cell
+				if (ray.mTerminationObject)
+				{
+					if (!mStoreKdPvs)
+					{
+						viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
+					}
+					else
+					{
+						/// get current leaf the point
+						KdLeaf *leaf = mOspTree->GetLeaf(ray.mTermination);
+
+						KdIntersectable *entry = mOspTree->GetOrCreateKdIntersectable(leaf);
+
+						viewcell->GetPvs().AddSample(entry, ray.mPdf);
+					}
+				}
+				
+#if COUNT_ORIGIN_OBJECTS
+				 if (ray.mOriginObject)
+					 viewcell->GetPvs().AddSample(ray.mOriginObject, ray.mPdf);
+#endif
+			}
+		}
+	}
+
+	return ray.mRelativePvsContribution;
+}
+
+
 void VspOspViewCellsManager::PrepareLoadedViewCells()
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1149)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1159)
@@ -1390,5 +1390,7 @@
 	int ComputePvsSize(const ObjectContainer &objects) const;
 
-	KdLeaf *GetLeaf(const Vector3 &pt, KdNode *node) const;
+	/** Returns kd leaf the point pt lies in, starting from root.
+	*/
+	KdLeaf *GetLeaf(const Vector3 &pt, KdNode *root = NULL) const;
 
 
