Multiple viewports to display one world

Multiple Viewports example
view3dscene with 4 viewports

1. Introduction

It is possible to have multiple viewports visible at the same time. As TCastleViewport is a regular user interface control, you can add as many viewports as you like on a particular TCastleView. And you can configure the size and position of each viewport.

These viewports may even display the same world (but from different cameras). This feature is very useful for split-screen games. Or if you want to show additional view of the world (e.g. from the camera attached to a missile, or from camera observing the world from the top).

2. Usage

To use this feature, just set the Items property of one viewport to Items from another viewport. Like this:

NewViewport.Items := ExistingViewport.Items;
Note
You cannot (yet) do this from the CGE editor. In the editor you can set multiple viewports and their cameras, but you cannot yet make them share the same items.

Moreover, when sharing Items across multiple viewports, all the cameras (from all the viewports) should also be added to the same Items (TCastleRootTransform). You always want to keep the connection that "the camera used by viewport, set in Viewport.Camera, is also part of the Viewport.Items ".

This usually means you should extend above example to remove NewViewport.Camera from the old world and add it to the new one. Like this:

NewViewport.Items.Remove(NewViewport.Camera);
NewViewport.Items := ExistingViewport.Items;
NewViewport.Items.Add(NewViewport.Camera);

3. Examples


To improve this documentation just edit the source of this page in AsciiDoctor (simple wiki-like syntax) and create a pull request to Castle Game Engine WWW (cge-www) repository.