Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//https://www.shadertoy.com/view/MslyRr
///////////////////////////////////////////////////////////////////////////////
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);
}
vec4 RotationQuat(vec3 axis, float angle)
{
axis = normalize(axis);
float c = cos(0.5 * angle);
float s = sqrt(1.0 - c * c);
return vec4(axis.x * s, axis.y * s, axis.z * s, c);
}
vec3 Rotate(vec3 p, vec4 q)
{
return p + 2.0 * cross(q.xyz, cross(q.xyz, p) + q.w * p);
}
vec3 Rotate(vec3 p, vec3 axis, float angle)
{
return Rotate(p, RotationQuat(axis, angle));
}
mat4 TranslationMatrix(vec3 p)
{
return mat4(1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
p.x, p.y, p.z, 1.0);
}
mat4 ScaleMatrix(float s)
{
return mat4(s, 0.0, 0.0, 0.0,
0.0, s, 0.0, 0.0,
0.0, 0.0, s, 0.0,
0.0, 0.0, 0.0, 1.0);
}
///////////////////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment