Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Created January 26, 2015 04:54
Show Gist options
  • Save sugi-cho/6bc1b257214ab202fbbc to your computer and use it in GitHub Desktop.
Save sugi-cho/6bc1b257214ab202fbbc to your computer and use it in GitHub Desktop.
memo! Quaternion
// Quaternion multiplication.
// http://mathworld.wolfram.com/Quaternion.html
float4 qmul(float4 q1, float4 q2)
{
return float4(
q2.xyz * q1.w + q1.xyz * q2.w + cross(q1.xyz, q2.xyz),
q1.w * q2.w - dot(q1.xyz, q2.xyz)
);
}
// Rotate a vector with a rotation quaternion.
// http://mathworld.wolfram.com/Quaternion.html
float3 rotate_vector(float3 v, float4 r)
{
float4 r_c = r * float4(-1, -1, -1, 1);
return qmul(r, qmul(float4(v, 0), r_c)).xyz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment