⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch21.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>



<HEAD>

   <TITLE>Chapter 21 -- Building VRML Models with Perl</TITLE>

   <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT COLOR=#FF0000>Chapter 21</FONT></H1>

<H1><B><FONT SIZE=5 COLOR=#FF0000>Building VRML Models with Perl</FONT></B></H1>

<P>

<HR WIDTH="100%"></P>

<P>

<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>

</FONT></FONT></H3>

<UL>

<LI><A HREF="#WhatIsVRML" >What Is VRML?</A>

<UL>

<LI><A HREF="#WhyWriteVRMLpm" >Why Write VRML.pm?</A>

<LI><A HREF="#WheretoGetMoreInformationonVRML" >Where to Get More Information on VRML</A>

</UL>

<LI><A HREF="#BasicsofVRML" >Basics of VRML</A>

<LI><A HREF="#BuildingtheVRMLPerlModule" >Building the VRML Perl Module</A>

<UL>

<LI><A HREF="#WheretoGettheSourceforthePerlto" >Where to Get the Source for the Perl to VRML Package</A>

</UL>

<LI><A HREF="#UsingVRMLpm" >Using VRML.pm</A>

<LI><A HREF="#HowtoViewYourWorld" >How to View Your World</A>

<LI><A HREF="#InsidetheVRMLpmPackage" >Inside the VRML.pm Package</A>

<LI><A HREF="#Summary" >Summary</A>

</UL>

<HR>

<P>

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

<TT><FONT FACE="Courier">VRML.pm</FONT></TT> that generates VRML-coded

text based on Perl scripts.

<H2><A NAME="WhatIsVRML"><B><FONT SIZE=5 COLOR=#FF0000>What Is

VRML?</FONT></B></A></H2>

<P>

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 <I>de facto</I> 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.

<P>

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 &quot;walk

through&quot; a virtual world and thus provides more functionality

for user interaction.

<P>

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.

<P>

Information on VRML 2.0 can be found on the Web site <TT><A HREF="http://www.sd.tgs.com/VRML/vrml2.htm" tppabs="http://www.sd.tgs.com/VRML/vrml2.htm">http://www.sd.tgs.com/VRML/vrml2.htm</A></TT>.<FONT FACE="AGaramond Bold">

</FONT>The letters <I>tgs</I> stand for Template Graphics Software.

<H3><A NAME="WhyWriteVRMLpm"><B>Why Write </B><TT><B><FONT SIZE=4 FACE="Courier">VRML.pm</FONT></B></TT><B><FONT SIZE=4>?</FONT></B></A>

</H3>

<P>

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 <TT><FONT FACE="Courier">VRML.pm</FONT></TT> module file and

rerun my scripts to generate new, up-to-date 3D worlds in the

modified VRML specification.

<P>

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.

<P>

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 &quot;yes,&quot; 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.

<P>

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!

<P>

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.

<H3><A NAME="WheretoGetMoreInformationonVRML"><B>Where to Get

More Information on VRML</B></A></H3>

<P>

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:

<UL>

<LI><FONT COLOR=#000000>For a VRML authoring primer, see the Web

site at </FONT><TT><A HREF="http://www.sd.tgs.com/VRML/Help/vrmlhint.htm" tppabs="http://www.sd.tgs.com/VRML/Help/vrmlhint.htm">http://www.sd.tgs.com/VRML/Help/vrmlhint.htm</A></TT>.

<LI><FONT COLOR=#000000>The FAQs for </FONT>VRML can be found

at <TT><A HREF="http://vrml.wired.com/listfaq.html" tppabs="http://vrml.wired.com/listfaq.html">http://vrml.wired.com/listfaq.html</A></TT>.

</UL>

<P>

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 <TT><A HREF="http://wwwiz.com/books" tppabs="http://wwwiz.com/books">http://wwwiz.com/books</A></TT>.

There are books here on HTML, VRML, Java, and related topics.

Select the <TT><FONT FACE="Courier">Subjects</FONT></TT> 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.

<P>

Free worlds can be found at <TT><FONT FACE="Courier"><A HREF="ftp://www.vrml.org/pub/graphics" tppabs="ftp://www.vrml.org/pub/graphics">ftp://www.vrml.org/pub/graphics</A></FONT></TT>.

The artwork is free but copyrighted, so be sure to look for disclaimers.

<H2><A NAME="BasicsofVRML"><B><FONT SIZE=5 COLOR=#FF0000>Basics

of VRML</FONT></B></A></H2>

<P>

A VRML file simply defines all the objects required to generate

a 3D virtual world. Objects within this file are also referred

to as <I>nodes</I>. 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?

