/****************************************************************************************

	Flat.cpp

	Copyright (c)1990 - 1999 MetaCreations. All rights reserved.


	Author:	Jerome Bignon

	Date:	05/05/99

	Revision History

	when		who			what & why
	==========	==========	======================================================
	08/20/99	cph			Put in Extension3DInit() and Extension3DCleanup() methods
	05/07/99	Jerome		Solved problems with getUVSpace methods
	05/05/99	Jerome		Creation

*****************************************************************************************/

#include "math.h"
#include "PublicUtilities.h"
#include "Flat.h"

const MCGUID CLSID_Flat(R_CLSID_Flat);


void Extension3DInit(IMCUnknown* utilities)
{
	// Perform your dll initialization here
}

void Extension3DCleanup()
{
	// Perform any nec clean-up here
}

TBasicUnknown* MakeCOMObject(const MCCLSID& classId)	// This method instanciate
{														// the object Flat
	TBasicUnknown* res = NULL;

	if (classId == CLSID_Flat) res = new Flat;

	return res;
}
         
// Constructor of the C++ Object :
Flat::Flat()
{
	// Nothing to do for this object
}
  
// I3DExGeometricPrimitive methods
// -- Geometry calls

MCCOMErr Flat::GetNbrLOD(int16& nbrLod)
{
	nbrLod = 1;
	return MC_S_OK;
}

  
MCCOMErr Flat::GetFacetMesh (uint32 lodIndex, FacetMesh** outMesh)
{
	FacetMeshAccumulator acc;

	real		size = 10.0f;
	TFacet3D	aF;
	TVector3	normal;

	normal[0]=0.0f;
	normal[1]=0.0f;
	normal[2]=1.0f;

	aF.fUVSpace=0;
	aF.fVertices[0].fVertex[0]=-size;
	aF.fVertices[0].fVertex[1]=-size;
	aF.fVertices[0].fVertex[2]=0.0f;
	aF.fVertices[0].fUV[0]=0.0f;
	aF.fVertices[0].fUV[1]=0.0f;
	aF.fVertices[0].fNormal=normal;

	aF.fVertices[1].fVertex[0]=size;
	aF.fVertices[1].fVertex[1]=-size;
	aF.fVertices[1].fVertex[2]=0.0f;
	aF.fVertices[1].fUV[0]=1.0f;
	aF.fVertices[1].fUV[1]=0.0f;
	aF.fVertices[1].fNormal=normal;

	aF.fVertices[2].fVertex[0]=-size;
	aF.fVertices[2].fVertex[1]=size;
	aF.fVertices[2].fVertex[2]=0.0f;
	aF.fVertices[2].fUV[0]=0.0f;
	aF.fVertices[2].fUV[1]=1.0f;
	aF.fVertices[2].fNormal=normal;

	acc.AccumulateFacet(&aF);

	aF.fVertices[0].fVertex[0]=size;
	aF.fVertices[0].fVertex[1]=size;
	aF.fVertices[0].fVertex[2]=0.0f;
	aF.fVertices[0].fUV[0]=1.0f;
	aF.fVertices[0].fUV[1]=1.0f;

	aF.fVertices[1].fVertex[0]=-size;
	aF.fVertices[1].fVertex[1]=size;
	aF.fVertices[1].fVertex[2]=0.0f;
	aF.fVertices[1].fUV[0]=0.0f;
	aF.fVertices[1].fUV[1]=1.0f;

	aF.fVertices[2].fVertex[0]=size;
	aF.fVertices[2].fVertex[1]=-size;
	aF.fVertices[2].fVertex[2]=0.0f;
	aF.fVertices[2].fUV[0]=1.0f;
	aF.fVertices[2].fUV[1]=0.0f;

	acc.AccumulateFacet(&aF);

	acc.MakeFacetMesh(outMesh);
	return MC_S_OK;
}

// Give the boundary Box
void Flat::GetBoundingBox(TBBox3D& bbox)
{
	bbox.fMin[0]=-10.0f;
	bbox.fMax[0]=10.0f;
	bbox.fMin[1]=-10.0f;
	bbox.fMax[1]=10.0f;
	bbox.fMin[2]=0.0f;
	bbox.fMax[2]=0.0f;
}
  
// -- Shading calls
uint32 Flat::GetUVSpaceCount()
{
	return 1; // the flat is describe with only 1 UV-Space
}

MCCOMErr Flat::GetUVSpaceRDS5(uint32 uvSpaceID, UVSpaceInfoRDS5* uvSpaceInfo)
// To be implemented only if your primitive existed in RDS 5 or earlier, so that UV spaces can be mapped properly to the new 0..1 boundaries
{
	real size;
	if (uvSpaceID == 0)
	{ 
		size = 10.0f;
		uvSpaceInfo->fMin[0] = 0.0f; // u coordinate goes from 0 to 10*2
		uvSpaceInfo->fMax[0] = size*2.0f;
		uvSpaceInfo->fMin[1] = 0.0f; // v coordinate goes from 0 to 10*2
		uvSpaceInfo->fMax[1] = size*2.0f;
		uvSpaceInfo->fWraparound[0] = false;  // No Wrap around
		uvSpaceInfo->fWraparound[1] = false;
	}
	return MC_S_OK;
}

MCCOMErr Flat::GetUVSpace(uint32 uvSpaceID, UVSpaceInfo* uvSpaceInfo)
// Evolve3D GetUVSpace. It's already normalized.
{
	if (uvSpaceID == 0)
	{ 
		uvSpaceInfo->fWraparound[0] = false;  // No Wrap around
		uvSpaceInfo->fWraparound[1] = false;
	}
	return MC_S_OK;
}