// PPLightManager.cpp: implementation of the CPPLightManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "pixelwhippro.h"
#include "PPLightManager.h"
#include <MLR\MLR.hpp>

extern CPixelWhipProApp theApp;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPPLightManager::CPPLightManager()
{

}

CPPLightManager::~CPPLightManager()
{

}


gosFX::Light *CPPLightManager::MakePointLight(const char* light_map, bool light_vertex)
{
int i;
	for(i=0;i<MidLevelRenderer::Limits::Max_Number_Of_Lights_Per_Primitive && theApp.LightState->m_lights[i];i++)
	{
	}

	MidLevelRenderer::MLRPointLight *new_light;

	gos_PushCurrentHeap(MidLevelRenderer::LightsHeap);
	new_light=new MidLevelRenderer::MLRPointLight;

if(i<MidLevelRenderer::Limits::Max_Number_Of_Lights_Per_Primitive)
	theApp.LightState->m_lights[i]=new_light;
	
	
	MString name = light_map;
	if(name)
	{
	Check_Object(MidLevelRenderer::MLRTexturePool::Instance);
	MidLevelRenderer::MLRTexture *texture =
	(*MidLevelRenderer::MLRTexturePool::Instance)(name, 0);
	if (!texture)
			texture = MidLevelRenderer::MLRTexturePool::Instance->Add(name, 0);
	Check_Object(texture);
	
	MidLevelRenderer::MLRLightMap *map = new MidLevelRenderer::MLRLightMap(texture);
	Check_Object(map);
	new_light->SetLightMap(map);
	if (!light_vertex)
	{
		int mask = new_light->GetLightMask();
		mask &= ~MidLevelRenderer::MLRState::VertexLightingMode;
		new_light->SetLightMask(mask);
	}
	}
	gos_PopCurrentHeap();

	return (gosFX::Light *)new_light;
}

void CPPLightManager::ChangeLight(gosFX::Light *light,Info *info)
{
	MidLevelRenderer::MLRLight *mlght=(MidLevelRenderer::MLRLight *)light;
	switch(mlght->GetLightType())
	{
	case	MidLevelRenderer::MLRLight::AmbientLight:
		{
		MidLevelRenderer::MLRAmbientLight *tlght=(MidLevelRenderer::MLRAmbientLight *)light;
		}
		break;

	case	MidLevelRenderer::MLRLight::InfiniteLight:
		{
		MidLevelRenderer::MLRInfiniteLight *tlght=(MidLevelRenderer::MLRInfiniteLight *)light;
		}
		break;

	case	MidLevelRenderer::MLRLight::InfiniteLightWithFallOff:
		{
		MidLevelRenderer::MLRInfiniteLightWithFalloff *tlght=(MidLevelRenderer::MLRInfiniteLightWithFalloff *)light;
		tlght->SetFalloffDistance(info->m_inner,info->m_outer);
		}
		break;

	case	MidLevelRenderer::MLRLight::PointLight:
		{
		MidLevelRenderer::MLRPointLight *tlght=(MidLevelRenderer::MLRPointLight *)light;
		tlght->SetFalloffDistance(info->m_inner,info->m_outer);
		tlght->SetLightToWorldMatrix(info->m_origin);
		}
		break;

	case	MidLevelRenderer::MLRLight::SpotLight:
		{
		MidLevelRenderer::MLRSpotLight *tlght=(MidLevelRenderer::MLRSpotLight *)light;
		tlght->SetSpreadAngle(info->m_spread);
		tlght->SetFalloffDistance(info->m_inner,info->m_outer);
		tlght->SetLightToWorldMatrix(info->m_origin);
		}
		break;

	case	MidLevelRenderer::MLRLight::LookUpLight:
		{
		MidLevelRenderer::MLRLookUpLight *tlght=(MidLevelRenderer::MLRLookUpLight *)light;
		}
		break;
	}
	
	mlght->SetColor(info->m_color);
	mlght->SetIntensity(info->m_intensity);
}

void CPPLightManager::DeleteLight(gosFX::Light *light)
{
MidLevelRenderer::MLRLight *mlght=(MidLevelRenderer::MLRLight *)light;

	int lidx,i;
	for(lidx=0;
		theApp.LightState->m_lights[lidx]!=mlght && lidx<MidLevelRenderer::Limits::Max_Number_Of_Lights_Per_Primitive;
		lidx++)
		{
		}
	
	if(lidx<MidLevelRenderer::Limits::Max_Number_Of_Lights_Per_Primitive)
	{
		for(i=lidx;i<MidLevelRenderer::Limits::Max_Number_Of_Lights_Per_Primitive-1;i++)
		{
		theApp.LightState->m_lights[i]=theApp.LightState->m_lights[i+1];
		}
		theApp.LightState->m_lights[MidLevelRenderer::Limits::Max_Number_Of_Lights_Per_Primitive-1]=NULL;
	}
	gos_PushCurrentHeap(MidLevelRenderer::LightsHeap);
	delete mlght;
	gos_PopCurrentHeap();

}