#include "stdAfx.h"
#include "face.h"
#include "facegroup.h"
#include "vector3d.h"
#include "vector3dgroup.h"
#include "mesh.h"
#include <math.h>

#define LEFT 0
#define RIGHT 1

void ObjectToScreen(Mesh *pMesh,/*out*/Vector3DGroup *pScreen, Vector3D vView, CRect screenRect, int side )
{
	float distZ  = 0;
	float fX, fY, fZ;
	float fRoll = 0;
	float fTemp = 0;
	float fFocalLength = 5;
	float viewPortX = 5;
	float viewPortY = 5;


	Vector3DGroup *pVecGroup;
	Vector3D vToOrigin, vTemp;

	/*********************Object to Object Origin*************************************/
	// get the group of vectors from the mesh

	pVecGroup   = pMesh->getVector3DGroupPtr();

	// calculate the vector from the geometric center to origin

	vToOrigin = pMesh->getGeometricCenter();
	vToOrigin.multiply(-1);

	// go through all the points in the mesh 

	for( int i = 0; i < pVecGroup->getNumElements(); i++ ) 
	{
		vTemp = pVecGroup->getElementAt(i);
		vTemp.add( vToOrigin );
		pScreen->setElementAt(i, vTemp);
	}	


	/*********************Centered Object transformation/translation******************************/
	for (i = 0; i < pVecGroup->getNumElements(); i++ ) 
	{
		vTemp = pScreen->getElementAt(i);

		fX = vTemp.fX * cos( fRoll ) + vTemp.fY * sin( fRoll );
		fY = vTemp.fX * sin( fRoll ) - vTemp.fY * cos( fRoll );

		fTemp = fX;

		fX = fTemp * cos( vView.fY ) + vTemp.fZ * sin( vView.fY );
		fZ = fTemp * sin( vView.fY ) - vTemp.fZ * cos( vView.fY );

		fTemp = fZ;

		fZ = fTemp * cos( vView.fX ) + fY * sin( vView.fX );
		fY = fTemp * sin( vView.fX ) - fY * cos( vView.fX );

		fTemp = fZ - vView.fZ;
		pScreen->setElementAt(i, fX*fFocalLength/fTemp, fY*fFocalLength/fTemp, fFocalLength);


	}

	/*********************Viewport to Screen*******************************************/
	
	//Calculate the screen multiple

	int screenWidth  = screenRect.right;
	int screenHeight = screenRect.bottom;
	int screenX = 0;
	int screenY = 0;
	float xMultiple = 0;
	float yMultiple = 0;
	float modifier  = 0;

	xMultiple = (screenWidth / 2)  / viewPortX;
	yMultiple = (screenHeight / 2) / viewPortY;
	
	if(xMultiple > yMultiple) {
		modifier = yMultiple;
	}
	else {
		modifier = xMultiple;
	}
	
	for(i = 0; i < pScreen->getNumElements(); i++) {
		vTemp = pScreen->getElementAt(i);
		screenX = (screenWidth / 4) + ((screenWidth * side) / 2)  + modifier*vTemp.fX;
		screenY = (screenHeight / 4)  - modifier*vTemp.fY;
		pScreen->setElementAt(i, screenX, screenY, 0);
	}
}


void DrawObject(CDC *pDC, Mesh *inMesh, Vector3DGroup *vScreenVectors)
{
	int iPointsInFace = 0;
	Vector3D tempVertex;
	FaceGroup *pFaceGroup;
	Face *pTempFace;
	CPen bluePen(PS_SOLID,1,RGB(0,0,255));
	CPen redPen(PS_SOLID, 1,RGB(255,0,0));
	CPen greenPen(PS_SOLID, 1,RGB(0,255,0));

	//Get the old pen and save it.
	CPen* pPen = pDC->GetCurrentPen();
	CPen* oldPen = pPen;
    //Now change the pen color to BLUE
	pPen = pDC->SelectObject(&bluePen);

	//Generate a pointsList for drawing the points in order(creates a serial index for
	//getting the points by number)	

	pFaceGroup = inMesh->getFaceGroupPtr();

	// loop through all the faces

	for(int i = 0; i < pFaceGroup->getNumElements(); i++) {

		// get a face

		pTempFace = pFaceGroup->getElementAtPtr(i);
		

		// move to first point

		tempVertex = vScreenVectors->getElementAt(pTempFace->getVertexIndexAt(0));
		pDC->MoveTo(tempVertex.fX, tempVertex.fY);
		
		// loop through verteces

		iPointsInFace = pTempFace->getNumVertex();
		
		for(int j = 1; j < iPointsInFace; j++) {
			tempVertex = vScreenVectors->getElementAt(pTempFace->getVertexIndexAt(j));
			pDC->LineTo(tempVertex.fX, tempVertex.fY);
			//trace( "%1.2f %1.2f", tempVertex.fX, tempVertex.fZ);
		}
		tempVertex = vScreenVectors->getElementAt(pTempFace->getVertexIndexAt(0));
		pDC->LineTo(tempVertex.fX, tempVertex.fY);
	}

	pDC->SelectObject( oldPen );
}

// EOF




