Cameras
Cameras are special group nodes which transform the objects in their sub tree from the 3D „world“ to the 2D screen. The objects are projected onto a rectangular area on screen. This area is called the viewport. Learn more about:
Cameras can be placed at arbitrary positions in the scene and freely oriented. Both common projection modes are supported: ‘perspective’ and ‘orthogonal’ or ‘parallel’.
Cameras in ILNumerics are of the type ILNumerics.Drawing.Camera. In contrast to other popular scene graph concepts,
Cameras in ILNumerics only ‚see‘ objects which are contained in their sub tree!
Position and Orientation
Three vectors are used to determine the position and orientation of the camera:
- Position places the camera at a point in 3D space. The camera itself is imagined as a point in space, having a zero volume.
- LookAt is the point in space where the camera is aiming at. Objects at the lookat point will appear in the center of the screen.
- Top is the direction of the camera up vector. It determines the rotation of the camera around the LookAt direction. Altering the Top vector will cause the objects in the scene to appear rotated – just like rotating a real camera without changing the point in focus.
The default camera is located at (0, 0, 10), looking at the origin (0, 0, 0) with a Top vector of (0, 1, 0).
- ZNear is the near clipping plane distance. Objects which are closer to the camera than this distance will be clipped / not shown.
- ZFar is the far clipping plane distance. Objects which are further away from the camera than this distance will be clipped / not shown.
The camera is placed at the left side of the gear:
Note that the same effect could have been created by making the camera rotate all its children by 90° in the other direction:
Now, we reposition the camera inside a tooth of the gear, looking towards the hole. The up vector is configured along the Z axis. The LookAt is also adjusted to achieve a more centered view:
Field of View
This doesn’t give us a very good overview. We can fix this by trimming the Camera.FieldOfView property:
Camera.FieldOfView trims the cameras aperture and makes the view frustum wider (smaller values) or closer (larger values). The default is 10. Apertures do only affect perspective cameras (see below: Projection).
Near and Far Plane
Camera.ZNear and Camera.ZFar is used to define the minimum and maximum distance from the camera. Only objects, which lay in between these limits are rendered.
In order to clip everything from a distance of more than 0.2 units from the camera:
The settings for the near and far plane serve as the limits or borders of the visible world in the ILNumerics Visualization Engine. Be careful not to be too generous when adjusting these values! Typically, ZNear should stay at around 1 (the default). ZFar should lay not too far behind the object most far away. One might follow the naive idea to extend this range to, let's say float.MaxValue and expect to be 'safe' and have all new objects to lay within the visible range of the scene – don't do this! It would create strongly visible artifacts by hurting depth buffer precision.
Projection
Two popular projection methods exist for cameras: perspective projection and orthogonal projection. With perspective projection, distant objects appear smaller than objects close to the camera. This corresponds to a more natural reproduction of „real“ physical objects. However, for scientific visualizations, orthographic projection is often used to minimize distortion of technical objects and improve readability of scales and axes. By default, cameras in common 3D scenes use perspective projection. For cameras in ILNumerics.Drawing.Plotting scenes, orthographic projection is the default:
Cameras of objects within the plotting API use orthographic projection by default.
In order to use perspective projection on a plotting scene, set the plot cubes projection to ‘Perspective’:
Note the different default settings for common 3D scenes (f.e. panel.Scene.Camera) and plotting scenes! However, both modes are applicable in any situation by reconfiguring the Camera.Projection setting (See the Plotting API chapter in order to learn how to create plots in ILNumerics).
Viewports
Viewports determine the location and size of the projected output within the available screens control area. Viewports are configured by the ScreenRect property.
A scene may contain an arbitrary number of cameras – each corresponds to an individual viewport. Viewport areas on screen may overlap or use disjunct areas. The user is responsible for distributing the viewports over the available screen area. By default, the viewport for a camera uses the full screen rendering area.
The ScreenRect property is determined by a System.Drawing.RectangleF structure, defined in relative units as fractions of the whole available panel area: ie. (0, 0, 1, 1) corresponds to the full available panel area, (0.5, 0, 0.5, 0.5) corresponds to the upper right quadrant.
The viewport of a camera also determines the area for user interaction which is handled by that camera. Mouse events are captured and redirected to the camera whose viewport rectangle contains the current mouse position. Multiple independent functional and visual areas can be realized that way.
In the following example, an interface very similar to a simple CAD application is created. The output area is divided into 3 sections:
- the main 3D output (perspective, lit)
- an orthographic left view (wireframes) and
- an orthographic top view (wireframe)
The code for this example configures 3 cameras: the default camera and two new cameras, which are added to the scene of ilPanel1.
The ScreenRect for every camera is configured in relative units: The main pane uses a screen area of the full height (1.0) and 0.6th of the full width. The smaller panes start in X where the main pane ends (0.6) and use only half of the available height (0.5).
The line
positions the main camera to achieve a nicer 3D view. Next, an additional light is added to the scene. It enhances the visibility of the gear with some backlight (See also: Lighting Section).
For the top view pane we use the default view of a camera – up front along the Z axis. However, for the left view pane we position the camera as seen before. For both top and left view panes, we need to scale the content down since orthographic projection does not scale by distances and we would get an unpleasant large result for these cameras otherwise. The scaling is done by directly supplying the Transform property with a scale matrix in the object initialization code for each camera.
A camera only „sees“ objects within its subtree. Now that we have split our rendering surface into three areas / cameras / viewport sections, we add content to each camera. This is done by simply copying the subtree from camMain to camLeft and camTop. As described in the section Duplicating Subscenes this creates efficient clones of the source objects. The clone shares all buffers with its original but still allows a good amount of individual configuration.
Here, we set the Visible property of the gear fill shape to false.
Aspect Ratio
The AspectRatioMode property of Camera controls the way objects appear in 3D space regarding the ratio of their width to their height in screen coordinates. This is directly related to the aspect ratio of Camera.ScreenRect (see above). Two values are available for AspectRatioMode:
- StretchToFill - the viewport of the camera tightly follows the extent of the configured ScreenRect. For non-square screen rectangles this may lead to distortion for the graphical objects.
- MaintainRatios - maintains an equal aspect ratio for the content. The viewport of the Camera is adopted to fit into the current Camera.ScreenRect. I.e.: to fit into the smaller of ScreenRect.Height or ScreenRect.Width. The corresponding other side of the viewport is extended to match the area given by ScreenRect. This is the default value for all camera nodes.
Now, all this is true for general camera nodes. Plot cubes, however, while deriving from Camera, slightly deviate from this behavior, in order to better match their special purpose. See: plot cubes.