Java Programing

February 5, 2007

catch (Exception e) { scene = null; System.err.println(e);

Filed under: Java 3D Programming — webmaster @ 3:26 pm

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); //create an Appearance and Material Appearance app = new Appearance(); Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, black, 80.0f)); //assign the appearance to the Shape shape.setAppearance( app ); //connect the scenegraph objTrans2.addChild( scene.getSceneGroup() ); objTrans1.addChild( objTrans2 ); objRoot.addChild( objTrans1 ); return objRoot; } 15.3.4 Third-party object loaders There are a wide variety of object loaders available for use with Java 3D. The list of available loaders is maintained by Bill Day at http://www.j3d.org. Data file loaders available for most of the common 3D data file formats are listed in table 15.1. Table 15.1 Data file loaders Loader Author File Format(s) Last Updated Milkshape3D Loader Gregory Pierce Milkshape3D 30 Aug 2001 Milkshape3D Loader Kevin Duling Milkshape3D 28 Aug 2001 AC3D Loader Ryan Wilhm/j3d.org AC3D 28 Aug 2001 NCSA Portfolio NCSA 3DS (3D-Studio) PDB (Protein Data Bank) DEM (Digital Elevation Map) IOB (Imagine) COB (Caligari trueSpace) OBJ (Wavefront) DXF (Drawing Interchange File) AutoCAD VRML 97 15 Sept 2000 264

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

catch (Exception e) { scene = null; System.err.println(e);

Filed under: Java 3D Programming — webmaster @ 3:26 pm

PLAY Xj3D Loader Extensible 3D (X3D) Task Group (former VRML97-Java Working Group) VRML97 X3D 15 Sept 2000 FullSail OpenFLT Loader Shawn Kendall OpenFLT 1 Aug 1999 Lw3dLoader Sun LWS (Lightwave Scene Format) uses LWO (Lightwave Object Format) 13 Jun 1998 ObjLoad Sun OBJ (Wavefront) 12 Jun 1998 NFF OBJ Loader Scott Metzger NFF (WorldToolKit) OBJ (Wavefront) 15 Jun 1998 Loader3DS Starfire Research 3DS(3D-Studio) March 2001 Import3DS Aaron Mulder 3DS (3D-Studio) 13 Jun 1998 Load3DS Wesley Layne 3DS (3D-Studio) 25 Feb 2001 Load3DS Rycharde Hawkes 3DS (3D-Studio) 16 Jun 1998 VTK Loader Arish Ali VTK (Visual Toolkit) 15 Sept 2000 Different loaders have different limitations and bugs. You should experiment with loaders and file formats until you find one that works for the files you need to display. 15.4 Summary Java 3D includes relatively basic support for processing geometry using the utility classes in the com.sun.j3d.utils.geometrypackage. The utilities are easy to use, and are useful for simple triangulation. For more powerful geometry manipulation operati ons (such as mesh generation or decimation you will have to convert one of the many utility libraries, usually written in C). The interfaces and classes defined in the com.sun.j3d.loaderspackage however have proved to be very useful. They have defined a standard upon which many of the community Java 3D developers can build, and a large variety of loaders have been released mos t are free. 265

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

15.3.1 LoaderBase java.lang.Object | +–com.sun.j3d.loaders.LoaderBase LoaderBaseis a convenience

Filed under: Java 3D Programming — webmaster @ 6:41 am

15.3.1 LoaderBase java.lang.Object | +–com.sun.j3d.loaders.LoaderBase LoaderBaseis a convenience class to manage the setting and retrieval of typical Loaderinformation, such as base URL and load flags. Developers implementing their own Loadercan populate, return, and interrogate a LoaderBaseinstance to provide a consis tent API for end-user developers. 15.3.2 SceneBase interface java.lang.Object | +–com.sun.j3d.loaders.SceneBase SceneBaseis a convenience class to manage the setting and retrieval of typical Sceneinformation, such as: Background nodes Behavior nodes Description Fog nodes FOV parameters Lights Objects Sound nodes Developers implementing their own Loadercan populate, return, and interrogate a SceneBaseinstance to provide a consistent API for end-user developers. 15.3.3 Using the ObjectFile loader The following example loads a simple Wavefront format .obj file (the hand1.obj file from the Sun Morphing demo). Note that it is also necessary to create lights for the scene and assign an Appearanceand Materialto the loaded Wavefront object. Figure 15 .3 shows rendered output. 262

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

15.3.1 LoaderBase java.lang.Object | +–com.sun.j3d.loaders.LoaderBase LoaderBaseis a convenience

Filed under: Java 3D Programming — webmaster @ 6:41 am

Figure 15.3 A Wavefront OBJ file loaded using the Sun ObjectFile loader From LoaderTest.java protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); //create a TransformGroup to flip the hand onto its end //and enlarge it. TransformGroup objTrans1 = new TransformGroup(); Transform3D tr = new Transform3D(); objTrans1.getTransform( tr ); tr.rotX(90.0 * Math.PI / 180.0); tr.setScale( 10.0 ); objTrans1.setTransform( tr ); //create a TransformGroup to rotate the hand TransformGroup objTrans2 = new TransformGroup(); objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); BoundingSphere bounds = new BoundingSphere( new Point3d(0.0,0.0,0.0), 100.0); //create a RotationInterpolator behavior to rotate the hand Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); RotationInterpolator rotator = new RotationInterpolator( rotationAlpha, objTrans2, yAxis, 0.0f, (float) Math.PI*2.0f); rotator.setSchedulingBounds(bounds); objTrans2.addChild(rotator); //Set up the global lights Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f); Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); AmbientLight aLgt = new AmbientLight(alColor); aLgt.setInfluencingBounds(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objRoot.addChild(aLgt); objRoot.addChild(lgt1); //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”); } 263

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

February 4, 2007

array. This coordinate array should first define the

Filed under: Java 3D Programming — webmaster @ 11:29 pm

array. This coordinate array should first define the outer boundary of the polygon, using counterclockwi se winding. Then define any polygons that are to be excluded from the generated composite triangular surface. This simple example in figure 15.1 defines two contours: the outer polygon and one hole. The stripCountArrayis an array of integers that delineates one con tour from the next. In figure 15.1, the stripCountArraywould be int[] stripCountArray = {4,3}; Figure 15.1 Counterclockwise winding for defining a polygon and a hole for Triangulation Figure 15.2 Output from TriangulatorTest: The surface generated rendered both as a solid (left) and as a wireframe (right) The first contour (A,B,C,D) contains four vertices, and the hole (F,G,H) contains three vertices. The Triangulatorclass is not very well documented, so the following example should illustrate the concepts of polygon triangulation using the Triangulatorclass. In particular, the contourCountArrayelement is misleadingly documented and should be set t o the number of contours (1 + the number of holes). This is always the same as the length of the stripCountArray. Why the contourCountArrayis necessary is not clear. From TriangulatorTest.java //Generate a surface from 10 vertices and //create a hole in the surface by removing //a hole defined using 5 vertices. Note that the hole //must be entirely within the outer polygon. private double[] m_VertexArray = {1,1,0, //0 0,3,0, //1 1,5,0, //2 2,4,0, //3 4,5,0, //4 3,3,0, //5 4,2,0, //6 4,0,0, //7 3,0,0, //8 260

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

array. This coordinate array should first define the

Filed under: Java 3D Programming — webmaster @ 11:29 pm

