[Up] [Previous] [Next]

12. Sharing Nodes

What if you need two copies of an object in your image, something like a Cube for example, each object a different colour. You could write an open inventor file that contains duplicate instructions for each object, something like the following:

    Separator {
      Material {
	diffuseColor  0.0 0.0 1.0
      }
      Cube {
	width 2
	height 3
	depth 1
      }
    }
    Separator {
      Translation {
        translation  5 0 0
      }
      Material {
	diffuseColor  1.0 0.0 0.0
      }
      Cube {
	width 2
	height 3
	depth 1
      }
    }

This would place two identically-shaped cubes at two different locations and with two different colours (blue and red, in this case). This description would work, but why type more description information than you have to? It is possible to share nodes so that you can access them from more than one place in the scene graph. When the scene graph is traversed, a shared node will be encountered multiple times. Each time it will have an effect on the image, but the effect at each encounter will depend on what other node-generated effects are active at that moment. By sharing nodes you not only make it easier for you to describe scenes but you also make it easier for Open Inventor to manage its own memory resources more efficiently. There are two steps to sharing nodes:

  1. You must first define a named instance of the node. You do this with the DEF keyword.
  2. You later use the shared node as many times as you want by referring to it by name with the USE keyword.

When you DEFine the node, this counts as the first place where the node appears in the scene graph. Anywhere that you USE the node counts as an additional appearance in the scene graph. If we revise the above code fragment to use DEF and USE we get:

    Separator {
      Material {
	diffuseColor  0.0 0.0 1.0
      }
      DEF MySlab Cube {
	width 2
	height 3
	depth 1
      }
    }
    Separator {
      Translation {
        translation  5 0 0
      }
      Material {
	diffuseColor  1.0 0.0 0.0
      }
      USE MySlab
    }

You should note a few things in this code:

There is one final point to touch on for this subject. It is possible (and in fact sometimes desirable) to use a name more than once. While this tends to be more useful for writing C++ code that searches for all nodes with a particular name, it can be handy even in writing .iv files. By using the same name we indicate that various nodes play very similar roles in our scene description. However, this brings up a problem: if we DEFine two nodes with the same name, how do we know which one we'll get when we USE that name? The solution is to append a number to the name both when it is DEFined and when it is USEd, like "MySlab+0" and "MySlab+1". Open Inventor recognizes that the name of those nodes is meant to be "MySlab", and that "+0" and "+1" are used to refer to each particular node with that name.

More Information

More information about sharing nodes in Open Inventor is available in chapters 3 and 11 of Inventor Mentor.


MIT home page HTML written and maintained by Reid M. Pinchback (reidmp@mit.edu)
Last modified 96/06/10; copyright © 1996 MIT