-
-
Save sugi-cho/6bc1b257214ab202fbbc to your computer and use it in GitHub Desktop.
memo! Quaternion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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