Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

3D graphics programming in Java: Part 2, Advanced Java 3D

Learn more about Java 3D performance, 3D content loaders, and the Java 3D VRML97 browser

  • Print
  • Feedback

Page 2 of 6

Behaviors can be keyed to any number of Java events or user interactions. When a user hovers near a certain object, for instance, you can cause the object to move, change color, or otherwise exhibit behavior. (In this case, the stimuli would be the user's presence inside of a specified bounds, or region, of space.)

Many behaviors can be controlled using interpolators, objects that accept a range of values to provide a time-varying behavior. You can build your own interpolators if you like, but Java 3D's built-in interpolators and Sun's utilities should suffice for most of your needs. In this month's Example 4, we modify last month's Example 3 to make the word JavaWorld rotate.


001 /**
002 * constructContentBranch() is where we specify the 3D graphics
003 * content to be rendered. We return the content branch group
004 * for use with our SimpleUniverse. We have added a RotationInterpolator
005 * to Example03 so that in this case, our "JavaWorld" text rotates
006 * about the origin. We have also removed the scaling and static
007 * rotation from the text, and the scaling from our ColorCube.
008 **/
009 private BranchGroup constructContentBranch() {
010 Font myFont = new Font("TimesRoman",Font.PLAIN,10);
011 Font3D myFont3D = new Font3D(myFont,new FontExtrusion());
012 Text3D myText3D = new Text3D(myFont3D, "JavaWorld");
013 Shape3D myShape3D = new Shape3D(myText3D, new Appearance());
014 Shape3D myCube = new ColorCube();
015
016 BranchGroup contentBranchGroup = new BranchGroup();
017 Transform3D myTransform3D = new Transform3D();
018 TransformGroup contentTransformGroup = new TransformGroup(myTransform3D);
019 contentTransformGroup.addChild(myShape3D);
020
021 Alpha myAlpha = new Alpha();
022 myAlpha.setIncreasingAlphaDuration(10000);
023 myAlpha.setLoopCount(-1);
024 RotationInterpolator myRotater =
025 new RotationInterpolator(myAlpha,contentTransformGroup);
026 myRotater.setAxisOfRotation(myTransform3D);
027 myRotater.setMinimumAngle(0.0f);
028 myRotater.setMaximumAngle((float)(Math.PI*2.0));
029 BoundingSphere myBounds = new BoundingSphere();
030 myRotater.setSchedulingBounds(myBounds);
031 contentTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
032 contentTransformGroup.addChild(myRotater);
033
034 contentBranchGroup.addChild(contentTransformGroup);
035
036 myTransform3D.setTranslation(new Vector3f(-0.5f,-0.5f,-2.3f));
037 TransformGroup cubeTransformGroup = new TransformGroup(myTransform3D);
038 cubeTransformGroup.addChild(myCube);
039 contentBranchGroup.addChild(cubeTransformGroup);
040
041 return(contentBranchGroup);
042 }


Rotating 'JavaWorld' in the 3D world using RotationInterpolator -- Part 1

Rotating 'JavaWorld' again in the 3D world using RotationInterpolator -- Part 2

By default, behaviors have no scheduling bounds and are never scheduled. You must set scheduling bounds yourself to execute behaviors in Java 3D. It's also important to remember that even with nondefault bounds specified, your behaviors may not be executed if the view platform isn't within the bounded region. This is, of course, by design, but it can make for frustrating debugging experiences if you're not careful.

  • Print
  • Feedback

Resources
  • Sun has made its "Java 3D 1.1 Performance Guide" available on its product site. This guide provides some short tips and tricks developers can heed to speed up their Java 3D applications. http://www.sun.com/desktop/java3d/collateral/j3d_perfguide.html
  • The home page for the Java 3D and VRML working group provides links to download the Java 3D VRML browser. http://www.vrml.org/WorkingGroups/vrml-java3d/
  • You can download the Java 3D VRML97 browser from the Java Developer Connection (free registration required). http://developer.java.sun.com/developer/earlyAccess/java3D/index.html
  • VRML97 is an international standard, officially designated ISO/IEC 14772-1:1997. The complete standard specification is available from the VRML Consortium Web site. http://www.vrml.org/Specifications/VRML97/
  • Get information on Transform3D with the API spec and documentation. API specification
    javadocs
  • If you need more information on how transforms are actually applied using matrix mathematics, I recommend the classic computer graphics primer "Introduction to Computer Graphics" (Addison-Wesley, ISBN0201609215). http://www1.clbooks.com/asp/bookinfo/bookinfo.asp?theisbn=0201609215
  • The Java 3D Archive links to loaders and shapes for use with Java 3D. You can use Java 3D loader software to read in models from common 3D file formats and translate them into a scene graph that the Java 3D runtime can render. The archive was assembled through the efforts of Matt Robinson, who has since passed its maintenance on to me. http://reality.sgi.com/bday/Java3DArchives/index.html
  • NCSA's Portfolio provides a consistent interface for loading many popular 3D modeling formats into the Java 3D runtime. It also supports domain-specific loaders for formats such as Protein Data Bank (PDB). Portfolio provides an implementation of Canvas3D, which is able to save snapshots into JPEG files. These files can then be used to make MPEG movies of Java 3D worlds. http://havefun.ncsa.uiuc.edu/Java3D/portfolio/
  • For more on the Java 3D API performance-oriented features, see "Introduction to Programming with Java 3D." http://www.sdsc.edu/~nadeau/Courses/SDSCjava3d/
  • JavaWorld has published several good VRML-related articles. In addition to the two below, be sure to visit the JavaWorld Topical Index for more VRML-related information.
  • "3D computer graphicsGetting the hang of VRML"
  • "Why Java and VRML?"
  • http://www.javaworld.com/topicalindex/jw-ti-vrml.html
  • The Java 3D mailing list archive stores lots of useful tips and tricks. Please search here before posting new questions to the list. http://java.sun.com/products/java-media/mail-archive/3D/index.html
  • Allen McPherson has made some example code available to help in the visual debugging of normal problems. http://java.sun.com/products/java-media/mail-archive/3D/1860.html
  • Learn how to get the frame rate from the Java 3D rendering engine. http://java.sun.com/products/java-media/mail-archive/3D/0093.html
  • Tips on coaxing Java 3D applets to run inside your Web browser. Includes step-by-step instructions for installing and configuring the Java 2 platform (formerly Java 1.2) and Java 3D to work with Netscape's browser. http://java.sun.com/products/java-media/mail-archive/3D/1212.html
  • Need to know the bounding volumes used by the Java 3D VRML97 browser? Try this sample code. http://java.sun.com/products/java-media/mail-archive/3D/1063.html
  • Read about an interesting real-world use of Java 3D and VRML, Nearlife's Virtual Fishtank. The article contains a special sidebar on Java 3D and the fishtank. http://java.sun.com/features/1998/11/fishtank.html
  • Download source code and classes for this column. http://www.javaworld.com/jw-01-1999/media/jw-01-media.jar
  • I've archived Media Programming resources on my Web site. This archive contains the up-to-date media.jar file with code fixes for all the examples in the column. http://reality.sgi.com/bday/Work/index.html
  • Read all my previous Media Programming columns. http://www.javaworld.com/topicalindex/jw-ti-media.html