#include "ElementRendererHeaders.hpp"
#include <MLR\MLRTriangleCloud.hpp>

//############################################################################
//############################  TriangleCloudElement  ################################
//############################################################################

ElementRenderer::TriangleCloudElement::ClassData*
	ElementRenderer::TriangleCloudElement::DefaultData = NULL;

ElementRenderer::Element::DrawMethod
	ElementRenderer::TriangleCloudElement::DrawMethods[DrawStateCount]=
{
	//
	// No billboarding
	//
	DRAW_METHOD(TriangleCloudElement,	Inherit),
	DRAW_METHOD(TriangleCloudElement,	Override)
};

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			TriangleCloudElementClassID,
			"ElementRenderer::TriangleCloudElement",
			BaseClass::DefaultData,
			reinterpret_cast<Factory>(&Make)
		);
	Register_Object(DefaultData);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::TerminateClass()
{
	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
ElementRenderer::TriangleCloudElement::TriangleCloudElement(
	Stuff::MemoryStream *stream,
	int version
):
	Element(DefaultData, stream, version)
{
	Check_Pointer(this);
	Check_Object(stream);

	unsigned index = (m_state>>DrawStateBit) & DrawStateMask;
	Verify(index < DrawStateCount);
	m_draw = DrawMethods[index];

	//
	//---------------------------------
	// Create the shape from the stream
	//---------------------------------
	//
#if 0
	m_MLRTriangleCloud = MidLevelRenderer::MLRTriangleCloud::Make(stream);
	Register_Object(m_MLRTriangleCloud);
#endif
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
ElementRenderer::TriangleCloudElement::TriangleCloudElement(unsigned max_tris):
	Element(DefaultData)
{
	Check_Pointer(this);

	unsigned index = (m_state>>DrawStateBit) & DrawStateMask;
	Verify(index < DrawStateCount);
	m_draw = DrawMethods[index];

	gos_PushCurrentHeap(MidLevelRenderer::Heap);
	m_MLRTriangleCloud = new MidLevelRenderer::MLRTriangleCloud(max_tris);
	Register_Object(m_MLRTriangleCloud);
	gos_PopCurrentHeap();

	m_MLRTriangleCloud->TurnAllOn();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
ElementRenderer::TriangleCloudElement::~TriangleCloudElement()
{
	Check_Object(this);
	Unregister_Object(m_MLRTriangleCloud);
	delete m_MLRTriangleCloud;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
ElementRenderer::TriangleCloudElement*
	ElementRenderer::TriangleCloudElement::Make(
		Stuff::MemoryStream *stream,
		int version,
		ShapeHolder shapes
	)
{
	Check_Object(stream);

	gos_PushCurrentHeap(g_Heap);
	TriangleCloudElement *element = new TriangleCloudElement(stream, version);
	gos_PopCurrentHeap();

	return element;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::Save(Stuff::MemoryStream *stream)
{
	Check_Object(this);
	Check_Object(stream);
	BaseClass::Save(stream);

	//
	//-----------------------------
	// Save the shape to the stream
	//-----------------------------
	//
#if 0
	Check_Object(m_MLRTriangleCloud);
	m_MLRTriangleCloud->Save(stream);
#endif
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::TestInstance()
{
	Verify(IsDerivedFrom(DefaultData));
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::AttachChild(Element *child)
{
	Check_Object(this);
	Check_Object(child);
	STOP(("TriangleCloud elements can't have children!"));
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::DetachChild(Element *child)
{
	Check_Object(this);
	Check_Object(child);
	STOP(("TriangleCloud elements can't have children!"));
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	ElementRenderer::TriangleCloudElement::CastCulledRay(CollisionQuery *query)
{
	Check_Object(this);
	Check_Object(query);

	STOP(("Shouldn't be called"));
	return false;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
ElementRenderer::Element*
	ElementRenderer::TriangleCloudElement::FindSmallestElementContainingCulled(SphereTest *test)
{
	Check_Object(this);
	Check_Object(test);

	STOP(("TriangleCloudElement is not collidable!"));
	return NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::SetDrawState()
{
	Check_Object(this);
	unsigned index = (m_state>>DrawStateBit) & DrawStateMask;
	Verify(index < DrawStateCount);
	m_draw = DrawMethods[index];
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::InheritDrawMethod(
		CameraElement *camera,
		const StateChange *inherited_state,
		int culling_state
	)
{
	Check_Object(this);
	Check_Object(inherited_state);
	Check_Object(camera);
	Verify(culling_state != -1);
	#if defined(_ARMOR)
		if (!culling_state)
			Verify(!Cull(camera));
	#endif

	//
	//--------------------------------------
	// Draw our bounds if we are supposed to
	//--------------------------------------
	//
	#if defined(LAB_ONLY)
		Callback *callback = GetCallbackSet();
		unsigned index = GetCallbackIndex();
		if (callback && callback[index])
		{
			Check_Pointer(callback[index]);
			(*callback[index])(camera, inherited_state, culling_state, this);
		}
	#endif

	//
	//----------------------------
	// Set up the draw information
	//----------------------------
	//
	MidLevelRenderer::DrawEffectInformation info;
	Check_Object(m_MLRTriangleCloud);
	info.effect = m_MLRTriangleCloud;
	info.clippingFlags.SetClippingState(culling_state);
	Check_Object(&m_localToWorld);
	info.effectToWorld = &m_localToWorld;
	info.state = inherited_state->GetMLRState();

	//
	//---------------
	// Draw the shape
	//---------------
	//
	MidLevelRenderer::MLRClipper *clipper = camera->GetClipper();
	Check_Object(clipper);

	Stop_Timer(Graph_Traversal);
	ELEMENT_RENDER("Culling::TriangleCloud::Inherited");
	Start_Timer(Draw_Effects);
	clipper->DrawEffect(&info);
	Stop_Timer(Draw_Effects);
	Start_Timer(Graph_Traversal);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::OverrideDrawMethod(
		CameraElement *camera,
		const StateChange *inherited_state,
		int culling_state
	)
{
	Check_Object(this);
	Check_Object(inherited_state);
	Check_Object(camera);
	Verify(culling_state != -1);
	#if defined(_ARMOR)
		if (!culling_state)
			Verify(!Cull(camera));
	#endif

	//
	//-------------------
	// Mix the properties
	//-------------------
	//
	Check_Object(m_stateChange);
	StateChange mixed;
	mixed.Mix(*inherited_state, *m_stateChange);

	//
	//--------------------------------------
	// Draw our bounds if we are supposed to
	//--------------------------------------
	//
	#if defined(LAB_ONLY)
		Callback *callback = GetCallbackSet();
		unsigned index = GetCallbackIndex();
		if (callback && callback[index])
		{
			Check_Pointer(callback[index]);
			(*callback[index])(camera, inherited_state, culling_state, this);
		}
	#endif

	//
	//----------------------------
	// Set up the draw information
	//----------------------------
	//
	MidLevelRenderer::DrawEffectInformation info;
	Check_Object(m_MLRTriangleCloud);
	info.effect = m_MLRTriangleCloud;
	info.clippingFlags.SetClippingState(culling_state);
	Check_Object(&m_localToWorld);
	info.effectToWorld = &m_localToWorld;
	info.state = mixed.GetMLRState();

	//
	//---------------
	// Draw the shape
	//---------------
	//
	MidLevelRenderer::MLRClipper *clipper = camera->GetClipper();
	Check_Object(clipper);

	Stop_Timer(Graph_Traversal);
	ELEMENT_RENDER("Culling::TriangleCloud::Overridden");
	Start_Timer(Draw_Effects);
	clipper->DrawEffect(&info);
	Stop_Timer(Draw_Effects);
	Start_Timer(Graph_Traversal);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ElementRenderer::TriangleCloudElement::SetDataPointers(
		const unsigned *used_tris,
		const Stuff::Point3D *point_data,
		const Stuff::RGBAColor *color_data
	)
{
	Check_Pointer(this);
	Check_Pointer(used_tris);
	Check_Pointer(point_data);
	Check_Pointer(color_data);

	Check_Object(m_MLRTriangleCloud);
	m_MLRTriangleCloud->SetData(
		reinterpret_cast<const int*>(used_tris),
		point_data,
		color_data
	);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
int
	ElementRenderer::TriangleCloudElement::CountTriangles()
{
	Check_Object(this);
	STOP(("Not implemented"));
	return 0;
}
