//===========================================================================//
// File:	D3DRMVideoRenderables.cpp                                        //
// Project: MUNGA           Brick: Win95 Video Renderer 	                 //
// Contents: Windows95 Layer Video Renderer									 //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 03/13/97  JTR  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1997, Virtual World Entertainment, Inc.                     //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "AdeptHeaders.hpp"

#include "VideoHeaders.hpp"

using namespace ElementRenderer;
#include <ElementRenderer\MultiLODElement.hpp>

//
// Class Data Support
//
Component::ClassData*
	MultiLODComponent::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MultiLODComponent::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			MultiLODComponentClassID,
			"Adept::MultiLODComponent",
			BaseClass::DefaultData,
			0,
			NULL
		);
	Register_Object(DefaultData);
}

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MultiLODComponent::MultiLODComponent(
	ClassData *shared_data,
	MemoryStream *stream,
	VideoComponentWeb *owning_web
):
	VideoComponent(
		shared_data,
		stream,
		owning_web,
		AllocateLOD()
	)
{
	//
	//---------------------------
	// Get the lOD implementation
	//---------------------------
	//
	MultiLODElement *lod = GetElement();
	Check_Object(lod);

	//
	//-----------------------------
	// Get the number of LOD ranges
	//-----------------------------
	//
	Check_Object(stream);
	unsigned ranges;
	*stream >> ranges;
	Verify(ranges < 65536);
	gos_PushCurrentHeap(ElementRenderer::MultiLODElement::s_Heap);
	lod->SetSize(static_cast<WORD>(ranges));
	gos_PopCurrentHeap();

	//
	//--------------------------------------------------------------
	// Spin through the ranges, setting the range for each LOD child
	//--------------------------------------------------------------
	//
	for (WORD i=0; i<ranges; ++i)
	{
		ComponentID component_id;
		component_id.scriptResourceID = owning_web->GetScriptResourceID();
		*stream >> component_id.componentNumber;
		Component *component = owning_web->FindComponent(component_id);
		Check_Object(component);
		VideoComponent *child = Cast_Object(VideoComponent*, component);
		MultiLODElement::Entry entry;
		*stream >> entry.m_nearSquared >> entry.m_fadeInSquared;
		*stream >> entry.m_fadeOutSquared >> entry.m_farSquared;
		lod->AttachLOD(i, child->GetElement(), entry);
	}

	//
	//--------------------------------------
	// Set the bounding sphere for this node
	//--------------------------------------
	//
	lod->NeedNewBounds();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MultiLODElement*
	MultiLODComponent::AllocateLOD()
{
	gos_PushCurrentHeap(ElementRenderer::MultiLODElement::s_Heap);
	MultiLODElement *lod = new MultiLODElement;
	gos_PopCurrentHeap();

	Register_Object(lod);
	return lod;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MultiLODComponent*
	MultiLODComponent::Make(
		MemoryStream *stream,
		VideoComponentWeb *owning_web
	)

{
	MultiLODComponent *lod =
		new MultiLODComponent(DefaultData, stream, owning_web);
	return lod;
}

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

