An Introduction to VRML

Mr. Lukka takes a look at VRML basics including scripting, animation and applications.

VRML is intended to be for virtual reality what HTML is for text—a structured, standard, cross-platform format for static or interactive hyperlinked content. Just like HTML, it can be written by hand or generated by a program (the latter is usually preferable).

About a year ago VRML was catching on quickly, but much of that enthusiasm seems to have faded. It has not yet taken its place alongside HTML, JPEG and CGI as one of the basic, widely deployed web technologies.

One reason why this might be true is that no workable VRML browser for Linux or other UNIX-like operating systems is available. The ones that do exist are incomplete and generally not able to display web content in VRML well enough for serious use. For example, the cross-platform offerings are not able to handle Script nodes, which are one of the most important additions to the new VRML97.

My theory as to why this might influence the general acceptance of VRML is that a large fraction of people who are interested in cool technologies are using Linux and do not want to support an application that does not work with Linux and other properly working operating systems.

My scientific work needs the interactive visualization that VRML provides. While many different 3-D packages are available, VRML has the advantage in that it supports many sorts of interactions with the scene. Also, I would be able to send the diagrams I made with it to colleagues by e-mail.

After finding the available browsers to be unsatisfactory, I decided to write my own VRML browser. This browser, called FreeWRL, is rapidly approaching VRML97 specification compliance, and supports most of the capabilities lacking in other Linux browsers. I wrote it in Perl, as it was the only language that would enable me to get it working quickly.

With the availability of FreeWRL, I think many free OS users might wish to evaluate or re-evaluate VRML. In this article, I'll attempt to lay out the basic concepts of VRML and explain how it might be useful.

To run the code listed here, you need a VRML browser. If you don't have one, and are running Linux or some other UNIX-like OS, go to and follow the installation instructions.

Notice in particular the words “if you have any trouble, e-mail me.” The very worst thing you can do with free software is to download it, see that it doesn't work for some reason, leave it, and tell your friends that it doesn't work. The author will never know what is wrong with his package and will not be able to improve it, while rumors will be spreading everywhere that the package is not worth trying. Good bug reports are the least you can do for free software developers in return for their effort. “Thank-you” e-mail is nice, but not nearly as vital.

The Basics

Instead of a lengthy description, let's start with the code for drawing a simple world (see Listing 1). We'll start with the inner section: a sphere is described with a radius of 0.5. (In VRML, the units are usually called meters but that is just a convention.) This sphere is the geometry of a Shape node, whose “appearance” has a diffuseColor of 0.8 0 0, i.e., very bright red. Therefore, the Shape actually describes a red sphere. This shape is one of the children of a Transform, whose translation is 0 1 0, i.e., one meter upwards on the Y axis, which, from the default viewpoint on the positive Z axis, is up. The first line is a comment telling the browser this file is written in VRML version 2.0.

Figure 1

In short, we have just described a red sphere with a radius of half a meter and lying one meter above the origin. (See Figure 1.) Now the code in Listing 1 may seem a somewhat onerous way of describing such a simple scene. For example, the following might be easier:

Sphere {
        center 0 1 0
        radius 0.5
        color 0.8 0 0

In fact, you can do this: it is possible to create your own nodes suitable for your own application using prototypes, which are similar in spirit to preprocessor macros in C, as shown in Listing 2.

Figure 2

Figure 3

The scene described by the code in Listing 2 is shown from two different angles in Figures 2 and 3. Now you can define any number of scenes like this, using the PROTO you just defined. Of course, you do have to deal with including the original PROTO in the beginning. It is also possible to refer to it in another file using so-called EXTERNPROTOs, which include the interface part (in brackets) but omit the definition (in braces). Even this might sometimes feel like too much typing. In that case, you can easily write a Perl script to include the correct PROTOs in your VRML files, or to do whatever other repetitive tasks you need done.

In scene graphs, you can give a node a name using the DEF statement and use it again in another place with a USE statement—see Listing 3.

Note that unlike with PROTOs, where a complete copy of the contents of the prototype is made, only one Appearance node is present, which is referred to in two places. For example, if we later animate the appearance to change the color from red to blue, both the box and the sphere change color; whereas if we animate the color of a Thing in the PROTO example, only that one Thing changes color. Once a node has been named with DEF, it can be used in USE statements any number of times.

Now that you have a sense of what is going on, let's have a more formal description of what we just did. A VRML world is described as a hierarchy of nodes called the scene graph. Each node is of a particular node type, and for each node type the VRML97 specification defines various fields. Each field has a type and a default value. The syntax for a node is

NodeType {
        field value
        field value

where the syntax for a value depends on the type of the field. For example, the value of an SFVec3f is three floating-point numbers and the value of an SFNode is another node just like above.

The single-valued (SF) field types are:

  • SFBool: a Boolean value, written TRUE or FALSE

  • SFFloat: a floating-point number

  • SFImage: an image, described by several integer values, first width, height and number of components, then width*height values for the pixels

  • SFInt32: a 32-bit integer

  • SFNode: a node

  • SFRotation: a rotation: 3 floating-point values for an axis and one for the angle in radians

  • SFString: a string in double quotes. Inside, double quotes and backslashes are quoted by a backslash

  • SFTime: a floating-point value, time in seconds since a specific origin

  • SFVec2f: a two-dimensional vector containing two floating-point values

  • SFVec3f: a three-dimensional vector containing three floating-point values

For most SF field types there exists a corresponding MF field type, which simply means zero-or-more values of the corresponding SF type. For example, the following would be legal values for a MFVec3f field:

0.1 2 3
[0.1 4 2]
[0.5 2 6 1 6 4 7 4 6]
[0.5 2 6, 1 6 4, 7 4 6]
That is, the values may or may not be separated by commas and must be surrounded by brackets if there are more or less than one of them.

These names for the field types are also used inside the PROTO declarations: the syntax of a PROTO is basically

PROTO Name [
        field Type fieldName defaultvalue
        exposedField Type fieldName defaultvalue
        eventOut Type fieldName
        eventIn Type fieldName
] {
        Node { ... }

where inside the PROTO body, field values can also be specified using IS statements, which equate that field with one of the published fields of the PROTO (e.g., the center field of the Thing PROTO above).

For descriptions of all the node types and their fields as well as the exact grammar and semantics of VRML, see the VRML97 specification (found at or a book. Figure 3 shows Netscape displaying the definition for the IndexedFaceSet node (which makes it possible to describe arbitrary polygonal geometry) from the web site.

This is basically all you need to know to create static VRML scenes, except for the nitty-gritty details which you can find in the above-mentioned source. Once you do create worlds using FreeWRL, send me a note by e-mail and I'll put a link to your worlds on the FreeWRL web page.