camera: fixed antimeridian divergence in stitching algorithm
- Commit
0b486a0838c84bbbdc4ed17791e170a86f8d92cc- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-20 15:27 UTC (4 months ago)
- Parents
ce92f33
Diff
camera/src/main/java/com/vayunmathur/camera/util/GpuStitcher.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/util/GpuStitcher.kt b/camera/src/main/java/com/vayunmathur/camera/util/GpuStitcher.ktindex 8c9286bdbb..327feb15b7 100644--- a/camera/src/main/java/com/vayunmathur/camera/util/GpuStitcher.kt+++ b/camera/src/main/java/com/vayunmathur/camera/util/GpuStitcher.kt@@ -198,6 +198,14 @@ object GpuStitcher { val uGain = GLES30.glGetUniformLocation(warpProgram, "uGain") val uFeather = GLES30.glGetUniformLocation(warpProgram, "uFeather") val uTex = GLES30.glGetUniformLocation(warpProgram, "uTex")+ val uXOffset = GLES30.glGetUniformLocation(warpProgram, "uXOffset")++ // Seam handling: one full circle in u is 2π·scale px → this much in NDC+ // x. Drawing each frame at {0, +circle, -circle} lets a frame crossing+ // the ±180° meridian appear on both canvas edges; copies that fall+ // outside the viewport are clipped, so a sub-360° pano is unaffected.+ val circleNdc = (2.0 * Math.PI * est.scale / est.canvasW * 2.0).toFloat()+ val xOffsets = floatArrayOf(0f, circleNdc, -circleNdc) var drawn = 0 for (cam in est.cams) {@@ -218,8 +226,11 @@ object GpuStitcher { GLES30.glEnableVertexAttribArray(1) GLES30.glVertexAttribPointer(1, 2, GLES30.GL_FLOAT, false, 4 * 4, verts) - indices.position(0)- GLES30.glDrawElements(GLES30.GL_TRIANGLES, indexCount, GLES30.GL_UNSIGNED_SHORT, indices)+ for (off in xOffsets) {+ GLES30.glUniform1f(uXOffset, off)+ indices.position(0)+ GLES30.glDrawElements(GLES30.GL_TRIANGLES, indexCount, GLES30.GL_UNSIGNED_SHORT, indices)+ } GLES30.glDisableVertexAttribArray(0) GLES30.glDisableVertexAttribArray(1) drawn++@@ -287,10 +298,19 @@ object GpuStitcher { return tex } - /** Build a GRID×GRID warp mesh: interleaved [ndcX, ndcY, u, v] per vertex. */+ /**+ * Build a GRID×GRID warp mesh: interleaved [ndcX, ndcY, u, v] per vertex.+ * Each vertex's `u` is unwrapped relative to the frame center so a frame+ * that straddles the ±180° seam stays horizontally continuous (no torn+ * triangle). The seam wrap itself is handled by drawing the mesh at ±one+ * full-circle X offset (see the draw loop).+ */ private fun buildFrameMesh(est: Estimate, cam: FrameCam): FloatBuffer { val cols = GRID + 1 val data = FloatArray(cols * cols * 4)+ val period = 2.0 * Math.PI * est.scale // u span of a full 360° circle+ // Reference u at the frame center — vertices unwrap toward this.+ val refU = forward(cam, est.scale, cam.ppx, cam.ppy)[0] var p = 0 for (j in 0..GRID) { val b = j.toFloat() / GRID@@ -301,7 +321,9 @@ object GpuStitcher { val sx = a.toDouble() * (cam.ppx * 2.0) val sy = b.toDouble() * (cam.ppy * 2.0) val uv = forward(cam, est.scale, sx, sy)- val outX = uv[0] - est.u0+ var u = uv[0]+ if (period > 0.0) u -= period * Math.round((u - refU) / period)+ val outX = u - est.u0 val outY = uv[1] - est.v0 val ndcX = (outX / est.canvasW * 2.0 - 1.0).toFloat() val ndcY = (outY / est.canvasH * 2.0 - 1.0).toFloat()@@ -535,10 +557,11 @@ object GpuStitcher { private const val WARP_VS = """#version 310 es layout(location = 0) in vec2 aPos; layout(location = 1) in vec2 aUv;+ uniform float uXOffset; out vec2 vUv; void main() { vUv = aUv;- gl_Position = vec4(aPos, 0.0, 1.0);+ gl_Position = vec4(aPos.x + uXOffset, aPos.y, 0.0, 1.0); } """
camera/src/main/java/com/vayunmathur/camera/util/PanoramaEngine.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/util/PanoramaEngine.kt b/camera/src/main/java/com/vayunmathur/camera/util/PanoramaEngine.ktindex 772306c32f..a6485ad49f 100644--- a/camera/src/main/java/com/vayunmathur/camera/util/PanoramaEngine.kt+++ b/camera/src/main/java/com/vayunmathur/camera/util/PanoramaEngine.kt@@ -108,8 +108,8 @@ class PanoramaEngine(private val context: Context) : SensorEventListener { private const val ALIGNMENT_THRESHOLD_DEGREES = 3f private const val PITCH_THRESHOLD_DEGREES = 5f private const val MAX_VELOCITY_DPS = 30f- // Safety auto-finish for a flat panorama that has swept far enough.- private const val PANO_MAX_ANGLE = 200f+ // Safety auto-finish once a flat panorama has swept a full circle (+ margin).+ private const val PANO_MAX_ANGLE = 380f } fun startSweep(fullSphere: Boolean = false) {@@ -126,7 +126,7 @@ class PanoramaEngine(private val context: Context) : SensorEventListener { _currentAngle.value = 0f _currentPitch.value = 0f - // Guide-dot layout: a full-sphere grid (pitch rows) or a flat horizontal ring.+ // Guide-dot layout: a full-sphere grid (pitch rows) or a full horizontal ring. val dots = if (fullSphere) { var idx = 0 val result = mutableListOf<GuideDot>()@@ -146,7 +146,8 @@ class PanoramaEngine(private val context: Context) : SensorEventListener { } result } else {- (0 until 7).map { i ->+ // Full horizontal ring: 12 dots every 30° around the whole circle.+ (0 until 12).map { i -> GuideDot(i, i * 30f, 0f, if (i == 0) GuideDotState.CAPTURING else GuideDotState.PENDING) } }