Index: trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 348)
+++ trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 350)
@@ -90,5 +90,5 @@
 					DecideVisible(node) && mHierarchyInterface->HasGeometry(node);
 
-				//if (mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId())
+				//if (mHierarchyInterface->LastVisited(node) >= mHierarchyInterface->GetFrameId())
 				//	Ogre::LogManager::getSingleton().logMessage("error");
 				
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 347)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 350)
@@ -306,6 +306,6 @@
 		// the objects are generated randomly distributed over the terrain
 		generateScene(1000, 0); // create soldiers, trees, ninjas
-		generateScene(1000, 1);
-		generateScene(200, 2);
+		generateScene(500, 1);
+		generateScene(100, 2);
 	}
 }
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp	(revision 347)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp	(revision 350)
@@ -105,5 +105,5 @@
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
 	// update bounds because we want tight octree
-	if (1) _updateBounds(); 
+	_updateBounds();
 	
 #endif //GTP_VISIBILITY_MODIFIED_OGRE
Index: trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLRenderSystem.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLRenderSystem.cpp	(revision 347)
+++ trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLRenderSystem.cpp	(revision 350)
@@ -2344,5 +2344,5 @@
             glDisableVertexAttribArrayARB_ptr(1); // disable weights
         }
-		glColor4f(1,1,1,1);
+
         glSecondaryColor3fEXT_ptr(0.0f, 0.0f, 0.0f);
 
Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 349)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 350)
@@ -120,5 +120,5 @@
 	Termination {
 		# autopartition
-		maxRays 10
+		maxRays 30
 		maxPolygons 0
 		maxDepth 100
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 341)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 350)
@@ -37,5 +37,5 @@
 	int GetSize() {return (int)mEntries.size();}
 	bool Empty() {return mEntries.size() == 0;}
-	void Merge(const Pvs<T> &a);
+	void Merge(const Pvs<T> &a, const Pvs<T> &b);
 
 	PvsData<T> *Find(T sample);
@@ -49,12 +49,8 @@
 
 template <typename T>
-void Pvs<T>::Merge(const Pvs<T> &a)
+void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
 {
-	std::map<T, PvsData<T>, LtSample<T> >::iterator it, it_end = mEntries.end();
-
-	for (it = mEntries.begin(); it != mEntries.end(); ++ it)
-	{
-		// TODO
-	}
+	//std::merge(a.mEntries.begin(), a.mEntries.end(), 
+	//	       b.mEntries.begin(), b.mEntries.end(), mEntries.begin());
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 349)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 350)
@@ -54,4 +54,7 @@
   /// samples contributing to pvs
   int contributingSamples;
+   /// sample contributions to pvs
+  int sampleContributions;
+
   // Constructor
   BspTreeStatistics() 
@@ -77,4 +80,5 @@
 	  pvs = 0;
 	  contributingSamples = 0;
+	  sampleContributions = 0;
   }
 
@@ -171,17 +175,4 @@
 					  const bool storePolys = false);
 
-	/** Subdivides the rays into front and back rays according to the split plane.
-		@param rays contains the rays to be split. The rays are 
-			   distributed into front and back rays.
-		@param frontRays returns rays on the front side of the plane
-		@param backRays returns rays on the back side of the plane
-		@param box the bounding box of the scene
-		@returns the number of splits
-	*/
-	int SplitRays(RayContainer &rays, 
-			      RayContainer &frontRays, 
-				  RayContainer &backRays,
-				  const AxisAlignedBox3 &box);
-
 	/** Stores polygon in node or discards them according to storePolys.
 		@param polys the polygons
@@ -228,7 +219,10 @@
 
 	/** Generates new view cell and adds rays to the PVS.
-		@returns the number of samples contributing to the pvs.
-	*/
-	int GenerateViewCell(const RayContainer &rays);
+		@param sampleContributions the number contributions of the sampels
+		@param contributingSampels the number of contributing rays
+	*/
+	void BspLeaf::GenerateViewCell(const RayContainer &rays, 
+							   int &sampleContributions,
+							   int &contributingSamples);
 
 protected:
