spherical linear interpolation - slerp

hi guys,
anyone have any experience using Slerp?

im trying to draw a the shortest distance between two points on a surface of a sphere, otherwise known as geodesics.

been fooling around with quaternions and it looks like they are the best bet for the job… but just need one solid example to fill the missing pieces.

any ideas would be immensly appreciated.


hey julapy,

We are about to add some quat math to the ofxVectorMath addon. It’s not there yet but for now you can use the code here:

Basically what you have to do is convert your start rotation and end rotation to quat representation. These two quats you then use in the slerp function along with an interpolation percentage. The quats you create with the makeRotate methods in this preliminary code.

wow, looks great!
ive been putting my own quaternion class together from bits of resources of the web… will run with yours from now on.

for now, a quick question…

is there an easier way of passing the ofxMatrix4x4 transformation into opengl.
currently im doing this, seems a little redudant to copy the matrix values from a 2D to 1D array.

	ofxQuat q0;  
	q0.makeRotate( 45 * 0.5 * DEG_TO_RAD, ofxVec3f( 0, 1, 0 ) );  
	ofxMatrix4x4 mat;  
	q0.get( mat );  
	m[ 0 ]  = mat._mat[ 0 ][ 0 ];  
	m[ 1 ]  = mat._mat[ 0 ][ 1 ];  
	m[ 2 ]  = mat._mat[ 0 ][ 2 ];  
	m[ 3 ]  = mat._mat[ 0 ][ 3 ];  
	m[ 4 ]  = mat._mat[ 1 ][ 0 ];  
	m[ 5 ]  = mat._mat[ 1 ][ 1 ];  
	m[ 6 ]  = mat._mat[ 1 ][ 2 ];  
	m[ 7 ]  = mat._mat[ 1 ][ 3 ];  
	m[ 8 ]  = mat._mat[ 2 ][ 0 ];  
	m[ 9 ]  = mat._mat[ 2 ][ 1 ];  
	m[ 10 ] = mat._mat[ 2 ][ 2 ];  
	m[ 11 ] = mat._mat[ 2 ][ 3 ];  
	m[ 12 ] = mat._mat[ 3 ][ 0 ];  
	m[ 13 ] = mat._mat[ 3 ][ 1 ];  
	m[ 14 ] = mat._mat[ 3 ][ 2 ];  
	m[ 15 ] = mat._mat[ 3 ][ 3 ];  
	glMultMatrixd( m );  

ya, you can just pass in a pointer to “mat”. This means if mat is on the stack like in your example you need to write it like “&mat”. Make sure though that you use glMultMatrixf if you are using floats. Also, there is a column order that might match opengl or not. I haven’t verified this yet with the code I sent you. It’s a bit tricky because the terminology is very ambiguous and column-major or row-major alone is still not clearly defined because different programming languages store 2D arrays in different ways.

The way to get this right is to make sure the translation components in are in the 13th, 14th, 15th slot. See: http://www.opengl.org/resources/faq/tec-…-ations.htm

There is some info abou te C array layout here:

cool, so i’ve been using ofxQuat for slerp interpolation - working out the shortest path along the surface of a sphere and im so close i can taste it! :slight_smile:

heres where im up to,

  1. i have two points between which im slerping, so to speak. these are a latitude and longitude pairs.
  2. i convert the latitude and longitude pairs into cartesian coordinates (XYZ) on the unit sphere.
  3. i represent the points as flat planes and rotate them so the out vector points to the center of the sphere, the right vector along the longitude (across) of the sphere and the up vector towards the poles.
  4. having this information i have enough to create a transformation matrix for each of the points btween which im interpolating.
	m[0]	= right.x;  
	m[1]	= right.y;  
	m[2]	= right.z;  
	m[3]	= 0;  
	m[4]	= up.x;  
	m[5]	= up.y;  
	m[6]	= up.z;  
	m[7]	= 0;  
	m[8]	= -out.x;  
	m[9]	= -out.y;  
	m[10] = -out.z;  
	m[11] = 0;  
	m[12] = 0;  
	m[13] = 0;  
	m[14] = 0;  
	m[15] = 1;  

  1. i then pass the two matrices for the two points into two quaternions and slerp between them
  2. for each step of the slerp i get a quaternion → get the matrix out of the quaternion and use the out vector of the matrix as the next point on the geodesic path.

issue now is, because the planes are oriented to always point up to the poles and point right along the longitude, the geodesic slightly curves… so it doesn’t give me a the shortest path… the attached example can demonstrate.


think the last thing left to do which will solve this problem is to make sure that the planes are aligned so they point up along the tangent of the great cirlce on which they are on.

i think…
please let me know if you know better.
or have any suggestions.

many thanks,