Skip to content

StereoVideoPlacement.kt

src/jvmMain/kotlin/afield/graphics/scene/video/StereoVideoPlacement.kt · 54 lines

package afield.graphics.scene.video

import afield.math.linalg.Matrix4f
import afield.math.linalg.Vector3f
import kotlin.math.*

class StereoVideoPlacement

A cylindrical movie screen. Width is arc length; zero bend is an ordinary flat rectangle.

val center = Vector3f(0f, 0f, -2.5f)

val rotationDegrees = Vector3f()

var widthMeters

    @Volatile
    var widthMeters = 3f

var aspect

Per-eye displayed width/height. Native full-resolution SBS uses (file width / 2) / height.

    @Volatile
    var aspect = 16f / 9f

var bendDegrees

Positive bends bring the edges toward the viewer; negative bends take them away.

    @Volatile
    var bendDegrees = 35f

fun validate()

    fun validate() {
        require(widthMeters.isFinite())
        require(aspect.isFinite() && aspect > 0f)
        require(bendDegrees.isFinite())
        require(
            listOf(center.x, center.y, center.z, rotationDegrees.x, rotationDegrees.y, rotationDegrees.z)
                .all {
                    it.isFinite()
                },
        )
    }

fun model(): Matrix4f

    fun model(): Matrix4f =
        Matrix4f()
            .translate(center)
            .rotateY(Math.toRadians(rotationDegrees.y.toDouble()).toFloat())
            .rotateX(Math.toRadians(rotationDegrees.x.toDouble()).toFloat())
            .rotateZ(Math.toRadians(rotationDegrees.z.toDouble()).toFloat())

fun point(u: Float, v: Float): Vector3f

Reference geometry in screen-local metres, with top-left texture coordinates.

    fun point(u: Float, v: Float): Vector3f {
        validate()
        val bend = Math.toRadians(bendDegrees.toDouble()).toFloat()
        val x = (u - .5f) * widthMeters
        val theta = (u - .5f) * bend
        return if (abs(bend) < 1e-4f) Vector3f(x, (.5f - v) * widthMeters / aspect, 0f)
        else
            Vector3f(
                widthMeters / bend * sin(theta),
                (.5f - v) * widthMeters / aspect,
                widthMeters / bend * 2f * sin(theta / 2f).pow(2),
            )
    }
}