void Renderer::setEyePos (float _rad, float _theta, float _phi)
{
	// We want the eye position to be (0, 0, r) when theta = phi = 0
	// To get this, have to add 90 degrees to theta
	_theta += PI*0.5;

	// Assume that theta and phi are in radians
	this->eye[0] = _rad*sin(_phi)*sin(_theta);
	this->eye[1] = _rad*cos(_theta);
	this->eye[2] = _rad*cos(_phi)*sin(_theta);
}

void Renderer::mouseMoveEvent (QMouseEvent *e)
{
	if (this->mousePressed == true && this->state == VIEWING)
	{
		float dx = -e->x() + this->xOld;
		float dy =  e->y() - this->yOld;
		
		// Dragging with the left button changes the distance from the camera to the origin
		if (this->button == LeftButton)
			this->rad += dx * 0.25;  // Dampen the movement
		
		// Dragging with the middle button rotates about the x/z axis
		else if (this->button == MidButton)
			this->theta = clamp(this->theta + dy * 0.25, 0.01, 179.9);
			
		// Dragging with the right button rotates about the y axis
		else if (this->button == RightButton)
			this->phi = mod(this->phi + dx * 0.25, 360.0);
			
		// Update the eye position based on the new values
		this->setEyePos(this->rad, radians(this->theta), radians(this->phi));
		
		updateGL();
	}
	
	this->xOld = e->x();
	this->yOld = e->y();
}

void Renderer::mousePressEvent (QMouseEvent *e)
{
	this->mousePressed = true;
	this->button = e->button();
	this->xOld = e->x();
	this->yOld = e->y();
}

void Renderer::mouseReleaseEvent (QMouseEvent *e)
{
	this->mousePressed = false;
}
