2. Key concepts

2.1 The structure of VRML files

VRML files contain:

Consider the following example:

Show example world in new VRML browser-window
A simple Cylinder
#VRML V2.0 utf8
# A Cylinder
Shape {
    appearance Appearance {
        material Material { }
    }
    geometry Cylinder {
        height 2.0
        radius 1.5
    }
}

VRML 2.0 files have to start with the "#VRML V2.0" header(followed by the encoding, in our cases mostly utf8): it has to be in the first line of the text-file. The next line "# A Cylinder" is a remark: everything after a "#" mark is ignored by the browser(but only in the same line, from the # to the end of line). Next one shape-node is included in the vrml file("Shape {...}"). Besides the geometric description of itself ("geometry Cylinder {...}"), it contains the "appearance" node("appearance Appearance {...}") which describes the appearance of the surface of this object. The "appearance" node contains a field for the material node: in this example everything in the appearance node left blank resulting a "default appeariance". (We will discuss the appearence node later.)

2.2 Understanding the coordinate-system

VRML uses a right handed coordinate system. Any distance in the coordinate space is theoretically measured in meters, but thanx to virtuality, you can think about measurement-units as you like.

Show the coordinate-system

By default all shapes are built at the origin of the world coordinate system.

Show the cylinder in coordinate-system

Since a VRML file builds components for a world (one file(world/scene) can contain multiple worlds/scenes), it offers you to use "local" coordinate systems (for any object or group of objects) besides the "global" coordinate system. For example if you specify the location of an object (instead of it's default position at 0.0 0.0 0.0 = in the center of the "global" coordinate-system), you do it by specifying a "shifted" coordinate-system for the object using the "transform" node.
Consider the following example:

Show the translated cylinder without the coordinate-system
Show the translated cylinder with the coordinate-system
#VRML V2.0 utf8
Transform {
	translation 2.0 2.0 2.0
		children[Shape {
		    appearance Appearance {
		        material Material { }
			    }
		    geometry Cylinder {
		        height 2.0
		        radius 1.5
			    }
			}
		]
	}

In this example the "Transform" node is the "upper-level" node:

Transform {
	...
	}
The "translation" node gives the parameters for "shifting"(translating) the (local) coordinate-space:
             X   Y   Z
translation 2.0 2.0 2.0

means 2 units translation in all the X Y and Z directions.
The "Shape" node (the cylinder) is in the children field of the "Transform" node, that's why it is translated 2 units in all the 3 directions. Consider the following example with a non-translated and a translated cylinder:

Show the 2 cylinders without the coordinate-system
Show the 2 cylinders with the coordinate-system
#VRML V2.0 utf8
#This shape is not in a Transform node, so it will be placed
#at the default position (0.0 0.0 0.0)
Shape {
		    appearance Appearance {
		        material Material { }
			    }
		    geometry Cylinder {
		        height 2.0
		        radius 1.5
			    }
			}
#This is a Transform node: all the objects in the children field
#are translated according to the values specified in the
#translation field (2.0 units in all the 3 directions)
Transform {
	translation 2.0 2.0 2.0
		children[
			Shape {
		    	appearance Appearance {
		        material Material { }
			    }
		    	geometry Cylinder {
		        height 2.0
		        radius 1.5
			    }
			}
		]
	}

2.3 Primitive shapes and text - understanding shape nodes

The building blocks of a VRML world are the shapes: you can study the basic structure of the shape node on the standard building blocks called primitives: Box, Cone, Cylinder and Sphere.
Shape nodes are describing their own geometry(form, or structure) and appearance(color and texture):

Shape {
              appearance . . .
              geometry   . . .
          }
The appearance node(discussed later) contains three nodes: material, texture and textureTransform, in the examples we use only a default material by specifying an empty material field.
Shape geometry is described in the geometry node, standard primitive geometry nodes(Box, Cone, Cylinder and Sphere) contain different fields for specyfiing dimensions (se the example below).

Show the primitives example
#VRML V2.0 utf8
#the 4 primitive shapes
Transform {
translation -3.0 0.0 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Box{
			####	X   Y   Z
			size 1.0 1.5 2.0 #the X,Y,Z dimensions of the box in units
				}
			}
		]
	}

