//////////////////////////////////////////////////////////////////////
//
//                             Pixie
//
// Copyright © 1999 - 2003, Okan Arikan
//
// Contact: okan@cs.berkeley.edu
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//
//  File				:	cacheview.cpp
//  Classes				:	CCacheView
//  Description			:	The irradiance cache viewer
//
////////////////////////////////////////////////////////////////////////
#include "cacheView.h"
#include "../ri/irradiance.h"

static	int		rootNode;

///////////////////////////////////////////////////////////////////////
// Function				:	readNode
// Description			:	Read an irradiance cache node
// Return Value			:	-
// Comments				:
// Date last edited		:	11/29/3003
static	void	readNode(FILE *in) {
	vector							center;
	float							side;
	int								numSamples;
	CIrradianceCache::CCacheSample	cSample;
	CIrradianceCache::CCacheNode	*c[8];

	fread(center,		sizeof(vector),1,in);
	fread(&side,		sizeof(float),1,in);
	fread(&numSamples,	sizeof(int),1,in);

	if (rootNode) {
		rootNode	=	FALSE;
		
		glScalef(1 / side,1 / side,1 / side);
		glTranslatef(-center[COMP_X],-center[COMP_Y],-center[COMP_Z]);
		
		glColor3f(1,1,1);
		glPointSize(5);
		//glBegin(GL_POINTS);
		glBegin(GL_LINES);
	}

	for(;numSamples>0;numSamples--) {
		fread(&cSample,sizeof(CIrradianceCache::CCacheSample),1,in);

		//glColor3f(1-cSample.coverage,1-cSample.coverage,1-cSample.coverage);
		//glColor3f(1-cSample.R,1-cSample.R,1-cSample.R);
		//glColor3f(cSample.coverage,cSample.coverage,cSample.coverage);
		float col	=	cSample.dP / (float) 0.5;
		//col	=	cSample.coverage;
		//glColor3f(col,col,col);

		//glColor3f(1-cSample.coverage,1-cSample.coverage,1-cSample.coverage);
		glVertex3fv(cSample.P);
		glVertex3f(cSample.P[0] + cSample.gP[0]*(float) 0.01,cSample.P[1] + cSample.gP[1]*(float) 0.01,cSample.P[2] + cSample.gP[2]*(float) 0.01);
	}

	fread(c,8,sizeof(CIrradianceCache::CCacheNode *),in);
	if (c[0] != NULL)	{	readNode(in);	}
	if (c[1] != NULL)	{	readNode(in);	}
	if (c[2] != NULL)	{	readNode(in);	}
	if (c[3] != NULL)	{	readNode(in);	}
	if (c[4] != NULL)	{	readNode(in);	}
	if (c[5] != NULL)	{	readNode(in);	}
	if (c[6] != NULL)	{	readNode(in);	}
	if (c[7] != NULL)	{	readNode(in);	}
}

///////////////////////////////////////////////////////////////////////
// Class				:	CCacheView
// Method				:	CCacheView
// Description			:	Ctor
// Return Value			:
// Comments				:
// Date last edited		:	11/29/2003
CCacheView::CCacheView(FILE *in) : CInterface() {
	int		maxDepth;
	matrix	tmp;

	fread(&maxDepth,	sizeof(int),1,in);
	fread(tmp,			sizeof(matrix),1,in);
	fread(tmp,			sizeof(matrix),1,in);

	rootNode	=	TRUE;

	cacheList	=	glGenLists(1);
	
	glNewList(cacheList,GL_COMPILE);

	readNode(in);
	glEnd();
	glEndList();
	glEnable(GL_DEPTH_TEST);
}

///////////////////////////////////////////////////////////////////////
// Class				:	CCacheView
// Method				:	~CCacheView
// Description			:	Dtor
// Return Value			:
// Comments				:
// Date last edited		:	11/29/2003
CCacheView::~CCacheView() {
	glDeleteLists(cacheList,1);
}


///////////////////////////////////////////////////////////////////////
// Class				:	CCacheView
// Method				:	draw
// Description			:	Do the actual drawing
// Return Value			:	-
// Comments				:
// Date last edited		:	1/14/2002
void		CCacheView::drawScene()	{
	glPushMatrix();

	glCallList(cacheList);

	glPopMatrix();
}

