#pragma once
#define MLR_MLR_I_TMESH_HPP

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

namespace MidLevelRenderer {

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


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

		static HGOSHEAP
			PrimitiveHeap;

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

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

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

		void
			Save(Stuff::MemoryStream *stream);

	public:
		virtual	void	InitializeDrawPrimitive(BYTE, int=0);

		virtual int
			GetNumPrimitives()
				{ Check_Object(this); return numOfTriangles; }

		virtual void
			SetSubprimitiveLengths(
				const BYTE *length_array,
				int subprimitive_count,
				bool setFaces=true
				)
				{
					Check_Object(this);
					numOfTriangles = subprimitive_count;
					if(setFaces==true)
						facePlanes.SetLength(numOfTriangles);
				}

		virtual void
			GetSubprimitiveLengths(const BYTE **length_array, int *subprimitive_count)
				{
					Check_Object(this);
					*length_array = NULL;
					*subprimitive_count = numOfTriangles;
				}

		virtual int
			GetSubprimitiveLength(int i) const
				{
					Check_Object(this);
					return numOfTriangles;
				}


		virtual void	FindFacePlanes();

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

		const Stuff::Plane *GetTrianglePlane(int i)
			{
				Check_Object(this);
				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
			);

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

		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();

		bool
			Copy(MLR_I_PMesh*);

		virtual MLRPrimitiveBase*
			HurtMe(const Stuff::LinearMatrix4D&, Stuff::Scalar radius, MLRTexture*);

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

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

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

	protected:
		int numOfTriangles;

		static Stuff::DynamicArrayOf<BYTE>	*testList;
		
		Stuff::DynamicArrayOf<Stuff::Plane> facePlanes;
		
	};

	#define ICO_X 0.525731112119133606f
	#define ICO_Z 0.850650808352039932f

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

	extern long triDrawn;

	MLR_I_TMesh*
		CreateIndexedTriCube_NoColor_NoLit(Stuff::Scalar, MLRState*);
	MLRShape*
		CreateIndexedTriIcosahedron_NoColor_NoLit(
			IcoInfo&,
			MLRState*
		);

}
