Полезная информация


Special Edition Using HTML 4

Previous chapterNext chapterContents


- 29 -
VRML Worlds

by Jim O'Donnell and Mark R. Brown

The Virtual Reality Modeling Language

The Virtual Reality Modeling Language (VRML) is a language intended for the design and use of three-dimensional, multiperson, distributed interactive simulations. To put it in simpler language, VRML's designers intend it to become the building block of cyberspace.

The World Wide Web is based on HTML (Hypertext Markup Language), which was developed from the SGML (Standard General Markup Language) standard. SGML and HTML are fundamentally designed as two-dimensional text formatting toolsets. Mark D. Pesce, Peter Kennard, and Anthony S. Parisi presented a paper called Cyberspace at the First International Conference on the Web in May 1994, in which they argued that because humans are superb visualizers and live and work in three dimensions, extending the Web with a third dimension would allow for better organization of the masses of data already on the Web. They called this idea the Virtual Reality Markup Language. The concept was welcomed, and the participants immediately began searching for a format to use as a data standard. Subsequently, the M in VRML was changed from Markup to Modeling to accentuate the difference between the text-based nature of the Web and VRML.


ON THE WEB: The paper, Cyberspace, is available over the Web at http://www.hyperreal.com/~mpesce/www.html.

Silicon Graphics' Open Inventor was chosen as the basis for creating the VRML 1.0 standard. Open Inventor is an object-oriented C++ developer's toolkit used for rapid development of three-dimensional graphic environments. Open Inventor has provided the basis for a number of standards, including the Keystone Interchange Format used in the entertainment industry and the ANSI/ISO's X3H3 3D Metafile specification.

The second version of VRML moved away from Open Inventor in order to achieve better performance and additional functionality.

VRML's design specifications were guided by the following three goals:

VRML Objects

The building blocks of VRML creations, usually called VRML worlds, are objects created in VRML. The VRML language specification contains a collection of commands, called nodes, for the creation of a variety of simple objects such as spheres, boxes, and cylinders, as well as objects consisting of an arbitrary collection of vertices and faces.


NOTE: For a three-dimensional object, faces are the flat surfaces that make up the object, and vertices are the points where the faces meet. A cube, for instance, is made up of six square faces, each defined by four vertices. There are eight vertices needed to define a cube, since each vertex is shared by three faces.

VRML allows for the creation of more complex objects through the combination of simple objects. It is a hierarchical language, with child objects attached to their parents in a transformation hierarchy. For instance, if a complex object was being defined to create a model of a human body, then the lower arms would be attached to the upper arms, which would be attached to the torso, and so on. The rest of this chapter focuses on the creation of VRML objects.

VRML Worlds

The assemblage of VRML objects into a (hopefully) coherent whole defines a VRML world. There are many example VRML worlds on the Web that use the three-dimensional paradigm for different purposes. To define the placement and relationship of different objects to one another, you need to be able to specify their relative sizes and positions, using VRML's coordinate system. Additionally, VRML allows you to define what lighting sources are present in your world and what preset views are included.

Adding Motion and More to VRML

VRML 1.0 worlds are static. The only motion within them is the movement of the viewpoint representing the user as he or she uses a VRML browser to travel through the VRML world. With the definition of VRML 2.0, VRML's capabilities were extended to allow the creation of dynamic worlds.

VRML 2.0 objects can be given movement of their own, and three-dimensional sound (audio that sounds different depending on the position of the listener with respect to the source) can be added. Another capability introduced with VRML 2.0 is the ability to add behaviors to VRML objects. Behaviors--which can be scripted or specified in Java applets, for instance--are characteristics of objects that depend on their relationship to other objects in the VRML world, to the viewer, or to other parameters, such as time. For instance, a VRML 2.0 fish in an aquarium might swim away if you get too close to it.

Why (and How) Should You Use VRML?

As a Web author interested in VRML, you need to ask yourself what you would like to achieve with it. There are two important constraints that must be kept in mind. The first is that badly authored VRML worlds can end up being big. Specifying three-dimensional objects as a collection of flat surfaces can lead to very large object descriptions, particularly when trying to model a curved surface. The other important issue is that the connection speed of the majority of people on the Internet is still limited to no higher than that achieved with a 28.8KBps modem.

Complex VRML worlds can take a long time to be transmitted over the Internet, which very often limits the audience to only those people looking for cool VRML worlds. These worlds can also be extremely complicated to define and set up, requiring much more discussion than the space we have here. (Something more along the lines of Que's Special Edition Using VRML is needed to adequately cover the subject.)

However, a very good use for VRML (one that doesn't have the problems of requiring huge files to be downloaded) is to add special effects to HTML Web pages. Because you can embed small VRML scenes into HTML Web pages, VRML is an ideal addition to the Web author's bag of tricks.

This chapter's primary focus is to familiarize you with enough VRML that you can create small VRML scenes to achieve specific special effects within your Web pages. In the course of doing so, you will also learn enough of the VRML language and syntax to give you a good grasp on the language fundamentals so that you can move on to the creation of larger VRML worlds, if you want.

Basic VRML Syntax

VRML files are plain ASCII (though they are often gzipped--compressed by using the GNU zip, or gzip, utility--to make them easier to transmit over the Internet), which means that you can create them using ordinary text editors. It is likely that you will decide to use a VRML authoring program if you want to create a very large, complex VRML world--and even for smaller worlds, if you have an authoring program available--in which case the details of VRML syntax are hidden from you. It is a good idea to get a basic grasp of the important VRML language elements, though. Later, this knowledge will help you get the results you want.

Listing 29.1 shows a simple VRML file that displays a red cone on a white background (see Figure 29.1).

Listing 29.1  rcone.wrl--Display a Red Cone on a White Background

#VRML V2.0 utf8
WorldInfo {
    info "Special Edition, Using HTML, 4th Edition4th Edition"
}
Background {
    skyColor 1 1 1
}
DEF RedCone Shape {
    appearance Appearance {
        material Material {
            diffuseColor 1 0 0 # the color red
        }
    }
    geometry Cone { }
}

FIG. 29.1
Specifying simple objects can be done with just a few lines of VRML code.

The VRML comment character is the pound sign (#); everything after a # on any line is a comment (such as the color red). The first line of the file begins with a #, so it is a comment. Unlike other comments in the file, this one is necessary. It identifies the file as being VRML and gives the version number of VRML used. VRML browsers require this information to be located at the start of any VRML file.

The first line of the file shown in Listing 29.1 reads #VRML V1.0 ascii, meaning the file conforms to version 1.0 of the VRML specification. The word ascii means the standard ASCII character set is used in the file. VRML 2.0 files generally have the comment #VRML V2.0 utf8 in their first line, indicating conformance to the VRML 2.0 specification and that an international character set based on ASCII is being used.

Other than ignoring comments, the file format is entirely free form. Anywhere there is white space--tabs, spaces, or carriage returns--you can have as much or as little space as you'd like. For instance, an equivalent listing to the one shown in Listing 29.1 is:

#VRML V2.0 utf8
WorldInfo { info "Special Edition, Using HTML, 4th Edition4th Edition" }
Background { skyColor 1 1 1 } DEF RedCone Shape { appearance
Appearance { material Material { diffuseColor 1 0 0 # the color red
}}
geometry Cone { } }


TIP: As with any programming language, you should structure and comment your VRML files well enough that they can be easily read and understood.

Nodes and Fields

If you are familiar with the C, C++, or Java programming languages, you might recognize the curly braces ({ and }) which define blocks of related information. VRML files are made up of nodes, which look like this: NodeType { fields} The NodeType refers to one of the types of nodes that is supported by the VRML specification. The full VRML 1.0 and 2.0 specifications can be found on the Web, through the VRML Repository, which is located at http://www.sdsc.edu/vrml/. The example shown in Listing 29.1 uses six different kinds of nodes, WorldInfo, Background, Shape, Appearance, Material, and Cone.

Configuring Nodes with Fields  Inside the braces are fields. In the example, the WorldInfo node has an info field, and the Material node has a field called diffuseColor. Each field has a name and a value. For the diffuseColor field, the value is 1 0 0, a set of three numbers that indicate the color to use for the Cone, which follows (the three numbers list the color's components in the order red, green, blue). Field values can be simple numbers, groups of numbers, strings, images, Boolean values, and more. Some fields can have multiple values, in which case the values are separated by commas and surrounded by square brackets. For example, you could specify three information strings in the WorldInfo node: WorldInfo { info [ "First line", "Second line", "Third line" ] }

Naming Nodes  Any node can be assigned a name by which it can be referred to later. This is done with the DEF prefix, as shown here and in Listing 29.1: DEF RedCone Shape { ... } You can assign any name to any node.

Objects, Hierarchies, Transforms

A VRML world can be thought of as a hierarchy of simple VRML objects. In VRML, the Transform node is used as the container for an object. Not all Transform nodes contain geometry (vertices and faces of VRML objects); they are used for grouping other objects into a more complex object. This is how object hierarchies are specified in VRML. The attachment information is specified by placing objects within other objects, using the Transform node and other grouping nodes. A node can contain a children field to list objects that are attached to it.

Simple VRML Objects

The VRML 2.0 specification includes ten nodes that let you specify geometric shapes. All VRML 2.0 objects are made up of one or more of these nodes. These nodes consist of four for geometric shapes, one for text, three for the creation of points, lines, and polygon-based objects, and two special nodes for creating terrain and extruded shapes.

Geometric Shapes

You already saw an example of the Cone node in Listing 29.1. Along with the Cone node, VRML allows you to directly create spheres, boxes, and cylinders. The syntax for each of these nodes is shown in the following list:

Cone {
     bottomRadius     radius
     height          height
     side            TRUE or FALSE
     top             TRUE or FALSE
}
Box {
     size       width height depth
}
Cylinder {
     radius          radius
     height          height
     sides           TRUE or FALSE
     top             TRUE or FALSE
     bottom          TRUE or FALSE 
}
Sphere {
     radius          radius
}

Each of these nodes has default values for each of its fields. So, if you specify a sphere by simply using Sphere{}, you will get a sphere with a radius of one.


NOTE: While the Sphere, Cone, and Cylinder nodes seem to specify curved surfaces in VRML, when the VRML file is parsed by a VRML browser the objects are converted into vertices and faces through a process called tessellation. To see this for yourself, load a VRML example showing one of these curved surfaces into a VRML browser and switch the browser into flat-shading mode to see the individual faces.

Text

The Text node allows you to create flat-text objects in VRML. Because the resulting text is flat, it is possible that it might not be visible when viewed, if looked at edge-on. The Text node has four fields, string, fontStyle, MaxExtent, and length, and its syntax is as follows:

Text {
     string          ["string(s)",...]
     fontStyle       FontStyle node that gives justification, spacing, font
     maxExtent       maximum size in any direction
     length          lengths of the text strings in the string field 
}

Listing 29.2 shows an example of the use of the Text node, with the results shown in Figure 29.2.

Listing 29.2  ascii2d.wrl--Two-Dimensional Text in VRML

4th Edition#VRML V2.0 utf8
WorldInfo {
    info "Special Edition, Using HTML, 4th Edition"
}
Background {
    skyColor 1 1 1
}
Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0 0 0
        }
    }
    geometry Text {
        string [ "This Is", "A Test", "Of Text" ]
        fontStyle FontStyle { spacing 2 justify "MIDDLE" }
    }
}

FIG. 29.2
VRML text is two-dimensional; if you rotate it so that you view it edge-on, it is not visible.

General VRML Shapes

The nodes discussed so far are useful to start, especially if you're building simple worlds by hand. Most VRML files, however, make extensive use of another node, IndexedFaceSet. An IndexedFaceSet node is a way of describing an object using a set of vertices that are joined by faces. Listing 29.3 shows an example of this, which creates a pyramid using five vertices and five faces to create the four sides and the base (see Figure 29.3).

Listing 29.3  pyrface.wrl--Building General Shapes Using IndexedFaceSet

4th Edition#VRML V2.0 utf8
WorldInfo {
    info "Special Edition, Using HTML, 4th Edition"
}
Background {
    skyColor 1 1 1
}
DEF Pyramid Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0 0 1
        }
    }
    geometry IndexedFaceSet {
        coord Coordinate {
            point [ -1 0 -1,
                     1 0 -1,
                     1 0  1,
                    -1 0  1,
                     0 2  0 ]
        }
        coordIndex [ 0, 4, 1, -1
                     1, 4, 2, -1,
                     2, 4, 3, -1,
                     3, 4, 0, -1,
                     0, 1, 2, 3, -1 ]
    }
}

The array of vertex coordinates is specified by using the Coordinate node and its own field, point. The point field takes multiple values, each of which is a triplet of numbers giving the X, Y, and Z coordinates of one vertex. There can be as many vertices as you need. Keep in mind, though, that each vertex only needs to be specified once, no matter how many faces it is used in, because vertices define single points that may be shared by multiple faces.

The IndexedFaceSet node contains a field called coordIndex that stores a list of faces, as specified by the indices of vertices in the order they are used for each face. For instance, the sequence 0, 4, 1, -1 is used to create one face from the 0th, 4th, and 1st vertices (vertices are numbered from 0). The -1 signifies the end of the current face; the next face begins with the next number.

FIG. 29.3
VRML indexed face sets can be used to construct arbitrary three-dimensional solids.


NOTE: Although IndexedFaceSet is a very powerful tool, there are some things to watch out for when you are using it.

Make sure all the vertices in a given face are coplanar, meaning that the face is flat. If one or more of the vertices are not in the same plane, then the object will look very strange when viewed from certain angles.

Avoid T-intersections. Two faces should always meet at a shared edge. If you have a face that touches another face without sharing common vertices, the round-off errors in the VRML browser will cause viewing problems.

Avoid using too many faces, when possible, because using too many will slow down rendering more than any other factor.


VRML includes two other nodes that work much the same as IndexedFaceSet, called IndexedLineSet and PointSet. Each is used similarly to IndexedFaceSet, except that IndexedLineSet is used to construct a general set of lines between a set of vertices, and PointSet creates a field of dots at the vertices.

Adding Color and Texture to VRML Objects

