Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
vec2 rotate(vec2 v, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return m * v;
}
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);
}
vec3 rotate(vec3 v, vec3 axis, float angle) {
mat4 m = rotationMatrix(axis, angle);
return (m * vec4(v, 1.0)).xyz;
}
@AnastasiaDunbar

This comment has been minimized.

Copy link

@AnastasiaDunbar AnastasiaDunbar commented Jul 5, 2018

Shouldn't it be mat2(c,s,-s,c);?

@maximumsomething

This comment has been minimized.

Copy link

@maximumsomething maximumsomething commented Apr 4, 2020

Why does it need to be a mat4?

@benbruland

This comment has been minimized.

Copy link

@benbruland benbruland commented Apr 7, 2021

I'm not sure whether or not it needs to be a mat4. It is probably a mat4 because we're dealing with homogeneous coordinates. Homogeneous coordinates unlock translations under matrix multiplication, as well as projective transformations. To rotate something we first translate it to the rotation axis, apply the rotation, and then translate it back. This is called a similarity transform.

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