#include "GL/glu.h"
#include "reconstruct_thread.h"

#include "reconmap.h"
#include "glpic.h"

#include "cyclestate.h"
#include "globals.h"

Reconstruct_Thread::Reconstruct_Thread() {
}

Reconstruct_Thread::~Reconstruct_Thread() {
}

Reconstruct_Thread& Reconstruct_Thread::CopyFrom(const Reconstruct_Thread &) {

	cerr << "Error: Reconstruct_Thread::CopyFrom() called" << endl;
	exit(-1);

	return *this;
}

/*
ostream& operator<<(ostream &co, const Reconstruct_Thread &R) {

	return co;
}

istream& operator>>(istream &ci, Reconstruct_Thread &R) {

	return ci;
}
*/

void Reconstruct_Thread::InitThread(GlobalState&) {

}

void Reconstruct_Thread::BeginCycle(CycleState &CS, GlobalState&GS) {

//	cerr << "RC CycleState id: " << CS._id << endl;

//	_y = CS._framebuffer->y() / 4;
	_y = CS._samplebuffer->y() / 4;

	_thread_done = 0;

	//	CS._interpolate_disabled = GS._disabled_flags & DISABLE_INTERPOLATE;

#if 0
	if ((truly_GS->_hw_interp_cutoff_ratio * CS._rays_cast) > 
	    (CS._x_winsize * CS._y_winsize))
	  CS._use_hardware_interpolation = 0; //too many rays: use software!
	else
	  CS._use_hardware_interpolation = 1;
#else //HW interp sucks!
	  CS._use_hardware_interpolation = 0; 
#endif
}

/*
int Reconstruct_Thread::Work(CycleState &CS, GlobalState&) {

	int x, y;

	GLPic &Pic = *(CS._framebuffer);

	int max = Pic.y() - 1;

	if ((max - _y) > 2)
		max = _y + 2;
	else
		_thread_done = 1;

	for (y = _y; y < max; y++)
		for (x=0; x<Pic.x(); x++) {

			int r = rand() | ((rand() & 0x001FF) << 9);
			r = r & (0x000F0F0F);

			Pic.Pixel(x, y, Pic.Pixel(x, y) ^ r);
		}

	_y = max;
	
	return 1;
}
*/

/*
int Reconstruct_Thread::Work(CycleState &CS, GlobalState&) {


	const int LINES = 64;

	int x, y;

	GLPic &Pic = *(CS._pixelbuffer);

	int min = 0;

	if (_y > LINES)
		min = _y - LINES;
	else
		_thread_done = 1;

	int x_size = Pic.x() / 4;

	for (y = _y-1; y >= min; y--) {
		for (x=x_size-1; x >= 0; x--) {

			PixelType p = Pic.Pixel(x, y);

			for (int i=0; i<4; i++)
				for (int j=0; j<4; j++)
					Pic.Pixel(x*4+i, y*4+j, p);
		}
	}

	_y = min;
	
	return 1;
}
*/

int Reconstruct_Thread::Work(CycleState &CS, GlobalState&GS) {

  if (CS._use_hardware_interpolation)
      return 1;

  if (CS._interpolate_disabled)
    {
      //      cerr << "not interpolatin'" << endl;
    }
  else
    {
      //      cerr << "interpolatin'" << endl;
      CS._samplebuffer->Reconstruct2(CS, *(CS._pixelbuffer));
      //      cerr << "finished interpol" << endl;
    }

  _thread_done = 1;
  
  return 1;
}

int Reconstruct_Thread::AnyWorkLeft(CycleState&, GlobalState&) {

	return !_thread_done;
}

void Reconstruct_Thread::EndCycle(CycleState&, GlobalState&) {
}

void Reconstruct_Thread::ShutdownThread() {
}