@@ -236,4 +230,5 @@
 	/// if NULL this does not correspond to feasible viewcell
 	ViewCell *mViewCell;
+	RayContainer mPiercingRays;
 };
 
@@ -569,4 +564,26 @@
 	bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const;
 
+	/** Bounds ray and returns minT and maxT.
+		@returns true if ray hits BSP tree bounding box
+	*/
+	bool BoundRay(const Ray &ray, float &minT, float &maxT) const;
+
+	/** Subdivides the rays into front and back rays according to the split plane.
+		
+		@param plane the split plane
+		@param rays contains the rays to be split. The rays are 
+			   distributed into front and back rays.
+		@param frontRays returns rays on the front side of the plane
+		@param backRays returns rays on the back side of the plane
+		
+		@returns the number of splits
+	*/
+	int SplitRays(const Plane3 &plane,
+				  RayContainer &rays, 
+			      RayContainer &frontRays, 
+				  RayContainer &backRays);
+
+	void MergeLeafs(BspLeaf *front, BspLeaf *back);
+
 	/// Pointer to the root of the tree
 	BspNode *mRoot;
@@ -596,6 +613,10 @@
 	/// view cell corresponding to unbounded space
 	ViewCell *mRootCell;
+
 	/// should view cells be stored or generated in the leaves?
 	bool mGenerateViewCells;
+
+	/// if rays should be stored that are piercing this view cell
+	bool mStorePiercingRays;
 
 public:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 349)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 350)
@@ -25,5 +25,6 @@
   enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
 
-  /// if ray is on back (front) side of plane, or goes from the front (back) to the back (front)
+  /// if ray is on back (front) side of plane, or goes from the 
+  /// front (back) to the back (front)
   enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 349)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 350)
@@ -165,67 +165,4 @@
 	else
 		DEL_PTR(*poly);
-}
-
-int BspInterior::SplitRays(RayContainer &rays, 
-						   RayContainer &frontRays, 
-						   RayContainer &backRays,
-						   const AxisAlignedBox3 &box)
-{
-	int splits = 0;
-
-	while (!rays.empty())
-	{
-		Ray *ray = rays.back();
-		rays.pop_back();
-
-		//-- ray-plane intersection
-		float maxT = 1e6;
-		float minT = 0;
-	
-		// test with tree bounding box
-		if (!box.GetMinMaxT(*ray, &minT, &maxT))
-			continue;
-
-		if (minT < 0) // start ray from origin
-			minT = 0;
-
-		// bound ray
-		if ((ray->GetType() == Ray::LOCAL_RAY) && 
-			(!ray->intersections.empty())      &&
-			(ray->intersections[0].mT <= maxT))
-		{
-			maxT = ray->intersections[0].mT;
-		}
-		
-		const int classification = ray->ClassifyPlane(mPlane, minT, maxT);
-		
-		ray->SetId(classification);
-
-		switch (classification)
-		{
-		case Ray::COINCIDENT:
-			break;
-		case Ray::BACK:
-			backRays.push_back(ray);
-			break;
-		case Ray::FRONT:
-			frontRays.push_back(ray);
-			break;
-		case Ray::FRONT_BACK:
-			ray->SetId(Ray::FRONT_BACK);
-			backRays.push_back(ray);
-			++ splits;			
-			break;
-		case Ray::BACK_FRONT:
-			frontRays.push_back(ray);
-			++ splits;
-			break;
-		default:
-			Debug << "Should not come here" << endl;
-			break;
-		}
-	}
-
-	return splits;
 }
 
@@ -313,17 +250,19 @@
 /*                  class BspLeaf implementation                */
 /****************************************************************/
