# Rendering

## Working with viewports and positioning the camera

OVITO manages multiple viewports, which show the three-dimensional scene from different directions. There is always one selected viewport, called the active viewport. It can be accessed from a script through the `activeViewport` variable, which holds a reference to a `Viewport` object.

A `Viewport` object provides various properties and functions that allow to set the position, orientation, and projection parameters of the virtual camera. For instance, to set up a camera with perspective projection, positioned at the xyz coordinates (100, 85, 50), and looking toward the origin, you would call the `Viewport.perspective()` function:

    activeViewport.perspective(
    	Point(100, 85, 50), 			// The camera's position
    	Vector(-100, -85, -50), 		// The viewing direction
    	70.0 * Math.PI/180.0)			// The field of view angle

The third parameter specifies the vertical field of view (FOV) of the camera. We have set it to a 70 degree angle, which needs to be converted to radians first.

Alternatively, we can set up a camera with parallel projection as follows:

    activeViewport.ortho(Point(0,0,0), Vector(0,-1,0), 30.0))

Here we made the camera look along the negative Y-axis. Note that, for parallel cameras, the FOV parameter determines the size of the visible region (in this case 30.0 length units in the vertical direction).

## Rendering pictures and movies

To let OVITO render a picture of the current viewport, use the `Viewport.render()` function:

    activeViewport.render()

This will start the rendering process using the current render settings such as image size, output filename, background color etc.

Note that if you started OVITO with the `--nogui` command line switch to disable the graphical user interface of the program, OpenGL rendering is not possible. This is because the program runs in a headless mode, which doesn't require a graphics terminal and allows to execute scripts on remote machines. Unfortunately, this restriction also means you cannot use the normal OpenGL-based renderer anymore to create output images. The software-based Tachyon renderer therefore becomes the default when using the `--nogui` switch.

You can modify the current render settings before calling `render()` by changing the corresponding properties of the `renderSettings` object, which is part of the global `ovito` object:

    rs = ovito.renderSettings

    rs.imageWidth = 320                // Sets the width in pixels of the output image
    rs.imageHeight = 240               // Sets the height in pixels of the output image
    rs.filename = "frame.png"          // Sets the output filename
    rs.saveToFile = true               // Set this flag, otherwise the generated image will not be saved to disk.

    // Control which frames should be rendered:
    rs.renderingRangeType = RenderSettings.CURRENT_FRAME        // Render only the current animation frame (default)
    rs.renderingRangeType = RenderSettings.ANIMATION_INTERVAL   // Render all frames of the animation interval
    rs.renderingRangeType = RenderSettings.CUSTOM_INTERVAL      // Render a custom interval of frames

As a shortcut, you can directly pass the parameters to the `render()` function as an object literal. For example:

    activeViewport.render({ filename : "output_image.png", imageWidth : 320, imageHeight : 240 })