// Face.cpp : implementation of the CFace class
//

#include "stdafx.h"

#include "Rock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRock construction/destruction

CRock::CRock()
{
	// TODO: add one-time construction code here

}

CRock::~CRock()
{
	//delete the list of faces
	int count = m_faces.GetCount();
	while (count > 0){
		CFace* obj = m_faces.RemoveHead();
		delete(obj);
		count--;
	}
}


/////////////////////////////////////////////////////////////////////////////
// CRock serialization

void CRock::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

void CRock::AddFace(CFace * face)
{
	m_faces.AddHead(face);
}

//draw ourselves
void CRock::Draw()
{
	//sequnce through our faces and draw each one
	//as a polygon
	POSITION pos = m_faces.GetHeadPosition();
	while (pos != NULL)
	{
		CFace* face = m_faces.GetAt(pos);
		//draw each point
		glBegin(GL_POLYGON);
			POSITION posf = face->m_points.GetHeadPosition();
			int index = 0;
			while (posf != NULL)
			{
				C3DPoint* point = face->m_points.GetAt(posf);
				//get the texture co-ordinate right
				switch (index)
				{
					case 0:
						glTexCoord2i(0, 0);
						break;
					case 1:
						glTexCoord2i(1, 0);
						break;
					case 2:
						glTexCoord2i(1, 1);
						break;
					case 3:
						glTexCoord2i(0, 1);
						break;
					default:
						glTexCoord2i(0, 0);
				}
				
				glVertex3f(point->m_x, point->m_y, point->m_z);
				face->m_points.GetNext(posf);
				index ++;
			}

		glEnd();
		m_faces.GetNext(pos);
	}
}

