
#include "walk_nav.h"

#include "vec4.h"
#include "matrix.h"
#include "jlmouse.h"

#include "globals.h"
#include "globalstate.h"

Walk_Nav::Walk_Nav() {
}

Walk_Nav::~Walk_Nav() {
}

Walk_Nav& Walk_Nav::CopyFrom(const Walk_Nav &) {

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

	return *this;
}

/*
ostream& operator<<(ostream &co, const Walk_Nav &W) {

	return co;
}

istream& operator>>(istream &ci, Walk_Nav &W) {

	return ci;
}
*/

void Walk_Nav::Navigate(CycleState &CS, JLMouse &M) {

	float x, y;

	M.GetFloatXYGivenViewport(x, y, truly_GS->_x_winsize, truly_GS->_y_winsize);

	Vec4 R = truly_GS->_user.GetCamera().R(); // eyepoint
	Vec4 D = truly_GS->_user.GetCamera().D(); // viewdir

#if 0
	// use mouse x for Y rotation of view direction
	JLmatrix Ry;
	Ry.SetToIdentity();
	Ry.YRotate(-x*0.1);
	D *= Ry;

	// use mouse y for movement along view direction
	Vec4FastAddScale(R, R, D, y*0.5);
#else
	{
	// map mouse x to turning Left/Right in body coords
	// map mouse y to forward/backward motion in body coords
	Vec4 U = truly_GS->_user.GetCamera().U(); // up dir

	// "Right" is Viewdir x Up
	Vec4 Right;
	Vec4FastCross3 ( Right, D, U ); Right.MakeUnit();

	// update view direction D by adding component of Right
	float framerate = truly_GS->_frame_rate;
	if (framerate == 0.)
	  framerate = 1.0;
	Vec4FastAddScale ( D, D, Right, x * truly_GS->_rotational_velocity / framerate); 
	D.MakeUnit();

	// move "forward" according to y
	Vec4FastAddScale(R, R, D, y * truly_GS->_translational_velocity / framerate);
	}
#endif

	truly_GS->_user.GetCamera().Set(R, D);
}


