//
// Tools_PhotonMapping_PolygonLightSource.h
//
// Created by RAR - 28/09/2004
//
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#ifdef ASURA_USE_PRAGMA_ONCE
	#pragma once
#endif

#ifndef __TOOLS_PHOTONMAPPING_POLYGONLIGHTSOURCE_H__
#define __TOOLS_PHOTONMAPPING_POLYGONLIGHTSOURCE_H__

#include "Core/Asura_Maths.h"
#include "Lighting/Tools_PhotonMapping_LightSource.h"


struct Tools_PhotonMapping_PolygonData
{
	int m_iIndex;
	float m_fTotalArea;//total area up to this polygon
	Asura_Vector_3 m_xNormal;
	Asura_Vector_3 m_xAxis1;
	Asura_Vector_3 m_xAxis2;
};

class Tools_PhotonMapping_PolygonLightSource : public Tools_PhotonMapping_LightSource
{
	public:
		Tools_PhotonMapping_PolygonLightSource()
		{
			m_iNumPolygons = 0;
			m_pxPolyData = 0;
		}
		~Tools_PhotonMapping_PolygonLightSource()
		{
			Shutdown();
		}

		void ResetToDefaults(void)
		{
			Tools_PhotonMapping_LightSource::ResetToDefaults();
			m_fAngle = 0.0f;//angle used differently for these lights , so different default
		}

		void Shutdown()
		{
			m_iNumPolygons = 0;
			delete [] m_pxPolyData;
			m_pxPolyData = 0;
		}

		// Access functions for light parameters
		void SetModuleIndex(int iModule)
		{
			m_iModule = iModule;
		}

		int GetModuleIndex() const
		{
			return m_iModule;
		}

		void SetLightID(u_int uLightID) 
		{
			m_uLightID = uLightID;
		}

		u_int GetLightID() const
		{
			return m_uLightID;
		}

		//Position of a polygon light is just used for the tools object , so need
		//to invalidate the photon map (as is done for normal lights)
		virtual void SetPosition(const Asura_Vector_3& xPosition)
		{
			m_xPosition = xPosition;
		}

		virtual void SetOrientation(const Asura_Matrix_3x3& xOrientation)
		{
			m_xOrientation = xOrientation;
		}


		//recalculates list of polygons associated with this light , together with their areas
		void UpdatePolygonList();

		int GetNumberOfPolygons() const {return m_iNumPolygons;}


		virtual void SetStratificationLevels(u_int uNumberOfSamples);

		//! Returns a possible photon position and direction based on the light's position and shape
		virtual void GetStratifiedPhotonPositionAndDirection(Asura_Vector_3& xPosition, Asura_Vector_3& xDirection, u_int uX, u_int uY);

		virtual float GetStratifiedPhotonEnergy();

		virtual bool IsPolygonLight(){return true;}

		virtual bool ShouldIgnoreLight();

		//Gets settings from the module mesh. Returns false if that mesh
		//has no data for this light
		bool SyncWithMesh();

		void InitialiseFromMeshData(int iModule,struct Asura_Mesh_PolygonLight* pxLightData);
		
	private:
		int					m_iModule;//index of module that contains the polygons for this light

		u_int				m_uLightID;//ID of light within that module

		int					m_iNumPolygons;
		Tools_PhotonMapping_PolygonData * m_pxPolyData;
		float 				m_fTotalArea;

		friend class Asura_Chunk_PolygonLights;
};


#endif //__TOOLS_PHOTONMAPPING_POLYGONLIGHTSOURCE_H__
