Test your Java 3D installation by running the HelloUniverse Java 3D demo. First run from the command line by going to the relevant installation directory and then typing: java HelloUniverse You can test the Java 2 plug-in installation by double-clicking the HelloUniverse_plug-in.html file. Your web browser should launch, the Java 2 plug-in window will appear, and the HelloUniverse applet should start. Once the tests are running you can safely delete the c:program filesjavasoft directory. 3.1.3 Documentation Java 3D programming involves general Java programming, high-performance programming, 3D graphics algorithms, 2D graphics programming, UI design, and many other issues. A good reference library or collection of electronic bookmarks will save you a lot of time and help you to avoid some of the pitfalls that have befallen your predecessors. Though far from exhaustive, the list of references which follows should give you some indication of fruitful areas to start researching. Java 2 SDK JavaDoc and reference books Java 2 is a complex technology. If you are going to write good Java 3D code you are going to require the latest Java 2 documentation and some good reference books. Swing reference book If you are developing an application that uses Swing (JFC) for the UI you will want to get a good Swing reference book. These weighty tomes can save you a lot of time during development. A good place to start is Swing by Mathew Robinson and Pavel Vorobiev (http://www.manning.com/Robinson/index.html). Java 3D JavaDoc Of course you should ensure that you download the latest API documentation for Java 3D. Sun collateral Java 3D tutorial The free Java 3D tutorial from Sun makes a good reference for many introductory topics and for those that like a structured tutorial style book to get started. Find it at http://www.javasoft.com/products/java-media/3D/collateral/. J3D.ORG You should check the J3D.ORG web site (http://www.j3d.org/) for FAQs and free example code. Many of the questions and problems that you run into have been faced and answered by other Java 3D users. Many answers are posted on the J3D.ORG web site or in the interest email list archives. J3D.ORG also contains useful utility code, tutorials, and examples that have been contributed by the active Java 3D community. Java 3D interest email list You should subscribe to this excellent forum (http://archives.java.sun.com/archives/java3d-interest.html) for asking fellow developers questions. Before posting your questions, take the time to search the archives for similar questions and answers. Java 3D user interface reference Building Java 3D User Interfaces by Jon Barrilleaux from Manning Publications will be very useful if you are building a complex 3D user interface. Jon answers many of the questions you will run into as you try to use 3D overlays, and presents solutions for the common UI requirements. For more information, surf to http://www.manning.com/Barrilleaux/index.html. There are several other Java 3D books coming into print check the J3D.ORG web site for the latest information. 3D graphics reference books If you are new to 3D graphics in general, you may want to pick up a good textbook on the subject. A good reference will cover the general aspects of 3D projections, transformation matrices, clipping, lighting, and rendering. Computer Graphics: Principles and Practice in C by James D. Foley, et al. (Addison-Wesley, 1995) is considered by many to be the bible of computer-generated 3D graphics. Many other useful books are reviewed in appendix B. 31
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
CHAPTER 3 Getting started, Hello Java 3D! 3.1 Installation 3.2 Your first Java 3D application 3.3 Exercises for the reader 3.4 Summary Now the fun begins. It s time to begin conquering the Java 3D development environment, setting ourselves up for serious Java 3D fun in the chapters to come. I ll introduce a realistic Java 3D application to test your configuration, and allow you to experiment with some of the features described in later chapters. You ll look at a simple Java 3D example, SimpleTest, that illustrates building an AWT-based Java 3D application. The SimpleTest example uses the Sun utility classes MainFrameand SimpleUniverse(included with your Java 3D distribution) to hide some of the complexities that we will be delving into in the chapters to come. 3.1 Installation Our first step, obviously, is to install everything we need for Java 3D development. Refer to appendix B and the bibliography for useful sources of information or additional help. 3.1.1 Java 2 SDK Check the Sun web site (http://java.sun.com) and download the latest release. Java 2 SDK 1.3.1 (JDK 1.3.1) is the latest release at the time of print. You can also find it at http://www.javasoft.com/j2se/1.3/. Remove all previous versions of the SDK, JDK, or JRE prior to installing the new SDK. After installation launch the Java plug-in control applet from the Windows Control Panel and set the Java Runtime Environment (JRE) to the newly installed SDK location. This will enable running Java 3D applets using the Java 2 plug-in. 3.1.2 Java 3D 1.2 JDK Download the latest release of the Java 3D SDK at http://www.javasoft.com/products/java-media/3D/index.html. The OpenGL version of Java 3D has historically been more stable and ahead of the DirectX release in terms of features. At the time of print the latest release is Java 3D 1.2.1. You should install Java 3D into the same directory as the Java 2 SDK, typically c:jdk1.3. This will ensure that all your Java 2 demo applications are installed into the same place. You can then use REGEDIT to edit the Windows registry to remove all references to the JRE installation directory (which is also installed when you install the SDK). Replace all occurrences of c:program filesjavasoftjre1.3 with the SDK installation location, usually c:jdk1.3jre This will enable running the Java 3D demos from the command line, and ensure that only one Java 2 runtime environment is installed on your machine. Do not run REGEDIT unless you are an experienced Windows user and IMPORTANT familiar with editing the registry. It is not strictly necessary to remove all references to the JRE install location. 30
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
myJava3D.setTitle( “MyJava3D” ); myJava3D.setSize( 300, 300 ); myJava3D.setVisible( true ); } } 2.5 Summary The MyJava3D example application should have demystified some of the magic of 3D rendering and provided the opportunity to experiment with and test your own rendering functionality. A useful exercise would be to add some form of depth sorting or a Z-buffer to the AwtRenderingEngine. With some enhancements, it might be useful in its own right as a lightweight 100 percent Java rendering engine. The example reinforces how much more convenient it is to leverage a graphics API such as Java 3D. Not only does Java 3D handle (through OpenGL or Direct3D) low-level issues such as Z-buffering, but it also defines classes for specifying geometry and a rendering abstraction called the scenegraph. The next chapter steps you through creating your first simple Java 3D application, so let s go! 29
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
// add the geometry to the rendering engine… renderingEngine.addGeometry( (GeometryArray) shape.getGeometry() ); // create a rendering surface and bind the rendering engine renderingSurface = new RenderingSurface( renderingEngine, geometryUpdater ); // start the rendering surface and add it to the content panel renderingSurface.start(); getContentPane().add( renderingSurface ); // disable automatic close support for Swing frame. setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); // add the window listener addWindowListener( new WindowAdapter() { // handle the system exit window message public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } public static void main( String[] args ) { MyJava3D myJava3D = new MyJava3D(); 28
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
private static int m_kWidth = 400; private static int m_kHeight = 400; private RenderingEngine renderingEngine = new AwtRenderingEngine(); private GeometryUpdater geometryUpdater = new RotatingGeometryUpdater(); private RenderingSurface renderingSurface; public MyJava3D( ) { // load the object file Scene scene = null; Shape3D shape = null; // read in the geometry information from the data file ObjectFile objFileloader = new ObjectFile( ObjectFile.RESIZE ); try { scene = objFileloader.load( “hand1.obj” ); } catch ( Exception e ) { scene = null; System.err.println( e ); } if( scene == null ) System.exit( 1 ); // retrieve the Shape3D object from the scene BranchGroup branchGroup = scene.getSceneGroup( ); shape = (Shape3D) branchGroup.getChild( 0 ); 27
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
public RotatingGeometryUpdater ( ) { } public boolean update( Graphics graphics, RenderingEngine engine, GeometryArray geometry, int index, long frameNumber ) { if ( lastFrame != frameNumber ) { lastFrame = frameNumber; Vector3d viewAngle = engine.getViewAngle( ); viewAngle.x += 1; engine.setViewAngle( viewAngle ); } return false; } } The MyJava3Dclass pulls all of these elements together. It creates an AwtRenderingEngineinstance, loads a GeometryArrayfrom disk using a Java 3D ObjectFileobject loader, adds the GeometryArrayto the AwtRenderingEngine, constructs a RenderingSurfacesupplying a RotatingGeometryUpdater, starts the RenderingSurface, and then adds it to the content pane of the JFramethat hosts the application. From MyJava3D.java /** * Render a 3D shape using a 3D rendering engine * that was written from scratch using AWT for * graphics operations. */ public class MyJava3D extends JFrame { 26
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
repainton the JPanel, forcing the scene to be continuously redrawn. The Surfaceclass, which is the base class for AnimatingSurface(both taken from Sun Java 2D demos), allows you to specify Java 2D rendering hints such as RenderingHints.VALUE_ANTIALIAS_OFF, which switches off antialiasing, and RenderingHints.VALUE_RENDER_SPEED, which tells the Graphicsobject to optimize for speed rather than rendering quality. It is interesting to see the effect of switching on antialiasing (figure 2.4 is on, figure 2.7 is off), as rendering APIs or 3D graphics hardware does not commonly support this functionality. Figure 2.7 MyJava3D rendering with Java2D antialiasing enabled The RotatingGeometryUpdaterclass is used to increase the X-angle of the viewer after each subsequent frame. From RotatingGeometryUpdater.java /** * Implementation of the GeometryUpdater interface * that rotates the scene by changing the viewer position * and the scale factor for the model. */ public class RotatingGeometryUpdater implements GeometryUpdater { long lastFrame = -1; 25
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
/** * Get the current View Angle used by the RenderEngine. View * angles are expressed in degrees. */ public Vector3d getLightAngle(); /** * Set the current View Angle used by the RenderEngine. */ public void setLightAngle( Vector3d angle ); /** * Set the Screen size used by the RenderEngine. */ public void setScreenSize( int width, int height ); /** * Set the scale used by the RenderEngine. */ public void setScale( double scale ); /** * Get the scale used by the RenderEngine. */ public double getScale(); } The RenderingEngineinterface is implemented by the AwtRenderingEngineclass, which uses simple Graphicsrendering calls (drawPolygon, setColor, drawLine, drawPoint) to render the 3D models. The RenderingEngineinstance is driven by a RenderingSurface, an instance of a JPanelthat provides a Graphicsobject for its client area and receives the frames of the rendered scene. The RenderingSurfaceextends AnimatingSurface, which creates a rendering thread, and calls 24
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
temp.normalize( ); temp.sub( light ); temp.normalize( ); cos_alpha = view.dot( temp ); intensity = (int) (lightMax * ( lightAmbient + lightDiffuse * cos_theta + lightSpecular * Math.pow( cos_alpha, lightGlossiness ))); } } } } return intensity; } 2.4 Putting it together MyJava3D The MyJava3D example defines the RenderingEngineinterface. This interface defines a simple rendering contract between a client and a 3D renderer implementation. The RenderingEngineinterface defines a simple renderer that can render 3D geometry described using a Java 3D GeometryArray. The GeometryArraycontains the 3D points and normal vectors for the 3D model to be rendered. In addition to adding GeometryArraysto the RenderingEngine(addGeometrymethod), the viewpoint of the viewer can be specified (setViewAngle), the direction of a single light can be specified (setLightAngle), the scaling factor to be applied to the model can be varied (setScale), and the size of the rendering screen defined (setScreenSize). To render all the GeometryArraysadded to the RenderingEngineusing the current light, screen, scale, and view parameters, clients can call the render method, supplying a Graphicsobject to render into, along with an optional GeometryUpdater. The GeometryUpdaterallows a client to modify the positions of points or rendering parameters prior to rendering. From AwtRenderingEngine.java /** * Definition of the RenderingEngine interface. A RenderingEngine 22
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
* can render 3D geometry (described using a Java 3D GeometryArray) * into a 2D Graphics context. */ public interface RenderingEngine { /** * Add a GeometryArray to the RenderingEngine. All GeometryArrays * will be rendered. */ public void addGeometry( GeometryArray geometryArray ); /** * Render a single frame into the Graphics. */ public void render( Graphics graphics, GeometryUpdater updater ); /** * Get the current Screen position used by the RenderEngine. */ public Vector3d getScreenPosition(); /** * Get the current View Angle used by the RenderEngine. View * angles are expressed in degrees. */ public Vector3d getViewAngle(); /** * Set the current View Angle used by the RenderEngine. */ public void setViewAngle( Vector3d viewAngle ); 23
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services