glm::mat4 VirtualTrackball::rotate(int x, int y) {
	//If not rotating, simply return the old rotation matrix
	if (!rotating) return quatToMat4(quat_old);

	glm::vec3 point_on_sphere_end;  //Current point on unit sphere
	glm::vec3 axis_of_rotation;		//axis of rotation
	float theta = 0.0f;				//angle of rotation

	point_on_sphere_end = getClosestPointOnUnitSphere(x, y);

	//Set the axeis of rotation by calculating cross product between the end and begin vectors
	axis_of_rotation = glm::cross(point_on_sphere_end, point_on_sphere_begin);

	//Set begin point on sphere to normalized start vector
	point_on_sphere_begin = glm::normalize(point_on_sphere_begin);

	//Set end point on sphere to normalized end vector
	point_on_sphere_end = glm::normalize(point_on_sphere_end);

	//Calculate the movement angle
	theta = glm::degrees(glm::acos(glm::dot(point_on_sphere_begin, point_on_sphere_end)));

	//Apply rotation to quaternion
	quat_new = glm::rotate(quat_old, theta, axis_of_rotation);
	
	return quatToMat4(quat_new);
}
glm::mat4 VirtualTrackball::rotate(int x, int y) {
	//If not rotating, simply return the old rotation matrix
	if (!rotating) return quatToMat4(quat_old) /*return glm::mat4_cast(quat_old)*/;

	glm::vec3 point_on_sphere_end; //Current point on unit sphere
	glm::vec3 axis_of_rotation; //axis of rotation
	float theta = 0.0f; //angle of rotation


	point_on_sphere_end = getClosestPointOnUnitSphere(x, y);

	axis_of_rotation = glm::cross(point_on_sphere_end, point_on_sphere_begin);
	theta = glm::degrees(glm::acos(glm::dot(point_on_sphere_begin, point_on_sphere_end)));

	quat_new = glm::rotate(quat_old, theta, axis_of_rotation);
	
	/*std::cout << "Angle: " << theta << std::endl;
	std::cout << "Axis: " << axis_of_rotation.x << " " << axis_of_rotation.y << " " << axis_of_rotation.z << std::endl;*/

	
	return quatToMat4(quat_new);
}
void VirtualTrackball::rotateBegin(int x, int y) {
	rotating = true;
	point_on_sphere_begin = getClosestPointOnUnitSphere(x, y);
}