Transform {
translation -1.0 0.0 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Cone{
			bottomRadius 1 #bottomradius in units
			height       1 #height in units
			side         TRUE #TRUE or FALSE determines the existence of the sides
			bottom       TRUE #TRUE or FALSE determines the existence of the bottom-cap
				}
			}
		]
	}

Transform {
translation 1.0 0.0 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Cylinder {
			bottom  TRUE #TRUE or FALSE determines the existence of the bottom-cap
        		height 1.0 #height in units
        		radius 1.0 #radius in units
			side    TRUE #TRUE or FALSE determines the existence of the sides
			top     TRUE #TRUE or FALSE determines the existence of the top-cap    				}
				}
			}
		]
	}

Transform {
translation 3.0 0.0 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Sphere {
			radius  1  #radius in units  				}
			}
		]
	}

Note: all the fields in the primitive-shape nodes have default values, you don't have to specify all the dimensions. To see the complete description of nodes, refer to the VRML/ISO Draft for International Standard (DIS) Node Reference:Box, Cylinder and Cone.

We will use text shapes in the following examples, and since using them is not more difficult than using primitives, we discuss it here. (In the inlined world we used to illustrate the coordinate space, you already saw text shapes indicating the axes.)
Text shapes are very useful in VRML worlds, but they are differ from other objects in the sense that they are flat and have no thickness. You can only specify the text shape's 2D properties: font family, style, size, etc. Text shapes can contain the following fields/nodes(the fields contain the default values):

Text { 
	string    []
	fontStyle NULL
	length    []      # [0,)
	maxExtent 0.0     # [0,)
}
We will not pay attention to the length(specifies the length of each text string in the local coordinate system: if the string is too short, it is stretched, if the string is too long, it is compressed) and maxExtent(it limits and compresses all of the text strings if the length of the maximum string is longer than the maximum extent, as measured in the local coordinate system) fields. To see the complete description of nodes, refer to the VRML/ISO Draft for International Standard (DIS) Node Reference.

For us the string(the text to build) field and the fontStyle(how to build the text) node are important. Consider the shape below containing a text object:

Shape {
              geometry Text {
                  string    . . .
                  fontStyle . . .
              }
          }

As the easiest way to use text is to omit the fontStyle node, you can use text the following way:

Show the simpletext example
#VRML V2.0 utf8
Shape {
    geometry Text {
        string [ "Text without fontStyle",
			"in two lines" ]
    			}
}

You don't have to specify all the fields in the fontStyle node, if you omit some, the default values will be used. The fields of the fontStyle node are:

FontStyle { 
	family       ["SERIF"]
	horizontal   TRUE
	justify      "BEGIN"
	language     ""
	leftToRight  TRUE
	SFFloat  size         1.0          # (0,)
	SFFloat  spacing      1.0          # [0,)
	SFString style        "PLAIN"
	SFBool   topToBottom  TRUE
}

We will not pay attention to some fields, for the complete description refer to the VRML/ISO Draft for International Standard (DIS) Node Reference.

In the following example you see two primitive shapes (from the previous example) and two text shapes:

Show the primitives with text example
#VRML V2.0 utf8
#2 primitive shapes with text

Transform {
translation -2.0 0.5 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Cone{
			bottomRadius 1 #bottomradius in units
			height       1 #height in units
			side         TRUE #TRUE or FALSE determines the existence of the sides
			bottom       TRUE #TRUE or FALSE determines the existence of the bottom-cap
				}
			}
		]
	}

