How to Program in VRML

VRML Basics

A great URL with more detail is  http://www.ee.surrey.ac.uk/Personal/ee41rs/vrmlguide/index.html. Some neat slides for a VRML speach are at  http://www.sdsc.edu/~nadeau/Courses/Multimedia97/slides/slides.htm .The basic idea is to make a text file describing the world you want.  The text file will consist of one header, and then definitions and nodes.  All comments in VRML start with a '#'.  VRML is case sensitive, so get the capitalization right!  The header MUST look like this, and must be on the VERY FIRST LINE.
    #VRML V2.0 utf8

VRML Nodes

A VRML world is just a set of nodes.  Each node will have a type, scale, rotation, translation, color, texture, and some others.  There will often be useful defaults for all these properties.

Nodes have for basic types: infomational, grouping, shape, and transformational.  An informational node just carries comments by the author.  A grouping node makes many other nodes into one node for easy processing.  A shape node is basic shape, like box, sphere, etc.  A transformational nodes gives all child nodes a common property, for example scale or color.

VRML Axes and Rotations

Remember that VRML uses the following axes system ... 
Rotations in VRML work by the right-hand rule. If you imagine wrapping your hand around one of the axes, with your thumb pointing in the positive direction, the direction of positive rotation is the same as the direction that your fingers wrap around in.  If you want to rotate an object 90 degrees away from you around the X axis, you would use a 90 degree negative rotation. All rotations are measured in radians not degrees.  Here is an easy conversion chart ...
 
Degrees 0 45 90 135 180 225 270 315 360
Radians 0 0.78 1.57 2.36 3.14 3.93 4.71 5.5 6.28
 

Translation Nodes

A translation nodes changes the shape, size, and/or position of any child nodes.  The 'translation' attribute tells where to move along the X, Y, and Z axes.  The 'rotation' attribute tells an axis to rotate about, and how much to spin.  The 'scale' attribute tells how big the thing should grow in the X, Y, and Z axes.  Remember that ORDER MATTERS (translate then rotate is not the same as rotate then translate).  An example is shown below.  Notice the ordering of the operations.
Transform {
        scale 2 1 2
        rotation 0 1 0 0.78
        translation 1 1 1
        children [
                USE FBOX
        ]

Material Nodes

A material node describes the appearance of any child nodes.  A material node can have up to six attributes:
Name  Description Arguments
diffuseColor  The normal colour of the object. A Color Triple
specularColor The colour of highlights on shiny objects. A Color Triple
emissiveColor  The object 'glows' with a light of it's own of this colour. It doesn't cast light on any other objects though. A Color Triple
ambientIntensity  The amount of ambient light that the object reflects.  Scale 0 ... 1
shininess How reflective the object is.  Scale 0 ... 1
transparency How transparent the object is. Note, some browsers will not support partly-transparent objects.  Scale 0 ... 1
An Example of a semi-transparent green box looks like this...
Shape {
        appearance Appearance {
                material Material {
                        diffuseColor 0 0.5 0
                        emissiveColor 0 0.8 0
                        transparency 0.5
                }
        }
        geometry Box {
        }
}
 

 ImageTexture  and MovieTexture Nodes

An imageTexture node makes all of it's children have a specific picture for a background.  Basically, you just tell it the URL, and wether to tile the picture horizontally and vertically.  The standard says only JPG files can be used, but the Cosmo player can also use GIFs.  A movieTexture node shows an MPEG movie on the surface.  It has all the attributes above, plus 'speed' and 'loop'.  MovieTexture can be unreliable, so be warned.
 
Appearance { 
        texture ImageTexture { 
                url "brick.jpg" 
                repeatS TRUE 
                repeatT TRUE 
        } 
}
Appearance { 
        texture MovieTexture { 
                url "random.mpg" 
                repeatS TRUE 
                repeatT TRUE 
                speed 2
                loop true 
        } 
}
 

Summary So Far

You've gotten all of this if you can make  this scene.  The URL for the balloon JPG is  http://euclid.nmu.edu/~randy/Classes/CS295/VRML/balloons.jpg .

Shape Nodes

These are the basic shapes
 
geometry Box { 
        size 5.5 3.75 1.0 
}
geometry Sphere { 
        radius 10,000,000 
}
geometry Cone { 
        bottomRadius 5 
        height 10 
        side TRUE      #this isn't actually needed 
        bottom FALSE 
}
geometry Cylinder { 
        radius 0.5 
        height 10 
        top FALSE 
        bottom TRUE
        side TRUE 
}
geometry Text { 
        string ["Hello", "World"] 
        fontStyle USE HELLOFONT 
        maxExtent 5 
        length [3, 3] 
}
ElevationGrid { 
        xDimension 6 
        zDimension 6 
        height [1.5, 1, 0.5, 0.5, 1, 1.5, 
                1, 0.5, 0.25, 0.25, 0.5, 1, 
                0.5, 0.25, 0, 0, 0.25, 0.5, 
                0.5, 0.25, 0, 0, 0.25, 0.5, 
                1, 0.5, 0.25, 0.25, 0.5, 1, 
                1.5, 1, 0.5, 0.5, 1, 1.5] 
        xSpacing 5.0 
        zSpacing 5.0 
}
 

Easy Example VRML Files

 
#VRML V2.0 utf8  
Shape { 
        appearance Appearance { 
             material Material { 
             } 
        }
        geometry Box { 
        } 
}
A Simple Box
DEF FBOX Shape {
        appearance Appearance {
               material Material {
               }
        }
        geometry Box {
        }
}
USE FBOX
 Reuse Demo  
Can you find the bug in this file?
#VRML V2.0 utf8 
DEF FBOX Shape { 
        appearance Appearance { 
                material Material { 
                } 
        } 
        geometry Box { 
        } 
} 

Transform { 
        scale 2 0.5 2 
        rotation 0 1 0 0.78 
        translation 0 -1.5 0 
        children [ 
                USE FBOX 
        ] 
}

 Transform Example 
#VRML V2.0 utf8 
Shape { 
        appearance Appearance { 
                material Material { 
                        diffuseColor 0 0.5 0 
                        emissiveColor 0 0.8 0 
                        transparency 0.5 
                } 
        } 
        geometry Box { 
        } 
}
 Semi-transparent Green Box 
#VRML V2.0 utf8 
Shape { 
        appearance Appearance { 
         texture ImageTexture { 
                url "balloons.jpg" 
                repeatS TRUE 
                repeatT TRUE 
         } 
 } 
        geometry Box { 
        } 
}
 Texture Demo