-BspLeaf::BspLeaf(): mViewCell(NULL)
-{
-}
-
-BspLeaf::BspLeaf(ViewCell *viewCell): mViewCell(viewCell)
-{
-}
-
-BspLeaf::BspLeaf(BspInterior *parent): BspNode(parent), mViewCell(NULL)
+BspLeaf::BspLeaf(): mViewCell(NULL), mPiercingRays(0)
+{
+}
+
+BspLeaf::BspLeaf(ViewCell *viewCell): 
+mViewCell(viewCell), mPiercingRays(0)
+{
+}
+
+BspLeaf::BspLeaf(BspInterior *parent): 
+BspNode(parent), mViewCell(NULL), mPiercingRays(0)
 {}
 
 BspLeaf::BspLeaf(BspInterior *parent, ViewCell *viewCell): 
-BspNode(parent), mViewCell(viewCell)
+BspNode(parent), mViewCell(viewCell), mPiercingRays(0)
 {
 }
@@ -344,7 +283,10 @@
 }
 
-int BspLeaf::GenerateViewCell(const RayContainer &rays)
-{
-	int contributingSamples = 0;
+void BspLeaf::GenerateViewCell(const RayContainer &rays, 
+							   int &sampleContributions,
+							   int &contributingSamples)
+{
+	sampleContributions = 0;
+	contributingSamples = 0;
 
 	mViewCell = new ViewCell();
@@ -355,12 +297,19 @@
 	for (it = rays.begin(); it != it_end; ++ it)
 	{
+		int contribution = 0;
+
 		if (!(*it)->intersections.empty())
 		{	
-            if (mViewCell->GetPvs().AddSample((*it)->intersections[0].mObject) > 0)
-				++ contributingSamples;
-		}
-	}
-
-	return contributingSamples;
+            contribution +=  mViewCell->GetPvs().AddSample((*it)->intersections[0].mObject);
+		}
+
+		contribution +=  mViewCell->GetPvs().AddSample((*it)->sourceObject.mObject);
+
+		if (contribution > 0)
+		{
+			sampleContributions += contribution;
+			++ contributingSamples;
+		}
+	}
 }
 
@@ -371,5 +320,6 @@
 
 BspTree::BspTree(ViewCell *viewCell): 
-mRootCell(viewCell), mRoot(NULL), mGenerateViewCells(false)
+mRootCell(viewCell), mRoot(NULL), mGenerateViewCells(false),
+mStorePiercingRays(true)
 {
 	Randomize(); // initialise random generator for heuristics
@@ -638,4 +588,5 @@
 
 	long startTime = GetTime();
+
 	Debug << "**** Extracting polygons from rays ****\n";
 
@@ -649,4 +600,6 @@
 		rays->push_back(ray);
 
+		//Debug << "extracting ray with intersections" << ray->intersections.size() << endl;
+
 		// get ray-face intersection. Store polygon representing the rays together
 		// with rays intersecting the face.
@@ -656,5 +609,5 @@
 			Face *face = obj->GetMesh()->mFaces[ray->intersections[0].mFace];
 
-			std::map<Face *, Polygon3 *>::iterator it= facePolyMap.find(face);
+			std::map<Face *, Polygon3 *>::iterator it = facePolyMap.find(face);
 
 			if (it != facePolyMap.end()) 
@@ -721,15 +674,33 @@
 		// generate new view cell for each leaf
 		if (mGenerateViewCells)
-			mStat.contributingSamples += leaf->GenerateViewCell(*tData.mRays);
+		{
+			int conSamp, sampCon;
+			leaf->GenerateViewCell(*tData.mRays, conSamp, sampCon);
+			
+			mStat.contributingSamples += conSamp;
+			mStat.sampleContributions += sampCon;
+		}
 		else
+		{
 			// add view cell to leaf
 			leaf->SetViewCell(tData.mViewCell);
+		}
+
+		EvaluateLeafStats(tData);
 		
-		EvaluateLeafStats(tData);
-
 		//-- clean up
+		
 		// remaining polygons are discarded or added to node
 		leaf->ProcessPolygons(tData.mPolygons, sStoreSplitPolys);
 		DEL_PTR(tData.mPolygons);
+		
+		if (mStorePiercingRays)
+		{
+			RayContainer::const_iterator it, it_end = tData.mRays->end();
+			for (it = tData.mRays->begin(); it != it_end; ++ it)
+			{
+				leaf->mPiercingRays.push_back(*it);
+			}
+		}
 		DEL_PTR(tData.mRays);
 
@@ -852,5 +823,5 @@
 	
 	// subdivide rays into front and back rays
-	interior->SplitRays(rays, frontRays, backRays, mBox);
+	SplitRays(interior->mPlane, rays, frontRays, backRays);
 	
 	// split polygons with split plane
@@ -1096,7 +1067,9 @@
 
 	// swap candidates to avoid testing same plane 2 times
-	Polygon3 *p = polys[candidateIdx];
+	std::swap(polys[currentIdx], polys[candidateIdx]);
+
+	/*Polygon3 *p = polys[candidateIdx];
 	polys[candidateIdx] = polys[currentIdx];
-	polys[currentIdx] = p;
+	polys[currentIdx] = p;*/
 	
 	return currentIdx;
@@ -1219,4 +1192,28 @@
 }
 
