/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/graphics/facebuffer.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/filesystem/filesystem.hpp"
#include "sgx/graphics/graphics.hpp"
#include <algorithm>

// TODO: Have a 'reference' face entry which points to existing (global) data

CFaceBuffer::CFaceBuffer(const tMEM_SIZE size)
{
	m_NumFaces = 0;
	m_MaxFaces = size;

	m_pAlphaList = new CFaceEntry[size]; 
}

CFaceBuffer::~CFaceBuffer()
{
	delete []m_pAlphaList;
}

tBOOL 
CFaceBuffer::isFull() const
{
	return TRUE ? m_NumFaces == m_MaxFaces : 0;
}

class CFaceSort {
public:
	CFaceEntry *pFace;
	tREAL32		depth;

	CFaceSort(CFaceEntry *p, const sgxVector3 &from) : pFace(p), depth(p->pAlphaData->getDepth(from)) {}
	CFaceSort() { sgxError("Not a valid ctor"); }
};

class CDepthTest {
public:

	bool operator()(const CFaceSort &s1, const CFaceSort &s2) 
	{
		return s2.depth < s1.depth;
	}
};

void 
CFaceBuffer::flush()
{
//	SGX_ENGINE_STATS("face buffer flush");

	sgxVector<CFaceSort> faces;
	
	tUINT32 face;
	CSGXCamera camera;

	CSGXGraphicsEngine::get()->getCamera(camera);
	
	faces.reserve(m_NumFaces);
	for(face=0;face<m_NumFaces;++face) {
		faces.push_back(CFaceSort(&m_pAlphaList[face], camera.m_vFrom));
	}

	std::sort(faces.begin(), faces.end(), CDepthTest());

	sgxVector<CFaceSort>::const_iterator cit = faces.begin();
	for(;cit!=faces.end();++cit) {
		(*cit).pFace->pAlphaData->draw();
	}	

	m_NumFaces = 0;
}
/*


*/

tREAL32 CSGXAlphaSortValue::getDepth(const sgxVector3 &from) const
{
	return from.getDistanceSquared(transform.pos);
}

void	
CSGXAlphaSortPolygon::draw() const
{
	CSGXGraphicsEngine::get()->setColor(color);
	CSGXGraphicsEngine::get()->drawAlphaPolygonImpl(poly, transform);
}

void	
CSGXAlphaSortSphere::draw() const
{
	CSGXGraphicsEngine::get()->setColor(color);
	CSGXGraphicsEngine::get()->drawPrimitiveSphereImpl(sgxGfxSphere(sphere.m_fRadius), transform);
}

void
CSGXAlphaSortCuboid::draw() const
{
	CSGXGraphicsEngine::get()->setColor(color);
	CSGXGraphicsEngine::get()->drawPrimitiveCuboidImpl(cuboid, transform);
}

void 
CSGXGraphicsEngine::addAlphaSorter(const CSGXAlphaSortData *pData)
{
	m_pAlphaFaces->add(pData);
}
// TODO: These should only be overloaded in the proxy-based CAlphaSorter class
// so as to not clutter the graphics library

void  
CSGXGraphicsEngine::addAlphaSorter(const sgxGfxCuboid &cuboid, const sgxMatrix43 &transform)
{
	void *pMemory = CSGXGraphicsEngine::get()->frameAlloc(sizeof(CSGXAlphaSortCuboid));
	CSGXAlphaSortCuboid *ptr = new (pMemory) CSGXAlphaSortCuboid;

	if (ptr) {
		ptr->color = m_PrimitiveColor.getState();
		ptr->transform = transform;
		ptr->cuboid = cuboid;

		m_pAlphaFaces->add(ptr);
	}
}

void 
CSGXGraphicsEngine::addAlphaSorter(const sgxGfxSphere &sphere, const sgxMatrix43 &transform)
{
	void *pMemory = CSGXGraphicsEngine::get()->frameAlloc(sizeof(CSGXAlphaSortSphere));
	CSGXAlphaSortSphere *ptr = new (pMemory) CSGXAlphaSortSphere;

	if (ptr) {
		ptr->color = m_PrimitiveColor.getState();
		ptr->transform = transform;
		ptr->sphere = sphere;

		m_pAlphaFaces->add(ptr);
	}
}

void 
CSGXGraphicsEngine::addAlphaSorter(const sgxGfxPolygon &poly, const sgxMatrix43 &transform)
{
	void *pMemory = CSGXGraphicsEngine::get()->frameAlloc(sizeof(CSGXAlphaSortPolygon));
	CSGXAlphaSortPolygon *ptr = new (pMemory) CSGXAlphaSortPolygon;

	if (ptr) {
		ptr->color = m_PrimitiveColor.getState();
		ptr->transform = transform;
		ptr->poly = poly;

		m_pAlphaFaces->add(ptr);
	}
}

void 
CFaceBuffer::add(const CSGXAlphaSortData *pData)
{
	sgxAssert(m_NumFaces != m_MaxFaces);

	if (isFull()) {
		CSGXGraphicsEngine::get()->drawAlphaFaces();
	}

	m_pAlphaList[m_NumFaces].pAlphaData = pData;

	++m_NumFaces;
}

void 
CFaceBuffer::draw() const
{
	// BUGWARN: Cast away const-ness, so we can flush the cache easily.
	CFaceBuffer *pNC = const_cast<CFaceBuffer *>(this);
	pNC->flush();
}
