#pragma once

//camera data structure
#include <Point.h>
#include <Vector.h>
using namespace mathtool;

struct Camera
{
	enum Camera_Type { ORTHOGONAL_CAM, PERSPECTIVE_CAM };

	Camera()
	{
		type = PERSPECTIVE_CAM;
		pos.set(0,0,200);
		lookat.set(0, 0, 0);
		up.set(0, 1, 0);

		viewport[0]=viewport[1]=0;
		viewport[2]=viewport[3]=500;
		near=1;
		far=1000;
	}

	bool init()
	{
		Vector3d view = lookat - pos;
		Vector3d right = up%view;
		if (right.normsqr() == 0)
		{
			cerr << "! Error: Camera view direction is parallel to the up vector." << endl;
			exit(1);
		}
		up = (view%right).normalize();

		//make sure that all positive numbers are positive
		if (near < 0)
		{
			cerr << "! Error: Camera near is negative." << endl;
			exit(1);
		}

		if (far < 0)
		{
			cerr << "! Error: Camera far is negative." << endl;
			exit(1);
		}

		if (viewport[2] < 0 || viewport[3]<0)
		{
			cerr << "! Error: Camera viewport width or height is negative." << endl;
			exit(1);
		}

		return true;
	}

	Camera_Type type;

	Point3d pos;    //position

	Point3d lookat; //look at position

	Vector3d up;    //the up vector of the camera, must not be parallel to the line of sight (lookat-pos)

	int viewport[4]; //this is similar to openGL view port, which has x, y, width, height
	                 //(x,y) specifies the lower left corner of the viewport rectangle, in pixels. The initial value is (0,0).
                     //(w,h) specifies the width and height of the viewport

	float near, far; //near and far view plane
};