Last active
February 15, 2019 01:54
-
-
Save lhog/fd64d7ea02f4195dc148fe52f4e107e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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