+bool BspTree::BoundRay(const Ray &ray, float &minT, float &maxT) const
+{
+	maxT = 1e6;
+	minT = 0;
+	
+	// test with tree bounding box
+	if (!mBox.GetMinMaxT(ray, &minT, &maxT))
+		return false;
+
+	if (minT < 0) // start ray from origin
+		minT = 0;
+
+	//Debug << "ray intersections " << ray.intersections.size() << endl;
+	// bound ray or line segment
+	if ((ray.GetType() == Ray::LOCAL_RAY) && 
+	    !ray.intersections.empty() && 
+		(ray.intersections[0].mT <= maxT))
+	{
+		maxT = ray.intersections[0].mT;
+	}
+
+	return true;
+}
+
 float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
 							  const RayContainer &rays) const
@@ -1233,20 +1230,7 @@
 		Ray *ray = *rit;
 
-		float maxT = 1e6;
-		float minT = 0;
-	
-		// test with tree bounding box
-		if (!mBox.GetMinMaxT(*ray, &minT, &maxT))
+		float minT, maxT;
+		if (!BoundRay(*ray, minT, maxT))
 			continue;
-		if (minT < 0) // start ray from origin
-			minT = 0;
-
-		// bound ray
-		if ((ray->GetType() == Ray::LOCAL_RAY) && 
-			(!ray->intersections.empty())      &&
-			(ray->intersections[0].mT <= maxT))
-		{
-			maxT = ray->intersections[0].mT;
-		}
 
 		const int classification = 
@@ -1438,10 +1422,10 @@
 	}
 	
-//#ifdef _DEBUG
+#ifdef _DEBUG
 	Debug << "BSP stats: "
 		  << "Depth: " << data.mDepth << " (max: " << sTermMaxDepth << "), "
 		  << "#polygons: " << (int)data.mPolygons->size() << " (max: " << sTermMaxPolygons << "), "
 		  << "#rays: " << (int)data.mRays->size() << " (max: " << sTermMaxRays << ")" << endl;
-//#endif
+#endif
 }
 
@@ -1452,24 +1436,10 @@
 	stack<BspRayTraversalData> tStack;
   
-	float maxt = 1e6;
-	float mint = 0;
-	
+	float maxt, mint;
+
 	Intersectable::NewMail();
 
-	// test with tree bounding box
-	if (!mBox.GetMinMaxT(ray, &mint, &maxt))
-		return 0;
-
-	if (mint < 0) // start ray from origin
-		mint = 0;
-
-	// bound ray
-	if ((ray.GetType() == Ray::LOCAL_RAY) && 
-		(!ray.intersections.empty())      &&
-	    (ray.intersections[0].mT <= maxt))
-	{
-		maxt = ray.intersections[0].mT;
-    }
-  
+	BoundRay(ray, mint, maxt);
+	
 	Vector3 entp = ray.Extrap(mint);
 	Vector3 extp = ray.Extrap(maxt);
