math: euler, eulerq fixes

main
Dominik Madarász 2024-09-04 00:10:44 +02:00
parent 5dca07d113
commit 9e12dbb10e
10 changed files with 45 additions and 60 deletions

View File

@ -234,9 +234,9 @@ if "%1"=="dpush" (
if "%1"=="push" ( if "%1"=="push" (
call make.bat tidy call make.bat tidy
if "%2"=="dp" ( @REM if "%2"=="dp" (
call MAKE.bat dpush auto call MAKE.bat dpush auto
) @REM )
git diff --quiet --exit-code git diff --quiet --exit-code
if !ERRORLEVEL! neq 0 ( if !ERRORLEVEL! neq 0 (

View File

@ -1639,7 +1639,7 @@ typedef struct lightmap_t {
bool gizmo_hover(); bool gizmo_hover();
typedef struct camera_t { typedef struct camera_t {
mat44 view, proj; mat44 view, proj;
vec3 position, updir, lookdir; vec3 position, updir, lookdir, rightdir;
float yaw, pitch, roll; float yaw, pitch, roll;
float speed, fov; float speed, fov;
float near_clip, far_clip; float near_clip, far_clip;

View File

@ -33,7 +33,7 @@ int main() {
for(int z = 0, i = 0; z < 128; ++z) { for(int z = 0, i = 0; z < 128; ++z) {
for(int x = 0; x < 128; ++x, ++i) { for(int x = 0; x < 128; ++x, ++i) {
vec3 pos = vec3(-x*3,0,-z*3); vec3 pos = vec3(-x*3,0,-z*3);
vec3 rot = vec3(0,0,-180); // kgirl: 0,0,0 vec3 rot = vec3(0,180,0); // kgirl: 0,0,0
vec3 sca = vec3(1,1,1); // kgirl: 2,2,2 vec3 sca = vec3(1,1,1); // kgirl: 2,2,2
compose44(M[i], pos, eulerq(rot), sca); compose44(M[i], pos, eulerq(rot), sca);
} }
@ -98,7 +98,7 @@ int main() {
} }
if( do_showgizmo ) { if( do_showgizmo ) {
static vec3 p = {0,0,0}, r = {0,0,0}, s = {1,1,1}; static vec3 p = {0,0,0}, r = {0,-90,0}, s = {1,1,1};
gizmo(&p, &r, &s); gizmo(&p, &r, &s);
compose44(mdl.pivot, p, eulerq(r), s); compose44(mdl.pivot, p, eulerq(r), s);
} }

2
depot

@ -1 +1 @@
Subproject commit dc53801aafff279cd12903aa85a5091d5a528ed5 Subproject commit 3abeaa482c1c2f88db6bf5816456d5e2e1faa6fb

View File

@ -17992,7 +17992,7 @@ API bool gizmo_hover();
typedef struct camera_t { typedef struct camera_t {
mat44 view, proj; mat44 view, proj;
vec3 position, updir, lookdir; vec3 position, updir, lookdir, rightdir;
float yaw, pitch, roll; // mirror of (x,y) lookdir in deg; float yaw, pitch, roll; // mirror of (x,y) lookdir in deg;
float speed, fov; // fov in deg(45) float speed, fov; // fov in deg(45)
float near_clip, far_clip; float near_clip, far_clip;
@ -377415,27 +377415,20 @@ vec3 rotate3q(vec3 v, quat r) { // rotate vec3 by quat @testme
} }
// euler <-> quat // euler <-> quat
vec3 euler (quat q) { // bugs? returns PitchYawRoll (PYR) in degrees. ref: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles vec3 euler (quat q) { // returns YawPitchRoll (YPR) in degrees. ref: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
float sr_cp = 2*(q.x*q.y + q.z*q.w), cr_cp = 1-2*(q.y*q.y + q.z*q.z); float sr_cy = 2*(q.z*q.w + q.x*q.y), cr_cy = 1-2*(q.y*q.y + q.z*q.z);
float sy_cp = 2*(q.x*q.w + q.y*q.z), cy_cp = 1-2*(q.z*q.z + q.w*q.w), sp = 2*(q.x*q.z-q.w*q.y); float sp_cy = 2*(q.y*q.w - q.x*q.z), cp_cy = 1-2*(q.z*q.z + q.w*q.w), sy = 2*(q.x*q.w+q.y*q.z);
float p = fabs(sp) >= 1 ? copysignf(C_PI / 2, sp) : asinf(sp); float y = fabs(sy) >= 1 ? copysignf(C_PI / 2, sy) : asinf(sy);
float y = atan2f(sy_cp, cy_cp); float p = atan2f(sp_cy, cp_cy);
float r = atan2f(sr_cp, cr_cp); float r = atan2f(sr_cy, cr_cy);
return scale3(vec3(p, y, r), TO_DEG); return scale3(vec3(y, p, r), TO_DEG);
} }
quat eulerq (vec3 pyr_degrees) { // bugs? quat eulerq (vec3 pyr_degrees) {
#if 0
quat x = vec3q(vec3(1,0,0),rad(pyr_degrees.x)); // x, not pitch
quat y = vec3q(vec3(0,1,0),rad(pyr_degrees.y)); // y, not yaw
quat z = vec3q(vec3(0,0,1),rad(pyr_degrees.z)); // z, not row
return normq(mulq(mulq(x, y), z));
#else
float p = rad(pyr_degrees.x), y = rad(pyr_degrees.y), r = rad(pyr_degrees.z); float p = rad(pyr_degrees.x), y = rad(pyr_degrees.y), r = rad(pyr_degrees.z);
float ha = p * 0.5f, hb = r * 0.5f, hc = y * 0.5f; float ha = y * 0.5f, hb = r * 0.5f, hc = p * 0.5f;
float cp = cosf(ha), sp = sinf(ha), cr = cosf(hb), sr = sinf(hb), cy = cosf(hc), sy = sinf(hc); float cp = cosf(ha), sp = sinf(ha), cr = cosf(hb), sr = sinf(hb), cy = cosf(hc), sy = sinf(hc);
return quat(sy*cr*cp - cy*sr*sp, cy*sr*cp - sy*cr*sp, cy*cr*sp + sy*sr*cp, cy*cr*cp + sy*sr*sp); return quat(cy*sr*cp - sy*cr*sp, cy*cr*sp + sy*sr*cp, sy*cr*cp - cy*sr*sp, cy*cr*cp + sy*sr*sp);
#endif
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -388977,6 +388970,7 @@ camera_t camera() {
cam.speed = 0.50f; cam.speed = 0.50f;
cam.position = vec3(10,10,10); cam.position = vec3(10,10,10);
cam.updir = vec3(0,1,0); cam.updir = vec3(0,1,0);
cam.rightdir = vec3(1,0,0);
cam.fov = 45; cam.fov = 45;
cam.frustum_fov_multiplier = 1.5f; cam.frustum_fov_multiplier = 1.5f;
cam.orthographic = false; cam.orthographic = false;
@ -389130,6 +389124,7 @@ void camera_fps2(camera_t *cam, float yaw, float pitch, float roll) {
float cosfa = cosf(r); float cosfa = cosf(r);
float sinfa = sinf(r); float sinfa = sinf(r);
vec3 right = cross3(cam->lookdir, up); vec3 right = cross3(cam->lookdir, up);
cam->rightdir = right;
float th = dot3(cam->lookdir, up); float th = dot3(cam->lookdir, up);
cam->updir.x = up.x * cosfa + right.x * sinfa + cam->lookdir.x * th * (1.0f - cosfa); cam->updir.x = up.x * cosfa + right.x * sinfa + cam->lookdir.x * th * (1.0f - cosfa);

View File

@ -292,27 +292,20 @@ vec3 rotate3q(vec3 v, quat r) { // rotate vec3 by quat @testme
} }
// euler <-> quat // euler <-> quat
vec3 euler (quat q) { // bugs? returns PitchYawRoll (PYR) in degrees. ref: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles vec3 euler (quat q) { // returns YawPitchRoll (YPR) in degrees. ref: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
float sr_cp = 2*(q.x*q.y + q.z*q.w), cr_cp = 1-2*(q.y*q.y + q.z*q.z); float sr_cy = 2*(q.z*q.w + q.x*q.y), cr_cy = 1-2*(q.y*q.y + q.z*q.z);
float sy_cp = 2*(q.x*q.w + q.y*q.z), cy_cp = 1-2*(q.z*q.z + q.w*q.w), sp = 2*(q.x*q.z-q.w*q.y); float sp_cy = 2*(q.y*q.w - q.x*q.z), cp_cy = 1-2*(q.z*q.z + q.w*q.w), sy = 2*(q.x*q.w+q.y*q.z);
float p = fabs(sp) >= 1 ? copysignf(C_PI / 2, sp) : asinf(sp); float y = fabs(sy) >= 1 ? copysignf(C_PI / 2, sy) : asinf(sy);
float y = atan2f(sy_cp, cy_cp); float p = atan2f(sp_cy, cp_cy);
float r = atan2f(sr_cp, cr_cp); float r = atan2f(sr_cy, cr_cy);
return scale3(vec3(p, y, r), TO_DEG); return scale3(vec3(y, p, r), TO_DEG);
} }
quat eulerq (vec3 pyr_degrees) { // bugs? quat eulerq (vec3 pyr_degrees) {
#if 0
quat x = vec3q(vec3(1,0,0),rad(pyr_degrees.x)); // x, not pitch
quat y = vec3q(vec3(0,1,0),rad(pyr_degrees.y)); // y, not yaw
quat z = vec3q(vec3(0,0,1),rad(pyr_degrees.z)); // z, not row
return normq(mulq(mulq(x, y), z));
#else
float p = rad(pyr_degrees.x), y = rad(pyr_degrees.y), r = rad(pyr_degrees.z); float p = rad(pyr_degrees.x), y = rad(pyr_degrees.y), r = rad(pyr_degrees.z);
float ha = p * 0.5f, hb = r * 0.5f, hc = y * 0.5f; float ha = y * 0.5f, hb = r * 0.5f, hc = p * 0.5f;
float cp = cosf(ha), sp = sinf(ha), cr = cosf(hb), sr = sinf(hb), cy = cosf(hc), sy = sinf(hc); float cp = cosf(ha), sp = sinf(ha), cr = cosf(hb), sr = sinf(hb), cy = cosf(hc), sy = sinf(hc);
return quat(sy*cr*cp - cy*sr*sp, cy*sr*cp - sy*cr*sp, cy*cr*sp + sy*sr*cp, cy*cr*cp + sy*sr*sp); return quat(cy*sr*cp - sy*cr*sp, cy*cr*sp + sy*sr*cp, sy*cr*cp - cy*sr*sp, cy*cr*cp + sy*sr*sp);
#endif
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -11,6 +11,7 @@ camera_t camera() {
cam.speed = 0.50f; cam.speed = 0.50f;
cam.position = vec3(10,10,10); cam.position = vec3(10,10,10);
cam.updir = vec3(0,1,0); cam.updir = vec3(0,1,0);
cam.rightdir = vec3(1,0,0);
cam.fov = 45; cam.fov = 45;
cam.frustum_fov_multiplier = 1.5f; cam.frustum_fov_multiplier = 1.5f;
cam.orthographic = false; cam.orthographic = false;
@ -164,6 +165,7 @@ void camera_fps2(camera_t *cam, float yaw, float pitch, float roll) {
float cosfa = cosf(r); float cosfa = cosf(r);
float sinfa = sinf(r); float sinfa = sinf(r);
vec3 right = cross3(cam->lookdir, up); vec3 right = cross3(cam->lookdir, up);
cam->rightdir = right;
float th = dot3(cam->lookdir, up); float th = dot3(cam->lookdir, up);
cam->updir.x = up.x * cosfa + right.x * sinfa + cam->lookdir.x * th * (1.0f - cosfa); cam->updir.x = up.x * cosfa + right.x * sinfa + cam->lookdir.x * th * (1.0f - cosfa);

View File

@ -6,7 +6,7 @@
typedef struct camera_t { typedef struct camera_t {
mat44 view, proj; mat44 view, proj;
vec3 position, updir, lookdir; vec3 position, updir, lookdir, rightdir;
float yaw, pitch, roll; // mirror of (x,y) lookdir in deg; float yaw, pitch, roll; // mirror of (x,y) lookdir in deg;
float speed, fov; // fov in deg(45) float speed, fov; // fov in deg(45)
float near_clip, far_clip; float near_clip, far_clip;

View File

@ -12445,27 +12445,20 @@ vec3 rotate3q(vec3 v, quat r) { // rotate vec3 by quat @testme
} }
// euler <-> quat // euler <-> quat
vec3 euler (quat q) { // bugs? returns PitchYawRoll (PYR) in degrees. ref: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles vec3 euler (quat q) { // returns YawPitchRoll (YPR) in degrees. ref: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
float sr_cp = 2*(q.x*q.y + q.z*q.w), cr_cp = 1-2*(q.y*q.y + q.z*q.z); float sr_cy = 2*(q.z*q.w + q.x*q.y), cr_cy = 1-2*(q.y*q.y + q.z*q.z);
float sy_cp = 2*(q.x*q.w + q.y*q.z), cy_cp = 1-2*(q.z*q.z + q.w*q.w), sp = 2*(q.x*q.z-q.w*q.y); float sp_cy = 2*(q.y*q.w - q.x*q.z), cp_cy = 1-2*(q.z*q.z + q.w*q.w), sy = 2*(q.x*q.w+q.y*q.z);
float p = fabs(sp) >= 1 ? copysignf(C_PI / 2, sp) : asinf(sp); float y = fabs(sy) >= 1 ? copysignf(C_PI / 2, sy) : asinf(sy);
float y = atan2f(sy_cp, cy_cp); float p = atan2f(sp_cy, cp_cy);
float r = atan2f(sr_cp, cr_cp); float r = atan2f(sr_cy, cr_cy);
return scale3(vec3(p, y, r), TO_DEG); return scale3(vec3(y, p, r), TO_DEG);
} }
quat eulerq (vec3 pyr_degrees) { // bugs? quat eulerq (vec3 pyr_degrees) {
#if 0
quat x = vec3q(vec3(1,0,0),rad(pyr_degrees.x)); // x, not pitch
quat y = vec3q(vec3(0,1,0),rad(pyr_degrees.y)); // y, not yaw
quat z = vec3q(vec3(0,0,1),rad(pyr_degrees.z)); // z, not row
return normq(mulq(mulq(x, y), z));
#else
float p = rad(pyr_degrees.x), y = rad(pyr_degrees.y), r = rad(pyr_degrees.z); float p = rad(pyr_degrees.x), y = rad(pyr_degrees.y), r = rad(pyr_degrees.z);
float ha = p * 0.5f, hb = r * 0.5f, hc = y * 0.5f; float ha = y * 0.5f, hb = r * 0.5f, hc = p * 0.5f;
float cp = cosf(ha), sp = sinf(ha), cr = cosf(hb), sr = sinf(hb), cy = cosf(hc), sy = sinf(hc); float cp = cosf(ha), sp = sinf(ha), cr = cosf(hb), sr = sinf(hb), cy = cosf(hc), sy = sinf(hc);
return quat(sy*cr*cp - cy*sr*sp, cy*sr*cp - sy*cr*sp, cy*cr*sp + sy*sr*cp, cy*cr*cp + sy*sr*sp); return quat(cy*sr*cp - sy*cr*sp, cy*cr*sp + sy*sr*cp, sy*cr*cp - cy*sr*sp, cy*cr*cp + sy*sr*sp);
#endif
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -24007,6 +24000,7 @@ camera_t camera() {
cam.speed = 0.50f; cam.speed = 0.50f;
cam.position = vec3(10,10,10); cam.position = vec3(10,10,10);
cam.updir = vec3(0,1,0); cam.updir = vec3(0,1,0);
cam.rightdir = vec3(1,0,0);
cam.fov = 45; cam.fov = 45;
cam.frustum_fov_multiplier = 1.5f; cam.frustum_fov_multiplier = 1.5f;
cam.orthographic = false; cam.orthographic = false;
@ -24160,6 +24154,7 @@ void camera_fps2(camera_t *cam, float yaw, float pitch, float roll) {
float cosfa = cosf(r); float cosfa = cosf(r);
float sinfa = sinf(r); float sinfa = sinf(r);
vec3 right = cross3(cam->lookdir, up); vec3 right = cross3(cam->lookdir, up);
cam->rightdir = right;
float th = dot3(cam->lookdir, up); float th = dot3(cam->lookdir, up);
cam->updir.x = up.x * cosfa + right.x * sinfa + cam->lookdir.x * th * (1.0f - cosfa); cam->updir.x = up.x * cosfa + right.x * sinfa + cam->lookdir.x * th * (1.0f - cosfa);

View File

@ -4059,7 +4059,7 @@ API bool gizmo_hover();
typedef struct camera_t { typedef struct camera_t {
mat44 view, proj; mat44 view, proj;
vec3 position, updir, lookdir; vec3 position, updir, lookdir, rightdir;
float yaw, pitch, roll; // mirror of (x,y) lookdir in deg; float yaw, pitch, roll; // mirror of (x,y) lookdir in deg;
float speed, fov; // fov in deg(45) float speed, fov; // fov in deg(45)
float near_clip, far_clip; float near_clip, far_clip;