#pragma once
#define MLR_MLRSHAPE_HPP

#if !defined(MLR_MLR_HPP)
	#include <MLR\MLR.hpp>
#endif

#if !defined(MLR_MLRPRIMITIVE_HPP)
	#include <MLR\MLRPrimitiveBase.hpp>
#endif

#if !defined(MLR_MLRLIGHT_HPP)
	#include <MLR\MLRLight.hpp>
#endif

#if !defined(MLR_GOSVERTEXPOOL_HPP)
	#include <MLR\GOSVertexPool.hpp>
#endif

namespace MidLevelRenderer {

	class MLRPrimitiveBase;

	class MLRClipper;

	//##########################################################################
	//##########################    MLRShape    ################################
	//##########################################################################
	// This class is a container for MLRPrimitve's. A shape has a Matrix and is
	// attached to the hierarchy

	class MLRShape :
		public Stuff::Plug
	{
		friend class MLRClipper;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Initialization
	//
	public:
		static void
			InitializeClass();
		static void
			TerminateClass();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Constructors/Destructors
	//
	protected:
		MLRShape(
			ClassData *class_data,
			Stuff::MemoryStream *stream,
			int version
		);
		~MLRShape();

	public:
		MLRShape(int, ClassData *class_data=MLRShape::DefaultData);

		static MLRShape*
			Make(
				Stuff::MemoryStream *stream,
				int version
			);

		virtual void
			Save(Stuff::MemoryStream *stream);

	public:
		void Add (MLRPrimitiveBase*);
		MLRPrimitiveBase* Find (int);
		int Find (MLRPrimitiveBase*);

	// use this functions with care --- they are slow
		MLRPrimitiveBase *Remove(MLRPrimitiveBase*);
		MLRPrimitiveBase *Remove(int);
		int Insert(MLRPrimitiveBase*, int);

		bool
			Replace(MLRPrimitiveBase*, MLRPrimitiveBase*);

	// returns the number of primitives in the container
		int GetNum ()
			{ Check_Object(this); return numPrimitives; };

	// returns the number of faces overall in the shape
		int
			GetNumPrimitives();

	// returns the number of drawn triangles in the shape
		int
			GetNumDrawnTriangles();

	// is to call at begin of every frame 
		void	InitializePrimitives(BYTE, const MLRState& master, int=0);

	// clips the geometry and fills the data into the vertex pool
	// the clipping states defines the planes against the shape might have be culled
	//	now done only on primitive level - int	Clip(MLRClippingState, GOSVertexPool*);

	// lights the geometry, uses the worldToShape matrix and an array of lights which
	// affect the shape in this frame and the number of lights in this array
		void	Lighting(const Stuff::LinearMatrix4D&, MLRLight* const*, int nrLights, BYTE=3);

	// casts an ray against the geometry contained in shape
		bool
			CastRay(
				Stuff::Line3D *line,
				Stuff::Normal3D *normal
			);

		void
			CleanMe();

		void
			HurtMe(const Stuff::LinearMatrix4D&, Stuff::Scalar radius);

		void
			HurtMe(const Stuff::LinearMatrix4D&, Stuff::Scalar radius, MLRTexture *tex);

		void
			StepOnMe(
				MLRShape *shape,
				const Stuff::LinearMatrix4D& foot,
				Stuff::Scalar radius,
				MLRTexture *tex
			);

		void
			DrawMyShadow(const Stuff::LinearMatrix4D&, Stuff::UnitVector3D&, Stuff::Scalar, DWORD=0xff800000);

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Class Data Support
	//
	public:
		static ClassData
			*DefaultData;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Reference counting
	//
	public:
		void
			AttachReference()
				{Check_Object(this); ++referenceCount;}
		void
			DetachReference()
				{
					Check_Object(this); Verify(referenceCount > 0);
					if ((--referenceCount) == 0)
					{
						Unregister_Object(this);
						delete this;
					}
				}

		int
			GetReferenceCount()
				{return referenceCount;}

	protected:
		WORD
			referenceCount;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Testing
	//
	public:
		void
			TestInstance() const
				{};

	protected:
//		int
//			FindBackFace(const Stuff::Point3D&, const Stuff::Normal3D&);

//		void
//			Transform(Stuff::Matrix4D*);

//		void
//			Transform();

		Stuff::DynamicArrayOf<MLRPrimitiveBase*>
			allPrimitives;

/*		const Stuff::LinearMatrix4D
			*worldToShape;

		Stuff::Matrix4D
			shapeToClipMatrix;
*/
	private:
		BYTE numPrimitives;
	};

}
