#pragma once
#define MLR_MLR_I_PMESH_HPP

#include <MLR\MLR.hpp>
#include <MLR\MLRIndexedPrimitiveBase.hpp>


namespace MidLevelRenderer {

	class MLR_I_TMesh;

	//##########################################################################
	//#### MLRIndexedPolyMesh with no color no lighting one texture layer  #####
	//##########################################################################


	class MLR_I_PMesh:
		public MLRIndexedPrimitiveBase
	{
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Initialization
	//
	public:
		static void
			InitializeClass();
		static void
			TerminateClass();

		static HGOSHEAP
			PrimitiveHeap;

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

	public:
		MLR_I_PMesh(ClassData *class_data=MLR_I_PMesh::DefaultData);

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

		void
			Save(Stuff::MemoryStream *stream);

	public:
		bool
			Copy(MLR_I_TMesh*);

		virtual	void	InitializeDrawPrimitive(BYTE, int=0);

		virtual void	SetSubprimitiveLengths(const BYTE *, int, bool=true);
		virtual void	GetSubprimitiveLengths(const BYTE **, int*);

		void	FindFacePlanes();

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

		virtual int
			GetNumPrimitives()
				{ Check_Object(this); return lengths.GetLength(); }

		virtual int
			GetSubprimitiveLength(int i) const
				{
					Check_Object(this);
					return lengths.GetLength();
				}


		const Stuff::Plane *GetPolygonPlane(int i)
			{
				Check_Object(this);
				Verify(i<facePlanes.GetLength());

				return &facePlanes[i];
			}

		virtual void	Lighting(MLRLight* const*, int nrLights, BYTE=3);
		virtual DWORD	CenterLighting(MLRLight* const*, int nrLights, BYTE=3);

		virtual void LightMapLighting(MLRLight*);

		virtual void
			PaintMe(const ColorType *paintMe);

		virtual int	TransformAndClip(Stuff::Matrix4D*, MLRClippingState, GOSVertexPool*, FogData*, DWORD=0xffffffff, bool=false);

		bool
			CastRay(
				Stuff::Line3D *line,
				Stuff::Normal3D *normal
			);

		virtual void
			TransformNoClip(Stuff::Matrix4D*, GOSVertexPool*, FogData*, DWORD=0xffffffff, bool=false);

	//	Initializes the visibility test list
		void
			ResetTestList();

	//	find which vertices are visible which not - returns nr of visible vertices
	//	the result is stored in the visibleIndexedVertices array
		int
			FindVisibleVertices();

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

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

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

		static Stuff::DynamicArrayOf<BYTE>	*testList;
		
		Stuff::DynamicArrayOf<Stuff::Plane> facePlanes;
		
		Stuff::ReadOnlyArrayOf<BYTE>	lengths;	// List of strip lengths 
	};

	#define ICO_X 0.525731112119133606f
	#define ICO_Z 0.850650808352039932f

	extern float vdata[12][3];
	extern unsigned int tindices [20][3];

	extern long triDrawn;

	void
		subdivide (
			Stuff::Point3D *coords,
			Stuff::Point3D& v1,
			Stuff::Point3D& v2,
			Stuff::Point3D& v3,
			long depth,
			long tri2draw, 
			float rad = 1.0f
		);

	MLR_I_PMesh*
		CreateIndexedCube_NoColor_NoLit(Stuff::Scalar, MLRState*);
	MLRShape*
		CreateIndexedIcosahedron_NoColor_NoLit(
			IcoInfo&,
			MLRState*
		);

}
