//--------------------------------------------------------
// COMP 238 Programming Assignemt 2 - stochastic ray tracer
// Adrian Ilie
// Inspired from David McAllister's COMP 136 page
// and the "simple" ray tracer from SourceForge
//--------------------------------------------------------

#include <list>
#include <string>
using namespace std;

//--------------------------------------------------------
// 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 by a scalar
	cColor operator/ (double s) const;// divide by a scalar
	cColor operator+ (const cColor& c) const;//add two colors
	cColor operator* (const cColor& c) const;//point multiply 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;
};

//--------------------------------------------------------
// 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;
};

//--------------------------------------------------------
// Surface class
//--------------------------------------------------------
class cSurface
{

public:
	cSurface();
	~cSurface();

	string name;
	cColor Ambient, Diffuse, Specular;
	double SpecularPower, Transparency, Reflectivity, RefractionIndex;

};

//--------------------------------------------------------
// Image class
//--------------------------------------------------------
class cImage
{

public:
	cImage(int width, int height);
	~cImage(){if (data) free(data);};

	int Width;
	int Height;
	string name;
	unsigned char* data;
	cColor getPixel(const int x, const int y);
	void setPixel(const int x, const int y, const cColor& c);
	void SaveToPPM(string filename);
	void LoadFromPPM(string filename);
};

//--------------------------------------------------------
// Shape class (abstract)
//--------------------------------------------------------
class cShape
{
public:
	cShape();
	virtual ~cShape(){};

	string Type;

	cSurface* surface;
	cImage* texture;

	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
	virtual cColor Texture(const cPoint& p) const = 0;//color on texture map at coords (u,v)
	cRay Reflect(const cPoint& p, const cRay& r) const;
	cRay Refract(const cPoint& p, const cRay& r) const;
};

//--------------------------------------------------------
// Sphere class
//--------------------------------------------------------
class cSphere : public cShape
{
public:
	cSphere(double r, double x, double y, double z);
	~cSphere(){};

	cPoint Center;
	double Radius;

	bool Intersect(const cRay& r, cPoint& p) const;
	cVector Normal(const cPoint& p1, const cPoint& p2) const;
	cColor Texture(const cPoint& p) const;
};

//--------------------------------------------------------
// Plane class
//--------------------------------------------------------
class cPlane : public cShape
{
public:
	cPlane(double x, double y, double z, double nx, double ny, double nz);
	~cPlane();

	cPoint Point;
	cVector Normal2;

	bool Intersect(const cRay& r, cPoint& p) const;
	cVector Normal(const cPoint& p1, const cPoint& p2) const;
	cColor Texture(const cPoint& p) const;
};

//--------------------------------------------------------
// Triangle class
//--------------------------------------------------------
class cTriangle : public cShape
{
public:
	cTriangle(double v1x, double v1y, double v1z,
			  double v2x, double v2y, double v2z,
			  double v3x, double v3y, double v3z);
	~cTriangle();

	cPoint A,B,C;
	double uA, uB, uC, vA, vB, vC;//texture coordinates
	cVector nA, nB, nC;//normals
	cVector AB, AC, BC;//sides
	double mAB, mAC, mBC;//sides lengths
	double areaABC;

	bool Intersect(const cRay& r, cPoint& p) const;
	cVector Normal(const cPoint& p1, const cPoint& p2) const;
	cColor Texture(const cPoint& p) const;
};

//--------------------------------------------------------
// Light class
//--------------------------------------------------------
class cLight
{
public:
	cLight(const double x, const double y, const double z, const double r, const double g, const double b);
	~cLight(){};

	cPoint Point;
	cColor Color;
	string Type;
	cColor Contribution(const cRay& r, const cShape* s, const cPoint& p);
};

//--------------------------------------------------------
// Extended Light class
//--------------------------------------------------------
class cExtendedLight : public cLight
{
public:
	cExtendedLight(const double x, const double y, const double z, const double r, const double g, const double b, double rad);
	~cExtendedLight(){};

	double radius;
};

//--------------------------------------------------------
// Scene list typedefs
//--------------------------------------------------------
typedef list<cLight*> cLightList;//lights
typedef list<cLight*>::iterator cLightListIterator;

typedef list<cShape*> cShapeList;//shapes
typedef list<cShape*>::iterator cShapeListIterator;

typedef list<cSurface*> cSurfaceList;//surface properties
typedef list<cSurface*>::iterator cSurfaceListIterator;

typedef list<cImage*> cImageList;//textures
typedef list<cImage*>::iterator cImageListIterator;

//--------------------------------------------------------
// Tracer class
//--------------------------------------------------------
class cTracer
{

public:
	cTracer();
	~cTracer();

	cColor Background;
	cPoint Eye;
	string FileName;
	cShapeList* Shapes;
	cLightList* Lights;
	cSurfaceList* Surfaces;
	cImageList* Images;
	int Width;
	int Height;
	cVector u,v,o,lookAt,upVector;//frustum vectors
	int fov;//angle in degrees
	cImage* img;
	cVector Translate, Rotate, Scale;//for current object

	void Trace();
	void AddShape(cShape* s);
	void AddLight(cLight* l);
	void AddSurface(cSurface* s);
	void AddImage(cImage* i);
	void SetupCamera();
	cSurface* GetLastSurface();
	cSurface* GetSurfaceByName(char* sn);
	cImage* GetLastImage();
	cImage* GetImageByName(char* in);
	cShape* GetLastShape();
	void ApplySurface(cShape* sh, char* sn);
	void ApplyImage(cShape* sh, char* in);
	cColor Cast(const cRay& r, int treeDepth);
	cShape* Query(const cRay& r, cPoint& p);
	cColor Shade(const cRay& r, const cShape* s, const cPoint& p);
	cColor Reflect(const cRay& r, const cShape* s, const cPoint& p, int treedepth);
	cColor Refract(const cRay& r, const cShape* s, const cPoint& p, int treedepth);
	double RaysToLight(const cShape* s, const cPoint& p, const cLight* l);
	void AntiAlias(const double x, const double y, const int c, const int l);
};
