/*****************************************************************************

File: ClipmapSceneManager.cpp
Desc: 
Date: 2003/02/23

Author: Martin Persson

*****************************************************************************/

#include <iostream>

#include "OgreMaterialManager.h"

#include "ClipmapSceneManager.h"
#include "ClipmapPatch.h"

#include "OgreEntity.h"
namespace Ogre
{

	//-----------------------------------------------------------------------
	const String ClipmapSceneManagerFactory::FACTORY_TYPE_NAME = "ClipmapSceneManager";
	//-----------------------------------------------------------------------
	void ClipmapSceneManagerFactory::initMetaData(void) const
	{
		mMetaData.typeName = FACTORY_TYPE_NAME;
		mMetaData.description = "Scene manager organising the scene on the basis of an clipmap, able to display planets.";
		mMetaData.sceneTypeMask = ST_EXTERIOR_REAL_FAR; // support all types
		mMetaData.worldGeometrySupported = true;
	}
	//-----------------------------------------------------------------------
	SceneManager* ClipmapSceneManagerFactory::createInstance(
		const String& instanceName)
	{
		return new ClipmapSceneManager(instanceName);
	}
	//-----------------------------------------------------------------------
	void ClipmapSceneManagerFactory::destroyInstance(SceneManager* instance)
	{
		delete instance;
	}
	//----------------------------------------------------------------------------
	ClipmapSceneManager::ClipmapSceneManager(const String &name) : 
	SceneManager(name)
	{
		mClipmapPatchManager = 0;
		mClipmapRoot = 0;
	}

	//----------------------------------------------------------------------------

	ClipmapSceneManager::~ClipmapSceneManager()
	{
		if (mClipmapPatchManager != 0)
			delete mClipmapPatchManager;
	}

	//----------------------------------------------------------------------------

	void ClipmapSceneManager::setWorldGeometry (const String &filename)
	{
		// If already instanstiated then flush existing world geometry out of memory
		flushWorldGeometry();

		if (!mClipmapRoot)
		{
			mClipmapRoot = getRootSceneNode()->createChildSceneNode("ClipmapPluginRoot");
		}

		// create an instance of a patchloader
		// INFO: change patchloaders here
		geometryReader = new SeperateCubemappedPlanetReader();
		geometryReader->initialise(filename);

		// create the patchmanager
		SceneNode *sn = mClipmapRoot->createChildSceneNode("node");
		mClipmapPatchManager = new ClipmapPatchManager();
		mClipmapPatchManager->initialise(mClipmapRoot, geometryReader);
		sn->attachObject(mClipmapPatchManager);
	}


	//----------------------------------------------------------------------------

	void ClipmapSceneManager::clearScene(void)
	{

		SceneManager::clearScene();
	}
	//----------------------------------------------------------------------------

	void ClipmapSceneManager::flushWorldGeometry()
	{
	}
	//----------------------------------------------------------------------------

	void ClipmapSceneManager::_renderVisibleObjects()
	{
		// Run the overloaded method
		SceneManager::_renderVisibleObjects();
	}


} // namespace Ogre

