|
Chapter 21Building VRML Models with Perl
CONTENTS
The Virtual Reality Modeling Language (VRML) is a relatively new language to surface on the Internet. The object-oriented features of Perl can be used with the three-dimensional object definitions of VRML to allow you as a user to produce relatively complex models in 3D space. This chapter introduces you to a Perl package called VRML.pm that generates VRML-coded text based on Perl scripts. What Is VRML?VRML is a language developed from the collective minds of a lot of Internet users. VRML enables you to define and lay out three-dimensional objects that can be viewed using a virtual program. VRML is often referred to as the virtual reality equivalent of HTML. It is becoming the de facto language on the Internet and World Wide Web for defining 3D objects. VRML is supported by many commercial vendors as the language to use in their software and hardware. Using VRML, you can describe virtual worlds. Components of these virtual worlds can be interconnected using the World Wide Web just as in HTML. All transactions in HTML involve getting a document into a browser and then viewing the document as a static entity. VRML extends this viewing capability by allowing a user to "walk through" a virtual world and thus provides more functionality for user interaction. The VRML 1.0 specification was the result of three individuals: Mark Pesce, Anthony Parisi, and Gavin Bell. The VRML 2.0 specification is still in the works; therefore, this chapter deals only with the 1.0 specification.
Information on VRML 2.0 can be found on the Web site http://www.sd.tgs.com/VRML/vrml2.htm.
The letters tgs stand for Template Graphics Software.
The VRML specification was in a state of flux at the time this
book was written. This volatility somehow justifies the use of
Perl to generate VRML code for worlds. If the changes to the VRML
specification drastically affect my VRML code, all I do is change
the VRML.pm module file and
rerun my scripts to generate new, up-to-date 3D worlds in the
modified VRML specification.
Added to this reason for using Perl is the strict type checking
and object-oriented features of Perl. Using Perl objects as building
blocks, complex images and worlds can be created to represent
results in a 3D environment.
The real reason is that I wanted to see whether it could be done.
That is, can we use Perl's object-oriented features to manage
3D objects in VRML? The answer is yes! There should be an asterisk
next to the "yes," though. The advantage gained in managing
VRML objects can be offset by the fact that a program, no matter
how well written, cannot crank out better VRML code than a human
being. Using this module I can set up and create simple worlds;
however, the code produced by the scripts can be optimized or
shortened considerably manually. The changes you make manually
will be overwritten the next time you run your program unless
you save them. In short, although it's possible to generate VRML
worlds from within Perl scripts, you might want to consider at
least looking at the output to see whether you can improve it
to reduce the size of the listings.
Also, before you get too deep into using Perl for generating objects
and VRML files, ask yourself this question: Do I need the object-oriented
features here? If your script is simply creating a model world
and you are supplying all the coordinates manually, you should
really consider buying a 3D modeling package. Of course, there
might be a wide variety of such programs available for your needs.
More and more tools are becoming available for VRML worlds as
this book goes to print. In my humble opinion, you should consider
looking at other VRML tools before you embark on writing your
own in Perl. A shareware or commercial application might just
do exactly what you need!
Also, if your Perl script is generating data that you want to
view visually in three dimensions, then you should consider using
this package. Some common uses for this package are creating chemical
models, accumulative distribution of particles on a non-uniform
surface, and contour seismic data.
Your first choice is to use the WebCrawler or InfoSeek search
results on VRML. The information from the online searches will
be far more current than any printed matter. Here are some sites
that have stood the test of time:
In addition to the resources on the Net, there are several texts
on the market. Some of the texts that deal with VRML and 3D graphics
are listed at http://wwwiz.com/books.
There are books here on HTML, VRML, Java, and related topics.
Select the Subjects link
to get a list of all the titles. Be sure to consult these books
to get more information on the details of 3D graphics and how
to work with VRML.
Free worlds can be found at ftp://www.vrml.org/pub/graphics.
The artwork is free but copyrighted, so be sure to look for disclaimers.
A VRML file simply defines all the objects required to generate
a 3D virtual world. Objects within this file are also referred
to as nodes. Each object knows the type of object it is,
has fields in it to identify its properties, possibly has a name,
and may have children. Sound like Perl yet?
All VRML files are written in ASCII and have the first line of
the file in this form:
Generally, the VRML files have a .WRL
extension, but this is not necessary. Some viewers may require
the use of .WRL as the default
extension.
Comments are possible with #,
as with the Perl or shell scripts. Strings can be within quotes.
Numbers can be grouped together within square brackets or can
be listed with white space between them. Numbers in VRML files
are floating point with few exceptions. Bitmapped flags are possible,
and so are Boolean variables.
The VRML specification lists numbers with an SF
prefix or an MF prefix when
defining types of variables. The SF
stands for single field; that is, the variable being referred
to has only one field in it. An example is SFLong
for a long integer and SFMatrix
for a transformation matrix. Note that even though SFMatrix
contains 16 values, these values are all part of one field. The
MF prefix stands for multiple-value
fields. An example of an MF
field is a vector.
Vectors and matrices are supported in VRML. Both two-dimensional
(SFVec2f) and three-dimensional
(SFvec3f) vectors are supported.
Matrices are stored in a row major format.
Coordinates for objects are given in an XYZ format. All objects
are created at the origin 0 0 0, unless otherwise specified at
the time of creation. You apply transformations to the objects
to move them to the desired location in the 3D space you are creating.
Colors are specified in RGB format, where floating numbers are
given as 0.0 for darkest and 1.0 for brightest. Thus, 000 is black,
001 is blue, and 111 is white. The brightness in each color component
is scaled from 0.0 to 1.0 in a linear fashion. There are two ways
to define colors. For a gray scale image (where the R:G:B ratios
are the same), use the SFColor
type to use only one field for R, G, and B. For other colors,
you have to use the MFColor
type to define different ratios for R:G:B.
You construct models based on types of objects by placing them
in the virtual world at different coordinates. The transformations
to place these objects in the world are of three basic types:
scaling to get the right size, translation to move
from one location to another, and a rotation of some degrees
about a point of origin.
Each object also has features such as color, texture, shininess,
and so on. These features are unique to the type of an object.
Object definitions can be made into a template of sorts to create
several objects of the same type and features.
There are several types of shapes that an object can take. The
full list supported in version 1.0 of VRML is shown here:
There is one important type of node that you should know about before we continue. This node is called the separator node. Basically, a separator node creates a new traversal tree for all following nodes. A traversal tree is simply a tree structure with nodes that are traversed by a VRML when rendering the image. Each leaf in the tree is a VRML object or the start of a new subtree with a separator node. It's generally very easy to group nodes together in a separator node, apply all translations (and so on) to these nodes, and then close the separator node to proceed with what you were doing earlier. With the help of several separator nodes, you can define different objects with different types of properties. The group node offers limited functionality of the separator node and is not supported by VRML.pm. The concept behind creating objects of a given type are simple. Let's look at a simple example of a VRML script. The script shown in Listing 21.1 is the output from a Perl script using the VRML.pm package. I will discuss the Perl script later in this chapter. For now, just look at the VRML script. Note that the line numbers are added for reference. Listing 21.1. The sample VRML file. 1 #VRML V1.0 ascii Lines 1 through 4 are comments generated from the VRML.pm module. In line 5, we define a separator which will contain the object just about to be defined in our virtual world. The ambient color for this object is defined in lines 6 through 9. More color, luminance, and reflectivity definitions could be defined in the Materials section. No transformation to move the object is applied in lines 10 and 11; however, the section is defined so that you can manually edit it. Lines 12 through 16 define the dimensions of a cube. The dimensions are given in a normalized coordinate scheme, i.e. ranging from 0 to 1. (To use a different reference for the size, you would have to apply a scaling transformation.) Line 17 ends the definition of this cube. The process of defining the material type, transformation, and object type of two more objects are repeated in lines 18-30 and 31-42. The process is repetitive: start a separator, define its type of material, apply a transformation to move, scale, or rotate it, and then define its normalized dimensions. The separator we opened in line 5 was terminated at line 43. This completes the definition of all the objects in our little world. The code required to generate the VRML script is shown in Listing 21.2. Admittedly, the number of lines in the Perl script are about the same as the number in the VRML script. As your models become more and more complex, this ratio should change to very few lines of Perl code per 100 lines of definitions in the VRML output. Listing 21.2. The code to generate two cubes. 1 #!/usr/bin/perl Let's look at the code shown in Listing 21.2. Lines 2 and 3 indicate that the VRML package and the Cube object are being used. Line 4 creates the VRML object, which will (eventually) contain all the definitions for the objects. Line 5 starts the required header for all VRML output. Line 6 starts a separator node, which will house all the current changes. In Lines 7 through 9, we create one cube by giving its width, height, and depth. The cube is placed at coordinates [1,0,0]. A second cube is placed at [1,1,0], via code in lines 11-14. All changes to the current separator node are ended at line 15. At this point, the script could start a completely different separator. It would be a good idea to examine the code in Listing 21.2 with the VRML output generated in Listing 21.1. By looking at how each line of Perl code generates each section of VRML, you should be able to see how to apply the VRML.pm module for your own programs. Building the VRML Perl ModuleThe VRML module is composed of several components. The main component is the VRML.pm file, and the rest of the components are shape object creators in the VRML directory. The VRML.pm file is located in the PERLLIBDIR directory (/usr/lib/perl5), and the shape creation files are located in the ${PERLLIBDIR}/VRML subdirectory. There are no extension files to worry about just yet. Where to Get the Source for the Perl to VRML PackageThe source of the Perl to VRML package is still in beta and resides at the ftp location ftp://ikra.com/pub/perl5/modules. No warranty of any sort applies in any terms whatsoever. You may copy, modify, and even use the contents of the package in commercial applications just as long as you credit me as the author and keep a note about there being absolutely no warranty. The same disclaimer and warranty for Perl is used for VRML.pm. Currently, only the following shape nodes are supported: Cube, Cone, Cylinder, and Sphere. All color, transform, PerspectiveCamera, and Material nodes are supported for shapes. As time progresses and the need arises, I will add more node definitions to this package. For comments, updates, and patches, please send me e-mail directly at khusain@ikra.com. If you have a better way of rewriting this module and would like to contribute, please let me know. Using VRML.pmYou have already seen a use of the VRML package in placing two cubes in a 3D world. That's nice, but not very useful. Why not use the power of Perl to do your expression for you? Look at Listing 21.3, which illustrates how to generate a staircase of values to show in 3D. Listing 21.3. Using VRML.pm to represent 3D data. 1 #!/usr/bin/perl In Listing 21.3, ten boxes are created to represent data. The height, width, and color of the cubes could be used to represent different features of each box. For the moment, we are stretching the cube to represent the box. The height of the box is a function of a value of an indexed item in an array. Each cube is placed next to each other along the X axis. The output from this script is shown in Listing 21.4. The point light sets a light source for illuminating the figures. (See Figure 21.1.) Note that I will use only the wireframe model rendering on my VRML viewer to display data because the colors and shading will not allow the models to be reproduced faithfully on paper. Figure 21.1 : Using boxes to represent data. Listing 21.4. Using boxes to represent data. 1 #VRML V1.0 ascii The script can be modified to use cylinders instead of cubes with a minor adjustment. The modified script to use cylinders is shown in Listing 21.5. Note the use of VRML::Cylinder instead of Cube. Also, note how the parameters to Cylinder are different from Cube, but the calling sequence to put each Cylinder at its location is not very different from the calling sequence used when placing Cubes. Listing 21.6 presents the output generated by the code in Listing 21.5. Compare Listing 21.6 with Listing 21.4 and notice how the cylinders are translated and created. Figure 21.2 illustrates using cylinders to represent data. Figure 21.2 : Using cylinders to represent data. Listing 21.5. Using cylinders instead of cubes to represent data. 1 #!/usr/bin/perl Listing 21.6. VRML output when using cylinders instead of cubes. 1 #VRML V1.0 ascii The for loop in Listing 21.5 is a pretty canned approach to generating the test data for this chapter. In most cases, you'll want to generate your data in a function of some sort and then display it. A sample usage of such a function is shown in Listing 21.7. Listing 21.7. Using a function to get data for display. 1 #!/usr/bin/perl In Listing 21.7, the subroutine getResults is used to generate the data required for display. The getResults subroutine in this example is only doing what the for loop did. However, there is nothing preventing you from replacing the getResults code to access data, for example. More than one column of data is also possible. Look at Listing 21.8. The output from this run is not listed here but is shown in Figure 21.3. Figure 21.3 : Using boxes and cylinders to represent data. Listing 21.8. Using multiple columns. 1 #!/usr/bin/perl In the code shown in Listing 21.8, two columns of data are created. Note the use of the Z offset in doing the translation of Cylinders. Also note that only one separator node is used for the entire image and one for each node. How to View Your WorldThere are several VRML viewers available on the market today. Some are totally free of charge, some free of charge up to 30 days, and the rest require a fee of anywhere from $50 to thousands of dollars. The following is a list of some of the vendors, their viewers, and what they have to offer. Most of the browsers are for Windows NT or Windows 95, but there are quite a few for UNIX workstations. A few of these are listed here. However, I would advise you to do a search on the Internet for a more comprehensive list. Here's the list:
VRML editors also offer great tools with which you can create your own worlds. Several tools are available for UNIX and Windows NT systems. Most of them let you create these worlds with far more ease than manually edited scripts will. The following tools are available from the commercial sector:
Inside the VRML.pm PackageLet's take a look at how the VRML.pm library is structured. Part of the main file, VRML.pm, is shown in Listing 21.8. Shape nodes are listed in separate files in the VRML subdirectory. The listing for the Cube.pm file is shown in Listing 21.9. Similar files exist for the Cylinder, Sphere, and Cone shapes. The project is still in its infancy and has to be developed further; however, you have enough here to construct your module and customize it for your needs. Listing 21.8. The VRML.pm file. 1 package VRML; Listing 21.9. Part of the VRML/Cube.pm file. 1 package VRML::Cube; SummaryThis chapter has introduced you to VRML and a Perl package that helps you create VRML files. Using the powerful features of Perl, you can generate VRML output in an object-oriented fashion. The use of the VRML.pm module is justified when you want to display the results of a calculation in three dimensions. Several VRML editors on UNIX and Windows are available on the Internet that allow you to create and view complex models.
|
|||||||||||||||||||
With any suggestions or questions please feel free to contact us |