Skip to content

Instantly share code, notes, and snippets.

@badjano
Created August 7, 2018 02:26
Show Gist options
  • Save badjano/f5022beab4dbddb99e475b845cba6db6 to your computer and use it in GitHub Desktop.
Save badjano/f5022beab4dbddb99e475b845cba6db6 to your computer and use it in GitHub Desktop.
Quaternion GLSL functions
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)
);
}
float3 rotate_vector(float4 r, float3 v) {
float4 r_c = r * float4(-1, -1, -1, 1);
return qmul(r, qmul(float4(v, 0), r_c)).xyz;
}
float4 q_conj(float4 q)
{
return float4(-q.x, -q.y, -q.z, q.w);
}
float4 q_inverse(float4 q)
{
float4 conj = q_conj(q);
return conj / (q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment