// KJL - 12-Apr-01 14:48:56
// 
// Asura_PlatformRenderMesh.h

#ifdef ASURA_USE_PRAGMA_ONCE
    #pragma once
#endif

#ifndef __ASURA_PLATFORMRENDERMESH_H__
#define __ASURA_PLATFORMRENDERMESH_H__

#include "Asura_Direct3D.h"

class Asura_Matrix_3x3;
class Asura_Mesh;
class Asura_Vector_3;

#define ASURA_PC_OFFSET_MESH_HIGHLIGHT_ACCORDING_TO_CAM_DIST (1)

class Asura_Direct3D_Mesh
{
	friend class Asura_Surface_Mesh_System;
	private:

		Direct3D_VertexBuffer*	VertexBuffer;
		Direct3D_IndexBuffer*	IndexBuffer;

		unsigned short* IndexList;
		int TotalNumberOfIndices;

		int NumberOfUniqueVertices;
		struct VertexFormat* VertexData;
		int VertexBufferIndex;
		
		bool IsValid;
		struct Asura_Polygon_Packet* PacketList;
		int NumberOfPackets;

		//true if the prelighting has been redone for the subdivided mesh , rather than interpolated
		bool m_bPrelightingBeingCalculated;
		int m_iVertexPrelightingProgress;

		//Depending on the route used to create the VertexBuffer , the vertices may or may not
		//have hade the module position already applied.... keep track of that here. RAR 10/04/03
		bool m_bVerticesOffsetByModulePosition;

		//index of polygon that each vertex came from
		int* m_piPolygonIndeces;

		Asura_Mesh* m_pxMesh;
		Asura_Vector_3 m_xPosition;

	public:

		Asura_Direct3D_Mesh()
		{
			IsValid = false;
			VertexData = 0;
			PacketList = 0;
			IndexList = 0;
			IndexBuffer = 0;
			VertexBuffer = 0;
			m_bPrelightingBeingCalculated = false;
			m_iVertexPrelightingProgress = 0;
			m_piPolygonIndeces = 0;
			m_pxMesh = 0;
		}
		~Asura_Direct3D_Mesh()
		{
			Shutdown();
		}

		void Initialise( Asura_Mesh* pxMesh );

	
		
		void Render( const Asura_Vector_3* pxPos, const Asura_Matrix_3x3* pxOri );
        void RenderHighlight( const Asura_Vector_3* pxPos, const Asura_Matrix_3x3* pxOri, u_int uColour, bool bWireframe, bool bOffset );
		void Asura_Direct3D_Mesh::SimpleRender(void);

		void UpdateLighting(class Asura_Mesh* mesh, class Asura_Vector_3* position, class Asura_Matrix_3x3* orientation);

		bool NotInitialised(void)
		{
			return !IsValid;
		}

		void ReleaseBuffers();

        u_int	GetNumTris()				const	{ return( IsValid ? TotalNumberOfIndices / 3 : 0 ); }
		int		GetNumStrips()				const	{ return NumberOfPackets; }
		int		GetNumUniqueVertices()		const	{ return NumberOfUniqueVertices; }
		Direct3D_VertexBuffer*	GetVertexBuffer()	{ return VertexBuffer; }
		Direct3D_IndexBuffer*	GetIndexBuffer()	{ return IndexBuffer; }
		const Asura_Polygon_Packet* GetStrip(int iStrip) const
		{
			Asura_Assert(0 <= iStrip && iStrip < NumberOfPackets, "Strip number out of range");
			return &PacketList[iStrip];
		}

	private:
		
		void Shutdown(void);
		void MakeVertexBuffer(VertexFormat* vertices, unsigned short* indexList);

		#ifdef ASURA_TOOLS
		void CalculatePrelighting();
		#endif
};

void SetRecalculatePrelightingForSubdividedMeshes(bool bRecalc);


#endif//__ASURA_PLATFORMRENDERMESH_H__
