Skip to content

Instantly share code, notes, and snippets.

@supahfunk
Forked from yiwenl/glsl-rotation-2d
Last active May 15, 2020 17:21
Show Gist options
  • Save supahfunk/2ea3774a86ca4f8166b6427f932fa421 to your computer and use it in GitHub Desktop.
Save supahfunk/2ea3774a86ca4f8166b6427f932fa421 to your computer and use it in GitHub Desktop.
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;
}
float theta = 0.;
float s = 0.;
float c = 0.;
// Twist X
theta = position.x;
c = cos( theta );
s = sin( theta );
mat3 twistX = mat3(
1, 0, 0,
0, c, -s,
0, s, c
);
// Twist Y
theta = position.y;
c = cos( theta);
s = sin( theta);
mat3 twistY = mat3(
c, 0, s,
0, 1, 0,
-s, 0, c
);
// Twist Z
theta = position.z;
c = cos( theta );
s = sin( theta );
mat3 twistZ = mat3(
c, -s, 0,
s, c, 0,
0, 0, 1
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment