There is one other important concept in VRML, that of events. Information is sent back and forth between nodes via events. The section on ROUTEing Events covers the basics of events and their use.
The default scaling of these objects is roughly 2 units on a side. So, for example, the cylinder will default to a radius of 1 and a height of 2. Objects can be scaled either by using the Transform node or by setting the appropriate fields in the geometry node.
For the sake of standardization you should consider the unit of distance to be meters, and choose sizes appropriately. This is a common standard and is designed to reduce the need for special scaling when combining VRML models together.
Shape { geometry Cone { height 3.0 # radius defaults to 1.0 } }
The Material node has a number of fields, all of which you should become intimately familiar with in the next project, Trace. For now you can get by using just the diffuseColor field. Colors are specified as floating point RGB triples.
Appearance { material Material { diffuseColor 0.3 0 0.4 } }
The Transform node is by far the most important of the grouping nodes. It has a number of additional fields such as scale, rotation, and translation. These transformations act on all of the children of the Transform node.
The three basic transformations of scale, rotation, and translation are always done in that order. The order in which you list the fields does not change this. If you want a different order you can either nest Transform nodes or make use the two additional fields center and scaleOrientation. The center field defines the center of the scaling and rotation. The scaleOrientation is a rotation done on the axes before the object is scaled.
Transform { translation 0.0 2.0 0.0 # happens third scale 0.5 2.0 0.5 # happens first rotation 0 0 1 3.141 # happens second children [ Shape { geometry Cone {} } ] }
There is another way of reusing code called a PROTO, check a reference for the details of how to define and use a PROTO. In some ways this may be more familiar to you than DEF because it bares a strong resemblance to function definitions in other programming languages. Be aware, though, that you will be using your VRML model again in project 4 and PROTOs are currently not supported in that software.
DEF a_cone Transform { scale 0.5 2.0 0.5 translation 0.0 2.0 0.0 children Shape { geometry Cone {} } } Transform { rotation 0 0 1 1.5707 children USE a_cone } Transform { rotation 0 0 1 3.1415 children USE a_cone } Transform { rotation 0 0 1 -1.5707 children USE a_cone }
Sensors are the most common source of events, and the TimeSensor is probably the most important of the sensors. When a TimeSensor is started it ticks off the time by sending off events. It has a startTime and stopTime as well as a cycleInterval and true or false field called loop. One of the events that it sends is the fraction_changed event, which has values from 0.0 to 1.0 depending on what part of the cycle it is in.
To make use of this fraction_changed event you will probably need an interpolator node. The PositionInterpolator for example will translate these fractions into 3D vectors which may be used to relocate objects in the scene. You can think of the PositionInterpolator's role as defining a parameterization of a path in 3D by the interval from 0 to 1. Other interpolators parameterize scalers, colors, or rotations.
An interpolator has key and keyValue fields. The key field is a list of increasing numbers in the range from 0 to 1, typically beginning with 0 and ending with 1. The keyValue field lists what output value is associated with each key. When the interpolator gets a fraction not specified in its key list it will linearly interpolate between the nearest specified points. The event input for an interpolator is set_fraction and the event output is value_changed.
Finally, we come to ROUTE. ROUTE is used to wire all of these events up. ROUTE is used this way
ROUTE name.event_out TO name.event_inIn the above,
name
refers to the identifier the node was given using
DEF. Even if you are not going to reuse a node you need to give it
a name with DEF so that it can send or receive events.
DEF ball_ts TimeSensor { cycleInterval 2 loop TRUE } DEF ball_pi PositionInterpolator { key [0 0.5 1] keyValue [0 0 0 0 3.0 0 0 0 0] } DEF ball_t Transform { translation 0 0 0 children Shape { geometry Sphere {} } } ROUTE ball_ts.fraction_changed TO ball_pi.set_fraction ROUTE ball_pi.value_changed TO ball_t.set_translation
Edit a text file, your file should have the standard suffix .wrl so that it will be recognized as VRML by your web browser. On the first line you need to add the magic incantation
#VRML V2.0 utf8This specifies the VRML version and character encoding of the file.
Next type in your code. It might be easiest to start off by using one of the small examples above. Once the .wrl file is saved you can open it up in Netscape. There is no need to put the file on a web server, just open it up locally. Cosmoplayer will take a little while to load, if all goes well your 3D world should appear, if not you will get an error window. There are a number of ways to move through the world, try right clicking in the Cosmoplayer window to bring up a menu with viewing options.