Skip to content

cvcGL: ViewportManager — N cameras/viewports in one window (PiP core) - #384

Merged
transfix merged 1 commit into
masterfrom
feat/cvcgl-viewport-core
Sep 21, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/cvcgl-viewport-core

Conversation

@transfix

@transfix transfix commented Sep 21, 2026 •

Copy link
Copy Markdown
Owner

cvcGL: ViewportManager — N cameras/viewports in one window (PiP core)

First, additive commit of the multi-viewport / picture-in-picture work. It
introduces the core the rest builds on and does not touch SceneRenderer.

Why

cvcgl_renderer's own test documents the gap this closes:

Two renderers over ONE scene are NOT independent: SceneGraph::setRenderer
is a single attachment, so the second constructor takes every actor and the
first is left drawing an empty frame. Nothing errors … a caller building a
picture-in-picture this way gets a blank main view and no diagnostic.

The demos want the minimap generalised: dedicated cameras for inset
viewports that show an alternate view of the main scene — or an entirely
separate scene graph — all in one application instance and one WebGL
context
(WASM has a single canvas, so multiple windows is not an option).

What

  • Viewport — one rectangle of the manager's single window: its own
    vtkRenderer, CameraController (rooted at
    "<scene prefix>.viewers.<name>.camera", so a PiP inset aims/follows
    independently of the main view) and the SceneGraph it draws. Placed by a
    normalized region (survives resize) and a composite layer; setBackground(…, opaque) chooses whether it clears its rect (opaque inset) or preserves the
    view under it (translucent overlay). Constructed only by the manager.
  • ViewportManager — owns ONE vtkRenderWindow and composites N
    Viewports as SetViewport/SetLayer regions in a single
    window->Render(). Constructed like a SceneRenderer (main scene + size +
    offscreen flag) and auto-creates a full-screen primary viewport, so the
    common single-view case is a drop-in. addSceneViewport(name, scene, region, layer) adds an inset over a different scene; re-attaching a scene another
    viewport already draws is now a loud std::invalid_argument
    — the exact
    silent trap above. Keeps the window's layer count above the highest layer in
    use; render() drains each scene's pending graphics events then composites
    once; frameRGB/writePNG capture the whole window.

Tests

New cvcgl_viewport (offscreen, headless CI):

  • a primary + an inset over a second scene both composite in one window —
    each rect shows its own background hue, and each scene's geometry reaches
    only its own viewport (per-rect frame delta);
  • the double-attach, duplicate-name, unknown-lookup and bad-size guards all
    fire.

Full cvcgl suite (28 tests) green locally on Windows/MSVC (built in-tree via
cvcpkg deps).

Follow-ups (separate PRs)

  • Mirror viewport — an alternate view of a scene another viewport already
    draws (per-frame GetViewProps sync); this is the true minimap.
  • Input router — FindPokedRenderer so a click/drag targets the viewport
    under the cursor.
  • State layout — persist region/layer/visible + active_viewport
    under the existing .viewers.<name>.* tree.
  • SceneRenderer → thin facade over a one-viewport ViewportManager
    (back-compat gated).
  • Promote the nav-city minimap demo onto this path.
  • SWIG bindings + Python ChaseCamera shim so volrover3 / grl-snam get the
    same multi-viewport surface from Python.

The picture-in-picture path the cvcgl_renderer test documents as unsolvable
with two SceneRenderers over one scene (a second SceneGraph::setRenderer
silently blanks the first view, no diagnostic). ViewportManager owns ONE
vtkRenderWindow — one GL context, the mandatory shape under WASM's single
canvas — and composites N Viewports as SetViewport/SetLayer regions drawn by a
single window->Render().

  * Viewport: one rectangle of the window with its OWN vtkRenderer,
    CameraController (rooted at "<scene prefix>.viewers.<name>.camera", so a
    PiP inset aims/follows independently) and the SceneGraph it draws. Placed
    by a normalized region (survives resize) and a composite layer; opaque vs
    translucent background controls whether it clears its rect.
  * ViewportManager: constructed like a SceneRenderer (main scene + size +
    offscreen) and auto-creates a full-screen PRIMARY viewport over that scene,
    so the single-view case is a drop-in. addSceneViewport adds an inset over a
    DIFFERENT scene; re-attaching an already-drawn scene is now a LOUD error
    instead of the silent blank. Keeps the window layer count above the highest
    layer in use. render() drains every viewport's scene then composites once;
    frameRGB/writePNG capture the whole window.

This is the generalised DBG minimap: alternate views of one scene, or entirely
separate scene graphs, all in one app instance and one WebGL context.

Additive — SceneRenderer is untouched (the SceneRenderer<->facade unification
is a follow-up). New cvcgl_viewport test: a primary + an inset over a second
scene both composite in one offscreen window (each rect shows its own
background hue, each scene's geometry reaches only its own viewport), and the
double-attach / duplicate-name / bad-size guards all fire. Full cvcgl suite
(28 tests) green locally.
@transfix
transfix merged commit 13510ad into master Sep 21, 2026
13 checks passed
@transfix
transfix deleted the feat/cvcgl-viewport-core branch September 21, 2026 01:44
transfix added a commit that referenced this pull request Sep 21, 2026
…p analysis (#389)

Concrete defects a gap-analysis pass found in the merged PiP stack (#384/#386/
#387), fixed before the minimap demo exercises them:

  * Mirror double-drew 2-D overlays: syncMirrorProps copied EVERY prop, so a
    mirror of a scene with FpsHud/ScreenTextHud drew the HUD a second time on the
    inset. It now skips vtkActor2D (a window-space overlay, not scene geometry) —
    matching the shipping wasm PiP.
  * Mirror rendered flat: syncMirrorProps copied view props but not lights, so a
    mirror of a StageLighting-rigged scene lost its lighting. It now copies the
    source's lights (world-space, correct from the mirror's own camera); with
    none, the mirror falls back to VTK's automatic headlight.
  * MB::Right was not the no-op the header claims: its button-down latched a drag
    target and called setActiveViewport, so a right-click silently stole keyboard
    focus and released the prior viewport's held keys. routeMouseButton now
    returns immediately for Right.
  * CameraController::viewportAspect preferred the WINDOW size over the renderer's
    for the Map/ortho fit, so a PiP inset in Map mode framed to the wrong aspect;
    it now prefers the viewport (renderer) size, matching the pan scaling, and
    falls back to the window before the first frame.
  * Fixed the stale "addMirrorViewport, a follow-up" comment (it shipped in #386).

Tests: cvcgl_viewport gains a check that a 2-D actor on the source does NOT reach
the mirror while a source light DOES; cvcgl_viewport_input gains a Right-button
no-op check (no focus steal, no camera motion). Full cvcgl suite (29) green
locally.
transfix added a commit that referenced this pull request Sep 21, 2026
…ython (#390)

Closes the largest gap the PiP work left: the whole ViewportManager/Viewport/
input-router surface (cvcGL #384/#386/#387) was C++-only, so volrover3 and
grl-snam — both Python — could not build multi-viewport or minimap layouts. This
wraps it, so the Python API mirrors C++:

    vm = pycvc_gl.ViewportManager(scene, 1024, 768, False, "main")
    inset = vm.addSceneViewport("inset", scene2, (0.62, 0.05, 0.97, 0.42), 1)
    mini  = vm.addMirrorViewport("mini", "main", (0.62, 0.58, 0.97, 0.95), 2)
    inset.camera().setMode(pycvc_gl.CameraController.Mode_Fly)  # per-viewport
    vm.routeMouseButton(pycvc_gl.ViewportManager.MouseButton_Left, True, x, y)
    vp = vm.viewportAt(x, y)          # None over the gutter
    frame = vm.frameRGB()            # bytes

Binding details, following the existing pycvc_gl patterns:
  * frameRGB() -> bytes (a scoped typemap, like SceneRenderer's — the SceneRenderer
    one does not apply to ViewportManager).
  * region as a Python 4-sequence in (addSceneViewport/addMirrorViewport) and a
    4-tuple out (Viewport.region()), via in/argout typemaps.
  * Viewport has a private ctor -> %nodefaultctor; it is only ever handed out by
    the manager. renderer()/renderWindow()/mirrorSource() reuse the existing
    vtkRenderer*/vtkRenderWindow* -> live vtkmodules typemaps.
  * KEEPALIVE chain mirrors C++ ownership so a live child pins its parent: the
    manager keeps its scene(s); every handed-out Viewport (primary/viewport/
    viewportAt/activeViewport/add*Viewport) keeps the manager; a Viewport's
    camera() keeps the Viewport.
  * MouseButton enum -> ViewportManager.MouseButton_Left/Middle/Right.

New test_pycvc_gl_viewport.py exercises the surface end to end: auto primary,
addSceneViewport over a second scene + addMirrorViewport, viewportAt (layer/
visibility/inputEnabled/gutter), focus-follows-click into cvc::state, the
per-viewport camera (its state under the viewer tree), region tuples, and
frameRGB bytes — self-skipping without a GL driver, hard-exiting past cvcGL's
static-teardown race like the other pycvc_gl tests.

Validated locally by SWIG generation (all 24 ViewportManager + 15 Viewport
methods, the MouseButton enum, and both typemaps wrap cleanly); the compiled
.pyd + this test run in the cvcpkg pycvc-gl recipe, which builds against the
python-enabled VTK the module needs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant