http://tecfa.unige.ch/ico/navi/tex2html/top.gifhttp://tecfa.unige.ch/ico/icons/vrml97-icon.gif next up previous contents index
Next: 2.2 Inlining other VRML Up: 2. Modularization and Abstraction Previous: 2. Modularization and Abstraction

   
2.1 Multiple instances of the same object

An important feature of VRML (and any ``programming language'') is being able to reuse an object by giving it a name and making copies of it. In the example below you can see that each node has been named with the DEF node. Most nodes can be named, not just geometry nodes! Even if you don't plan reusing objects, it is good practise to name at least major nodes because it implictly adds documentation to your file and you never know if later you want to reuse an object in bigger scenes and/or modify it's contents with a script. An example of ``names everywhere'' is below:

#VRML V2.0 utf8
DEF ROOTNODE Transform {
     children [
         DEF SOL Transform {
             children Shape {
                 appearance Appearance {
                     material Material {
                         diffuseColor 1 1 0
                         shininess 1
                       }
                   }
                 geometry Sphere {
                     radius 5
                   }
               }
           }
         DEF TERRA Transform {
             translation 10 10 10
             children [
                 DEF EARTH Transform {
                     children Shape {
                         appearance Appearance {
                             material Material {
                                 diffuseColor 0 1 0
                               }
                           }
                         geometry Sphere {
                           }
                       }
                   }
                 DEF LUNA Transform {
                     translation 1 1 1
                     children Shape {
                         geometry Sphere {
                             radius 0.5
                           }
                       }
                   }
               ]
           }
         DEF JUPITER Transform {
             children [
                 # Jupiter and it's 14 moons in here
               ]
           }
      ]
  }

A good and slightly more complex example is the solar system, look at the source form Pesce's book (translated into VRML II). In addition it has anchors to other URLs (see section 1.2.6).

Now let's see how we can reuse a named object. This example shows for instance how to do the spikes of a wheel (taken from [Ames et al., 1996b, page 90]).

Example 2.1.1   Rotation: Spikes of a Wheel  

VRML: ../examples/inter/inter-rotation-2.wrl
Source: ../examples/inter/inter-rotation-2.text

Note the usage of DEF and USE which will allow you to use multiple instances of the same Object.       In order to better understand what (where) we have drawn we also inlined a drawing of the x (red color), y (green) and z (blue) axis.

You can make use of the coordinates.wrl file yourself if you are not sure what your translations and rotations do (inlining is explained in section 2.2).

#VRML V2.0 utf8

# Draws 6 spikes using 3 cylinders
# we give a name to our first cylinder so that we can reuse it again
# Check the DEF and USE commands

DEF AXE Shape {
   geometry Cylinder {
      radius 0.5
      height 5
   }
}
Transform {
   rotation 0 0 1  1.0472
   children USE AXE
}
Transform {
   rotation 0 0 1  2.0944
   children USE AXE
}

# Import the coordinates axes (ignore the code right now)
Inline {
   url "coordinates.wrl"
   bboxSize 10 10 10
   bboxCenter 0 0 0
}

The next example translates and scales this wheel, i.e. it squeezes it together (half size on y and z axis and a quarter size on the x axis).

Example 2.1.2   Rotation: Spikes of a Wheel II  

VRML: ../examples/inter/inter-rotation-3.wrl
Source: ../examples/inter/inter-rotation-3.text

#VRML V2.0 utf8

Transform {
   children [
      DEF TUBE Shape {
         appearance Appearance {
            material Material {
               diffuseColor 0.6 0.3 0.1
            } }
         geometry Cylinder {
            radius 0.5
            height 5
         }
      }
      Transform {
         rotation 0 0 1  1.0472
         children USE TUBE
      }
      Transform {
         rotation 0 0 1  2.0944
         children USE TUBE
      }
     ]
}
Transform {
   translation 0 5 0
   scale       0.25 0.5 0.5
   children USE TUBE
}
Transform {
   translation 0 5 0
   scale       0.25 0.5 0.5
   rotation    0 0 1  1.0472
   children USE TUBE
}
Transform {
   translation 0 5 0
   scale       0.25 0.5 0.5
   rotation    0 0 1  2.0944
   children USE TUBE
}

# Import the coordinates axes (ignore the code)
Inline {
   url "coordinates.wrl"
   bboxSize 10 10 10
   bboxCenter 0 0 0
}


next up previous contents index http://tecfa.unige.ch/ico/navi/tex2html/top.gifhttp://tecfa.unige.ch/ico/icons/vrml97-icon.gif
Next: 2.2 Inlining other VRML Up: 2. Modularization and Abstraction Previous: 2. Modularization and Abstraction
D.K.S. - 1998-03-18