VRML offers several different ways of changing the appearance of its objects. One of these, the Material node, shown previously in Listing 29.1, is used to create a red cone. Two nodes in particular can be used to affect the color, texture, and general appearance of VRML objects: the Material and ImageTexture nodes.

Material

The most common use of the Material node is that shown in Listing 29.1--using its diffuseColor field to specify the color of an object. The Material node supports other fields for achieving different effects.

ImageTexture

With the ImageTexture node, it is possible to achieve a much wider variety of effects. The ImageTexture node maps an image file to a VRML object. Listing 29.4 shows an example with my picture mapped onto a VRML cube (see Figure 29.4).

Listing 29.4  cubejod.wrl--Map Images to VRML Objects Using Texture2

4th Edition#VRML V2.0 utf8
WorldInfo {
    info "Special Edition, Using HTML, 4th Edition"
}
Background {
    skyColor 1 1 1
}
DEF Pyramid Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0 0 1
        }
        texture ImageTexture {
            url "bernie.gif"
        }
    }
    geometry Box { }
}

Because the faces are all flat in the preceding example, the image used isn't noticeably distorted. That isn't the case when images are mapped to curved surfaces, such as those created by Sphere, Cone, or Cylinder nodes. In these nodes, images are distorted as they are mapped to the curved surfaces. Note that different graphics formats can be used as textures; which formats are supported is VRML browser-specific.

Creating VRML Worlds and Inline VRML Scenes

In the context of this discussion, a VRML world refers to a VRML file that is designed to be stand-alone, and will be loaded into a VRML-compatible Web browser on its own, without being embedded in an HTML-based Web page. Though there is no lower or upper limit on the size and complexity of a VRML world, these worlds tend to be fairly large. The three-dimensional VRML paradigm is often used to allow the user to "move through" the VRML world, visiting the parts of interest to him.

FIG. 29.4
Images can be mapped to the faces of VRML objects.

One example of good use of VRML worlds is demonstrated through sites that model an actual three-dimensional object, building, or geographic location, allowing remote users from all over the world to actually "see" what that object looks like, perhaps even to travel through it. The University of Essex, in Britain, maintains such a site at http://esewww.essex.ac.uk/campus-model.wrl. This VRML world consists of a three-dimensional model of their campus. Users can move around the campus and see it from any conceivable angle. In addition, each building on the campus contains a hyperlink to an HTML Web page with information about that building.

Inline VRML scenes are best used to achieve a given special effect within an HTML Web page. By creating a very small, very specialized VRML scene and displaying it inline, you can achieve a variety of special effects. This is particularly true if you add some of the animation and movement extensions possible with VRML 2.0, as discussed later in this chapter. An inline VRML scene can be used to achieve a similar effect to an animated GIF; depending on the desired effect, this can be done with a VRML file smaller than the GIF.

Design Considerations

After you come up with an idea for your VRML environment, you need to consider a number of other factors that influence the final design. As well as deciding what objects you want to put in the VRML environment and where they are with respect to each other, there are other factors that might limit what you can achieve. How big a VRML environment should you create? How detailed should it be? How should it be shaped? How should everything be laid out? How should you create it?

Size and Detail

The first thing you should consider before drafting your environment is size--not in terms of the space it takes up in the virtual world, but the final size of your WRL file. In a perfect world, everyone has a high-powered graphics workstation and a T1 line connecting them to the Internet, and you don't have to worry about how big your VRML file is, how long it takes to transmit over the Internet, or how long it takes to render after it arrives at the client machine.

In reality, however, things are quite different. Most people are running 486 and Pentium PCs over 14.4Kbps and 28.8Kbps modems. If you come up with a VRML world that is 10M, you severely limit your audience because of the hour and a half download time and the time it takes for the client computer to render it. No matter why you are interested in providing VRML environments on the Web, no one will look at it if it takes that long.

Therefore, you need to consider how big you are going to make your VRML environment and how detailed it should be. It is a question of compromise. You can have a very large environment, but then you cannot add a great amount of small detail. Or, if you have only a few objects in your environment, they can probably be displayed with a great deal of detail. It becomes a trade-off between size and detail.

That is why it is important to start the process with a purpose for your VRML environment. If you are trying to sell something and you want your customers to understand what they are getting, you should probably opt for multiple VRML environments, each of which displays a few objects--or even just one--in great detail. However, if you want to give your users a sense of what it's like to stand next to the Pyramids in Egypt, texturing each pyramid brick by brick isn't necessary. If you want to let your users tour a model of your entire electronics workshop, you might not need to include every oscilloscope and soldering iron. But, if you want them to see the tools used, you can limit your environment to a single workbench.

Design and Layout

