banner



Creating A 3d Design Program In Java

Java 3D has the potential to allow Java programmers of all sorts to include vibrant 3D content in their applets and applications. This is fact, but not certainty, as Sun faces heavy competition from other technologies, especially OpenGL. Beyond the basics that every 3D API must provide, what does Java 3D have going for it?

Last week we began our exploration of Java 3D with static content and small scenes. This week we'll conclude our introduction by delving into loaders for larger scene graphs and giving our worlds behaviors and interactivity. We'll also discuss the interplay between Java 3D and VRML, review the performance characteristics of Java 3D, and set the stage for next month's look at Java OpenGL bindings.

Java 3D Transforms and Placement

I received several questions about the placement of the 3D objects in last week's examples. One reader in particular asked why, when he modified the transform so the cube's z coordinate was positive, he could no longer see the cube. Let's reexamine the example code from last week's Example 3 (see Resources for links to the code archive) to learn more about how things are situated in three dimensional space using Java 3D.

The critical line of code from this example is:

myTransform3D.setTranslation(new Vector3f(-0.5f,-0.5f,-2.3f));        

The arguments in the Vector3D constructor are x, y, and z coordinates. The cube is placed by this transform at x = -0.5; y = -0.5; and z = -2.3 (in floating point values, hence the "f" suffixes).

It's critical to note that your viewing location can change. This is determined by the location of the associated ViewPlatform, which, in turn, is situated in the 3D world using the TransformGroup above it (actually, using a Transform3D node to transform the TransformGroup above it).

Also note that in Example 3, we don't move the ViewPlatform; we construct all of the view branch using the default constructors, which use an identity Transform3D node (more on identity later) in the TransformGroup above ViewPlatform. Thus, the ViewPlatform is located at x = 0.0; y = 0.0; and z = 0.0, the origin of the 3D coordinate system.

And finally, it's important to note that the coordinate system is aligned with your view into it. With the default view branch, the +x axis runs from left to right across the screen and the +y axis runs from bottom to top, which means, using the right-hand rule, that the +z axis runs out of the screen towards you. By default, your view into the world is looking in the -z direction and the origin is located in the middle of your screen.

Figure 1: Default Java 3D coordinate axes, as seen from directly in front of the monitor. We are looking in the -z axis direction, directly into the monitor. The origin is located in the center of the x-y plane.

Figure 2: Default Java 3D coordinate axes, as seen from directly above the front of the monitor. We are looking in the -y axis direction along the x-y plane. The +z axis is sticking straight out of the monitor.

Because you're "sitting" at the origin (the view platform is located at 0.0, 0.0, 0.0) looking in the negative-z direction, anything in the positive-z section of the 3D world is behind you and you can't see it. Don't you just love 3D! (See Resources for more information on Transform3D.)

Exploring the com.sun.j3d class archives
Now, let's return to the issue of the contents of Sun's private jar files. Last week I briefly mentioned that Sun's Java 3D implementation installs both standard Java 3D classes (as specified in the Java 3D API specification) and nonstandard (Sun implementation-specific) classes. These classes are made available in one of several jar archives placed within the new Java 2 platform (aka JDK 1.2 for developers) extensions directory (see last week's article for more information on this). We have discussed the standard classes and jars in sufficient detail, but we still have some ground to cover on the Sun-specific ones.

As a reminder, Sun's archives include:

  • j3daudio.jar, which archives the com.sun.j3d.audio packages, described last week
  • j3dutils.jar, which encapsulates a variety of Sun utility classes in 17 total packages and subpackages underneath com.sun.j3d (more details in a moment)

When you explode the j3dutils.jar archive, you see the following subpackages of com.sun.j3d:

  • audioengines
  • audioengines.javasound
  • loaders
  • loaders.lw3d
  • loaders.objectfile
  • utils
  • utils.applet
  • utils.audio
  • utils.behaviors
  • utils.behaviors.interpolators
  • utils.behaviors.keyboard
  • utils.behaviors.mouse
  • utils.behaviors.picking
  • utils.geometry
  • utils.image
  • utils.internal
  • utils.universe

Now that you know the basic layout of Sun's utility class archives, you can delve into them more deeply if you need any of the functionality they provide. For instance, if you're looking to capture mouse events to control interpolated behaviors and you need to load in LightWave 3D content, you should review com.sun.j3d.utils.behaviors.mouse, com.sun.j3d.utils.behaviors.interpolators, and com.sun.j3d.loaders.lw3d.

Please note that while some documentation is provided for these packages in the general Java 3D documentation from Sun, the quality of documentation varies from package to package and from class to class. I recommend that you monitor the java3d-interest mailing list for information on the particular utility classes you need to use.

Behaviors and Interpolators

Interactivity results from being able to program an object to react to certain stimuli. In Java 3D, these behaviors are scheduled when the view platform crosses the stimulus bounds, a region of space defined by the programmer. Bounds assign a spatial scope to objects. There are bounds for both sounds and behaviors in Java 3D.

The advantage of requiring bounds for objects is that the runtime can safely disregard (not render) things that are out of bounds. If you build behaviors into your Java 3D applications, you will be required to set bounds in order to have the Java 3D runtime scheduler prepare your behaviors for execution. The scene graph includes a set of nodes for setting up and scheduling behaviors and bounds. In addition, several of the utilities described above help you to more easily deal with interactivity, including the aptly named "behaviors" subpackages.

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 Example 4 (see Resources), we modify last week's Example 3 to make the Gamasutra 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 "Gamasutra" 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, "Gamasutra"); 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   }        

Figure 3: Rotating 'Gamasutra' in the 3D world using RotationInterpolator -- Part 1

Figure 4: 'Gamasutra' continues to rotate around in the 3D world -- 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.

Creating A 3d Design Program In Java

Source: https://www.gamasutra.com/view/feature/3312/3d_graphics_programming_in_java_.php

Posted by: lightyproffecanded69.blogspot.com

0 Response to "Creating A 3d Design Program In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel