9.3. The apply() Method

The apply() method of the highlight action renders the scene graph in such a way that selected objects stand out. You could use a two-pass approach, in which the scene is rendered first, and the additional highlights are rendered second. Or your highlight action might render only the selected objects, or render selected objects in a totally different manner (for example, as boxes or spheres). As for any action, the apply() method is an overloaded virtual function (it can take a node, a path, or a path list). The derived highlight class provides stubs for the path and path list forms of the apply() method. Because selection makes the most sense in the context of a scene graph (since the selection list is a list of paths), highlighting paths and path lists is not defined. The function stubs simply call the corresponding apply() method of SoGLRenderAction SoGLRenderAction SoGLRenderAction for paths and path lists.

It's also worth examining the apply() method of the sample classes in more detail, since it uses caching for improved efficiency. As shown in Example 9.2, “ OverlayHL.c++, the apply() method uses a search action to find the selection node and then caches this path. Paths from the root of the scene graph to the selected objects are constructed as follows:

  1. The apply() method first determines whether the cached path to the selection node is still valid. If the root of the new path is the root of the scene graph and the tail is the selection node, it considers the cached path to be valid (since paths are automatically updated when nodes are added or removed).

  2. Next, it checks to be sure that something is selected. The selection node keeps track of the paths to the selected objects. The path from the root to the selection node (in Figure 9-1, the path from Node A to Node B) is prepended to each of the paths maintained in the selection list.

    Highlight Path

    Figure 9.1.  Highlight Path

    If the selection path ends in a node kit, additional work is performed to extend the path all the way down to the selected object. In Figure 9-1, the selection path is from Node B to Node D (a node kit). First, the last kit in the node kit path is obtained (here, Node D). Then, the node kit path is cast to a full path, and the tail of that path is obtained (Node E). Figure 9.1, “ Highlight Path ” shows the complete highlight path, which extends from the root of the scene graph to the selection node (Node A to Node B), and from the selection node to the selected object (Node B to Node E).