After you have decided how big your environment will be and what to include in it, the next step is to decide how it will look. A VRML environment is like any other space, virtual or not. If it looks cluttered and unkempt, people won't want to look at it. You need to decide how you want things to be laid out and how you want people to navigate through your environment. Is it a scene that they will be looking at from a distance? Or do you want them to jump in and poke around?

Again, the important factor in answering these questions is your environment's purpose. For example, if you are creating a VRML world that requires users to follow a particular sequence, then you need to find ways to direct their travels through your world. On the other hand, if you want people to be able to freely explore through your VRML world, you might want to have a more open environment. Even if you are recreating a space that exists in the "real world," you need to consider what is necessary and not necessary.

VRML World Design Steps

We will now go through the steps required to design a very simple VRML world. First, we go through the process of initial layout, building VRML objects together into compound objects and placing them in our world. Then, we find out about some of the ways to add realism to our VRML world, through the use of textures, lighting, and the addition of multiple camera viewpoints. Finally, we find out how to link our VRML world to other VRML worlds, HTML pages, or anything with an URL.

While the process of building this VRML world might be easier with a VRML authoring or three-dimensional modeling software package, this chapter instead shows how it is done by hand. By performing the steps of VRML world-building manually, you get a much better grasp of the fundamentals of the VRML language, and if you subsequently want to use a VRML authoring tool, this foundation makes it much easier.


ON THE WEB: Yahoo! lists a plethora of VRML authoring tools at http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/Virtual_Reality_Modeling_Language__VRML_/Authoring/.

Mapping Your VRML Environment

Rather than charging off and starting to throw together VRML objects that you might need in your world, the first step in the design process should be to sketch out what you want your world to look like. An important tool at this point of the design process is a simple sheet of graph paper. By using graph paper, including both a top view and a side view of what you would like to put in your world, you get a very good first idea of the following important points:

VRML Coordinate Systems  While it is important to have a visual way of thinking to design your VRML environment, the way it is stored is as a set of coordinates and mathematical transformations. You need to convert your visual design into these coordinates and transformations--this is one of the reasons that sketching out your world on graph paper is a good idea. To fully understand how to accomplish this, you need to know a bit about the coordinate systems that VRML uses, as well as the vectors and transformations it employs. Cartesian coordinates, those used in VRML, are named after the geometry developed by René Descartes. They are basically the standard way of describing the two- or three-dimensional geometry of something. By default, when you begin looking at a VRML scene, the positive direction of the x-axis goes from left to right, the positive direction of the y-axis goes from down to up, and the positive direction of the z-axis goes from the back of the environment toward the front. This is called a right-handed coordinate system because if you curl the fingers of your right hand from the x- towards the y-axis, your thumb will point along the z-axis.

The right-handedness of the coordinate system also comes into play when you discuss rotations. The direction of a positive rotation about an axis is determined by the right-hand rule. For instance, to determine the direction of a positive rotation about the z-axis, point your right thumb along the z-axis in its positive direction. The way your fingers curl defines a positive rotation. Figure 29.5 shows the Cartesian coordinate system used within VRML.

FIG. 29.5
VRML uses the Cartesian coordinate system shown to create objects and define where objects are placed with respect to one another.

Vectors  A point in the Cartesian coordinate system is called a vertex. A vertex is simply a location in space represented by three numbers: x, y, and z. A vector is related to a vertex, in that it is also represented by x, y, and z coordinates. Whereas a vertex represents a point in space, a vector represents a direction. So, the vertex (1,0,1) represents the point x=1, y=0, and z=1. The vector (1,0,1) represents the direction you would be traveling in going from the origin, the point (0,0,0), to the vertex (1,0,1).

VRML Units  When specifying coordinates and rotation angles in VRML, you need to remember the measure of distance is a meter, and the measure of rotation angle is a radian.


NOTE: A radian is a unit used to measure angles and rotations. There are 2[pi] radians in 360°, so you can determine the number of radians from a given number of degrees by multiplying by [pi]/180, about 0.00175.

Putting Your Design on Paper

The first step in the design of a VRML world is to sketch what you want it to look like. By putting this down on graph paper, you are already a long way toward defining the coordinates, size, and position of the things in the world.

Figures 29.6 and 29.7 show a top and side view of the VRML world you will try to put together throughout the rest of this chapter. Figure 29.6 shows a "front" view of the world, with the z-axis pointing straight out of the paper. The two drawings define what the world should look like pretty well; you might find it helpful to include another side view, however, looking down the x-axis, for example.

FIG. 29.6
Top view of your planned VRML world.

Now that you have your VRML world sketched out, go back and see if these sketches answer the questions asked about the design:

FIG. 29.7
Here is a side view of your planned VRML world.

Moving Things Around

Creating the objects needed for our example shouldn't be very difficult. However, we now need to be able to move them around within the VRML environment. Without the capability to do this, all created objects will be lumped together at the origin of the VRML coordinate system. This moving is done using VRML's Transform node, whose syntax is:

