Skip to content

Commit f31ff72

Browse files
committed
SciView: add window resizing functionality when toggling VR
1 parent 1fa5f89 commit f31ff72

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

src/main/kotlin/sc/iview/SciView.kt

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ import graphics.scenery.controls.TrackerInput
5050
import graphics.scenery.primitives.*
5151
import graphics.scenery.proteins.Protein
5252
import graphics.scenery.proteins.RibbonDiagram
53-
import graphics.scenery.utils.ExtractsNatives
54-
import graphics.scenery.utils.ExtractsNatives.Companion.getPlatform
5553
import graphics.scenery.utils.LogbackUtils
5654
import graphics.scenery.utils.SceneryPanel
5755
import graphics.scenery.utils.Statistics
@@ -84,8 +82,8 @@ import net.imglib2.type.numeric.RealType
8482
import net.imglib2.type.numeric.integer.UnsignedByteType
8583
import net.imglib2.view.Views
8684
import org.joml.Quaternionf
85+
import org.joml.Vector2f
8786
import org.joml.Vector3f
88-
import org.joml.Vector4f
8987
import org.scijava.Context
9088
import org.scijava.`object`.ObjectService
9189
import org.scijava.display.Display
@@ -100,8 +98,6 @@ import org.scijava.service.SciJavaService
10098
import org.scijava.thread.ThreadService
10199
import org.scijava.util.ColorRGB
102100
import org.scijava.util.Colors
103-
import org.scijava.util.VersionUtils
104-
import sc.iview.commands.demo.animation.ParticleDemo
105101
import sc.iview.commands.edit.InspectorInteractiveCommand
106102
import sc.iview.event.NodeActivatedEvent
107103
import sc.iview.event.NodeAddedEvent
@@ -131,12 +127,10 @@ import java.util.function.Predicate
131127
import java.util.stream.Collectors
132128
import kotlin.collections.ArrayList
133129
import kotlin.collections.HashMap
134-
import kotlin.collections.LinkedHashMap
135130
import kotlin.concurrent.thread
136131
import javax.swing.JOptionPane
137132
import kotlin.math.cos
138133
import kotlin.math.sin
139-
import kotlin.system.measureTimeMillis
140134

141135
/**
142136
* Main SciView class.
@@ -1689,22 +1683,28 @@ class SciView : SceneryBase, CalibratedRealInterval<CalibratedAxis> {
16891683
}
16901684

16911685
private var originalFOV = camera?.fov
1686+
private var originalWinSize = getWindowSize()
16921687

16931688
/**
1694-
* Enable VR rendering
1689+
* Enable or disable VR rendering. Automatically stores the original controls and FOV and restores them
1690+
* after VR is toggled off again.
1691+
* @param resizeWindow changes the window resolution to match the stereo rendering of the selected headset.
1692+
* @param resolutionScale Factor that allows changing the VR resolution
16951693
*/
1696-
fun toggleVRRendering() {
1694+
fun toggleVRRendering(resizeWindow: Boolean = true, resolutionScale: Float = 1f) {
16971695
var renderer = renderer ?: return
16981696

16991697
// Save camera's original settings if we switch from 2D to VR
17001698
if (!vrActive) {
17011699
originalFOV = camera?.fov
1700+
originalWinSize = getWindowSize()
17021701
}
17031702

17041703
// If turning off VR, store the controls state before deactivating
17051704
if (vrActive) {
17061705
// We're about to turn off VR
17071706
controls.stashControls()
1707+
setWindowSize(originalWinSize.first, originalWinSize.second)
17081708
}
17091709

17101710
vrActive = !vrActive
@@ -1721,6 +1721,20 @@ class SciView : SceneryBase, CalibratedRealInterval<CalibratedAxis> {
17211721
if (hmd.initializedAndWorking()) {
17221722
hub.add(SceneryElement.HMDInput, hmd)
17231723
ti = hmd
1724+
// Disable the sidebar if it was still open
1725+
if ((mainWindow as SwingMainWindow).sidebarOpen) {
1726+
toggleSidebar()
1727+
}
1728+
if (resizeWindow) {
1729+
val perEyeResolution = hmd.getRenderTargetSize()
1730+
// Recommended resolution is about x1.33 larger than the actual headset resolution
1731+
// due to distortion compensation.
1732+
// Too high resolution gets in the way of volume rendering, so we scale it down a bit again
1733+
setWindowSize(
1734+
(perEyeResolution.x * 2f / 1.33f * resolutionScale).toInt(),
1735+
(perEyeResolution.y / 1.33f * resolutionScale).toInt()
1736+
)
1737+
}
17241738
} else {
17251739
logger.warn("Could not initialise VR headset, just activating stereo rendering.")
17261740
}
@@ -1938,15 +1952,15 @@ class SciView : SceneryBase, CalibratedRealInterval<CalibratedAxis> {
19381952
// Update the main window frame if it exists
19391953
if (mainWindow is SwingMainWindow) {
19401954
val swingWindow = mainWindow as SwingMainWindow
1941-
swingWindow.frame.setSize(width, height)
1955+
val scale = getScenerySettings().get("Renderer.SurfaceScale") ?: Vector2f(1f)
1956+
val scaledWidth = (width / scale.x()).toInt()
1957+
val scaleHeight = (height / scale.y()).toInt()
1958+
// We need to scale the swing window with taking the surface scale into account
1959+
swingWindow.frame.setSize(scaledWidth, scaleHeight)
19421960

19431961
// Update the renderer dimensions
1944-
renderer?.let { r ->
1945-
r.reshape(width, height)
1946-
}
1947-
1948-
// Update camera aspect ratio
1949-
camera?.perspectiveCamera(50.0f, width, height, 0.1f, 1000.0f)
1962+
// TODO Is this even needed? Since outdated semaphores will trigger a swapchain recreation anyway
1963+
renderer?.reshape(width, height)
19501964
}
19511965

19521966
log.info("Window resized to ${width}x${height}")

0 commit comments

Comments
 (0)