//
// Tools_Mode_Carpet.h
//
// Created by Dave Reed - 22/11/2005
// 
// Copyright (C) 2005 Rebellion (Derby), All Rights Reserved.
//

#ifdef ASURA_USE_PRAGMA_ONCE
#pragma once
#endif

#ifdef ASURA_USE_CARPET

#ifndef __TOOLS_CARPET_MESH_H__
#define __TOOLS_CARPET_MESH_H__

class Tools_Carpet_Mesh
{
public:		
	static void Init();
	static void Reset();
	static void Render(bool bShowPolys, int iSelectedStyle);	

	static int  GetNumTriangles() {return s_iTriangleCount;}

	static void	Delete(Asura_Vector_3 xPos, float fRadius);
	
	// Take the lighting from the environment lighting. Currently just takes diffuse RGBs,
	// but could be changed to take the SH coefficients?
	static void CaptureLighting();
	static void CaptureLightingForOneTri(Asura_Carpet_Triangle* pxTri);

	static void CleanMesh();
	static void ClearMesh( void );
	static void ClearMeshIfDontRender( void );

	static Asura_Carpet_Triangle* GetFirstTriangle(void);
	static Asura_Carpet_Triangle* GetNextTriangle(void);
	static bool AddTriangle(Asura_Carpet_Triangle* pxTri);

	static void RenderCarpetTriangle( const class Asura_Carpet_Mesh_Data_Frame& xDataFrame, Asura_Carpet_Triangle* pxTri);


	static void BeginAdjustVertex(Asura_Vector_3& xPos, float fRadius);
	static void EndAdjustVertex(void);
	static void AdjustVertex(Asura_Vector_3& xPos, int iStyle, float fDensity, bool bAbsolute, 
							 bool bRestrictToSelection, float fMaxSlopeAngle, bool bDeleteTris = false, bool bUseTextureFlags = true, bool bUsePolyFlags = true);

	static void BeginFindScenePoly();
	static void EndFindScenePoly();
	static bool FindScenePoly( const Asura_Carpet_Triangle* pxTri, int iVtx, 
							   int* const piModule, int* const piPoly, int* const piVtx, Asura_Vector_3* const pxNormal, u_int* const puPaintedColour );

	static void AddPolyInAdjustSphereCallback(uint32 uPolygonIndex);
	
private:

	static void RemoveTriangle(int iModule, Asura_Carpet_Triangle* pxTri);
	static bool LocateOrAddTriangle(int iModule,int iStyle,Asura_Vector_3** ppvVerts,Asura_Carpet_Triangle** ppxTri, bool bDontAdd=false);

	// List of triangles in each module
	static Asura_LinkList<Asura_Carpet_Triangle>* s_pxModuleLists;
	
	// Number of lists allocated. (Should always equal Asura_Module_System::GetNumberOfModules())
	static int  s_iNumLists;

	// Total number of triangles in the carpet mesh
	static int	s_iTriangleCount;

	// For iterating through the triangle lists with GetFirstTriangle/GetNextTriangle
	static int  s_iListIndex;
	static Asura_ListElt<Asura_Carpet_Triangle>* s_pxElt;

	static float s_fTimer;
	
};



#endif //__TOOLS_CARPET_MESH_H__
#endif // ASURA_USE_CARPET