Transform{
     translation          x y z
     rotation             rx ry rz ra
     scale                x y z
     scaleOrientation     rx ry rz ra
     center               x y z
     children             [ ]
}

The Transform node is used to move, scale, and rotate all the objects in its children field. Listing 29.5 shows the beginnings of our VRML world, showing the ground and the house, after the house has been moved to its correct position (see Figure 29.8).

Listing 29.5  world1.wrl--The Transform Node Is Used to Move Objects

4th Edition#VRML V2.0 utf8
WorldInfo {
    info "Special Edition, Using HTML, 4th Edition"
}
Background {
    skyColor 1 1 1
}
Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0 0.75 0 
        }
    }
    geometry IndexedFaceSet {
        coord Coordinate {
            point [ -5 0  5,
                     5 0  5,
                     5 0 -5,
                    -5 0 -5 ]
        }
        coordIndex [ 0, 1, 2, 3, -1 ]
    }
}
Transform {
    translation -2.5 1 -3
    children [
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 0 0 0 
                }
            }
            geometry Box { size 2.3333 2 1.3333 }
        }
    ]
}

Creating Object Hierarchies

Unless your VRML world is very simple--even simpler than our example--you might find yourself very often building up more complex VRML objects from simpler ones. While it is possible to treat each of the objects separately--and move and scale each individually--it's a lot easier to create the compound object from the individual ones and then manipulate that object with one operation. The VRML Transform node is used to create compound objects by enclosing other nodes. An object hierarchy is created by defining simpler objects and specifying their positions with respect to one another using a Transform node. Then, the entire hierarchy can be manipulated at once.

FIG. 29.8
The Transform node allows simple and compound VRML objects to be moved around the VRML environment, as well as rotated and scaled.

Listing 29.6 shows the next addition to our VRML world, the addition of the roof to our house. Note that the roof is created with an IndexedFaceSet and positioned on top of the bottom part of the house using a Transform node. Then, the compound object representing the complete house is moved into the correct position within the VRML environment (see Figure 29.9).


NOTE: In this listing, differences between it and the previous one are shown in italic. For further listings, as we build up this example, we will show only the new elements.

Listing 29.6  world2.wrl--Creating Compound Objects with the Transform Node

#VRML V2.0 utf8
WorldInfo {
    info "Special Edition, Using HTML, 4th Edition"
}
Background {
    skyColor 1 1 1
}
#
# The Ground
#
Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0 0.75 0 
        }
    }
    geometry IndexedFaceSet {
        coord Coordinate {
            point [ -5 0  5,
                     5 0  5,
                     5 0 -5,
                    -5 0 -5 ]
        }
        coordIndex [ 0, 1, 2, 3, -1 ]
    }
}
#
# The House
#
Transform {
    translation -2.5 1 -3
    children [
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 0 0 0 
                }
            }
            geometry Box { size 2.3333 2 1.3333 }
        }
        Transform {
            translation 0 1 0
            children [
                Shape {
                    appearance Appearance {
                        material Material {
                            diffuseColor 0 0 1
                        }
                    }
                    geometry IndexedFaceSet {
                        solid FALSE
                        coord Coordinate {
                            point [
                                -1.5 0 -1,
                                -1.1667 1 -0.6667,
                                1.5 0 -1,
                                1.1667 1 -0.6667,
                                1.5 0 1,
                                1.1667 1 0.6667,
                                -1.5 0 1,
                                -1.1667 1 0.6667
                            ]
                        }
                        coordIndex [
                            0, 2, 4, 6, -1,
                            1, 3, 5, 7, -1,
                            0, 2, 3, 1, -1,
                            2, 4, 5, 3, -1,
                            4, 6, 7, 5, -1,
                            6, 0, 1, 7, -1
                        ]
                    }
                }
            ]
        }
    ]
}

FIG. 29.9
Creating compound objects in VRML makes it much easier to create and manipulate complex scenes.

With the ability to create compound objects, we can add the rest of the elements to our VRML world--a tree made up of a sphere and a cylinder, and the wishing well made up of two cylinders. Listing 29.7 shows the new VRML code to produce these objects, and the resulting scene is shown in Figure 29.10.

Listing 29.7  world3.wrl (excerpt)--Completing the VRML World

