//--------------------------------------------------------
// COMP 236 Programming Assignemt 4 - simple ray tracer
// Adrian Ilie
// Inspired from David McAllister's COMP 136 page
// and the "simple" ray tracer from SourceForge
//--------------------------------------------------------

#include <list>
using namespace std;
#include <string>

//--------------------------------------------------------
// Point class
//--------------------------------------------------------
class cPoint
{
public:
	cPoint();
	cPoint(double vX, double vY, double vZ);
	~cPoint(){};

	double X, Y, Z;

	double Distance(const cPoint& p) const;
	bool operator==(cPoint& p) const;
};

//--------------------------------------------------------
// Vector class
//--------------------------------------------------------
class cVector
{
public:
	cVector();
	cVector(const cPoint& p1, const cPoint& p2);
	cVector(double vI, double vJ, double vK);
	~cVector(){};

	double I, J, K;

	cVector Normalize() const; // returns a unit vector of this vector
	double Magnitude() const;
	double DotProd(const cVector& v) const; // returns the dot product of this and rVector
	cVector CrossProd(const cVector& v) const; // returns the cross product of this and rVector
	cVector operator+ (cVector v) const; // returns the sum of two vectors
	cVector operator- (cVector v) const; // returns the difference of two vectors
	cPoint operator+ (cPoint p) const; // returns the sum of a vector and a Point 
	cVector operator* (double s) const; // returns multiplication of a scalar with a vector
	cPoint VectorToPoint() const;	// converts a vector to a point
};

//--------------------------------------------------------
// Color class
//--------------------------------------------------------
class cColor
{
public:
	cColor(){};
	cColor(double vR, double vG, double vB);
	~cColor(){};
	
	double R, G, B;

	cColor operator* (double s) const;// multiply with a scalar
	cColor operator+ (const cColor& c) const;//add two colors
	unsigned char DoubleToByte(double s) const;//scale 0..1 to 0..255

	unsigned char bR() const;
	unsigned char bG() const;
	unsigned char bB() const;
};

//--------------------------------------------------------
// Light class
//--------------------------------------------------------
class cLight
{
public:
	cLight(); //light at (0,0,0)
	cLight(const cPoint& p);
	~cLight(){};

	cPoint Point;
	double Intensity; // 0..1
	cColor Color;
};

//--------------------------------------------------------
// Ray class
//--------------------------------------------------------
class cRay
{
public:
	cRay(){};
	cRay(const cPoint& p, const cVector& d);
	cRay(const cPoint& p1, const cPoint& p2);
	~cRay(){};

	cVector Direction;
	cPoint Origin;
};

//--------------------------------------------------------
// Shape class (abstract)
//--------------------------------------------------------
class cShape
{
public:
	cShape();
	virtual ~cShape(){};

	string Type;
	cColor Color;
	double Diffuse; //0..1
	double Ambient; //0..1
	double SpecularSize;//0..?

	virtual bool Intersect(const cRay& r, cPoint& p) const = 0;
	virtual cVector Normal(const cPoint& p1, const cPoint& p2) const = 0;//normal at p1 towards p2
	cRay Reflect(const cPoint& p, const cRay& r) const;
};

//--------------------------------------------------------
// Sphere class
//--------------------------------------------------------
class cSphere : public cShape
{
public:
	cSphere(){};
	cSphere(cPoint p, double r);
	~cSphere(){};

	cPoint Center;
	double Radius;

	bool Intersect(const cRay& r, cPoint& p) const;
	cVector Normal(const cPoint& p1, const cPoint& p2) const;
};

//--------------------------------------------------------
// Scene list typedefs
//--------------------------------------------------------
typedef list<cPoint> cPointList;
typedef list<cPoint>::iterator cPointListIterator;

typedef list<cLight*> cLightList;
typedef list<cLight*>::iterator cLightListIterator;

typedef list<cShape*> cShapeList;
typedef list<cShape*>::iterator cShapeListIterator;

//--------------------------------------------------------
// Tracer class
//--------------------------------------------------------
class cTracer
{

public:
	cTracer();
	~cTracer();

	cColor Background;
	cColor Ambient;
	cPoint Eye;
	double AmbientIntensity;	
	string FileName;
	cShapeList* Shapes;
	cLightList* Lights;
	int Width;
	int Height;
	cVector u,v,o,look,up;//frustum vectors
	int fov;//angle in degrees

	void Trace();
	void AddShape(cShape* s);
	void AddLight(cLight* l);
	cColor Cast(const cRay& r);
	cShape* Query(const cRay& r, cPoint& p);
	double Shade(const cShape* s, const cPoint& p);
	double ComputeSpecular(const cShape* s, const cPoint& p, const cLight* l);		
};

