Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement SLERP in GLSL/HLSL

I'm attempting to SLERP from GLSL (HLSL would also be okay as I'm targeting Unity3D)

I've found this page: http://www.geeks3d.com/20140205/glsl-simple-morph-target-animation-opengl-glslhacker-demo

It contains the following listing:

#version 150
in vec4 gxl3d_Position;
in vec4 gxl3d_Attrib0;
in vec4 gxl3d_Attrib1;
out vec4 Vertex_Color;
uniform mat4 gxl3d_ModelViewProjectionMatrix;
uniform float time;

vec4 Slerp(vec4 p0, vec4 p1, float t)
{
  float dotp = dot(normalize(p0), normalize(p1));
  if ((dotp > 0.9999) || (dotp<-0.9999))
  {
    if (t<=0.5)
      return p0;
    return p1;
  }
  float theta = acos(dotp * 3.14159/180.0);
  vec4 P = ((p0*sin((1-t)*theta) + p1*sin(t*theta)) / sin(theta));
  P.w = 1;
  return P;
}

void main()
{
  vec4 P = Slerp(gxl3d_Position, gxl3d_Attrib1, time);
  gl_Position = gxl3d_ModelViewProjectionMatrix * P;
  Vertex_Color = gxl3d_Attrib0;
}

The maths can be found on the Wikipedia page for SLERP: http://en.wikipedia.org/wiki/Slerp

But I question the line

  float theta = acos(dotp * 3.14159/180.0);

That number is 2π/360, i.e. DEG2RAD And dotp, a.k.a cos(theta) is not an angle

i.e. it doesn't make sense to DEG2RAD it.

Isn’t the bracketing wrong?

float DEG2RAD = 3.14159/180.0;
float theta_rad = acos(dotp) * DEG2RAD;

And even then I doubt acos() returns degrees.

Can anyone provide a correct implementation of SLERP in GLSL?

like image 360
P i Avatar asked May 21 '14 21:05

P i


1 Answers

All that code seems fine. Just drop the " * 3.14159/180.0 " and let it be just:

float theta = acos(dotp);
like image 192
Drout Avatar answered Oct 10 '22 14:10

Drout