// Initialize the region. This takes a global description and creates the scene node that will be
// the base for the region an, if needed, creates the water level.
void Region::Init( double globalX, double globalY, double globalZ, float sizeX, float sizeY, float waterHeight) {
	LG::Log("Region::Init: r=%s, <%lf,%lf,%lf>", this->Name.c_str(), globalX, globalY, globalZ);
	this->GlobalX = globalX;
	this->GlobalY = globalY;
	this->GlobalZ = globalZ;
	this->LocalX = (float)globalX;
	this->LocalY = (float)globalY;
	this->LocalZ = (float)globalZ;
	// create scene Node
	Ogre::Quaternion orient = Ogre::Quaternion(Ogre::Radian(-3.14159265f/2.0f), Ogre::Vector3(1.0f, 0.0f, 0.0f));
	Ogre::SceneNode* regionNode = LG::RendererOgre::Instance()->CreateSceneNode(this->Name.c_str(), 
			(Ogre::SceneNode*)NULL, false, true, this->LocalX, this->LocalY, this->LocalZ, 
			1.0, 1.0, 1.0, orient.w, orient.x, orient.y, orient.z);
	// add ocean to the region

	Ogre::SceneNode* oceanSceneNode = CreateOcean(regionNode, sizeX, sizeY, waterHeight, "Water/" + this->Name);
	this->OceanSceneNode = oceanSceneNode;
	this->OceanHeight = waterHeight;
	
	Ogre::SceneNode* terrainSceneNode = CreateTerrain(regionNode, sizeX, sizeY, "Terrain/" + this->Name);
	this->TerrainSceneNode = terrainSceneNode;

	this->AddRegionSceneNode(regionNode, LG::RegionRezCodeHigh);
	// m_visCalc->RecalculateVisibility();
	return;
}
void EC_MeshmoonWater::SetVisible(bool display)
{
    if (tritonVisible_ == display)
        return;
    tritonVisible_ = display;
    
    Ogre::SceneManager *ogreSceneManager = OgreSceneManager();
    if (!ogreSceneManager)
        return;

    if (!tritonVisible_)
        DestroyOcean();
    else
        CreateOcean();
}
void EC_MeshmoonWater::OnParentEntitySet()
{
    if (!framework || framework->IsHeadless())
        return;

    OgreRendererPtr renderer = Renderer();
    if (!renderer)
    {
        // Try to resolve the renderer now. Might have not been done in ctor if constructed as unparented!
        OgreRenderingModule* renderingModule = framework->Module<OgreRenderingModule>();
        if (!renderingModule)
            return;
        renderer_ = renderingModule->Renderer();
        if (renderer_.expired())
            return;
        renderer = renderer_.lock();
    }
        
    if (world_.expired() && ParentScene())
        world_ = ParentScene()->GetWorld<OgreWorld>();

    connect(renderer.get(), SIGNAL(DeviceCreated()), this, SLOT(RecreateOcean()), Qt::UniqueConnection);
    connect(renderer.get(), SIGNAL(MainCameraChanged(Entity *)), this, SLOT(OnActiveCameraChanged(Entity *)), Qt::UniqueConnection);
    connect(framework->Frame(), SIGNAL(Updated(float)), this, SLOT(OnUpdate(float)), Qt::UniqueConnection);

    OnActiveCameraChanged(renderer->MainCamera());

    // Detect sky components if already in scene, otherwise will be detected via ComponentAdded signal
    EntityList ents = ParentScene()->EntitiesWithComponent(EC_MeshmoonSky::TypeIdStatic());
    for (EntityList::iterator iter = ents.begin(); iter != ents.end(); ++iter)
    {
        IComponent *comp = (*iter)->Component(EC_MeshmoonSky::TypeIdStatic()).get();
        if (comp) OnComponentAdded(0, comp, AttributeChange::LocalOnly);
    }
    ents = (windConditionsComp_.expired() ? ParentScene()->EntitiesWithComponent(EC_SkyX::TypeIdStatic()) : EntityList());
    for (EntityList::iterator iter = ents.begin(); iter != ents.end(); ++iter)
    {
        IComponent *comp = (*iter)->Component(EC_SkyX::TypeIdStatic()).get();
        if (comp) OnComponentAdded(0, comp, AttributeChange::LocalOnly);
    }

    connect(ParentScene(), SIGNAL(ComponentAdded(Entity*, IComponent*, AttributeChange::Type)), 
        this, SLOT(OnComponentAdded(Entity*, IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
    connect(ParentScene(), SIGNAL(ComponentRemoved(Entity*, IComponent*, AttributeChange::Type)), 
        this, SLOT(OnComponentRemoved(Entity*, IComponent*, AttributeChange::Type)), Qt::UniqueConnection);

    CreateOcean();
}
void EC_MeshmoonWater::RecreateOcean()
{
    DestroyOcean();
    CreateOcean();
}