XdkWrl: Generating VRML files and the pretty printer

Home | Hierarchy | Members | Alphabetical | Related Pages

# Generating VRML files and the pretty printer

The XdkWrl library can be used not only for parsing VRML file but also to produce them. Indeed, you can use the API to create any hierarchy of VRML nodes as in the example below:

      Scene* scene = new Scene;

      Shape* facdShape    = new Shape("Facade_shape");

      Appearance* facdApp = new Appearance("Facade_appearance");
      ImageTexture* facdI = new ImageTexture("Facade_imagetexture");
      facdI->url.push_back("Facade.jpg");

      IndexedFaceSet* facdIfs       = new IndexedFaceSet("Facade_ifs");
      wrl::Coordinate* facdVCoord   = new wrl::Coordinate("Facade_vcoord");
      TextureCoordinate* facdTCoord = new TextureCoordinate("Facade_tcoord");

      MFInt32* facdVIdx = &facdIfs->coordIndex;
      MFInt32* facdTIdx = &facdIfs->texCoordIndex;

     ...
     Here we fill the coords and index for the Indexed Face Set
     ...

      facdShape->appearance = facdApp;
      facdApp->texture      = facdI;
      facdShape->geometry   = facdIfs;
      facdIfs->coord        = facdVCoord;
      facdIfs->texCoord     = facdTCoord;

      scene->nodes.push_back(facdShape);

But then you are faced with the inverse problem of parsing: how do you "dump" this C++ structure into a text file? Well, you can probably figure by yourself how to write a traversal (now that you know!) to will do that. However, the XdkWrl library provides you with a pretty printer that will do this job with you, the icing on the cake being that the outpout is gracefully formatted (even though it should be still improved in further release). In order to print the above scene to the standard output, just do:

      wrl::PrettyPrinter p(cout);
      p<<scene;

Of course you have to include the appropriate header:

     #include <xdkwrl/tools/prettyprinter.h>

You can output to any C++ stream, so for example if you want to save to a file, you can do:

      #include <fstream>

      using namespace std;

    ...

      ofstream of("Facade.wrl");
      wrl::PrettyPrinter p(of);
      p<<scene;
      of.close()

Generated on 5 Jan 2007 with version 1.5.1. |