Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Tutorial Java3D Capitulo 3 - Criando Conteudo 3D Simples, Notas de estudo de Engenharia Informática

Tutorial Java3D Permission to use, copy, modify, and distribute this documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies.

Tipologia: Notas de estudo

Antes de 2010

Compartilhado em 10/04/2010

alessandro-borges-10
alessandro-borges-10 🇧🇷

3

(2)

12 documentos

1 / 34

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
tutorial v1.5 (Java 3D API v1.1.2)
Getting Started with
the Java 3D API
Chapter 3
Easier Content Creation
Dennis J Bouvier
K Computing
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Pré-visualização parcial do texto

Baixe Tutorial Java3D Capitulo 3 - Criando Conteudo 3D Simples e outras Notas de estudo em PDF para Engenharia Informática, somente na Docsity!

tutorial v1.5 (Java 3D API v1.1.2)

Getting Started with

the Java 3D

API

Chapter 3

Easier Content Creation

Dennis J Bouvier

K Computing

Getting Started with Java 3D

The Java 3D Tutorial

© 1999 Sun Microsystems, Inc. 2550 Garcia Avenue, Mountain View, California 94043-1100 U.S.A All Rights Reserved.

The information contained in this document is subject to change without notice.

SUN MICROSYSTEMS PROVIDES THIS MATERIAL "AS IS" AND MAKES NO WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SUN MICROSYSTEMS SHALL NOT BE LIABLE FOR ERRORS CONTAINED HEREIN OR FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS IN CONNECTION WITH THE FURNISHING, PERFORMANCE OR USE OF THIS MATERIAL, WHETHER BASED ON WARRANTY, CONTRACT, OR OTHER LEGAL THEORY).

THIS DOCUMENT COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY MADE TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.

Some states do not allow the exclusion of implied warranties or the limitations or exclusion of liability for incidental or consequential damages, so the above limitations and exclusion may not apply to you. This warranty gives you specific legal rights, and you also may have other rights which vary from state to state.

Permission to use, copy, modify, and distribute this documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies.

This documentation was prepared for Sun Microsystems by K Computing (530 Showers Drive, Suite 7-225, Mountain View, CA 94040, 770-982-7881, www.kcomputing.com). For further information about course development or course delivery, please contact either Sun Microsystems or K Computing.

Java, JavaScript, Java 3D, HotJava, Sun, Sun Microsystems, and the Sun logo are trademarks or registered trademarks of Sun Microsystems, Inc. All other product names mentioned herein are the trademarks of their respective owners.

