Skip to content

Instantly share code, notes, and snippets.

@sketchpunk
Created September 6, 2018 04:08
Show Gist options
  • Save sketchpunk/3deb882734330c83cc8ca7d4ed0367b4 to your computer and use it in GitHub Desktop.
Save sketchpunk/3deb882734330c83cc8ca7d4ed0367b4 to your computer and use it in GitHub Desktop.
Dual Qaternion Vec3 Transform function in GLSL
vec3 dqVecTransform(mat2x4 dq, vec3 v){
vec4 Qr = dq[0].xyzw; //real (rot)
vec4 Qd = dq[1].xyzw; //dual (trans)
vec3 pos = v + cross(2.0 * Qr.xyz, cross(Qr.xyz, v) + Qr.w * v); //Rotate Vector
vec3 tran = 2.0 * (Qr.w * Qd.xyz - Qd.w * Qr.xyz + cross(Qr.xyz, Qd.xyz)); //Pull out Translation from DQ
return pos + tran;
}
//For Joint / Bone Transformation, you need to blend weighted dual quaternions that you can then apply to a vertex.
//This sample is for just two joints, but I commented out use of 4 joints total which is enough for any skinning needs.
vec3 dqWeightTransform_2(vec3 vpos, mat2x4[60] jDQ, vec4 jIndex, vec4 jWeight){
/* NORMALIZE DATA First */
float t = 1.0 / (jWeight.x + jWeight.y); // + jWeight.z + jWeight.w
mat2x4 dqSum = jDQ[ int( jIndex.x ) ] * jWeight.x * t +
jDQ[ int( jIndex.y ) ] * jWeight.y * t;
//jDQ[ int( jIndex.z ) ] * jWeight.z * t;
//jDQ[ int( jIndex.w ) ] * jWeight.w * t;
return dqVecTransform(dqSum, vpos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment