#include "kernel.h"
#include "rendermodule.h"
#ifndef VRUT_HPC
#include "rendermanager.h"
#endif

using namespace VRUT;

void RenderModule::processEvent(Event * evt)
{
	SceneModule::processEvent(evt);
	switch (evt->type)
	{
	case Event::EVT_PARAM_SET:
		{
			if (((EventParamSet*)evt)->paramIdentifier == sceneIDParamID)
			{
				RegisterCameraGUI();
			}
			else if (((EventParamSet*)evt)->paramIdentifier == cameraIDParamID)
			{
				UPDATE_PARAM_FROM_EVENT_ID(cameraIDParamID, cameraID, evt);
				/*if ((cameraID != NODE_ID_NONE) && (sceneID != SCENE_ID_NONE))
					PostToKernel(GET_EVT_SCENE_CAM_FIT_NEAR_FAR(sceneID, cameraID));
				*/
			}
		}
		break;
	case Event::EVT_SCENE_CAM_PROJECTION_CHANGED:
		{
			if ((((EventSceneCamProjectionChanged*)evt)->sceneID == GetSceneID()) && (((EventSceneCamProjectionChanged*)evt)->cameraID == GetCameraID()))
			{
				if (scene)
				{
					smart_pointer<SceneNode> camNode (NULL);
					scene->GetNode(camNode, GetCameraID());
					if (camNode && camNode->IsOfType(SceneNode::CAMERA))
						resizeUpdate = true;
				}
			}
			break;
		}
#ifndef VRUT_HPC
	case Event::EVT_RENDER_WIN_RESIZED:
		if (((EventRenderWinResized*)evt)->windowID==KERNEL->renderManager->GetGLWindow(this)->GetID())
			resizeUpdate = true;
		break;
#endif
	default:
		break;
	}
}

void RenderModule::Draw()
{
	if (resizeUpdate)
	{
		resizeUpdate = false;
		if (!scene)
			return;

#ifndef VRUT_HPC
		smart_pointer<Camera> camera (NULL);
		scene->GetNode(camera, cameraID);
		if (camera)
			renderTarget.ResizeAttachments(camera->GetWidth(), camera->GetHeight());
#endif
	}
}