Getting Started with Java 3D Chapter 3. Contents The Java 3D Tutorial 3-ii

  • Chapter Table of Contents
    • Easier Content Creation .......................................................................................................................3-
      • 3.1 What is in this Chapter ..............................................................................................................3-
      • 3.2 Loaders .....................................................................................................................................3-
        • 3.2.1 Simple Example of Using a Loader.....................................................................................3-
        • 3.2.2 Publicly Available Loaders.................................................................................................3-
        • 3.2.3 Loader Package Interfaces and Base Classes ......................................................................3-
        • 3.2.4 Writing a Loader................................................................................................................3-
      • 3.3 GeometryInfo ............................................................................................................................3-
        • 3.3.1 Simple GeometryInfo Example...........................................................................................3-
        • 3.3.2 Classes for GeometryInfo...................................................................................................3-
      • 3.4 Text2D....................................................................................................................................3-
        • 3.4.1 Simple Text2D Example ..................................................................................................3-
        • 3.4.2 Classes Used in Creating Text2D Objects.........................................................................3-
      • 3.5 Text3D....................................................................................................................................3-
        • 3.5.1 Simple Text3D Example ..................................................................................................3-
        • 3.5.2 Classes Used in Creating Text3D Objects.........................................................................3-
      • 3.6 Background.............................................................................................................................3-
        • 3.6.1 Background Examples......................................................................................................3-
        • 3.6.2 Background Class ............................................................................................................3-
      • 3.7 BoundingLeaf..........................................................................................................................3-
        • 3.7.1 BoundingLeaf Class.........................................................................................................3-
      • 3.8 User Data................................................................................................................................3-
      • 3.9 Chapter Summary....................................................................................................................3-
      • 3.10 Self Test..................................................................................................................................3-
  • Figure 3-1 Recipe for Using a Loader....................................................................................................3- List of Figures
  • Figure 3-2 A GeometryInfo Polygon and One Possible Triangulation .....................................................3-
  • Figure 3-3 Two Renderings of a Car (facing opposite directions) Created Using GeometryInfo...............3-
  • Figure 3-4 Class Hierarchy for the GeometryInfo Utility Class, and Related Classes ..............................3-
  • Figure 3-5 Recipe for Text2D .............................................................................................................3-
  • Figure 3-6 Image from Text2DApp.java..............................................................................................3-
  • Figure 3-7 The Class Hierarchy for Text2D.........................................................................................3-
  • Figure 3-8 Recipe for Creating a Text3D Object..................................................................................3-
  • Figure 3-9 The Default Reference Point and Extrusion for a 3DText Object.........................................3-
  • Figure 3-10 Class Hierarchy for Text3D .............................................................................................3-
  • Figure 3-11 Recipe for Backgrounds ...................................................................................................3-
  • Figure 3-12 Viewing the “Constellation” in the Background of BackgroundApp.java............................3-
  • Figure 3-13 The Class Hierarchy for Background ................................................................................3-
  • Figure 3-14 BoundlingLeaf Moves with a Visual Object and Independently of a Light Source ..............3-
  • Figure 3-15 Java 3D API Class Hierarchy for BoundingLeaf ...............................................................3-
  • Table 3-1 Publicly Available Java 3D Loaders ......................................................................................3- List of Tables
    • Alignment and Path .....................................................................................................................3- Table 3-2 The Orientation of Text and Position of the Reference Point for Combinations of Text3D
  • Code Fragment 3-1 A Portion of jdk1.2/demo/java3d/ObjLoad/ObjLoad.java................3- List of Code Fragments
  • Code Fragment 3-2 Using GeometryInfo, Triangulator, NormalGenerator, and Stripifier Utilities...........3-
  • Code Fragment 3-3 A Text2D Object Created (excerpt from Text2DApp.java) ....................................3-
  • Code Fragment 3-4 Making a Two-sided Text2D Object. ....................................................................3-
  • Code Fragment 3-5 Creating a Text3D Visual Object ..........................................................................3-
  • Code Fragment 3-6 Adding a Colored Background ..............................................................................3-
  • Code Fragment 3-7 Adding a Geometric Background...........................................................................3-
  • Code Fragment 3-8 Adding a BoundingLeaf to the View Platform for an "Always-On" Bounds............3-

Getting Started with Java 3D Chapter 3. Contents

The Java 3D Tutorial 3-iii

List of Reference Blocks

Class ObjectFile....................................................................................................................................3- com.sun.j3d.loaders Interface Summary.................................................................................................3- com.sun.j3d.loaders Class Summary......................................................................................................3- Interface Loader Method Summary........................................................................................................3- LoaderBase Constructor Summary ........................................................................................................3- SceneBase Method Summary (partial list: loader users' methods) ...........................................................3- SceneBase Constructor Summary..........................................................................................................3- GeometryInfo Constructor Summary ...................................................................................................3- GeometryInfo Method Summary (partial list).......................................................................................3- Triangulator Constructor Summary .....................................................................................................3- Triangulator Method Summary ...........................................................................................................3- Stripifier Constructor Summary ..........................................................................................................3- Stripifier Method Summary.................................................................................................................3- NormalGenerator Constructor Summary..............................................................................................3- NormalGenerator Method Summary....................................................................................................3- Text2D Constructor Summary ............................................................................................................3- Text2D Method Summary...................................................................................................................3- Text3D Constructor Summary ............................................................................................................3- Text3D Method Summary...................................................................................................................3- Text3D Capabilities Summary ...........................................................................................................3- Font3D Constructor Summary.............................................................................................................3- Font3D Method Summary...................................................................................................................3- Font Constructor Summary (partial list) ..............................................................................................3- FontExtrusion Constructor Summary ..................................................................................................3- FontExtrusion Method Summary.........................................................................................................3- Background Constructor Summary......................................................................................................3- Background Method Summary ............................................................................................................3- Background Capabilities Summary......................................................................................................3- BoundingLeaf Constructor Summary...................................................................................................3- BoundingLeaf Method Summary .........................................................................................................3- SceneGraphObject Methods (Partial List - User Data Methods) ...........................................................3-

Preface to Chapter 3

This document is one part of a tutorial on using the Java 3D API. You should be familiar with Java 3D API basics to fully appreciate the material presented in this Chapter. Additional chapters and the full preface to this material are presented in the Module 0 document available at: http://java.sun.com/products/javamedia/3d/collateral

Cover Image

The cover image is of a galleon as modeled in an obj file and rendered by Java 3D. The Java 3D program that produced the rendering, ObjLoadApp.java, is a modification of the ObjLoad.java example program discussed in Section 3.2. The program source code for ObjLoad.java and the galleon model file are available with the JDK 1.2 distribution.

software. Loaders exist today for Alias obj files, VRML files, Lightwave files, Autocad dfx files, and a variety of other 3D file formats. New loaders are in development as well. The most important feature is the ability to write custom loaders for Java 3D.

Section 3.3 presents the GeometryInfo utility class, another alternative to hand coded geometry. GeometryInfo, along with the Triangulator, Stripifier, and NormalGeneration classes allows you to specify visual object geometry as arbitrary polygons. These classes convert the polygons to triangles, make strips of the triangles, and compute normals for the triangles at runtime, potentially saving you much coding time.

The next three sections present specific content creation techniques. Sections 3.4 and 3.5 present the Text2D utility and Text3D classes, respectively. These two classes represent two easy ways to add text to the contents of your virtual world. Section 3.6 presents the Background class. The Background class allows you to specify a color, image or geometry as the background for a virtual world.

The next two sections don’t have as much to do with content. However, these are important topics. Section 3.7 presents the BoundingLeaf class. BoundingLeaf objects are useful in conjunction with backgrounds, behaviors, lights, fog, and other Java 3D classes that require a bounds specification. Section 3.8 discusses the use of the UserData field of SceneGraphObject class.

Of course, the Chapter concludes with a summary and Self-Test exercises for the adventurous.

3.2 Loaders

A loader class reads 3D scene files (not Java 3D files) and creates Java 3D representations of the file's contents that can be selectively added to a Java 3D world and augmented by other Java 3D code. The utility com.sun.j3d.loaders package provides the means for loading content from files created in other applications into Java 3D applications. Loader classes implement the loader interface defined in the utility com.sun.j3d.loaders package.

Since there are a variety of file formats for the purpose of representing 3D scenes (e.g., .obj, .vrml, etc.) and there will always be more file formats, the actual code to load a file is not part of Java 3D or of the loaders package; only the interface for the loading mechanism is included. With the interface definition, the Java 3D user can develop file loader classes with the same interface as other loader classes.

3.2.1 Simple Example of Using a Loader

Without a class that actually reads a file, it is not possible to load content from a file. With a loader class it is easy. Figure 3-1 presents the recipe for using a loader.

  1. find a loader (if one is not available, write one: see section 3.2.4)
  2. import the loader class for your file format (see Section 3.2.2 to find a loader)
  3. import other necessary classes
  4. declare a Scene variable (don't use the constructor)
  5. create a loader object
  6. load the file in a try block, assigning the result to the Scene variable
  7. insert the Scene into the scene graph

Figure 3-1 Recipe for Using a Loader

A loader example based on the ObjectFile class is distributed with JDK 1.2. It is found in jdk1.2/demo/java3d/ObjLoad. Code Fragment 3-1 presents a excerpt from the code from this demo.

The ObjectFile class is distributed in the com.sun.j3d.loaders package as an example file loader. Other loaders are available (some are listed in Table 3-1).

Class ObjectFile

Package: com.sun.j3d.loaders Implements: Loader

The ObjectFile class implements the Loader interface for the Wavefront .obj file format, a standard 3D object file format created for use with Wavefront's Advanced Visualizer ™. Object Files are text based files supporting both polygonal and free-form geometry (curves and surfaces). The Java 3D .obj file loader supports a subset of the file format, but it is complete enough to load almost all commonly available Object Files. Free-form geometry is not supported.

Code Fragment 3-1 is annotated with numbers corresponding to the loader usage recipe given in Figure 3-1.

  1. import com.sun.j3d.loaders.objectfile.ObjectFile; å
  2. import com.sun.j3d.loaders.ParsingErrorException; ç
  3. import com.sun.j3d.loaders.IncorrectFormatException; ç
  4. import com.sun.j3d.loaders.Scene; ç
  5. import java.applet.Applet;
  6. import javax.media.j3d.*;
  7. import javax.vecmath.*;
  8. import java.io.*; ç
  9. public class ObjLoad extends Applet {
  10. private String filename = null;
  11. public BranchGroup createSceneGraph() {
  12. // Create the root of the branch graph
  13. BranchGroup objRoot = new BranchGroup();
  14. é ObjectFile f = new ObjectFile();
  15. è Scene s = null;
  16. ê try {
  17. s = f.load(filename);
  18. }
  19. catch (FileNotFoundException e) {
  20. System.err.println(e);
  21. System.exit(1);
  22. }
  23. catch (ParsingErrorException e) {
  24. System.err.println(e);
  25. System.exit(1);
  26. }
  27. catch (IncorrectFormatException e) {
  28. System.err.println(e);
  29. System.exit(1);
  30. }
  31. ë objRoot.addChild(s.getSceneGroup());
  32. }

Code Fragment 3-1 An Excerpt from jdk1.2/demo/java3d/ObjLoad/ObjLoad.java.

This program goes on to add behaviors (the default spin, or the mouse interaction - covered in Chapter 4) and lights (Chapter 6) to provide a shaded rendering of the object model. Of course, you can do many

com.sun.j3d.loaders Interface Summary

Loader The Loader interface is used to specify the location and elements of a file format to load.

Scene The Scene interface is a set of methods used to extract Java 3D scene graph information from a file loader utility.

In addition to the interfaces, the com.sun.j3d.loaders package provides base implementations of the interfaces.

com.sun.j3d.loaders Class Summary

LoaderBase This class implements the Loader interface and adds constructors. This class is extended by the authors of specific loader classes.

SceneBase This class implements the Scene interface and extends it to incorporate methods used by loaders. This class is also used by programs that use loader classes.

The methods defined in the loader interface are used by programmers using loader classes.

Interface Loader Method Summary

Package: com.sun.j3d.loaders

The Loader interface is used to specify the location and elements of a file format to load. The interface is used to give loaders of various file formats a common public interface. Ideally the Scene interface will be implemented to give the user a consistent interface to extract the data.

Scene load(java.io.Reader reader) This method loads the Reader and returns the Scene object containing the scene.

Scene load(java.lang.String fileName) This method loads the named file and returns the Scene object containing the scene.

Scene load(java.net.URL url) This method loads the named file and returns the Scene object containing the scene.

void setBasePath(java.lang.String pathName) This method sets the base path name for data files associated with the file passed into the load(String) method.

void setBaseUrl(java.net.URL url) This method sets the base URL name for data files associated with the file passed into the load(URL) method.

void setFlags(int flags) This method sets the load flags for the file. LOAD_ALL This flag enables the loading of all objects into the scene. LOAD_BACKGROUND_NODES This flag enables the loading of background objects into the scene. LOAD_BEHAVIOR_NODES This flag enables the loading of behaviors into the scene. LOAD_FOG_NODES This flag enables the loading of fog objects into the scene. LOAD_LIGHT_NODES This flag enables the loading of light objects into the scene. LOAD_SOUND_NODES This flag enables the loading of sound objects into the scene. LOAD_VIEW_GROUPS This flag enables the loading of view (camera) objects into the scene.

LoaderBase class provides an implementation for each of the three load() methods of Interface Loader. LoaderBase also implements two constructors. Note the three loader methods return a Scene object.

LoaderBase Constructor Summary

Package: com.sun.j3d.loaders Implements: Loader

This class implements the Loader interface. The author of a file loader would extend this class. The user of a file loader would use these methods.

LoaderBase() Constructs a Loader with default values for all variables.

LoaderBase(int flags) Constructs a Loader with the specified flags word.

In addition to the constructors listed in the reference block above, the methods listed in the following reference block are used by programmers using any loader class.

SceneBase Method Summary (partial list: loader users' methods)

In a departure from the normal formatting of a reference block, this reference block lists the methods.

Background[] getBackgroundNodes() Behavior[] getBehaviorNodes() java.lang.String getDescription() Fog[] getFogNodes() float[] getHorizontalFOVs() Light[] getLightNodes() java.util.Hashtable getNamedObjects() BranchGroup getSceneGroup() Sound[] getSoundNodes() TransformGroup[] getViewGroups()

3.2.4 Writing a Loader

As I mentioned above, one of the most important features of loaders is that you can write your own - which means that all other Java 3D users can too! Space and time constraints do not permit me to go into details on writing a loader here. However, the point of this section is to outline the process. If you have no intention of writing a loader, at least not now, you can skip to the next section.

In writing a loader, the author of the new loader class extends the LoaderBase class defined in the com.sun.j3d.loaders package. The new loader class uses the SceneBase class of the same package.

There should be little need for future loaders to subclass SceneBase, or to implement Scene directly, as the functionality of a SceneBase is fairly straightforward. SceneBase class is responsible for both the storage and retrieval of data created by a loader while reading a file. The storage methods (used only by Loader authors) are all of the add* routines. The retrieval methods (used primarily by Loader users) are all of the get* routines.

Writing a file loader can be quite complex depending on the complexity of the file format. The hardest part is to parse the file. Of course, you have to start with the documentation for the file format you want to write a loader class for. Once the format is understood, begin by reading the loader and scene base classes. The new loader class will extend the loader base class and use the scene base class.

In extending the loader base class, most of the work will be writing methods that recognize the various types of content that can be represented in the file format. Each of these methods then create the

An example program, GeomInfoApp.java, using the GeometryInfo, Triangulator, Stripifier, and NormalGeneration classes to create a car is included in the examples/easyContent directory. Figure 3-3 shows two renderings produced by GeomInfoApp.java. In both renderings the blue outlines shows the contours specified in the GeometryInfo object. The red triangles (filled and shaded on the left, outline on the right) were computed by the GeometryInfo object with Triangulation, NormalGeneration, and Stripification done automatically.

Figure 3-3 Two Renderings of a Car (facing opposite directions) Created Using GeometryInfo

A single planar polygon, similar to the one in shown in Figure 3-2, specifies the profile of the car (each side) in the GeomInfoApp example. Quadrilaterals specify the hood, roof, trunk lid, and other surfaces of the car.

3.3.1 Simple GeometryInfo Example

Using a GeometryInfo object is as easy as using core GeomertryArray classes, if not easier. In creating a GeomertyInfo object, simply specify the type of geometry you are going to need. The choices are POLYGON_ARRAY, QUAD_ARRAY, TRIANGLE_ARRAY, TRIANGLE_FAN_ARRAY, and TRIANGLE_STRIP_ARRAY. Then set the coordinates and strip counts for the geometry. You don’t have to tell the GeometryInfo object how many coordinates are in the data; it will be automatically calculated.

Code Fragment 3-2 shows an example GeometryInfo application. Lines 1 through 3 of Code Fragment 3- show creating a GeometryInfo object and the initial geometry specification.

After having created the GeometryInfo object, the other classes may be used. If you want to use the NormalGenerator, for example, first create a NormalGenerator object, then pass the GeometryInfo object to it. Lines 8 and 9 of Code Fragment 3-2 do just that.

  1. GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
  2. gi.setCoordinates(coordinateData);
  3. gi.setStripCounts(stripCounts);
  4. Triangulator tr = new Triangulator();
  5. tr.triangulate(gi);
  6. NormalGenerator ng = new NormalGenerator();
  7. ng.generateNormals(gi);
  8. Stripifier st = new Stripifier();
  9. st.stripify(gi);
  10. Shape3D part = new Shape3D();
  11. part.setAppearance(appearance);
  12. part.setGeometry(gi.getGeometryArray());

Code Fragment 3-2 Using GeometryInfo, Triangulator, NormalGenerator, and Stripifier Utilities.

3.3.2 Classes for GeometryInfo

The GeometryInfo and related classes are members of the com.sun.j3d.util.geometry package and are subclasses of Object. Figure 3-4 shows the hierarchy for these classes.

java.lang.Object

com.sun.j3d.utils.geometry.GeometryInfo

com.sun.j3d.utils.geometry.NormalGenerator

com.sun.j3d.utils.geometry.Stripifier

com.sun.j3d.utils.geometry.Triangulator

Figure 3-4 Class Hierarchy for the GeometryInfo Utility Class, and Related Classes

The GeometryInfo class has only one constructor and in this constructor you specify the kind of geometry to be specified by the coordinate data. The following reference block gives more detail.

GeometryInfo Method Summary (partial list)

void recomputeIndices() Redo indexes to guarantee connection information.

void reverse() Reverse the order of all lists.

void setColorIndices(int[] colorIndices) Sets the array of indices into the Color array.

void setColors(Color3f[] colors) Sets the colors array.

void setColors(Color4f[] colors) Sets the colors array. There are other setColors methods.

void setContourCounts(int[] contourCounts) Sets the list of contour counts.

void setCoordinateIndices(int[] coordinateIndices) Sets the array of indices into the Coordinate array.

void setCoordinates(Point3f[] coordinates) Sets the coordinates array.

void setCoordinates(Point3d[] coordinates) Sets the coordinates array. There are other setCoordinates methods.

void setNormalIndices(int[] normalIndices) Sets the array of indices into the Normal array.

void setNormals(Vector3f[] normals) Sets the normals array.

void setNormals(float[] normals) Sets the normals array.

void setStripCounts(int[] stripCounts) Sets the array of strip counts.

void setTextureCoordinateIndices(int[] texCoordIndices) Sets the array of indices into the TextureCoordinate array.

void setTextureCoordinates(Point2f[] texCoords) Sets the TextureCoordinates array. There are other setTextureCoordinates methods.

Each of the GeometryInfo 'helper' classes are used in a way similar to the GeometryInfo class. The following reference blocks show the constructors and methods for Triangulator, Stripifier, and NormalGenerator, in that order. This is the order in which they would be used for a POLYGON_ARRAY.

The Triangulator utility is only used with POLYGON_ARRAY geometry. GeometryInfo objects with other primitive geometry would only use Stripifier and NormalGenerator, as appropriate.

The default constructor of the Triangulator class simply creates a Triangulation object. See the reference block for more information.

Triangulator Constructor Summary

Package: com.sun.j3d.utils.geometry Extends: java.lang.Object

Triangulator is a utility for turning arbitrary polygons into triangles so they can be rendered by Java 3D. Polygons can be concave, nonplanar, and can contain holes (see GeometryInfo).

Triangulator() Creates a new instance of the Triangulator.

The only method of the Triangulator class is to triangulate a polygon array GeometryInfo object.

Triangulator Method Summary

void triangulate(GeometryInfo gi) This routine converts the GeometryInfo object from primitive type POLYGON_ARRAY to primitive type TRIANGLE_ARRAY using polygon decomposition techniques.

The only constructor of the Stripifier class creates a stripification object^5.

Stripifier Constructor Summary

Package: com.sun.j3d.utils.geometry Extends: java.lang.Object

The Stripifier utility will change the primitive of the GeometryInfo object to Triangle Strips. The strips are made by analyzing the triangles in the original data and connecting them together.

For best results Normal Generation should be performed on the GeometryInfo object before Stripification.

Stripifier() Creates the Stripifier object.

The only method of the Stripifier class is to stripify the geometry of a GeometryInfo class.

Stripifier Method Summary

void stripify(GeometryInfo gi) Turn the geometry contained in the GeometryInfo object into an array of Triangle Strips.

The NormalGenerator class has two constructors. The first constructs a NormalGenerator with a default value for the crease angle. The second constructor allows the specification of a crease angle with the construction of the NormalGenerator object. See the reference block below.

(^5) Each paragraph introduces a new word into the English language.

  1. Create a Text2D object
  2. Add it to the scene graph

Figure 3-5 Recipe for Text2D

Text2D objects are implemented using a polygon and a texture. The polygon is transparent so that only the texture is visible. The texture is of the text string set in the named typeface with the specified font parameters^6. The typefaces available on your system will vary. Typically, Courier, Helvetica, and TimesRoman typefaces will be available, among others. Any font available in the AWT is available to you for Text2D (and Text3D) applications. Using a Text2D object is straightforward as demonstrated in the next section.

3.4.1 Simple Text2D Example

Code Fragment 3-3 shows an example of adding a Text2D object to a scene. The Text2D object is created on lines 21 through 23. In this constructor, the text string, color, typeface, size, and font style are specified. The Text2D object is added to the scene graph on line 24. Note the import statement for Font (line 5) used for the font style constants.

  1. import java.applet.Applet;
  2. import java.awt.BorderLayout;
  3. import java.awt.Frame;
  4. import java.awt.event.*;
  5. import java.awt.Font;
  6. import com.sun.j3d.utils.applet.MainFrame;
  7. import com.sun.j3d.utils.geometry.Text2D;
  8. import com.sun.j3d.utils.universe.*;
  9. import javax.media.j3d.*;
  10. import javax.vecmath.*;
  11. // Text2DApp renders a single Text2D object.
  12. public class Text2DApp extends Applet {
  13. public BranchGroup createSceneGraph() {
  14. // Create the root of the branch graph
  15. BranchGroup objRoot = new BranchGroup();
  16. // Create a Text2D leaf node, add it to the scene graph.
  17. Text2D text2D = new Text2D("2D text is a textured polygon",
  18. new Color3f(0.9f, 1.0f, 1.0f),
  19. "Helvetica", 18, Font.ITALIC));
  20. objRoot.addChild(text2D);

Code Fragment 3-3 A Text2D Object Created (excerpt from Text2DApp.java)

Text2DApp.java is a complete program that includes the above Code Fragment. In this example, the Text2D object rotates about the origin in the scene. As the application runs you can see, by default, the textured polygon is invisible when viewed from behind.

(^6) A font is a specific typeface set at a size with a set of font style attributes. Refer to the glossary for definitions of

font and typeface.

Figure 3-6 Image from Text2DApp.java

Some attributes of a Text2D object can be changed by modifying the referenced appearance bundle and/or Geometry NodeComponent. Code Fragment 3-4 shows the code to make text2d, the Text2D object created in Code Fragment 3-3, two-sided through modification of its appearance bundle.

  1. Appearance textAppear = text2d.getAppearance();
  2. // The following 4 lines of code make the Text2D object 2-sided.
  3. PolygonAttributes polyAttrib = new PolygonAttributes();
  4. polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
  5. polyAttrib.setBackFaceNormalFlip(true);
  6. textAppear.setPolygonAttributes(polyAttrib);

Code Fragment 3-4 Making a Two-sided Text2D Object.

The texture created by one Text2D object can be applied to other visual objects as well. Since the application of textures to visual objects is the subject of Chapter 7, the details on this are left until then.

3.4.2 Classes Used in Creating Text2D Objects

The only class needed is the Text2D class. As you can see from Figure 3-7, Text2D is a utility class which extends Shape3D.

java.lang.Object

javax.media.j3d.SceneGraphObject

javax.media.j3d.Node

javax.media.j3d.Leaf

javax.media.j3d.Shape3D

com.sun.j3d.utils.geometry.Text2D

Figure 3-7 The Class Hierarchy for Text2D

In the Text2D constructor, the text string, typeface, font size, and font style are specified.