2,1,0, //9 //these are vertices for the hole 1,3,0, //10 2,3,0, //11 3,2,0, //12 3,1,0, //13 2,2,0};//14 //triangulate the polygon GeometryInfo gi = new GeometryInfo( GeometryInfo.POLYGON_ARRAY ); gi.setCoordinates( m_VertexArray ); //the first 10 points make up the outer edge of the polygon, //the next five make up the hole int[] stripCountArray = {10,5}; int[] countourCountArray = {stripCountArray.length}; gi.setContourCounts( countourCountArray ); gi.setStripCounts( stripCountArray ); Triangulator triangulator = new Triangulator(); triangulator.triangulate( gi ); //also generate normal vectors so that the surface can be light NormalGenerator normalGenerator = new NormalGenerator(); normalGenerator.generateNormals( gi ); //create an appearance Appearance ap = new Appearance(); //render as a wireframe PolygonAttributes polyAttrbutes = new PolygonAttributes(); polyAttrbutes.setPolygonMode( PolygonAttributes.POLYGON_LINE ); polyAttrbutes.setCullFace( PolygonAttributes.CULL_NONE ) ; ap.setPolygonAttributes( polyAttrbutes ); //add both a wireframe and a solid version //of the triangulated surface Shape3D shape1 = new Shape3D( gi.getGeometryArray(), ap ); Shape3D shape2 = new Shape3D( gi.getGeometryArray() ); After the geometry has been triangulated and normal vectors have been calculated, the geometry can be very easily stripified: //invoke the Stripifier on the GeometryInfo Stripifier st = new Stripifier() st.stripify(gi); //extract the stripified GeometryArray Shape3D shape2 = new Shape3D( gi.getGeometryArray() ); 15.3 Object loaders Sun has defined the com.sun.j3d.loaders.Loaderinterface that provides a standard set of methods for accessing the information read in from a 3D data file format. Because there is such a wide variety of 3D data file formats available, the Loaderinterfac e is fairly high level and does not return graphical information directly but encapsulates it in an object implementing the Sceneinterface. 261

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

Figure 14.27 Transparency = 0.2, PolygonAttributes.CULL_NONE. Shape3D with

Filed under: Java 3D Programming — webmaster @ 4:22 pm

Figure 14.27 Transparency = 0.2, PolygonAttributes.CULL_NONE. Shape3D with transparent image applied (texture2n.gif) from AppearanceTest. 1 = MODULATE, 2 = DECAL, 3 = BLEND, 4 = REPLACE 14.6 Animated (video) texture mapping Many recent computer games use animated texture maps, for example, to map an MPEG video clip onto the face of a cube. 3D accelerator hardware is also starting to support video textures. Drawing animated textures is at present problematic in Java 3D because, although you can draw into an ImageComponentand use it as a texture image, the ImageComponentis copied into texture memory. Java 3D 1.2 should go some way to addressing this issue, but performance problems are likely to remain an issue for some time. For very simple texture animations (a few frames), each frame of the animation can be pasted (either at runtime or as a preprocess) into a composite texture image. At runtime the texture coordinates of vertices can be modified to cycle through the various frames of the animation. 14.7 Summary This chapter has given you a taste of the power of texture mapping and the important role it plays in most 3D applications, be they educational, scientific, or entertainment. Texture mapping requires a little more work from the application developer, in terms of learning new terminology and methods, but the end results justify the extra development time. Clever use of lighting and texture mapping sets the great, visually immersive 3D applications apart from flat, uninspiring computer graphics. 258

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Figure 14.27 Transparency = 0.2, PolygonAttributes.CULL_NONE. Shape3D with

Filed under: Java 3D Programming — webmaster @ 4:22 pm

CHAPTER 15 Geometry utility classes and object loaders 15.1 Introduction 15.2 Triangulator, normal vector generator, stripifier 15.3 Object loaders 15.4 Summary This chapter explains some of the utility classes that Java 3D supplies to help you import or generate geometry for your scenes. 15.1 Introduction Java 3D comes with three utility classes that facilitate runtime geometry creation: Triangulatorconverts a list of points and connectivity information into a GeometryArray composed of triangles. NormalGeneratorgenerates Normal vectors for a list of triangles. Stripifierredefines an existing triangle list such that the triangles are specified in longs strips, with adjacent triangles sharing as many vertices as possible. Triangle lists are stripified to speed rendering by graphics hardware and software underlying Java 3D. The geometry compression classes allow Java 3D to create an internal compressed representation of geometry information. If the graphics hardware supports using compressed geometry, the result may be faster rendering time and lower memory usage. Geometry compression can also be useful for applications that need to send Java 3D geometry across network connections using serialization or Java RMI. Refer to the API documentation for further details. The Java 3D object loaders define an architecture for writing files to import geometry data into Java 3D. Sun supplies two loaders as part of the Java 3D utilities package: Lightwave 3D Object file In addition, the Java 3D/VRML working groups maintain a VRML loader to import VRML (WRL) files into Java 3D. The VRML loader must be downloaded separately (VRML97.JAR). The example VrmlPickingTestuses the VRML97 loader. The VRML97 loader is no longer be ing developed and is being replaced by the loaders being developed for the X3D standard, hosted at http://www.web3d.org. 15.2 Triangulator, normal vector generator, stripifier Triangulation is a mechanism to convert arbitrary polygons to triangular surfaces for rendering. The com.sun.j3d.utils.geometry.Triangulatorclass can be used not only to convert an arbitrary n-sided polygon (which does not have to be planar) to triangul ar surfaces, but also to create holes in the generated composite surface. To use the Triangulator, put your vertex coordinates into a double or float 259

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

//the color cube with the left mouse button

Filed under: Java 3D Programming — webmaster @ 9:33 am

Figure 14.25 Shape3D with an opaque image applied (texture0n.jpg) from AppearanceTest. 1 = MODULATE, 2 = DECAL, 3 = BLEND, 4 = REPLACE Figure 14.26 uses a transparent version (texture2n.gif) of the original texture image. The white background of the texture image has been marked as a transparent color in the GIF image. Figure 14.26 Shape3D with transparent image applied (texture2n.gif) from AppearanceTest. 1 = MODULATE, 2 = DECAL, 3 = BLEND, 4 = REPLACE Figure 14.27 uses the transparent texture image but also disables back-face removal using PolygonAttributes.CULL_NONE. Frame 2 (DECAL) suffered from continuous redraw because the back faces were redrawn over the front faces, and then the back faces were redrawn. 257

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

//the color cube with the left mouse button

Filed under: Java 3D Programming — webmaster @ 9:33 am

//the color cube with the left mouse button MouseRotate mouseRot = new MouseRotate( subTg ); subTg.addChild( mouseRot ); //assign a transformChanged callback, because we want //to be notified whenever the rotation of the ColorCube changed //(”this” implements MouseBehaviorCallback ); mouseRot.setupCallback( this ); mouseRot.setSchedulingBounds( getApplicationBounds() ); transTg.addChild( subTg ); return transTg; } //this is a callback method that the MouseRotate behavior calls //when its Transform3D has been modified (by the user) public void transformChanged(int type, Transform3D transform) { //update the rotation of the TextureAttributes m_TextureAttributes.setTextureTransform( transform ); } 14.5 Using transparent geometry with transparent texture images The Textureclass allows texture images to have red, green, blue, and alpha (transparency) channels through the RGBA mode. Appearances(and hence geometry) can also have transparency information, either through per-vertex COLOR_4colors, or through the TransparencyAtttributes NodeComponent. Figures 14.25 14.27 illustrate what happens when partially transparent images are applied to partially transparent Shape3Ds. The easiest way to use transparent images is to use the GIF image format, which can include a transparent color. Most bitmap editors, such as JASC PaintShop Pro or Adobe Photoshop, can save GIF images with a transparent color. Figures 14.25 14.27 were generated using the AppearanceTestexample application. The Boxhad the appearance attributes shown on table 14.5. Table 14.5 Box appearance attributes Transparency: 0.5, NICEST Material: Ambient = white, Diffuse = white, Emissive = blue, Specular = black, Shininess = 1 Texture: MagFilter = BASE_LEVEL_LINEAR, MinFilter = MULTI_LEVEL_LINEAR MIPMAPs were enabled. The front face (smaller) of the cube uses per-vertex colors with transparency and hence is unaffected by the overall TransparencyAttributes of the Box s Appearance. Figure 14.25 is provided for contrast; it uses the opaque texture image (texture0n.jpg). 256

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Next Page »

Powered by Java Web Hosting