Transform {
translation 2.0 0.5 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Cylinder {
			bottom  TRUE #TRUE or FALSE determines the existence of the bottom-cap
        		height 1.0 #height in units
        		radius 1.0 #radius in units
			side    TRUE #TRUE or FALSE determines the existence of the sides
			top     TRUE #TRUE or FALSE determines the existence of the top-cap    				}
				}
			}
		]
	}


Transform {
translation -2.0 -1.2 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Text {
			string ["Cone"]
 	       	fontStyle FontStyle {
				size         1.5		#the height of the text is 1.5 units
				family       ["SANS"]	#the font is SANS = TIMES
				justify      "MIDDLE"	#justification is middle
				style        "BOLD"	#style is bold
        				}
				}
			}
		]
	}


Transform {
translation 2.0 -1.2 0.0 #the translation in X Y Z directions
	children[
		Shape{
		appearance Appearance{
			material Material { }
				}
		geometry Text {
			string ["Cylinder"]
 	       	fontStyle FontStyle {
				size         1.5		#the height of the text is 1.5 units
				family       ["SANS"]	#the font is SANS = TIMES
				justify      "MIDDLE"	#justification is middle
				style        "BOLD"	#style is bold
        				}

				}
			}
		]
	}

2.4 Viewpoints: understanding camera-positions

To have a better understanding on the coordinate-space, you should take a look to "viewpoints".
Theoretically when you are examining the virtual space, you view it through a camera: when you move, you change the position of your camera. Viewponts are pre-defined named camera-positions. Browser will usually present a list of viewpoints using their names(description strings). When you "travel" to a viewpoint, the browser may(but doesn't have to) animate between the viewpoints. Consider the following example with our simple cylinder, and the "inlined" coordinate axes(see the description of inlining later), and six viewpoints:

Show the viewpoints example
#VRML V2.0 utf8
# A Cylinder
Viewpoint {
#this is the "front" view, 
         position    0 0 10	#the camera positioned to X=0, Y=0 and Z=10
         orientation 0 0 1 0	#"default" orientation
         description "front"	#the name of the view,
					#the browser will present the views by their names
     }

Viewpoint {
         position    0 0 -10		#the camera positioned to X=0, Y=0 and Z=-10
         orientation 0 1 0 3.14	#the camera is rotated around the Y axis 180 degrees
						#so the orientation is from negative Z to positive Z
         description "back"
     }

Viewpoint {
         position    10 0 0		#the camera positioned to X=10, Y=0, Z=0
         orientation 0 1 0 1.57	#the camera is rotated around the Y axis 90 degrees
						#the orientation is from positive X to negative X
         description "right"
     }
Viewpoint {
         position    -10 0 0		#the camera positioned to X=-10, Y=0, Z=0
         orientation 0 1 0 -1.57	#the camera is rotated around the Y axis -90 degrees
						#the orientation is from negative X to positive X
         description "left"
     }

Viewpoint {
         position    0 10 0		#the camera positioned to X=0, Y=10, Z=0
         orientation 1 0 0 -1.57	#the camera is rotated around the X axis -90 degrees
						#the orientation is from positive Y to negative Y
         description "top"
     }

Viewpoint {
         position    0 -10 0		#the camera positioned to X=0, Y=-10, Z=0
         orientation 1 0 0 1.57	#the camera is rotated around the X axis 90 degrees
						#the orientation is from negative Y to positive Y
         description "bottom"
     }


	Inline {				#the coordinate axes are "inlined" 
              url "coords.wrl"	#from a separate file called "coords.wrl"
          }					#see the description of the inline node later

Shape {
    appearance Appearance {
        material Material { }
    }
    geometry Cylinder {
        height 2.0
        radius 1.5
    }
}

The "default" viewpoint (the one where the camera is placed when the world is loaded) is the first one found in the VRML file. (Viewpoints in Inlines cannot be initial viewpoint.)
Viewpoints are subject to transformations, as you will see in the next chapter.