#include "stdafx.h"
#include "vector3d.h"
#include "vector3dgroup.h"
#include "face.h"
#include "facegroup.h"
#include "mesh.h"

#define IMAXFACE 9
#define IMAXVERT 16
#define IPOINTSINFACE 4
#define M 4
#define N 4

void initializer(/*out*/Mesh **ppInMesh,/*in*/float fM,/*in*/float fN) {

	float fX = 0;
	float fY = 0;
	float fZ = 0;
	int j = 0;
	int i = 0;

	Mesh **ppMesh;
	Face *pFace;


	ppMesh = ppInMesh;

	// instantiate mesh class

	(*ppMesh) = new Mesh((fN*fM), ((fN-1)*(fM-1)));


	//add points to control mesh

	  for(i = 0; i < fM; i++) {
		for(j = 0; j < fN; j++) {
			(*ppMesh)->getVector3DGroupPtr()->addElement( Vector3D( fX, fY, fZ ) );
			fZ += (1/(fN - 1));//*/.3333333333;
		}
		fZ = 0;
		fX += (1/(fM - 1));//*/.3333333333;
	}

	//Create the face group for the Mesh	

	FaceGroup *pFaceGroup = (*ppMesh)->getFaceGroupPtr();

	//IT'S DYNAMIC BABY!!!!!  This code will generate faces for any rectangular 'checkerboard' Mesh
	for(i = 0; i < (fM - 1); i++) {
		for(j = 0; j < (fN - 1); j++) {
			pFace = new Face( IPOINTSINFACE );
			pFace->addVertexIndex( (i*fN + j) );
			pFace->addVertexIndex( ((i + 1)*fN + j) );
			pFace->addVertexIndex( ((i + 1)*fN + j + 1) );
			pFace->addVertexIndex( (i*fN + j + 1) );
			pFaceGroup->addElement( *pFace );
			delete pFace;
		}
	}
}