Hello World

tested with ++


There is not much in the digital world which doesn't start with the 'Hello World' program. This will be no exception for me but instead of projecting these famous words onto your screen, you will be greeted by what has become a landmark in VRML: Cube, Cone, and Sphere.

The file starts with the obligatory header

#VRML V2.0 utf8
This is a must for all VRML2.0 compliant files. The pound sign (#) indicates a comment, V2.0 indicates the version (as opposed to V1.0) and utf8 is a encoding scheme to enable international character display. Next we insert a Group node:

Group {
Everything in between the parenthesis will be treated as one entity. Grouping is useful for logical ordering of your model or objects. It is not really necessary in this case but you will definitely need it later.
After the Group node the children field embraces all the objects for the Group node (think of group as a parent).

  children [
We continue with a Shape node which creates rendered objects in our world.

      Shape {
Shape nodes contain a geometry, in our case a Box

        geometry Box { }
We clean up by closing all the open parenthesis.

      }

  ]	

}
This is about the simplest VRML-file you can write. Here is the preliminary Hello World source file and the Hello World.wrl file to look at.
You see, it's easy but not very interesting, just a grey box somewhere out there. Let's add a color to it. The Shape node has one more field besides the geometry field: appearance. The appearance field contains an Appearance node (I know, it's confusing at first) which in turn has a field material. This field in turn contains a Material node.

        appearance Appearance {

          material Material { }

        }
To change the box's color the appearance field has to be in front of the geometry field.

    Shape {

      appearance Appearance {

        material Material { }

      }

      geometry Box { }

  }
Well, still the box is grey so what should we do? You may have noticed that the Material node is empty. Right now there is no material defined. A diffuseColor field will tell the browser to render the box in a certain color. VRML uses the RGB (red green blue) color model. Every color is represented by a mixture of red, green and blue color percentages. For a red box I just have to say Material { diffuseColor 1 0 0 }.

Please look at Hello World source file and the Hello World.wrl file again.
The blue cube is placed at its default (x=0, y=0, z=0, or world origin). If you want to move it you have to place the Shape node into a Transform node and translate the Shape node where you want it to be.

Transform {

  translation 5 0 0

  children [

    Shape {

      appearance Appearance {

        material Material { }

      }

      goemetry Box { }

    }

  ]

}
The translation field moves the following children 5 units (meters) to the right (x-axis) and zero units in the y- and z-directions. The Transform node is similar to the Group node with the difference of their translation, scale, and rotation fields. Again, look at the next step of the Hello World source file and the Hello World.wrl file.
The box moved to the right (quot erat expectandum). Now you are in a position to copy the entire box Transform node and paste it as a second and third child.

Group {

  children [

    Transform { 

      translation 5 0 0

      children [

        Shape { ... geometry Box { } }

      ]

    }

    Transform {

      translation 0 0 0

      children [

        Shape { ... geometry Sphere { } }

      ]

    }

    Transform {

      translation -5 0 0

      children [

        Shape { ... geometry Cone { } }

      ]

    }

  ] # end of Group children

}
Pay careful attention to the parenthesis and discipline yourself while setting them otherwise you will soon lose overview of them (believe me, there can be *many* parenthesis in a wrl file). Each Transform node now has a different translation field. The second (sphere) node remains at the origin (0 0 0) and the third and last (cone) moves 5 units to the right. As you can see I changed the node Box to Sphere and Cone for the second and third Transform node respectively. Still they have the same color so we change this too: The Sphere becomes green (0 1 0) and the Cone becomes blue (0 0 1). Finally to make this file usable for future tutorials, we give names to the three Transforms:

DEF box Transform { ... }

DEF sphere Transform { ... }

DEF cone Transform { ... }
The final version of the Hello World source file and the Hello World.wrl file close today's tutorial. You might have noticed that nothing is moving etc. This file could easily be replicated in VRML1.0. and serves only as the foundation for following tutorials with true VRML2.0 content.




Copyright © 1996-98 Markus Roskothen. All rights reserved.