Skip to content

Instantly share code, notes, and snippets.

@neilmendoza
Last active June 9, 2023 14:22
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save neilmendoza/4512992 to your computer and use it in GitHub Desktop.
Save neilmendoza/4512992 to your computer and use it in GitHub Desktop.
Function to return matrix for rotation about an arbitrary axis in GLSL.
mat4 rotationMatrix(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
}
@johnnyshankman
Copy link

johnnyshankman commented Jun 8, 2023

bless for this, where does the math for this come from? ie any references?

i'm used to seeing three separate operations each using their own mat3 for rotation on each axis. this is awesome that it's all in one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment