/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "scene3d/scene.h"
		#include "scene3d/mobject.h"
		#include "scene3d/memitter.h"
		#include "scene3d/mterrain.h"
		#include "scene3d/mitem_event_interface.h"
		#include "ui/ui.h"
		#include "core/renderer.h"
		#include "core/camera.h"

		using namespace GS;
		using namespace GS::S3D;


//------------------------------------------------------------------------------
void				Scene::PushRenderable(Renderer &renderer)
{
	ScopedSystemProfile(profiler.push_to_render);

	{
		ScopedSystemProfile(profiler.push_active_list);

		ArrayListForeachPtr(MItem *, ci, active_list)
			if	(Core::Item *b = ci->GetBaseItem())
			{
				// Check exclusion.
				if	(b->item_flags.IsSet(ItemFlagInvisible))
					continue;
				if	(ci->mitem_flags.IsSet(MItem::Flag_EditorHidden))
					continue;

				// Render objects and emitters.
				switch (ci->GetItemType())
				{
					case Type_Emitter:
						renderer.PushRenderable((Core::Emitter *)b);
						break;
					case Type_Terrain:
						renderer.PushRenderable((Core::Terrain *)b);
						break;

					default: break;
				}
			}
	}

	// Push culling systems.
	{
		ScopedSystemProfile(profiler.push_simple_culling);
		renderer.PushRenderable(&simple_culling_system);
	}
	{
		ScopedSystemProfile(profiler.push_octree_culling);
		renderer.PushRenderable(&octree_culling_system);
	}
}
void				Scene::Render(Renderer &renderer)
{
	renderer.SetEnvironmentInterface(irenderer_environment);

	scene_event->OnRender(this);

	if	(!flags.IsSet(FlagRenderless))
	{
		PushRenderable(renderer);

		renderer.BeginDrawList();
		renderer.RenderList();
		renderer.EndDrawList();

		renderer.DeleteRenderableList();
	}

	renderer.SetEnvironmentInterface(NULL);

	scene_event->OnRenderDone(this);
}
void Scene::RenderUI(Renderer &renderer, GPU::TriangleBatch *batch)
{
	if (renderer.ideal_render_system_vr_resolution.x != -1)
	{
		uint w, h;
		renderer.GetOutputWindow()->GetSize(w, h);
		renderer.dimensions.Set(w, h);
		renderer.SetViewport(fRect(0, 0, w, h));
	}

	ui->Render(renderer, batch);

	scene_event->OnRenderUIDone(this);

	ui->RenderGlobalFade(renderer);
	
	if(renderer.ideal_render_system_vr_resolution.x != -1)
	{
		renderer.dimensions.Set(renderer.ideal_render_system_vr_resolution.x, renderer.ideal_render_system_vr_resolution.y);
		renderer.ideal_render_system_vr_resolution.x = -1;
	}
}
//------------------------------------------------------------------------------
