12.4. Writing a VRML/X3D file

Figure 12.3. VRML action classes



These two actions, SoToVRMLAction SoToVRMLAction SoToVRMLAction and SoToVRML2Action SoToVRML2Action SoToVRML2Action , are applied in the same manner as any other action. They convert an Open Inventor scene graph to a VRML 1.0 format or VRML97 (X3D) format scene graph respectively.

Specify options

Many options can be set. For instance, to keep unknown nodes, use the keepUnknownNodes() method. If shared geometry nodes in the input scene graph should be shared in the new scene graph, use the reuseGeometryNode() method. All these options must be set before applying the action.

Apply the action

As with other actions, the apply() method must be called on an Inventor scene graph.

  • getVRMLSceneGraph() returns a VRML 1.0-style scene graph.

  • getVRML2SceneGraph() returns a VRML 2.0-style scene graph.

Store the VRML file

The SoWriteAction SoWriteAction SoWriteAction action must be used to generate the VRML 1.0 (or 2.0) file. You must remember to specify the correct header string with the method SoOutput::setHeaderString() . You must also specify that the file is an ASCII file (the only storage mode available in VRML) with the method SoOutput::setBinary(FALSE) . The header strings should be:

#VRML V1.0 ascii

for VRML 1.0, and

#VRML V2.0 utf8

for VRML 2.0

Example 12.6. Example of conversion from Inventor format to VRML 2.0 format

This shows the conversion of an Inventor file containing a red cube with a translation (1, 0, 0) to a VRML 2.0 file using SoToVRML2Action.

#Inventor V2.1 ascii

  Separator
    {
    Translation
      {
      translation 1 0 0
      }
    Material
      {
      diffuseColor 1 0 0
      }
    Cube{}
    }
#VRML V2.0 utf8

  Collision
    {
    children
      Group
      {
      children [
        NavigationInfo
        {
        type [ "WALK" , "ANY" ]
          avatarSize [ 0.25 , 1.6 , 0.75 ]
        } ,

        WorldInfo
        {
        } ,

        Transform
        {
        children
          Shape
          {
          appearance
            Appearance
            {
            material
              Material
              {
              ambientIntensity 0.2
                diffuseColor 1 0 0
                specularColor 0 0 0
                emissiveColor 0 0 0
                shininess 0.2
                transparency 0
              }
            }
          geometry
            Box
            {
            }
          }
        translation 1 0 0
        } ]
      }
    collide FALSE
    }


Example 12.7. Extract of IvToVRMLAction.cxx

The example supplied in: $OIVHOME/src/Inventor/examples/Features/IvToVRMLAction/IvToVRMLAction.cxx is a utility that takes an Open Inventor file (.iv file) as an input parameter and generates a VRML 1.0 or VRML 2.0 file (depending on a command line option) as output.

The following code fragment shows the use of SoToVRML2Action SoToVRML2Action SoToVRML2Action in the source code of IvToVRMLAction.cxx:

...
Read Inventor input file in the separator umbrellaRoot.
...
SoToVRML2Action *toVRML2Action = new SoToVRML2Action;
toVRML2Action->apply(umbrellaRoot);

SoNode *convertedRoot = (SoNode *) toVRML2Action->getVRML2SceneGraph();
convertedRoot->ref();

SoOutput out;
out.openFile(outputfile);
out.setBinary(FALSE);
out.setHeaderString("#VRML V2.0 utf8");

SoWriteAction writeAction(&out);
writeAction.apply(convertedRoot);

out.closeFile();
  
            tobereviewed_C# : C# example does not seem to be implemented
          
...
Read Inventor input file in the separator root.
...
SoToVRML2Action toVRML2Action = new SoToVRML2Action();
toVRML2Action.apply(root);

SoNode convertedRoot = (SoNode ) toVRML2Action.getVRML2SceneGraph();

SoOutput out = new SoOutput();
out.openFile(outputFile);
out.setBinary(false);
out.setHeaderString("#VRML V2.0 utf8");

SoWriteAction writeAction = new SoWriteAction(out);
writeAction.apply(convertedRoot);

out.closeFile();