6

I am trying to slerp between 2 quaternions using Eigen(thought would be the easiest).

I found two different examples

One,

for(int i = 0; i < list.size(); i++)
{
  Matrix3f m;
  Quaternion<float,0> q1 = m.toRotationMatrix();

  Quaternion<float,0> q3(q1.slerp(1,q2));
  m_node->Rotation(q3.toRotationMatrix());
}

Second,

Vec3 slerp(const Vec3& a, const Vec3& b, const Real& t)
{
 Quaternionf qa;
 Quaternionf qb;
 qa = Quaternionf::Identity();
 qb.setFromTwoVectors(a,b); 
  return (qa.slerp(t,qb)) * a; 
 }

I cant really say which one is correct. There is not many documentation about this. Can anyone tell me if I should use a different library? or how can I slerp using eigen.

genpfault
  • 51,148
  • 11
  • 85
  • 139
james456
  • 137
  • 1
  • 2
  • 9

3 Answers3

15

Doing a SLERP between two quaternions is just a matter of calling the slerp method:

Quaterniond qa, qb, qres;
// initialize qa, qb;
qres = qa.slerp(t, qb);

where t is your interpolation parameter.

ggael
  • 28,425
  • 2
  • 65
  • 71
1

Use the second variant.

Both code snippets implement a SLERP, however the first one does something with elements in a list, which your snippet doesn't show. Also the second variant is the computationally more efficient one, as it doesn't take a detour over a rotation matrix.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I have more than one quaternion so I was keeping them in a list. I want to move from q1 to q2 and when I come to q2 I want to move to q3 – james456 Oct 20 '13 at 20:03
  • @james456: This is a classical segmented interpolation problem. You store your quaternions in a list of pairs `t, q` where t designates the starting "time" for the associated quaternion. Then you map the range from `t_{n} … t_{n+1}` to the range `0 … 1` and multiply that with the given `q`. – datenwolf Oct 20 '13 at 20:10
0

I think GLM is the best choice in an openGL application because the GLM functions are the same as in GLSL.

The glm slerp takes argouments as the mix function (which is the glm function for lerp). The mix function gives you, for a call like

 result = mix (first, second, alpha); // result = (1-alpha)*first + alpha*second;

The alpha parameter works the same way for slerp, so a typical example of using slerp to interpolate between quaternion over time can be

glm::quat interpolated_quaternion; //the result
std::vector<glm::quat> my_quaternion; //vector of quaternions, one per frame.
float frame_time; //the time passed since the previous frame
int frame; //the actual frame
interpolated_quaternion = slerp( my_quaternion[frame],my_quaternion[frame+1],frame_time);
darius
  • 837
  • 9
  • 22