Most graphics APIs are state-based. The program sets up drawing state in the display manager that defines the colour, font, translation, fill texture, etc. used by drawing operations. This reduces the amount of data that needs to be passed across the kernel boundary or network for each drawing operation.
How do you make StructuredGraphics composable?
If you are writing a graphics program that uses StructuredGraphics
, you may want to define a composite shape and then draw it in different places on the display, at a different size, rotated or in a different colour.
Each StructuredGraphics object should encapsulate a modification to the drawing state held in the display manager, such as the SingleTransform. Composing graphical objects will cause the child to inherit the drawing state created by its tree of containers.
Define the visual objects using a DirectedAcyclicGraph
(DAG) in which the leaves represent primitive shapes and intermediate nodes either encapsulate a small, reversible change to the state of the display manager that is applied to a single subgraph or compose multiple subgraphs.
Drawing is performed by walking over the graph in depth-first order, performing the following operations on the various types of nodes:
- Intermediate Nodes: apply the state change to the display manager, draw the subgraph, then reverse the state change in the display manager.
- Composite Nodes: draw each of their subgraphs in turn. Advanced systems can implement other composition operators, such as constructive area (or solid) geometry (CAG or CSG).
- Leaf Nodes: draw a primitive shape. The drawing will be affected by all the state changes applied to the display manager while traversing the graph.
A shape can be drawn multiple times, with different transformations and other properties, by defining it once and then linking it into different parts of the graph so that different transformations are applied to it each time it is visited during the graph traversal.
However, compared to a simple StructuredGraphics
framework, it is harder to map from the leaves of a SceneGraph
to areas of the display, because arbitrary transformations can be applied to the shapes at the leaves of the graph. These calculations are required to calculate minimim RefreshRectangles
or to implement interactivity.
Such mappings must be performed by walking over the graph, keeping track of transformations by performing the matrix multiplications in the visitor.
Even though this is computationally more expensive than algorithms required by a simple StructuredGraphics
framework, it can still reduce the drawing time significantly when used to calculate minimum RefreshRectangles
is often used as a high-level interface for assembling CompiledGraphicCommands
is an optimisation of the SceneGraph
This pattern is widely used in toolkits for 3D graphics. In particular, Open
and Java3D all use
s to structure scenes. It is less widely used in 2D graphics. Examples include the MorphicInterface
framework in the SelfLanguage
, the SceneBeans
and Jazz toolkits for Java, the GnomeCanvas
display widget, and the SVG graphics file format defined by the W3C.
Also see GraphicsPatterns