Skip to content

Instantly share code, notes, and snippets.

@stephent
Forked from sketchpunk/eulerRotation.c
Created March 16, 2023 03:24
Show Gist options
  • Save stephent/7b89dbf7c796995a7d3e9001ed47d774 to your computer and use it in GitHub Desktop.
Save stephent/7b89dbf7c796995a7d3e9001ed47d774 to your computer and use it in GitHub Desktop.
Apply Euler Rotation on position & normals in GLSL
/*
vec3 rot = vec3( 90.0, 90.0, 0.0 );
vec3 pos = position * scale;
vec3 norm = normal;
eulerRotation( rot * DEG2RAD, pos, norm );
*/
const float DEG2RAD = 0.01745329251; // PI / 180
// Apply YXZ radian rotation
vec3 eulerRotation( vec3 rot, out vec3 pos, out vec3 norm ){
float s;
float c;
vec3 v;
if( rot.z != 0.0 ){
c = cos( rot.z );
s = sin( rot.z );
v = pos;
pos.x = v.x * c - v.y * s;
pos.y = v.x * s + v.y * c;
v = norm;
norm.x = v.x * c - v.y * s;
norm.y = v.x * s + v.y * c;
}
if( rot.x != 0.0 ){
c = cos( rot.x );
s = sin( rot.x );
v = pos;
pos.y = v.y * c - v.z * s;
pos.z = v.y * s + v.z * c;
v = norm;
norm.y = v.y * c - v.z * s;
norm.z = v.y * s + v.z * c;
}
if( rot.y != 0.0 ){
c = cos( rot.y );
s = sin( rot.y );
v = pos;
pos.x = v.z * s + v.x * c;
pos.z = v.z * c - v.x * s;
v = norm;
norm.x = v.z * s + v.x * c;
norm.z = v.z * c - v.x * s;
}
return pos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment