From 16c30ac052d1359ddac24939f10e5e102d2a8fb6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 30 Mar 2026 09:35:53 -0600 Subject: [PATCH] [gpu] Add pinch-to-rotate alongside pinch-to-zoom Two-finger pinch now does both zoom (distance change) and Z-axis rotation (angle change) simultaneously. Adds demo_view_rotate_z() for direct Z-axis rotation from touch input. Co-Authored-By: Claude Opus 4.6 (1M context) --- util/gpu/demo-view.cc | 10 +++++++++ util/gpu/demo-view.h | 3 +++ util/gpu/web/build.sh | 2 +- util/gpu/web/hb-gpu-web-webgpu.cc | 9 ++++++++ util/gpu/web/hb-gpu-web.cc | 6 ++++++ util/gpu/web/shell.html | 36 +++++++++++++++++++++---------- 6 files changed, 54 insertions(+), 12 deletions(-) diff --git a/util/gpu/demo-view.cc b/util/gpu/demo-view.cc index bf53eac42..761c4779c 100644 --- a/util/gpu/demo-view.cc +++ b/util/gpu/demo-view.cc @@ -505,6 +505,16 @@ demo_view_scroll_func (demo_view_t *vu, double xoffset, double yoffset) vu->needs_redraw = true; } +void +demo_view_rotate_z (demo_view_t *vu, double angle) +{ + float dquat[4]; + float axis[3] = {0, 0, 1}; + axis_to_quat (axis, (float) angle, dquat); + add_quats (dquat, vu->quat, vu->quat); + vu->needs_redraw = true; +} + void demo_view_motion_func (demo_view_t *vu, double x, double y) { diff --git a/util/gpu/demo-view.h b/util/gpu/demo-view.h index e39bdbb10..99f632717 100644 --- a/util/gpu/demo-view.h +++ b/util/gpu/demo-view.h @@ -45,6 +45,9 @@ demo_view_mouse_func (demo_view_t *vu, int button, int action, int mods); void demo_view_scroll_func (demo_view_t *vu, double xoffset, double yoffset); +void +demo_view_rotate_z (demo_view_t *vu, double angle); + void demo_view_motion_func (demo_view_t *vu, double x, double y); diff --git a/util/gpu/web/build.sh b/util/gpu/web/build.sh index faee8f1f0..216bc55d0 100755 --- a/util/gpu/web/build.sh +++ b/util/gpu/web/build.sh @@ -31,7 +31,7 @@ em++ \ -sUSE_WEBGL2=1 \ -sFULL_ES3=1 \ -sALLOW_MEMORY_GROWTH=1 \ - -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_malloc","_free"]' \ + -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_rotate_z","_malloc","_free"]' \ -sEXPORTED_RUNTIME_METHODS='["UTF8ToString","lengthBytesUTF8","stringToUTF8"]' \ --shell-file "$SRCDIR/util/gpu/web/shell.html" \ -o "$OUTDIR/index.html" diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index c5517d508..6db26b338 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -501,6 +501,7 @@ on_keydown (int type, const EmscriptenKeyboardEvent *e, void *ud) /* Touch handling */ static double pinch_dist; +static double pinch_angle; static bool three_finger_active; static EM_BOOL @@ -517,6 +518,7 @@ on_touchstart (int type, const EmscriptenTouchEvent *e, void *ud) double dx = e->touches[1].targetX - e->touches[0].targetX; double dy = e->touches[1].targetY - e->touches[0].targetY; pinch_dist = sqrt (dx * dx + dy * dy); + pinch_angle = atan2 (dy, dx); } else if (e->numTouches >= 3) { @@ -548,9 +550,16 @@ on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud) double dx = e->touches[1].targetX - e->touches[0].targetX; double dy = e->touches[1].targetY - e->touches[0].targetY; double dist = sqrt (dx * dx + dy * dy); + double angle = atan2 (dy, dx); if (pinch_dist > 0) demo_view_scroll_func (vu, 0, (dist - pinch_dist) * 0.05); + double dAngle = angle - pinch_angle; + if (dAngle > M_PI) dAngle -= 2 * M_PI; + if (dAngle < -M_PI) dAngle += 2 * M_PI; + if (fabs (dAngle) > 0.01) + demo_view_rotate_z (vu, dAngle); pinch_dist = dist; + pinch_angle = angle; } return EM_TRUE; } diff --git a/util/gpu/web/hb-gpu-web.cc b/util/gpu/web/hb-gpu-web.cc index ff24b279d..5582d4cdf 100644 --- a/util/gpu/web/hb-gpu-web.cc +++ b/util/gpu/web/hb-gpu-web.cc @@ -120,6 +120,12 @@ web_get_text () return current_text; } +EMSCRIPTEN_KEEPALIVE void +web_rotate_z (float angle) +{ + demo_view_rotate_z (vu, angle); +} + } /* extern "C" */ static void diff --git a/util/gpu/web/shell.html b/util/gpu/web/shell.html index 1bad6b570..974b77862 100644 --- a/util/gpu/web/shell.html +++ b/util/gpu/web/shell.html @@ -114,7 +114,7 @@ Shortcuts: Text editor:
drag: pan · scroll: zoom · click: animate · right-drag: rotate
space: animate · b: dark mode · g: gamma · s: stem · d: debug · r: reset · ?: help
-drag: pan · pinch: zoom · 3-finger: rotate · tap: animate +drag: pan · pinch: zoom/rotate · 3-finger: 3D rotate · tap: animate