//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------		
#include<Render/Accelerator.hpp>

namespace Render
{
	//-----------------------------------------------------------------
	// C/D-Tor
	//-----------------------------------------------------------------
	Aggregate::Aggregate() : Accelerator() 
	{

	}
	//-----------------------------------------------------------------
	Aggregate::~Aggregate() 
	{
	
	}
	//-----------------------------------------------------------------
	void Aggregate::Add(Primitive* _primitive)
	{
		primitives.push_back(_primitive);
	}
	//-----------------------------------------------------------------
	bool Aggregate::Intersect(const Ray& _ray, Intersection& _intersection) const
	{
		bool isIntersected = false;
		TPrimitiveCollection::const_iterator it;
		for(it=primitives.begin(); it!=primitives.end(); ++it)
		{
			// Process intersection on current object
			// Compare to update intersection with nearest object
			Intersection localIntersection;	
			if((*it)->Intersect(_ray, localIntersection) && 
			   (localIntersection.T < _intersection.T))
			{
				_intersection = localIntersection;
				isIntersected = true;
			}
		}

		return isIntersected;
	}
	//-----------------------------------------------------------------
	bool Aggregate::Unoccluded(const VisibilityTester& _tester) const
	{
		TPrimitiveCollection::const_iterator it;
		for(it=primitives.begin(); it!=primitives.end(); ++it)
		{
			if(!(*it)->Unoccluded(_tester))
				return false;
		}
		return true;
	}
}