#VRML V2.0 utf8
WorldInfo {
    info "Special Edition, Using HTML, 4th Edition"
}
Background {
    skyColor 1 1 1
}
#
# The Ground
#
Shape {
    appearance Appearance {
        material Material {
            diffuseColor 0 0.75 0 
        }
    }
    geometry IndexedFaceSet {
        coord Coordinate {
            point [ -5 0  5,
                     5 0  5,
                     5 0 -5,
                    -5 0 -5 ]
        }
        coordIndex [ 0, 1, 2, 3, -1 ]
    }
}
#
# The House
#
Transform {
    translation -2.5 1 -3
    children [
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 0 0 0 
                }
            }
            geometry Box { size 2.3333 2 1.3333 }
        }
        Transform {
            translation 0 1 0
            children [
                Shape {
                    appearance Appearance {
                        material Material {
                            diffuseColor 0 0 1
                        }
                    }
                    geometry IndexedFaceSet {
                        solid FALSE
                        coord Coordinate {
                            point [
                                -1.5 0 -1,
                                -1.1667 1 -0.6667,
                                1.5 0 -1,
                                1.1667 1 -0.6667,
                                1.5 0 1,
                                1.1667 1 0.6667,
                                -1.5 0 1,
                                -1.1667 1 0.6667
                            ]
                        }
                        coordIndex [
                            0, 2, 4, 6, -1,
                            1, 3, 5, 7, -1,
                            0, 2, 3, 1, -1,
                            2, 4, 5, 3, -1,
                            4, 6, 7, 5, -1,
                            6, 0, 1, 7, -1
                        ]
                    }
                }
            ]
        }
    ]
}
#
# The Tree
#
Transform {
    translation 2.5 1.5 -1.5
    children [
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 0.5 0.25 0
                }
            }
            geometry Cylinder {
                radius 0.1667
                height 3
            }
        }
        Transform {
            translation 0 1.5 0
            children [
                Shape {
                    appearance Appearance {
                        material Material {
                            diffuseColor 0 1 0
                        }
                    }
                    geometry Sphere {
                        radius 1.5
                    }
                }
            ] 
        }
    ]
}
#
# The Wishing Well
#
Transform {
    translation -3 0.3333 3
    children [
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 1 1 1
                }
            }
            geometry Cylinder {
                radius 1
                height 0.6667
            }
        }
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 0 0 0 
                }
            }
            geometry Cylinder {
                radius 0.8 
                height 0.7
            }
        }
    ]
}

Adding Realism with Textures

Earlier you learned a little about how to use the ImageTexture node to add realism to an object through the addition of image file textures. By default, VRML maps the image file specified by ImageTexture to each entire face of the solid in question--the six faces of a cube or the top, bottom, and curved surface of a cylinder, for instance. When mapping a texture to a surface to make it more realistic, it is best to tile small images repeatedly over the different faces. This can be done by using the TextureTransform node. Its syntax is

TextureTransform {
     translation     x y
     rotation     angle
     scale     x y
     center          x y
}

FIG. 29.10
All of your objects have been placed in the VRML world.

These fields allow you to move, rotate, scale, and center the image on the solid to determine how it is applied. To make a tiled texture more realistic looking, the most important field is scale, which determines how many times the image will be tiled. Note that scale refers to how the coordinates of the object to which the texture is being mapped will be scaled, so scale factors that are greater than one result in the image appearing smaller on the object and tiled more times.

Listing 29.8 shows an example of the texturing applied to our VRML world (the full listing on the CD shows the textures applied to all of the objects). In this case, a rocky appearance is given to the wishing well, and other, more realistic appearances are given to the other objects in the VRML world.

Listing 29.8  world4.wrl (excerpt)--Using Textures to Add Realism

#
# The Wishing Well
#
Transform {
    translation -3 0.3333 3
    children [
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 1 1 1
                }
                texture ImageTexture {
                    url "bernie.gif"
                }
                textureTransform TextureTransform {
                    scale 8 1
                }
            }
            geometry Cylinder {
                radius 1
                height 0.6667
            }
        }
        Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 0 0 0 
                }
            }
            geometry Cylinder {
                radius 0.8 
                height 0.7
            }
        }
    ]
}

FIG. 29.11
While textures certainly lend a more realistic appearance to a VRML world, they do slow down the file transmission and rendering, so use them only when needed.

Linking to the Web

VRML, like HTML, is a language meant to be used on the Internet and the Web. An essential element for this is the hyperlink. This allows VRML worlds to be linked to other VRML worlds. And, if the VRML browser supports it, URLs can also be followed to HTML Web pages and other Internet resources.

Inline  One use of VRML's capability to link to the Web is through the Inline node. This node allows you to include VRML objects in your VRML worlds from any local VRML file or any VRML world on the Web. The syntax of the Inline node is

WWWInline {
   url "path of local file or URL"
}

The VRML object, objects, and VRML world defined by that file or URL are placed into your VRML world as if the code for them was entered in the same place in your VRML code.

Anchor  Hyperlinks are implemented in VRML by using the Anchor node. The important fields for this node are the url field, which is used to specify the URL hyperlink, and the description field, which gives a text description of the link. Objects that are defined within the Anchor node are the objects to which the hyperlink is attached. Like Transform, Anchor is a container node; the other VRML nodes that it contains become the anchors for the hyperlink defined by the node. The syntax of the Anchor node is

