#pragma once
#define MLR_MLR_TERRAIN2_HPP

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

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

namespace MidLevelRenderer {

#define CALCULATEuvONtheFLY
#undef BORDERPIXEL

	//##########################################################################
	//#### MLRIndexedPolyMesh with no color no lighting w/ detail texture  #####
	//##########################################################################


	class MLR_Terrain2:
		public MLR_I_DeT_TMesh
	{
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Initialization
	//
	public:
		static void
			InitializeClass();
		static void
			TerminateClass();

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

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

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

		void
			Save(Stuff::MemoryStream *stream);

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

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

		void
			SetCurrentDepth(BYTE d);

		BYTE
			GetCurrentDepth()
				{ Check_Object(this); return currentDepth; }

		void
			SetDepthData(BYTE md, BYTE mad)
				{ Check_Object(this); maxDepth = md; maxAllDepth = mad; }

		void
			SetTileData(BYTE tx, BYTE tz)
				{ Check_Object(this); tileX = tx; tileZ = tz; }

		void
			GetTileData(BYTE& tx, BYTE& tz)
				{ Check_Object(this); tx = tileX; tz = tileZ; }

		void
			SetFrame(int res, Scalar xMin, Scalar zMin, Scalar xMax, Scalar zMax)
				{ 
					Check_Object(this); 
					frame[res][0] = xMin; 
					frame[res][1] = zMin; 
					frame[res][2] = xMax; 
					frame[res][3] = zMax;
					OneOverX[res] = 1.0f/(frame[res][2] - frame[res][0]);
					OneOverZ[res] = 1.0f/(frame[res][3] - frame[res][1]);
				}

		Scalar
			GetFrame(int res, int p)
				{ Check_Object(this); return frame[res][p]; }

#ifdef BORDERPIXEL
		void
			SetBorderPixel(Stuff::Scalar bp)
				{ Check_Object(this); borderPixelFun = bp; }
#endif

		void
			CalculateUVs();
		void
			CalculateUV(int i, Scalar &u, Scalar &v);

		void
			SetLevelTexture(int lev, int handle);

		int
			GetLevelTexture(int lev)
				{ Check_Object(this); Verify(lev>=0 && lev<8); return textures[lev]; }

		void
			LightMapLighting(MLRLight*);

		enum TerrainType {
			UnderWater = 1
		};

		bool IsTerrain(TerrainType tt)
			{ Check_Object(this); return (tt & type)>0; }

		void SetTerrain(TerrainType tt)
			{ Check_Object(this); type |= (BYTE)tt; }

		virtual MLRPrimitiveBase*
			StepOnMe(const Stuff::LinearMatrix4D& foot, Stuff::Scalar radius, MLRTexture *tex);

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

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

	protected:
		int textures[8];
		Scalar frame[8][4];
		Scalar OneOverX[8];
		Scalar OneOverZ[8];

		BYTE tileX, tileZ;
		BYTE currentDepth, maxDepth, maxAllDepth;

		BYTE type;

#ifdef BORDERPIXEL
		Stuff::Scalar borderPixelFun;
#endif

#ifndef CALCULATEuvONtheFLY
		Stuff::DynamicArrayOf<Vector2DScalar>
			dynamicUVs;
#endif
		static Stuff::DynamicArrayOf<Vector2DScalar> *detailTexCoords;	// Base address of texture coordinate list 
	};
}