@@ -1608,39 +1578,28 @@
 {
 	stack<BspNode *> nodeStack;
-	stack<BspLeaf *> leafStack;
-
 	nodeStack.push(mRoot);
-	
-	// collect leaves
+
+	BspLeaf *rightLeaf = NULL;
+
 	while (!nodeStack.empty()) 
 	{
 		BspNode *node = nodeStack.top();
-		
 		nodeStack.pop();
 
-		if (node->IsLeaf())
-		{
-			BspNode *leaf = dynamic_cast<BspLeaf *>(node);
-
-			// get other leaf we can merge with
-			while (!leafStack.empty() && leafStack.top()->mParent == node->mParent)
-			{
-				leafStack.pop();
-				BspNode *mergedNode = MergeNodes(node->mParent);
-
-				if (mergedNode->IsLeaf())
-					leafStack.push(dynamic_cast<BspLeaf *>(mergedNode));
-				else
-					leafStack.push(dynamic_cast<BspLeaf *>(node));
-			}
-		}
-		else
+		if (node->IsLeaf()) 
+		{
+			BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
+
+			if (rightLeaf && MergeNecessary(leaf, rightLeaf))
+				MergeLeafs(leaf, rightLeaf);
+		}
+		else 
 		{
 			BspInterior *interior = dynamic_cast<BspInterior *>(node);
+
 			nodeStack.push(interior->mFront);
 			nodeStack.push(interior->mBack);
 		}
 	}
-
 	return 0;
 }
@@ -1648,82 +1607,78 @@
 bool BspTree::MergeNecessary(BspLeaf *front, BspLeaf *back) const
 {
+	//if (Raydiff
 	return true;
 }
 
-BspNode *BspTree::MergeNodes(BspInterior *node)
-{
-	BspLeaf *front = dynamic_cast<BspLeaf *>(node->mFront);
-	BspLeaf *back = dynamic_cast<BspLeaf *>(node->mBack);
-
-	if (!MergeNecessary(front, back))
-		return node;
+void BspTree::MergeLeafs(BspLeaf *front, BspLeaf *back)
+{
+	//std::merge(front->mPvs.mEntries.begin(), back->mPvs.mEntries.begin());
+	ViewCell *viewCell = new ViewCell();
+	viewCell->GetPvs().Merge(front->mViewCell->GetPvs(), back->mViewCell->GetPvs());
 
 	DEL_PTR(front->mViewCell);
-	DEL_PTR(front);
+	DEL_PTR(back->mViewCell);
+
+	front->SetViewCell(viewCell);
+	back->SetViewCell(viewCell);
+}
+
+void BspTree::SetGenerateViewCells(int generateViewCells)
+{
+	mGenerateViewCells = generateViewCells;
+}
+
+BspTreeStatistics &BspTree::GetStat()
+{
+	return mStat;
+}
+
+int BspTree::SplitRays(const Plane3 &plane,
+					   RayContainer &rays, 
+					   RayContainer &frontRays, 
+					   RayContainer &backRays)
+{
+	int splits = 0;
+
+	while (!rays.empty())
+	{
+		Ray *ray = rays.back();
+		rays.pop_back();
+
+		//-- ray-plane intersection
+		float maxT, minT;
+	
+		if (!BoundRay(*ray, minT, maxT))
+			continue;
+				
+		const int classification = ray->ClassifyPlane(plane, minT, maxT);
 		
-	DEL_PTR(back->mViewCell);
-	DEL_PTR(back);
-
-	BspLeaf *leaf = new BspLeaf(new ViewCell());
-	BspInterior *parent = node->GetParent();
-
-	// replace a link from node's parent
-	if (!node->IsRoot())
-	{
-		parent->ReplaceChildLink(node, leaf);
-		node->SetParent(parent);
-	}
-	else // new root
-	{
-		mRoot = leaf;
-	}
-		
-	return leaf;
-}
-
-/*BspNode *BspTree::MergeNodes(BspNode *node)
-{
-	BspNode *front = MergeNodes(dynamic_cast<BspInterior *>(node)->mFront);
-	BspNode *back = MergeNodes(dynamic_cast<BspInterior *>(node)->mBack);
-
-	if (!front->IsLeaf() && !back->IsLeaf())
-
-	{
-		if (!MergeNecessary(front, back))
-			return node;
-
-		DEL_PTR(front->mViewCell);
-		DEL_PTR(front);
-		
-		DEL_PTR(back->mViewCell);
-		DEL_PTR(back);
-
-		BspLeaf *leaf = new BspLeaf(new ViewCell());
-		BspInterior *parent = node->GetParent();
-
-		// replace a link from node's parent
-		if (!node->IsRoot())
-		{
-			parent->ReplaceChildLink(node, leaf);
-			node->SetParent(parent);
-		}
-		else // new root
-		{
-			mRoot = leaf;
-		}
-		
-		return leaf;
-	}
-
-	return node;
-}*/
-
-void BspTree::SetGenerateViewCells(int generateViewCells)
-{
-	mGenerateViewCells = generateViewCells;
-}
-
-BspTreeStatistics &BspTree::GetStat()
-{
-	return mStat;
-}
+		ray->SetId(classification);
+
+		switch (classification)
+		{
+		case Ray::COINCIDENT:
+			break;
+		case Ray::BACK:
+			backRays.push_back(ray);
+			break;
+		case Ray::FRONT:
+			frontRays.push_back(ray);
+			break;
+		case Ray::FRONT_BACK:
+			ray->SetId(Ray::FRONT_BACK);
+			backRays.push_back(ray);
+			++ splits;			
+			break;
+		case Ray::BACK_FRONT:
+			frontRays.push_back(ray);
+			++ splits;
+			break;
+		default:
+			Debug << "Should not come here" << endl;
+			break;
+		}
+	}
+
+	return splits;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 349)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 350)
@@ -161,6 +161,4 @@
 		cout<<ray<<endl;
 	}
