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:
DEF
keyword.
USE
keyword.
When you DEF
ine 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:
USE
d it wouldn't
be blue by default.
USE
MySlab we create a second appearance of
it in the scene, colour it, and translate it to a new position.
height
field of MySlab to animate a change in height,
both MySlab objects in the final scene would change
height at the same time.
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 DEF
ine 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 DEF
ined
and when it is USE
d, 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 about sharing nodes in Open Inventor is available in chapters 3 and 11 of Inventor Mentor.