http://tecfa.unige.ch/ico/navi/tex2html/top.gifhttp://tecfa.unige.ch/ico/icons/vrml97-icon.gif next up previous contents index
Next: 2. Modularization and Abstraction Up: 1. Starting with VRML Previous: 1.1 Introduction to the

Subsections


   
1.2 Basic static VRML

When you play around with your own little VRML files, make sure they really get reloaded right after you change them. To make sure reload them holding down the SHIFT key on your machine when you click on reload in Netscape. If you read this on paper, note that URLs in the examples need to be completed by ``http://tecfa.unige.ch/guides/vrml/''

   
1.2.1 Getting Started

   
The structure of a WRL File:

VRML (*.wrl) files have 3 basic elements:

1.
A header which tells the browser that the file is VRML and which version also. A header line is mandatory.

2.
Comments are preceded by a #.

3.
Nodes: Most everything else are nodes. Nodes generally contain:
(a)
The type of node (required). Nodes always are in Capital letters.
(b)
A set of curly braces {.....} (required)
(c)
A number of fields, all or some of which are optional. Note that there is no mandatory ordering of fields.
(d)
Fields with that can have multiple values require braces [ ...]. Fields always start with lowerCase letters.

Here is a typical VRML file with a single node (don't worry if you don't understand it):

# VRML V2.0 utf8
# A sample file with a single stupid node

Transform {
   translation 0 2 0
   children [
      Shape {
         geometry Sphere {}
      }
     ]
 }
VRML II has about 9 major node types. We will shortly list the features of some important nodes as explained in detail in the Concepts and the =>Node Reference sections in the VRML 97 specification. However, don't worry too much about the exact meaning of these features yet. The purpose of this list is just giving you an overview of some of ``what's there''. Once you master some basics you then should consider going back to reading the the Concepts Section of VRML 97 specification in whole !

1.
Grouping nodes: =>Grouping nodes are used to create hierarchical transformation graphs. Grouping nodes have a children field that contains a list of nodes which are the transformation descendants of the group. Each grouping node defines a coordinate space for its children. This coordinate space is relative to the parent node's coordinate space-that is, transformations accumulate down the scene graph hierarchy

2.
Special groups: The =>Inline is a grouping node that reads its children data from a location in the World Wide Web. The =>LOD specifies various levels of detail or complexity for a given object, and provides hints for browsers to automatically choose the appropriate version of the object based on the distance from the user. The =>Switch grouping node traverses zero or one of the nodes specified in the choice field

3.
Common nodes: Used for things like light, sound, scripts, etc. See the =>Node Reference Chapter in the specs.

4.
Sensors: =>Sensor nodes generate events. Geometric sensor nodes (ProximitySensor, VisibilitySensor, TouchSensor, CylinderSensor, PlaneSensor, SphereSensor and the Collision group) generate events based on user actions, such as a mouse click or navigating close to a particular object. TimeSensor nodes generate events as time passes.

5.
Geometry nodes: =>Geometry nodes must be contained by a Shape node in order to be visible to the user. The Shape node contains exactly one geometry node in its geometry field.

6.
Appearance: The Appearance node specifies the visual properties of geometry (see above) by defining the material and texture nodes.

7.
Interpolators: =>Interpolator nodes are designed for linear keyframed animation.

