1. Introduction

Camera determines what do you see in the viewport.

It defines 3 vectors:

  1. TCastleCamera.Position - observer position.

  2. TCastleCamera.Direction - direction in which you look.

  3. TCastleCamera.Up - together with direction, the up vector determines the camera orientation.

Camera also defines projection:

See the Tutorial: Designing a 3D world and Tutorial: Designing a 2D world to see the typical workflow how do you manipulate the camera using the editor.

2. The initial camera

There are various ways to set the initial camera:

  • If you design your world using the Castle Game Engine editor, we recommend to set the initial camera following the Tutorial: Designing a 3D world. This means that you navigate to set the best camera, and then use "Camera Current := Initial (stored in the design file)" command.

  • Another approach is to just set the camera vectors by code during your state start. Call Viewport.Camera.SetView like this:

      Vector3(-11.34, 30.04, 96.07), // position
      Vector3(0.10, -0.49, -0.87), // direction
      Vector3(0.35, 0.83, -0.43), // up (current)
      Vector3(0.00, 1.00, 0.00) // gravity up

    You can even generate such Pascal code: Use the "Clipboard → Print Current Camera (Viewpoint) (Pascal)" menu item in view3dscene.

  • Alternatively, automatically initialize the camera defaults (including position, direction, up vectors) based on the information in the model.

    To activate this auto-detection, set TCastleViewport.AutoCamera to true.

    The way this auto-detection works:

    • If the scene set as Viewport.Items.MainScene defines a default camera, then use it.

      For example glTF format allows to define a default camera. Blender can export such glTF models.

      Models in In X3D can also define a default camera, using X3D Viewpoint or OrthoViewpoint nodes. If you write X3D files by hand, you can even generate such nodes using the "Console → Print Current Camera (Viewpoint)" menu item in view3dscene.

    • Otherwise (if there is no Viewpoint node,or you didn’t even set MainScene) then the camera will be auto-detected to look at the world bounding box.

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.