-
-	//	cout<<object->GetId()<<" "<<TimeDiff(t1, t2)<<endl;
 
 	if (mViewCellsType == BSP_VIEW_CELLS)
@@ -419,19 +417,11 @@
 					
 					// construct a ray
-					SetupRay(ray, point, direction, 
-						collectSamplesForBsp ? Ray::LINE_SEGMENT : Ray::LOCAL_RAY);
+					SetupRay(ray, point, direction, Ray::LOCAL_RAY);
 			
-					// also add origin to sample in order to extract it as input polygons
-					if (0 && collectSamplesForBsp)
-					{
-						MeshInstance *meshInst = dynamic_cast<MeshInstance *>(object);
-						ray.intersections.push_back(Ray::Intersection(0.0, meshInst, faceIndex));
-					}
-
 					sampleContributions = CastRay(object, ray);
 
 					//-- CORR matt: put block inside loop
 					if (sampleContributions) {
-						passContributingSamples++;
+						passContributingSamples ++;
 						passSampleContributions += sampleContributions;
 					}
@@ -455,4 +445,8 @@
 						if (collectSamplesForBsp)
 						{
+							// also add origin to sample in order to extract it as input polygons
+							MeshInstance *mi = dynamic_cast<MeshInstance *>(object);
+							ray.sourceObject = Ray::Intersection(0.0, mi, faceIndex);
+							
                             mSampleRays.push_back(new Ray(ray));
 						}
@@ -465,6 +459,7 @@
 
 								cout << "generated " << (int)mViewCells.size() << " view cells" << endl;
+
 								passContributingSamples += mBspTree->GetStat().contributingSamples;
-								passSampleContributions += mBspTree->GetStat().pvs;
+								passSampleContributions += mBspTree->GetStat().sampleContributions;
 
 								BspTreeStatistics(Debug);	
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 349)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 350)
@@ -343,4 +343,6 @@
 	if (!eq(Magnitude(plane.mNormal), 1.0f))
 		return false;
+
+	return true;
 }
 