8.
Bindable Nodes: =>Bindable Nodes have the unique behavior that only one of each type can be active (i.e. affecting the user's experience) at any point in time.

VRML 1.0 contained 6 major (and 36 minor node) types, i.e:

1.
Shape Nodes (such as cubes, text, sphere)
2.
Geometry and Material Nodes (such as texture and font style)

3.
Transformation Nodes (such as rotation and translation)

4.
Camera Nodes

5.
Lightning Nodes

6.
Group Nodes

Now be careful! VRML is a caseSensitive Language. Respect that or you will get strange errors!

VRML versions are easy to identify: Just look at the first line in the file You can translate most of old VRML I files with the vrml1to2 program from Sony.

Example 1.2.1   A simple VRML 2.0 file  
VRML: ../examples/basics/basics-1.wrl
Source: ../examples/basics/basics-1.text

You don't need to worry about details yet, it will just give you an idea of the structure of a VRML file

#VRML V2.0 utf8

#      ^^^^^  this is the MANDATORY header line 
# The VRML Primer
# 1997 Daniel K. Schneider, TECFA
# No Copyright, steal this!
#

# Draw a blue cylinder - a first node specifying an object
# Radius and height can be floating numbers
Shape {
   geometry Cylinder {
      radius 0.1
      height 3.0
      }
   appearance Appearance {
      material Material { diffuseColor 0 0 1  }
   }
}

Transform {
   # Move the pen up - a second node specifying a translation
   # and a sphere
   translation 0 2 0
   children [
      Shape {
         geometry Sphere {}
      }
     ]
}

Ordering is quite simple: VRML is a declarative language and all declaration are executed in the order they appear. Basically speaking, VRML goes ``do this'', ``do this'', etc. However, VRML nodes can be nested at any level of detail (and should be as you will learn later). The usual scoping principles apply. E.g. if you insert a Transform node within a Transform node (you will see that later) transformations applied in the child node are applied within or before the transformations in the parent node. E.g. within a low level node you can rotate some objects and then place those objects somewhere else with a higher level node.

Now all you need is a VRML manual and you can start producing VRML files or write programs that generate VRML. Or can you continue reading this for a little while.

1.2.2 Basic Geometry (Shapes)

Per definition, all basic predefined shapes in VRML are drawn around the origin and have a standard size. Size is measured in units (roughly meters in reference to the real world). E.g. a cube has a standard size of 2 by 2 by 2 meters. Note that sizes are given with floating point numbers, e.g. 2.0 or .01 or 0.2. Degrees are given in radians.


 
Table 1.1: Degrees to radians conversion
30: 0.5236
60: 1.0472
90: 1.5708
180: 3.1416
270: 4.7124

You can also use our Javascript Radian Converver

Some objects (i.e. Cylinders and Cones) have named parts which you can avoid drawing (e.g. the bottom of a cylinder). Per default, all parts are drawn.

Cubes:
You can look now at this most simple cube.  

#VRML V2.0 utf8
#A simple box (was called cube in VRML I)

Shape {
        geometry Box {}
}

The box (cube) above might appear as a flat rectangle in your browser, turn it around to be sure it's a 3D object. Also it does not have any color so it will appear as without any shading (in most or all browsers). If you can't see it in your browser skip this example. Per default, all objects are drawn off the center of the current origin in all directions. E.g. in the above case we draw around the [0 0 0] point: 1 left/right, 1 up/down and 1 forward/backwards leading to a 2 by 2 cube. Just remember this right now: You don't draw towards right, up and forward, but around the origin, i.e. left-to-right, down-to-up, backwards-to-forwards of the origin (the current coordinates of the ``pen'').

Example 1.2.2   A simple floor  

VRML: ../examples/basics/basics-shapes-2.wrl
Source: ../examples/basics/basics-shapes-2.text
VRML Full: ../examples/basics/basics-shapes-2V.wrl
Source Full: ../examples/basics/basics-shapes-2V.text

The box here represents something like a floor. If you can't see it in your browser, click on the ``Full'' version. As you will learn later, you have to define appearance and position the user someplace he can see something when the scene loads.

#VRML V2.0 utf8
#A floor, i.e. a thin and large box

Shape {
        geometry Box {
                size 20 0.1 30
        }
}

Spheres:
or balls:

#VRML V2.0 utf8
#A large sphere
      Shape {
         geometry Sphere {
               radius 2}
      }

Cylinders:
Cylinders are more complex. Here is a simple and a more complex example.  

#VRML V2.0 utf8
#A long stick
Shape {
   geometry Cylinder {
      radius 0.1
      height 3.0
   }

Example 1.2.3   A simple cup  

VRML: ../examples/basics/basics-shapes-5.wrl
Source: ../examples/basics/basics-shapes-5.text
VRML Full: ../examples/basics/basics-shapes-5V.wrl
Source Full: ../examples/basics/basics-shapes-5V.text

In the cup example we tell VRML which parts we need. By definition, VRML renders all parts of an object. If you want to display just some of them you must specify which ones like in the following example. Note: this replaces the parts field in VRML 1 that took as arguments a list of parts in parenthesis separated by vertical bars, i.e. (...|...|...).

#VRML V2.0 utf8
#A round large cup
Shape {
   geometry Cylinder{
      height 0.5
      radius 0.5
      bottom TRUE
      side TRUE
      top FALSE
   }
}

Also note that you wouldn't even display a simple cup this way. You'd have to add some color and position either the cup or the default view somewhere else (you will learn that later).

Cones
have a height and bottomRadius field and ``Sides'' and ``Bottom'' parts

   
1.2.3 Positioning Shapes

In order to position objects in a VRML scene (unless you want to move the user's viewpoint) you have to use the =>Transform Node. We will introduce some of its features here.

Example 1.2.4   A simple translation  

VRML: ../examples/basics/basics-1.wrl
Source: ../examples/basics/basics-1.text

  Let's have a look again at the first VRML scene shown in this tutorial ( and ignore the contents of the appearance field right now). The Sphere is placed on top of a stick (the cylinder) because we told VRML to move the pen 2.5 up on the y axis. Since the stick is drawn from -1.5 to + 1.5 along the y axis we draw the Sphere (which has a radius of 1) starting with an origin of 1 further up the y axis.

#VRML V2.0 utf8
# The VRML Primer
# 1997 Daniel K. Schneider, TECFA
# No Copyright, steal this!
#

# Draw a blue cylinder - a first node specifying an object
# Watch out: radius and height are floating numbers !
Shape {
   geometry Cylinder {
      radius 0.1
      height 3.0}
   appearance Appearance {
      material Material { diffuseColor 0.1 0.1 0.9 }
   }
}

Transform {
   # Move the pen up - a second node specifying a translation
   # and a red sphere
   translation 0 2.5 0
   children [
      Shape {
         geometry Sphere { radius 1 }
         appearance Appearance {
            material Material { diffuseColor 1 0 0 }
         }
      }
     ]
}

To move to another point in the same frame of reference the Transform node has a translation field which allows to move the Pen (e.g. the following objects in the group) in some x-y-z direction.

Within a Transform node, objects can be embedded (combined) and addressed as a whole. Concretly, this means that a translation will affect all the renderings within the same Transform node) This is why you must use the ``children'' field to indicate the nodes which are affected by a transformation. (You will see more of that later).

You can also draw overlapping shapes like in the following example:

Example 1.2.5   Overlapping objects  

VRML: ../examples/basics/basics-shapes-10.wrl
Source: ../examples/basics/basics-shapes-10.text
Full VRML: ../examples/basics/basics-shapes-10V.wrl
Full Source: ../examples/basics/basics-shapes-10V.text

#VRML V2.0 utf8
# Overlapping shapes
 
Shape {
        geometry Box {
                size 3 0.1 0.1
        }
}
Shape {
        geometry Box {
                size 0.1 3 0.1
        }
}
Shape {
        geometry Box {
                size 0.1 0.1 3
        }
}
Shape {
        geometry Sphere { radius 0.5 }
}

Note that rendering overlapping node is less efficient than drawing ajacent nodes. E.g. if you want to draw a forest with some balls on top of sticks make sure that the ball does not overlap the stick (altough the visible result would be the same). Also, if you think that this object looks too dull on the screen, apply some Material like we did in the Full VRML scene. We will look into this in section 1.2.5).

    
1.2.4 Rotations

Rotations can be around the x, y and z axis (yaw, roll and pitch). The rotation field takes 4 arguments: The axis and the degree in radians. A 180 degree rotation is 3.14159 (Pi). Here is a simple example of a rotation around the z axis:

Example 1.2.6   Simple Rotation example  

VRML: ../examples/inter/inter-rotation-1.wrl
Source: ../examples/inter/inter-rotation-1.text
Full VRML: ../examples/inter/inter-rotation-1V.wrl
Full Source: ../examples/inter/inter-rotation-1V.text

The cone is rotated 3.14 radians around z by giving the following arguments to the rotation field of the Transform node.

        rotation 0 0 1  3.14
Note that the Boxes represent the x,y,z axis.

#VRML V2.0 utf8

Shape {
        geometry Box {
                size 0.1 10 0.1
        }
}
Shape {
        geometry Box {
                size 10 0.1 0.1
        }
}
Shape {
        geometry Box {
                size 0.1 0.1 10
        }
}
Transform {
        rotation 0 0 1  3.14
        children [
                Shape {
                        geometry Cone {}
                }
        ]
}

Rotation is not that intuitive, but if you play around with a few examples you might find the grasp for it. When you define a rotation you first of all define the angle of rotation in radians (the 4th number in the field). Then you specify around which axis to rotate. Imagine that the the three axis parameters define a diagonal line within a cube. Rotation will happen around this diagonal ! [drawing needed here]. Rotation has the same effects as translation. It ``reorients'' the pen like any other ``transform''.

PlayTime:

You can intuitively learn more about size, translation and rotation by playing with the Tiny 3D Applet/Java1.02. Note that you need a EAI capable browser (most will do). If your browser understands Java 1.1 widgets (e.g. patched Netscape 4) you can try Tiny 3D Applet/Java1.1.

    
1.2.5 Defining appearance

Usually you must use an =>Appearance node when drawing objects. Else they will appear blank, or white, that is dull (or if your headlights are turned off you won't even see a thing at all. Here you will learn how to use simple colors and how to ``dress'' objects with bitmap files.

   
Simple Materials

Here is an simple example representing Earth and Sun. The Sun is behind to the right. If you can't see it, put your client in flip mode and turn the scene around. You will learn later how to define correct viewpoints. (Well if you are in a hurry look at the Full Vrml example).

Example 1.2.7   Simple Materials  

VRML: ../examples/basics/basics-materials-1.wrl
Source: ../examples/basics/basics-materials-1.text
Full VRML: ../examples/basics/basics-materials-1V.wrl
Full Source: ../examples/basics/basics-materials-1V.text

#VRML V2.0 utf8
# Different materials for Earth and Sun
# Sun turns around Earth :)
 
# The Earth in blue
Shape {
   appearance Appearance {
      material Material {
         diffuseColor 0 0 1
      }
   }
   geometry Sphere {
      radius 1
   }
}

#The Sun yellow and shining
Transform {
   translation 2 1 -2
   children [
      Shape {
         appearance Appearance {
            material Material {
               emissiveColor 1 1 0
               shininess 1
            }
         }
         geometry Sphere {
            radius 1
         }
      }
     ]
}

What is new here are the appearance and the material node. =>Appearance nodes define the visual properties (skin) of a polygon. The =>material nodes specify surface material properties for associated geometry nodes and are used by the VRML lighting equations during rendering. You can select from several types of color. Colors themselves are given as three RGB numbers in the range of [0,1]. E.g. ``diffuseColor 0 0 1'' gives a diffuse blue.

VRML's lightning model is quite complex and we will only make a few short comments about the fields of the Material node here.

 Material { 
   exposedField SFFloat ambientIntensity  0.2         
   exposedField SFColor diffuseColor      0.8 0.8 0.8 
   exposedField SFColor emissiveColor     0 0 0       
   exposedField SFFloat shininess         0.2         
   exposedField SFColor specularColor     0 0 0       
   exposedField SFFloat transparency      0           
 }
Those fields define both the color of an objects and the way it reflects light from the defined light sources. But please note that lightning effects are also (and mainly) function of the lights you put into the scene.

The Color Applet:

You should now play a little bit with this applet. It will give you a good feeling for VRML colors under simple lightning conditions. In addition it will generate a complete VRML scene for your own usage.
Color Appplet: eai/color/

   
Image Textures

There are various of ways of using textures in an =>Appearance node. Here, we will see how to use =>ImageTexture node. VRML 2 browsers are required to support JPEG and PNG file formats. GIF support is only an option, so use JPEGs or PNGs if ever possible !

Here is a very simple node that dresses a cube with a bitmap file.

Shape {
   appearance Appearance {
      texture ImageTexture {
         url "tecfa/mendelsohn.gif"
      }
   }
   geometry Box { }
}

Per default each side of an object will be painted with an image (e.g. cubes will have 6 images, spheres one, and cylinders two).

Here is a small VRML file that shows some Tecfa people using a few few basics geometric shapes.

Example 1.2.8   Image texture example  

VRML: ../examples/basics/basics-persons-1.wrl
Source: ../examples/basics/basics-persons-1.text

Note how you can repeat textures easily by using a =>TextureTransform. The ``floor'' in the example above has been created using this technique. The scale field tells how many times to repeat a bitmap in the s and t direction. (Note: s and t is more or less like a standard x, y of a plane). Look at the code below:

Transform {
   translation -4 -2 2
   children [
      Shape {
         appearance Appearance {
            texture ImageTexture {
               repeatS TRUE
               repeatT TRUE
               url "tecfa/sylvere.gif"
            }
            textureTransform TextureTransform {
               scale 10 10
            }
         }
         geometry Box {size 10 0.1 10 }
      }
     ]
}

There is much more to texturing with Bitmaps, e.g. you can combine colors and textures (read the specifications or another tutorial).

    
1.2.6 Anchors and Teleportals

Anchors

=>Anchors are group nodes, i.e. they have to be wrapped around the object(s) that represent the anchor. Here is an example that leads to our WWW home page if you click on the Sphere.

Example 1.2.9   Anchors  

VRML: ../examples/basics/basics-anchors-1.wrl
Source: ../examples/basics/basics-anchors-1.text

#VRML V2.0 utf8
 
Transform {
   rotation 1 0 0 0.4
   children Shape {
      appearance Appearance {
         material Material {
            diffuseColor 0.2 0.1 0.6
         } }
      geometry Box {
         size 20 0.1 10
      }  } }
   
Anchor {
   url         "http://tecfa.unige.ch/"
   description "A link to the Tecfa Home page"
   children [
      Shape {
         appearance Appearance {
            material Material {
               diffuseColor 0 0 1
            }
         }
         geometry Sphere {}
      }
     ]
}

Teleportals

Teleportals are doors into other VRML worlds (scenes). They are done with WWW Anchors. Split up your scene into several ones when rendering, downloading, memory usage, etc. grows too high. But don't, if you want to avoid loading when people walk frequently back and forth.

    
1.2.7 Text

This example draws 2 texts near the origin. Per default, text is aligned left and bottom (i.e. without translation starts at 0 0 0.

Example 1.2.10   Simple Text  

VRML: ../examples/basics/basics-text-2.wrl
Source: ../examples/basics/basics-text-2.text

#VRML V2.0 utf8

Shape {
   geometry Text {
      string "Le chemin vers l'enfer"
      fontStyle FontStyle {
         size 2
      }   }
}

Transform {
   translation 0 1.5 0
   children [
      Shape {
         appearance Appearance {
            material Material {
               diffuseColor 1 1 0
            }
         }
         geometry Text {
            string [
                    "Le chemin vers",
                    "l'autre enfer"
                   ]
            fontStyle FontStyle {
               size 2
            }    }       }
     ]
}

   
1.2.8 Exercises

Exercice 1.2.1   Play with the Tiny-3d-2 Applet.

It will allow you to create simple VRML geometry, color, size and move around it. It also allows you to generate VRML code that you can paste into your editor.
Java 1.1.x (Netscape 4x PATCHED): eai/tiny3D-2/tiny3D-2.html
Java 1.0.x (Netscape 3x,4x): eai/tiny3D-2-java1.0.2/tiny3D-2.html

Exercice 1.2.2   You now might consider building a 3-D information page that points to HTML files.

You can look at some (simple) pages produced by our students.

   
1.2.9 Moving On

You should at least glance at the next chapter (2) in order to know how you can reuse nodes, build your own nodes, inline other scences etc.

In any case, you then should read chapter 3 on page [*]. Section 3.1 is absolutly mandatory reading.

If you think that you deserve a break go and check out the worlds at SGI's VRML gallery which can be found at http://vrml.sgi.com/worlds/.


next up previous contents index http://tecfa.unige.ch/ico/navi/tex2html/top.gifhttp://tecfa.unige.ch/ico/icons/vrml97-icon.gif
Next: 2. Modularization and Abstraction Up: 1. Starting with VRML Previous: 1.1 Introduction to the
D.K.S. - 1998-03-18