/*	kd:
	
	Here's how to do projections.
	
	*/

class Gc_ProjScreen {
	
	// kd: Screen info
	protected vector2				resolution;
	protected vector2				tan_fov_2;
	protected double				pixel_stretch;
	protected double				aspect_ratio;
	
	// kd: Setup calls which you'll need to call at least once.
	void CacheResolution () {
		CacheCustomResolution((Screen.GetWidth(), Screen.GetHeight()) );
	}
	
	void CacheCustomResolution (vector2 new_resolution) {
		
		// kd: This is for convenience and converting normal <-> screen pos.
		resolution = new_resolution;
		
		// kd: This isn't really necessary but I kinda like it.
		pixel_stretch = level.pixelstretch;
		
		// kd: Get the aspect ratio.
		aspect_ratio = Screen.GetAspectRatio();
		
		// Gc_DefaultCvar cvar_aspect;
		// cvar_aspect.Find("vid_aspect", players [consoleplayer]);
		
		// kd: I dunno if this is what gzd does but it works!
		/*switch(cvar_aspect.GetInt()) {
		default:
			aspect_ratio = Screen.GetAspectRatio();
			break;
		case 1:
			aspect_ratio = 16.0 / 9;
			break;
		case 2:
			aspect_ratio = 16.0 / 10;
			break;
		case 3:
			aspect_ratio = 4.0 / 3;
			break;
		case 4:
			// kd: 5:4 is what gzd calls it but that's not it.
			// aspect_ratio = 5.0 / 4;
			aspect_ratio = 4.0 / 3;
			break;
		case 5:
			aspect_ratio = 17.0 / 10;
			break;
		case 6:
			aspect_ratio = 21.0 / 9;
			break;
		}*/
	}
	
	double AspectRatio () const {
		return aspect_ratio;
	}
	
	// kd: Once you know you got screen info, you can call this whenever your
	// fov changes. Like CacheFov(player.fov) will do.
	void CacheFov (double hor_fov = 90) {
	
		// kd: This holds: aspect ratio = tan(horizontal fov) / tan(ver fov).
		// gzd always uses hor fov, but the fov only holds in 4:3 (in a 4:3 box
		// in your screen centre), so we just extend it.
		tan_fov_2.x	= tan(hor_fov / 2) * aspect_ratio / (4.0 / 3);
		tan_fov_2.y	= tan_fov_2.x / aspect_ratio;
	}
	
	// kd: Also need some view info. Angle is yaw, pitch, roll in world format
	// so positive pitch is up. Call one of the following functions.
	protected vector3				view_ang;
	protected vector3				view_pos;
	
	ui void OrientForRenderOverlay (RenderEvent event) {
		Reorient(
			event.viewpos, (
			event.viewangle,
		-	event.viewpitch,
		-	event.viewroll));
	}
	
	void OrientForPlayer (PlayerInfo player) {
		Reorient(
			player.mo.vec3offset(0, 0, player.viewheight), (
			player.mo.angle,
		-	player.mo.pitch,
		-	player.mo.roll));
	}
	
	virtual void Reorient (vector3 world_view_pos, vector3 world_ang) {
		view_ang = world_ang;
		view_pos = world_view_pos;
	}
	
	// kd: Now we can do projections and such.
	protected double				depth;
	protected vector2				proj_pos;
	
	virtual void	BeginProjection	() {}
	virtual void	ProjectWorldPos	(vector3 world_pos) {}
	virtual vector2	ProjectToScreen	() const { return (0, 0); }
	virtual vector2	ProjectToNormal	() const { return (0, 0); }
	
	bool IsInFront () const {
		return 0 < depth;
	}
	
	bool IsInScreen () const {
		if(	proj_pos.x < -depth || depth < proj_pos.x ||
			proj_pos.y < -depth || depth < proj_pos.y) {
			return false;
		}
		
		return true;
	}
	
	// kd: Deprojection (point on screen, go into the world):
	virtual void	BeginDeprojection		() {}
	
	virtual vector3	DeprojectNormalToDiff	(
	vector2	normal_pos,
	double	depth = 1) const {
		return (0, 0, 0);
	}
	
	virtual vector3	DeprojectScreenToDiff	(
	vector2	screen_pos,
	double	depth = 1) const {
		return (0, 0, 0);
	}
	
	// kd: A normal position is in the -1 <= x, y <= 1 range on your screen.
	// This will be your screen no matter the resolution:
	
	/*
	
	(-1, -1) --	---	---	(0, -1) ---	---	---	---	(1, -1)
	|												|
	|												|
	|												|
	(-1, 0)				(0, 0)					(1, 0)
	|												|
	|												|
	|												|
	(-1, 1)	---	---	---	(0, 1)	---	---	---	---	(1, 1)
	
	*/
	
	// So you call this to get a position to draw into for stuff like
	// Screen.DrawLine, Screen.DrawTexture and such. For DrawTexture, remember
	// to set
	
	// DTA_VIRTUALWIDTHF, resolution.x,
	// DTA_VIRTUALHEIGHTF, resolution.y
	
	// if you're unfamiliar.
	
	vector2 NormalToScreen (vector2 normal_pos) const {
		normal_pos = 0.5 * (normal_pos + (1, 1));
		return (
			normal_pos.x * resolution.x,
			normal_pos.y * resolution.y);
	}
	
	// kd: And this brings a screen position to normal. Make sure the resolution
	// is the same for your cursor.
	
	vector2 ScreenToNormal (vector2 screen_pos) const {
		screen_pos = (
			screen_pos.x / resolution.x,
			screen_pos.y / resolution.y);
		return 2 * screen_pos - (1, 1);
	}
}
