/****************************************************************************************************

		SimpleVolumetricEffect.h
		Copyright: (c) 2001 Eovia Corp. All rights reserved.

		Author:	Lionel
		Date:	10/5/2004

****************************************************************************************************/

#ifndef __SimpleVolumetricEffect__
#define __SimpleVolumetricEffect__


#include "SimpleVolumetricEffectDef.h"

#include "BasicCOMImplementations.h"
#include "MCBasicTypes.h"

#include "MCCountedPtr.h"
#include "MCCountedPtrArray.h"

#include "BasicPostRenderer.h"
#include "BasicVolumetricEffect.h"
#include "BasicCOMImplementations.h"

#include "lcPMAP.h"
#include "lightconecontrol.h"

#include "I3DShTreeElement.h"
#include "I3DShLightSource.h"
#include "I3DShCamera.h"
#include "MCSmp.h"

struct IShChannel;
struct IShRasterLayer;



// ********************************************************************************
//						class TSimpleLightCone
//
//  This object contains all the data related to the scene and lights,
//    and actually performs the volumetric effect (Initialise and DirectionFilter)
//
class TSimpleLightCone
{

public :  
	TSimpleLightCone();
	~TSimpleLightCone();
  
	void	Initialise(I3DShScene* scene);
	void	DirectionFilter(const TVector3& origin,
							const TVector3& direction,
							real			maxt,
							TMCColorRGBA&	inOutFilter,
							boolean			isShadowCasting);
protected:
	
	uint32*	GetLightColorBuffer(uint32 lightIndex,I3DShLightsource *light,TTransform3D &tranform,TMCColorRGB &resultColor);
	void	RenderLightGel(uint32 lightIndex,real x1,real y1,real x2,real y2,real &red,real &green,real &blue);
	void	RenderShadow(uint32 lightIndex,real x1,real y1,real z1,real x2,real y2,real z2,real &Rl,real &Gl,real &Bl);
	void	CreateShadowBuffer(uint32 lightIndex);
	
	I3DShScene*								fScene;		// no need to count that
	TMCCountedPtrArray<I3DShLightsource>	fLights;
	TMCArray<TTransform3D>					fTransforms;
	TMCArray<TMCColorRGB>					fLightColor;
	TMCArray<real>							fZScale;
	TMCPtrArray<uint32>						fLightColorBuffers;

	TMCArray<LightConeInfo>					fLightData;
	TMCCountedPtrArray<IShChannel>			fShadowBuffer;
};


// ********************************************************************************
//						class TLightConeVolumetric
//
//  The methods of this object will be called as the standard methods of a volumetric effect
//
class TLightConeVolumetric : public TSimpleLightCone,public TBasicVolumetricEffect
{
public :  
	TLightConeVolumetric();
	~TLightConeVolumetric();
  
	// IUnknown methods
	STANDARD_RELEASE;

	virtual int32		MCCOMAPI GetParamsBufferSize		() const	{ return 0;}

	static const MCGUID sClassId;

	// I3DExVolumetricEffect methods
	virtual void		MCCOMAPI Initialise(I3DShScene* scene);
	virtual void		MCCOMAPI DirectionFilter(const TVector3& origin,const TVector3& direction,real maxt,TMCColorRGBA& inOutFilter,boolean isShadowCasting);
};


// ********************************************************************************
//						class TLightConePostRenderer
//
// The lightcone postrenderer is used for the lightcone preview.
//
class TLightConePostRenderer : public TSimpleLightCone,public TBasicPostRenderer
{
public :  
	TLightConePostRenderer();
	~TLightConePostRenderer();
  
	// IUnknown methods
	STANDARD_RELEASE;

	static const MCGUID sClassId;

	// IExDataExchanger methods
	virtual void*    MCCOMAPI GetExtensionDataBuffer() { return NULL; }
	virtual int32	 MCCOMAPI GetParamsBufferSize		() const	{ return 0;}

	// I3DExPostRenderer methods
	virtual void	 MCCOMAPI GetBufferNeeds	(RenderFilterNeeds& needs, void* renderer);

	virtual MCCOMErr MCCOMAPI PrepareDraw		(IShRasterLayer*	input,
													 const TMCRect&		bounds,
													 IShChannel*		buffers[],
													 I3DShScene*		scene,
													 I3DShCamera*		renderingCamera,
													 const TBBox2D&		uvBox,
													 const TBBox2D&		productionFrame);

	virtual MCCOMErr MCCOMAPI DrawRect			(const TMCRect&				outputRect,
												 const TChannelDataBucket*	outputTile[],
												 const TBBox2D&				uvBox);

	virtual MCCOMErr MCCOMAPI FinishDraw();

protected:

	TMCCountedPtr<I3DShCamera>				fCamera;
	TBBox2D									fuvBox;
	real									fDeltaU;
	real									fDeltaV;

	TMCCountedPtr<IShRasterLayer>			fColorOffscreen;
	TMCCountedPtr<IShChannel>				fZBuffer;
};

#endif