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

  	WireRenderer.h

	Copyright (c)1990 - 1999 MetaCreations. All rights reserved.

	Author:	Jerome Bignon

	Date:	19/07/99

	Revision History

	when		who			what & why
	=========== ===========	=============================================================
	19/07/99	Jerome		Creation

************************************************************************************************************/
#if CP_PRAGMA_ONCE
#pragma once
#endif

#include "BasicFinalRenderer.h"
#include "WireRendererDef.h"

#include "MCColorRGB.h"
#include "Transforms.h"
#include "I3DShEnvironment.h"
#include "I3DShInstance.h"
#include "I3DShGroup.h"
#include "I3DShCamera.h"
#include "RealAffine3.h"
#include "I3DShTreeElement.h"
#include "IShRasterLayer.h"

// define the WireRenderer CLSID

extern const MCGUID CLSID_WireRenderer;

// This struct will be used as a buffer where we can draw all our image
struct FlatBuffer
{
	uint8* red;
	uint8* green;
	uint8* blue;
};



struct InstanceAndTransform			// This structure will be usefull during the rendering
{
	I3DShInstance*	fInstance;
	TTransform3D	fT;
};

class WireRenderer : public TBasicFinalRenderer, public I3DExTileRenderer
{
	public:

		WireRenderer();
		// IUnknown methods
		virtual MCErr    MCCOMAPI QueryInterface		(const MCIID& riid, void** ppvObj)	{ return TBasicFinalRenderer::QueryInterface(riid,ppvObj); }
		virtual uint32   MCCOMAPI AddRef				()									{ return TBasicFinalRenderer::AddRef(); }
		STANDARD_RELEASE;

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

		// TBasicFinalRenderer methods
		virtual MCCOMErr MCCOMAPI SetTreeTop			(I3DShGroup* treeTop);
		virtual MCCOMErr MCCOMAPI SetCamera				(I3DShCamera* camera);
		virtual MCCOMErr MCCOMAPI SetEnvironment		(I3DShEnvironment* environment);
		virtual MCCOMErr MCCOMAPI SetAmbientLight		(const TMCColorRGB& ambiantColor);
		virtual MCCOMErr MCCOMAPI Dehydrate				(int16 level);
		virtual uint32   MCCOMAPI GetRenderingTime		();
		virtual MCCOMErr MCCOMAPI SetFieldRenderingData	(int32 useFieldRendering,int16 firstFrame);
		virtual void	 MCCOMAPI GetRenderStatistics	(I3DRenderStatistics** renderstats);
		virtual MCCOMErr MCCOMAPI PrepareDraw			(const TMCPoint& size,const TBBox2D& uvBox,const TBBox2D& productionFrame, boolean (*callback) (int16 vv, void* priv),void* priv);
		virtual MCCOMErr MCCOMAPI FinishDraw			();

		// called by the shell (renderingmodule) to tell the renderer to abort
		// this allow the renderer to cleanly abort and to release everything
		virtual MCCOMErr MCCOMAPI Abort					()									{ return MC_S_OK; }
		virtual void	 MCCOMAPI GetTileRenderer		(I3DExTileRenderer** tileRenderer);

		// I3DExTileRenderer
		virtual void	MCCOMAPI Init					(const TMCRect& rect,const TBBox2D& uvBox);
		virtual boolean MCCOMAPI GetNextSubTile			(TMCRect& rect);
		virtual void	MCCOMAPI RenderSubTile			(RTData& pixels);
		virtual void	MCCOMAPI FinishRender			(const RTData& pixels);

	protected:

		// Methods of the wire renderer.
		void	DrawLine	(int16 x1,int16 y1,int16 x2,int16 y2, TMCColorRGBA* color);
		void	Rasterize	(TFacet3D facet, TTransform3D L2C, I3DShInstance *CurrentInstance );
		void	RenderScene	(const TMCRect* area);
		void	BlocCopy	(const RTData* pixels);
		void	PixelSet	(int32 x, int32 y, TMCColorRGBA* color);
		
		// Datas of the renderer
		TMCCountedPtr<I3DShTreeElement>		fTree;
		TMCCountedPtr<I3DShGroup>			fTreeTop;
		const TRenderableAndTfmArray*		fInstances;
			
		TMCCountedPtr<IShRasterLayer>		fImage;			// Image to render
		TMCCountedPtr<I3DShCamera>			fCamera;		// Camera used for the rendering
		TMCCountedPtr<I3DShEnvironment>		fBackground;	// Environment image used for the rendering
		TMCCountedPtr<I3DShEnvironment>		fBackdrop;		// Screen Background image used for the rendering
		TMCCountedPtr<I3DShEnvironment>		fAtmosphere;	// Atmosphere effect used for the rendering

		TMCColorRGB			fAmbientColor;	// Ambient color for the rendering
		TVector2 			fZoom;			// Zoom factor Univers / OffScreen
		TMCRect				fImageArea;		// The full image obtained by the rendering process
		TMCRect				fDrawingArea;	// An area, used by the renderer, which can be a tile of the fImageArea
		int16				fMode;			// Rendering mode, i.e. Wireframe, Ray Tracer, ...
		int16				fImageWidth;	// Dimensions of the image to be drawn
		int16				fImageHeight;	
		int16				fDepth;			// Number of bits to code the color
		TVector2			fOffscrOffset;	// Indicates a distance between the offscreen and the bitmapArea
		TBBox2D				fUVMinMax;		// Dimension of backdrop
		FlatBuffer			fBuffer;

		TMCRect				fCurrentTileRect;	// rectangle of the current tile
		TBBox2D				fCurrentUVBox;		// uv box of the current tile
};
