Language > Objects > Creating an Object Graphics Display > Rendering Objects

# Rendering Objects

In Object Graphics, rendering occurs when the Draw method of a destination object is called. A scene, viewgroup, or view is typically provided as the argument to this Draw method. This argument represents the root of a graphics hierarchy. When the destination’s Draw method is called, the graphics hierarchy is traversed, starting at the root, then proceeding to children in the order in which they were added to their parent.

For example, suppose we have the following hierarchy:

oWindow = OBJ_NEW('IDLgrWindow')

oView = OBJ_NEW('IDLgrView')

oModel = OBJ_NEW('IDLgrModel')

oView->Add, oModel

oXAxis = OBJ_NEW('IDLgrAxis', 0)

oModel->Add, oXAxis

oYAxis = OBJ_NEW('IDLgrAxis', 1)

oModel->Add, oYAxis

To draw the view (and its contents) to the window, the Draw method of the window is called with the view as its argument:

oWindow->Draw, oView

The window’s Draw method will perform any window-specific drawing setup, then ask the view to draw itself. The view will then perform view-specific drawing (for example, clearing a rectangular area to a color), then calls the Draw method for each of its children (in this case, there is only one child, a model). The model’s Draw method will push its transformation matrix on a stack, then step through each of its children (in the order in which they were added) and ask them to draw themselves. In this example, oXAxis will be asked to draw itself first; then oYAxis will be asked to draw itself. Once each of the model’s children is drawn, the transformation matrix associated with the model is popped off of the stack.

Thus, for each object in the hierarchy, drawing essentially consists of three steps:

- Perform setup drawing for this object.
- Step through list of contained children and ask them to draw themselves.
- Perform follow-up drawing actions before returning control to parent.

The order in which objects are added to the hierarchy will have an impact on when the objects are drawn. Drawing order can be changed by using the Move method of a scene, viewgroup, view, or model to change the position of a specific object within the hierarchy.

The first time a visualization object (such as an axis, plot line, or text) is drawn to a given destination, a device-specific encapsulation of its visual representation is created and stored as a cache. Subsequent draws of this visualization object to the same destination can then be drawn very efficiently. The cache is destroyed only when necessary (for example, when the data associated with the visualization object changes). Graphic attribute changes (such as color changes) typically do not cause cache destruction. To gain maximum benefit from the caches, modification of object graphic properties should be kept to bare minimum.

Note: See Performance Tuning Object Graphics for other performance enhancing strategies.

## Simple Plot Example

The following section shows the IDL code used to create a simple object tree. While you are free to enter the commands shown at the IDL command line, remember that the IDL Object Graphics API is designed as a programmer’s interface, and is not as well suited for interactive, ad hoc work at the IDL command prompt as are IDL Direct Graphics.

The following IDL commands construct a simple plot of an array versus the integer indices of the array. Note that no axes, title, or other annotations are included; the commands draw only the plot line itself. (This example is purposefully simple; it is meant to illustrate the skeleton of a graphics tree, not to produce a useful plot.)

; Create a view 2 units high by 100 units wide

; with its origin at (0,-1):

view = OBJ_NEW('IDLgrView', VIEWPLANE_RECT=[0,-1,100,2])

; Create a model:

model = OBJ_NEW('IDLgrModel')

; Create a plot line of a sine wave:

plot = OBJ_NEW('IDLgrPlot', SIN(FINDGEN(100)/10))

; Create a window into which the plot line will be drawn:

window = OBJ_NEW('IDLgrWindow')

; Add the plot line to the model object:

model->ADD, plot

; Add the model object to the view object:

view->ADD, model

; Render the contents of the view object in the window:

window->DRAW, view

To destroy the window and remove the objects created from memory, use the following commands:

; Destroy the window and the view.

; Destroying the view object destroys all

; of the objects contained in the view:

OBJ_DESTROY, [window, view]