WWWAnchor {
     url            "url of some VRML world"
     description "description of that world
     children other VRML node(s)...
}

Features of VRML 2.0

The VRML 1.0 language specification allows for the creation, presentation, and viewing of static three-dimensional scenes and worlds. VRML 2.0 was designed to build on that standard to provide much more. The goal of the VRML 2.0 standard is to provide the tools to create three-dimensional worlds that include movement and sound and allow the objects within the world to be programmed with behaviors that allow them to react to your presence. VRML fish can be programmed to swim away from you if you get to close, for instance.

A second goal of VRML 2.0 is to create a foundation for Web-based three-dimensional content that can continue to evolve and grow. As computers continue to grow more and more powerful, the Internet continues to develop, and high-bandwidth, high-speed connections become more commonplace, the VRML standard will continue to be developed to take advantage of the new capabilities.

The capabilities of VRML 2.0 over VRML 1.0 fall into the five general categories of static world enhancements, interaction, animation, scripting, and prototyping. These are discussed in the VRML 2.0 specification, available at http://vrml.sgi.com/moving-worlds/. They are summarized in the following sections.

Enhanced Static Worlds

VRML 2.0 supports several new nodes and fields that allow the static geometry of VRML worlds to be made more realistic. You can create separate backdrops for the ground and the sky, using colors or images. Objects such as clouds and mountains can be put in the distance, and fog can be used to obscure distant objects. Irregular terrain can be created, rather than using flat planes for your surface. VRML 2.0 also provides three-dimensional sound to further enhance realism.

Interaction

VRML 2.0 includes a new class of nodes, called sensor nodes, that are able to set off events in response to different inputs. Touch and proximity sensors react to the presence of the viewer either touching or coming close to an object. A time sensor is capable of keeping track of the passage of time, allowing time-correlated events to be added to your VRML world. And VRML 2.0 supports realistic collision detection and terrain following to ensure that your viewers bounce off of (or at least stop at) your walls and solid objects and are able to travel through your world while easily following things like steps and inclines.

Animation

VRML 2.0 interpolator nodes allow you to create predefined animations for any of the objects in your VRML world. These animations can be programmed to occur automatically or in response to some other factor, either an action of your viewer or at a given time. With these interpolators, you can create moving objects, objects such as the sun or the moon that change color as they move, or objects that change shape. The viewpoint can also be animated to create an automatic guided tour of your VRML world.

Scripting

The key to many of VRML 2.0's other features--particularly the movement of VRML 2.0 objects--is its support of scripting. Scripting is used to program objects' behaviors, not only allowing them to move but also giving them the capability to react realistically to objects around them. A script is the link that is used to take an event--generated by a sensor node, for instance--and generate the appropriate action.

Prototyping

The final category of enhancement to VRML 2.0 is the capability for prototyping. What this allows you to do is to create your own nodes. By grouping a set of nodes to achieve a specific purpose, within a new prototype node, that node becomes available for reuse.


NOTE: For more information, tutorials, and examples of VRML 2.0, as well as VRML 1.0 and other VRML implementations, take a look at the excellent Web site provided by Vijay Mukhi at http://www.neca.com/~vmis/vrml.htm.

VRML Resources

Probably the central clearinghouse of all things VRML, appropriately called the VRML Repository, is located at http://www.sdsc.edu/vrml/. This Web site contains a vast array of information about VRML, links to sites containing VRML browser and authoring software, and just about anything else you can think of that has to do with VRML.

VRML Software

A wide range of software is available on the Web, most of it listed on the VRML Repository. The general categories of software available include the following:

VRML Object Libraries

If you are interested in creating VRML objects for use in a VRML world or with an embedded VRML special effect in a Web page, you should probably take a look around on the Web before starting from scratch. There are many places to get three-dimensional objects that you can use for your purposes, both in VRML files and in other formats that can be converted to VRML.

The best place to start, predictably, is the VRML Repository. This site maintains a library of example applications, categorized by topic, that can be examined through the Web page at http://www.sdsc.edu/vrml/.

Among the many other sites featuring VRML examples, applications, and objects, one is the VRML Models site located at http://www.ocnus.com/models/models.html. This site, like the VRML Repository library, features an indexed list of VRML objects and worlds. A unique feature of the VRML Models site is its VRML Mall, which is an actual three-dimensional gallery through which you can view all of their VRML objects.

Finally, the Mesh Mart, located at http://www.meshmart.org/, was set up as a source of three-dimensional objects. While most of these objects are not in VRML format, they are available in formats that can be easily converted to VRML. Through the Mesh Mart, a program called wcvt2pov.exe is available that can convert between many different three-dimensional formats. It can read in files with the following formats:

In addition, it can write out files in the following formats:


Previous chapterNext chapterContents


© Copyright, Macmillan Computer Publishing. All rights reserved.