<P>

All VRML files are written in ASCII and have the first line of

the file in this form:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#VRML V1.0 ascii</FONT></TT>

</BLOCKQUOTE>

<P>

Generally, the VRML files have a <TT><FONT FACE="Courier">.WRL</FONT></TT>

extension, but this is not necessary. Some viewers may require

the use of <TT><FONT FACE="Courier">.WRL</FONT></TT> as the default

extension.

<P>

Comments are possible with <TT><FONT FACE="Courier">#</FONT></TT>,

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.

<P>

The VRML specification lists numbers with an <TT><FONT FACE="Courier">SF</FONT></TT>

prefix or an <TT><FONT FACE="Courier">MF</FONT></TT> prefix when

defining types of variables. The <TT><FONT FACE="Courier">SF</FONT></TT>

stands for <I>single field</I>; that is, the variable being referred

to has only one field in it. An example is <TT><FONT FACE="Courier">SFLong</FONT></TT>

for a long integer and <TT><FONT FACE="Courier">SFMatrix</FONT></TT>

for a transformation matrix. Note that even though <TT><FONT FACE="Courier">SFMatrix</FONT></TT>

contains 16 values, these values are all part of one field. The

<TT><FONT FACE="Courier">MF</FONT></TT> prefix stands for <I>multiple-value

fields</I>. An example of an <TT><FONT FACE="Courier">MF</FONT></TT>

field is a vector.

<P>

Vectors and matrices are supported in VRML. Both two-dimensional

(<TT><FONT FACE="Courier">SFVec2f</FONT></TT>) and three-dimensional

(<TT><FONT FACE="Courier">SFvec3f</FONT></TT>) vectors are supported.

Matrices are stored in a row major format.

<P>

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.

<P>

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 <TT><FONT FACE="Courier">SFColor</FONT></TT>

type to use only one field for R, G, and B. For other colors,

you have to use the <TT><FONT FACE="Courier">MFColor</FONT></TT>

type to define different ratios for R:G:B.

<P>

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:

<I>scaling</I> to get the right size, <I>translation</I> to move

from one location to another, and a <I>rotation</I> of some degrees

about a point of origin.

<P>

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.

<P>

There are several types of shapes that an object can take. The

full list supported in version 1.0 of VRML is shown here:

<UL>

<LI><TT><FONT FACE="Courier">Cube</FONT></TT> (supported by <TT><FONT FACE="Courier">VRML.pm</FONT></TT>)

<LI><TT><FONT FACE="Courier">Cone </FONT></TT>(supported by <TT><FONT FACE="Courier">VRML.pm</FONT></TT>)

<LI><TT><FONT FACE="Courier">Cylinder</FONT></TT> (supported by

<TT><FONT FACE="Courier">VRML.pm</FONT></TT>)

<LI><TT><FONT FACE="Courier">Sphere</FONT></TT> (supported by

<TT><FONT FACE="Courier">VRML.pm</FONT></TT>)

<LI><TT><FONT FACE="Courier">PointSet</FONT></TT> (for multiple

points)

<LI><TT><FONT FACE="Courier">IndexedLineSet</FONT></TT> (for multiple

vectors)

<LI><TT><FONT FACE="Courier">IndexedFaceSet</FONT></TT> (for multiple

surfaces)

<LI><TT><FONT FACE="Courier">AsciiText</FONT></TT> (for annotation)

</UL>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD ><B>Note</B></TD></TR>

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

The <TT><FONT FACE="Courier">VRML.pm</FONT></TT> module does not include support for generating all these types. As soon as this book goes to print, I will add the code to support these items. Stay tuned to the <TT><FONT 

FACE="Courier">README.VRML</FONT></TT> file at <TT><FONT FACE="Courier">ikra.com/pub/perl5/modules</FONT></TT> directory for latest updates.

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

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.

<P>

The group node offers limited functionality of the separator node

and is not supported by <TT><FONT FACE="Courier">VRML.pm</FONT></TT>.

<P>

The concept behind creating objects of a given type are simple.

Let's look at a simple example of a <TT><FONT FACE="Courier">VRML</FONT></TT>

script. The script shown in Listing 21.1 is the output from a

Perl script using the <TT><FONT FACE="Courier">VRML.pm</FONT></TT>

package. I will discuss the Perl script later in this chapter.

For now, just look at the <TT><FONT FACE="Courier">VRML</FONT></TT>

script. Note that the line numbers are added for reference.

<HR>

<BLOCKQUOTE>

<B>Listing 21.1. The sample </B><TT><B><FONT FACE="Courier">VRML</FONT></B></TT><B>

file.<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -