Java Programing

March 27, 2007

BranchGroup sceneBranchGroup = createSceneBranchGroup(); sceneBranchGroup.compile(); //add the scene

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

//meters, you can see geometry objects that are positioned at 0,0,0. Transform3D t3d = new Transform3D(); t3d.setTranslation( new Vector3d( 0.0, 0.0, 20.0 ) ); tg.setTransform( t3d ); Now we need to create the Viewobject itself and attach it to the ViewPlatformthat was added to the scenegraph. //create the View object View view = new View(); //create the PhysicalBody and PhysicalEnvironment for the View //and attach to the View PhysicalBody pb = new PhysicalBody(); PhysicalEnvironment pe = new PhysicalEnvironment(); view.setPhysicalEnvironment( pe ); view.setPhysicalBody( pb ); //attach the View to the ViewPlatform view.attachViewPlatform( vp ); //set the near and far clipping planes for the View view.setBackClipDistance( 110 ); view.setFrontClipDistance( 10 ); Finally, create a Canvas3Dcomponent (an AWT object) and add it to the View s list of Canvases to be rendered into. //create the Canvas3D that the View will render into. //get the graphics capabilities of the system and create //the best Canvas3D possible. GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D(); gc3D.setSceneAntialiasing( GraphicsConfigTemplate.PREFERRED ); GraphicsDevice gd[] = GraphicsEnvironment. getLocalGraphicsEnvironment().getScreenDevices(); Canvas3D c3d = new Canvas3D( gd[0].getBestConfiguration( gc3D ) ); //set the size of the Canvas3D c3d.setSize( 512, 512 ); //add the Canvas3D to the View so that it is rendered into view.addCanvas3D( c3d ); //add the Canvas3D component to a parent AWT or Swing Panel add( c3d ); 6.3 SimpleUniverse To simplify creating the Viewside of the scenegraph, Sun has provided the SimpleUniverseclass (figure 6.2). SimpleUniverseis defined in the com.sun.j3d.utilspackage and as such should not be considered part of the core Java 3D API. The SimpleUniverseclass hides some of the complexity of manually defining the Viewside of the scenegraph at the expense of the flexibility of using the core API classes only. 87

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

BranchGroup sceneBranchGroup = createSceneBranchGroup(); sceneBranchGroup.compile(); //add the scene

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

BranchGroup sceneBranchGroup = createSceneBranchGroup(); sceneBranchGroup.compile(); //add the scene BranchGroup to the Locale locale.addBranchGraph( sceneBranchGroup ); Note that this code will not yet make the geometry visible until a Viewand a ViewPlatformare created. This is the subject of the next section. 6.2 View, ViewPlatform, and Locale Now that a Localehas been created and populated with geometry, Java 3D must be instructed to render the Locale. The key to Java 3D rendering is the Viewclass which attaches to the ViewPlatforminstance that is within the scenegraph. The Viewcontrols Java 3D rendering and must have an attached Canvas3D component to render into. Multiple Canvas3Dinstances can be attached to the View, allowing multiple (identical) copies of the Viewto be rendered simultaneously. The clipping planes for the View control how much of the scene is rendered. In the example, CLIPPING the front plane is 10 meters from the viewer, while the back clipping plane is 110 meters from PLANES the viewer. However, since the viewer has been moved back 20 meters from the origin, the scene will be rendered from 10 to +90 in the Z direction. Note that one cannot set arbitrary clipping planes. The clipping planes have a physical significance in that they define a view frustum. The view frustum defines the pyramidal volume of 3D space that is rendered. The ViewPlatformis a simple Java 3D Leaf Nodeand can be added to the Locale as shown by the Java3dApplet code: Based on Java3dApplet.java //create the ViewPlatform BranchGroup BranchGroup vpBranchGroup = new BranchGroup(); //create a TransformGroup to scale the ViewPlatform //(and hence View) TransformGroup tg = new TransformGroup(); //create the ViewPlatform ViewPlatform vp = new ViewPlatform(); vp.setViewAttachPolicy( View.RELATIVE_TO_FIELD_OF_VIEW ); //attach the ViewPlatform to the TransformGroup tg.addChild( vp ); //attach the TransformGroup to the BranchGroup vpBranchGroup.addChild( tg ); //finally, add the ViewPlatform BranchGroup to the Locale locale.addBranchGraph( vpBranchGroup ); Note that the TransformGroupcreated just before the ViewPlatformcan be used to scale, translate, or rotate the scene rendered by the Viewattached to the ViewPlatform. For example: //Move the camera BACK a little. Note that Transformation //matrices above the ViewPlatform are inverted by the View //renderer prior to rendering. By moving the camera back 20 86

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

How would one go about building such a

Filed under: Java 3D Programming — webmaster @ 12:25 pm

How would one go about building such a model for the viewer to be able to switch between three different viewing modes? Galaxy: See the Sun with the planets rotating about it. Earth: See the planet Earth as it spins about its axis. House: View the house on earth. Localeswere designed to handle applications such as that just described. The HiResCoordTestexample implements the application shown in figure 6.1. Creating multiple Localesis a fairly lengthy process, so the whole example cannot be included here. Figure 6.1 Views of the three Locales defined in HiResCoordTest.java The first problem encountered is how to specify the location of a Locale. From HiResCoordTest.java protected Locale createLocaleEarth( VirtualUniverse u ) { int[] xPos = { 0, 0, 0, 0, 0, 0, 0, 0 }; int[] yPos = { 0, 0, 0, 0, 0, 0, 0, 0 }; int[] zPos = { 0, 0, 0, 1, 0, 0, 0, 0 }; HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos ); return new Locale( u, hiResCoord ); } A HiResCoordis created using three 8-element integer arrays. An int is 32 bits, so 8 * 32 = 256 bits. NOTE The integer at index 0 contains the most significant bit, while the integer at index 7 contains the least significant bit. The decimal point is defined to lie between indexes 3 and 4, that is, 0×0 0×0 0×0 0×0 . 0×0 0×0 0×0 0×0 The 8-element integer array to specify a coordinate can be considered an 8-digit number in base 232. The numbers that can be expressed by such an entity are mind-boggling, but table 6.1 (from the API specification) can be used to get you into the right ballpark for the quantity you are trying to 84

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

How would one go about building such a

Filed under: Java 3D Programming — webmaster @ 12:25 pm

express. Table 6.1 Physical dimensions expressed as a power of 2 2n Meters Units 87.29 Universe (20 billion light-years) 69.68 Galaxy (100,000 light-years) 53.07 Light-year 43.43 Solar system diameter 23.60 Earth diameter 10.65 Mile 9.97 Kilometer 0.00 Meter 19.93 Micron 33.22 Angstrom 115.57 Planck length For example, to specify a distance in light-years (9,460 billion kilometers), find from table 6.1 that 253.07 is equal to 9,460 billion kilometers. That is, a 1 at the 53rd bit position will be approximately one light-year. When mapped into the integer array, the 53rd bit is located within the integer at index 3 53/32 = 2. It is the 21st bit (53 32) within the third integer of the array. The number 221 equals 0×200000, so setting the integer at index 2 of the integer array to 0×200000 will create a HiResCoordinstance that stores approximately a light-year. The integer at index 3 is defined to store meters, so simply setting the integer at index 3 will define a HiResCoordin meters. Conversely, the 20th bit to the right of the decimal point position is approximately a micron. A useful exercise would be to develop a Java class that returned a HiResCoordfor quantities expressed in the various units shown in table 6.1. //creates a Locale that is positioned (232 + 5) meters //away from the origin in the +Z direction. int[] xPos = { 0, 0, 0, 0, 0, 0, 0, 0 }; int[] yPos = { 0, 0, 0, 0, 0, 0, 0, 0 }; int[] zPos = { 0, 0, 1, 5, 0, 0, 0, 0 }; HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos ); Once a Localehas been created and positioned, it can be populated with geometry by attaching a BranchGroupcontaining scenegraph elements: //create the Universe m_Universe = new VirtualUniverse(); //create the position for the Locale int[] xPos = { 0, 0, 0, 0, 0, 0, 0, 0 }; int[] yPos = { 0, 0, 0, 0, 0, 0, 0, 0 }; int[] zPos = { 0, 0, 1, 5, 0, 0, 0, 0 }; HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos ); //create the Locale and attach to the VirtualUniverse Locale locale = new Locale( u, hiResCoord ); //create the BranchGroup containing the Geometry for the scene 85

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

Figure 5.5 A TransformGroup is used to rotate

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

Figure 5.5 A TransformGroup is used to rotate a ColorCube and Text2D label to a desired orientation A detailed understanding of the mathematics behind the Transform3Dobject is very useful, but is beyond the scope of this book. Some useful references for mathematics for 3D graphics are presented in appendix B. Transform3Dincludes methods that allow application developers to apply transformations while remaining largely ignorant of the underlying implementation. 5.11 Summary This chapter has introduced many of the Nodetypes available in Java 3D. The Java 3D Nodescover the basic requirements of most scenegraphs: Propagation of boundary information Specification of collision boundary information Grouping of Nodesinto logical units (Group) Attach and detach Groups (BranchGroup) Influence the order of rendering within a Group(OrderedGroup) Sharing of Groups across the scenegraph hierarchy (SharedGroupand Link) Rotate, translate and scale the children of a Group (TransformGroup) Armed with the information in chapters 4 and 5 you should be able to tackle the high-level scenegraph design for your application. The chapters to come will also be very useful, as we start to discuss how the scenegraph fits into the Java 3D VirtualUniverseas well as the rendering model (chapter 6), the data model for your application (chapter 7), and Java 3D s geometry description capabilities (chapter 8) . 82

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

Figure 5.5 A TransformGroup is used to rotate

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

CHAPTER 6 Defining the universe 6.1 Locales and HiResCoord 6.2 View, ViewPlatform, and Locale 6.3 SimpleUniverse 6.4 Background geometry 6.5 Using multiple views 6.6 Summary One of the fundamental choices you will have to make in your choice of VirtualUniverseconfiguration is whether to use the SimpleUniverseutility classes or to rely upon the lower level VirtualUniverse classes. By the end of this chapter you should understand the elements of the Java 3D scenegraph rendering model. Essential elements of the Java 3D scenegraph are covered: Defining regions within your universe using Locales Attaching the Viewmodel to the VirtualUniversethrough the ViewPlatform Using multiple Viewsfor rendering SimpleUniverseutility classes Creating Views, Geometry, and PlatformGeometryfor Avatars 6.1 Locales and HiResCoord The VirtualUniverseclass contains the virtual world that an application developer populates with Geometry, Behaviors, Lights, and so forth. The VirtualUniverseconsists of a collection of Locales. A Localedefines a geographical area within the VirtualUniverseand is anchored at a given 3D coordinate. A Localeuses 256-bit coordinates to specify the anchored x, y, and z position. The 256-bit coordinates are stored in a HiResCoordobject and allow a Localeto be positioned within a virtual space the size of the known (physical) universe yet also maintain a precision of a Planck length (smaller than the size of a proton). Most applications do not require more than one Locale; hence the SimpleUniverseclass creates a VirtualUniverseinstance with a single Locale. SimpleUniverseis derived from VirtualUniverseand is covered in detail in chapter 3. The default constructor for a Localepositions the Localeat the origin of the VirtualUniverse. Within a Locale, doubles or floats are used to specify the positions of objects. If a double is large enough to represent the positions of all the objects within your scene accurately, then a single Localeshould be sufficient. However, imagine a scenario: your application is to model parts of the galaxy. The model is to contain the planets orbiting the Sun. On the Earth, the model contains a geometry object a few meters across to represent a house. All the objects in the model are to be created to scale. 83

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

java.lang.Object | +–javax.media.j3d.SceneGraphObject | +–javax.media.j3d.Node | +–javax.media.j3d.Leaf |

Filed under: Java 3D Programming — webmaster @ 1:24 am

SharedGroupscan be very useful in minimizing the memory footprint of an application because a single SharedGroupcan be referenced hundreds of times within a scenegraph to position repeating geometry within a scene. 5.9 Primitive java.lang.Object | +–javax.media.j3d.SceneGraphObject | +–javax.media.j3d.Node | +–javax.media.j3d.Group | +–com.sun.j3d.utils.geometry.Primitive Primitiveis a Group-derived class that serves as the base class for the geometric primitives (Box, Sphere, Cone, etc.) that are defined in the Java 3D utilspackage. Objects of the Primitiveclass should not be instantiated directly, and the Primitiveclass is very difficult to extend usefully. For a full discussion of the Primitiveclass, see chapter 8. 5.10 TransformGroup java.lang.Object | +–javax.media.j3d.SceneGraphObject | +–javax.media.j3d.Node | +–javax.media.j3d.Group | +–javax.media.j3d.TransformGroup The TransformGroup Nodeis central to almost all scenegraph designs. A TransformGroup incorporates the rotation, translation, and scaling information that is applied to all of its child Nodes. Without the TransformGroup, scenegraphs would be static, always positioned at 0,0,0, uniformly unit scaled, and without rotation about any of the axes. Not a very interesting interactive scene. The TranformGroupcontrols orientation of its child Nodes through the Transform3Dobject that it encapsulates (figure 5.5). The Transform3Dobject represents such transformations using a typical 4 4 transformation matrix. The 4 4 matrix of double precision numbers allows scaling, rotation, and translation information to be stored and applied by a single matrix. 81

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

java.lang.Object | +–javax.media.j3d.SceneGraphObject | +–javax.media.j3d.Node | +–javax.media.j3d.Leaf |

Filed under: Java 3D Programming — webmaster @ 1:24 am

java.lang.Object | +–javax.media.j3d.SceneGraphObject | +–javax.media.j3d.Node | +–javax.media.j3d.Leaf | +–javax.media.j3d.Link The Link Nodeis used in association with a SharedGroup. Since the SharedGroupcan appear in several locations in the scenegraph, and a Nodeobject can only have a single parent Node, a unique Link Nodemust be used as a placeholder within the scenegraph for the SharedGroup Node. When the scenegraph is traversed, and the Link Nodeis encountered, the traversal algorithm will step into the SharedGroupthat is internally referenced by the Link Node(figure 5.4). Figure 5.4 A Switch Node with two child Link Nodes. Both Link Nodes reference the same SharedGroup (SG). This type of structure is not possible without using Link Nodes as a single Node can only have one parent Node From NodesTest.java //create the SharedGroup SharedGroup sharedGroup1 = new SharedGroup(); //add geometry to the SharedGroup sharedGroup1.addChild ( createLabel( “4. Shared Group 1″, labelScale ) ); //add the first Link for the SharedGroup switchGroup.addChild( new Link( sharedGroup1 ) ); //add the second Link for the SharedGroup switchGroup.addChild( new Link( sharedGroup1 ) ); //Note. The InterpolatorTest example also uses SharedGroups //and Link Nodes in a more substantial manner. 80

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

March 26, 2007

| +–javax.media.j3d.Group | +–javax.media.j3d.BranchGroup BranchGroupdefines a Group Nodethat

Filed under: Java 3D Programming — webmaster @ 5:31 pm

An OrderedGroup(or DecalGroupwhich is derived from OrderedGroup) can be useful for defining the rendering order for essentially coplanar surfaces, such as a road lying on terrain, or a cloth covering a table (figure 5.3). Note that both of these examples make implicit assumptions about the range of possible viewing angles for the objects, if the viewer can sit underneath the table, the tablecloth should not be rendered before the table! Other uses for an OrderedGroupmight be to implement signs or labels for geometric objects the labels should always be rendered on top of the objects they are labeling, irrespective of the viewer s position. Figure 5.3 The effect of rendering three coplanar child Nodes using an OrderedGroup. The item at index 0 (a Text2D object with the text 3. OrderedGroup ), overlaps the item at index 1 (Child 1), which overlaps the item at index 2 (Child 2) From NodesTest.java OrderedGroup orderedGroup = new OrderedGroup(); orderedGroup.addChild ( createLabel( “3. OrderedGroup”, labelScale ) ); orderedGroup.addChild( createLabel( “Child 1″, labelScale ) ); orderedGroup.addChild( createLabel( “Child 2″, labelScale ) ); 5.8 SharedGroup and link java.lang.Object | +–javax.media.j3d.SceneGraphObject | +–javax.media.j3d.Node | +–javax.media.j3d.Group | +–javax.media.j3d.SharedGroup The SharedGroup Nodedefines a scenegraph management Nodethat can be attached to several parent Nodes. The SharedGroupitself can be arbitrarily complex but must occur (as a whole) as a Leaf Node within the scenegraph. A SharedGroupmust be wrapped in an instance of a Linkobject before being added to the scenegraph. The Linkmust have one unique parent. SharedGroups cannot contain the following Nodes: Background Behavior-derived Clip Fog Soundscape ViewPlatform 79

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

| +–javax.media.j3d.Group | +–javax.media.j3d.BranchGroup BranchGroupdefines a Group Nodethat

Filed under: Java 3D Programming — webmaster @ 5:31 pm

| +–javax.media.j3d.Group | +–javax.media.j3d.BranchGroup BranchGroupdefines a Group Nodethat can be dynamically inserted or removed from a parent Group at runtime. In addition the root parent Groupof the scenegraph must be a BranchGroupsince only BranchGroupscan be inserted into a Locale. To allow a BranchGroupto be dynamically removed from its parent, the BranchGroupmust have the BranchGroup.ALLOW_DETACHcapability set. To allow a BranchGroupto be dynamically inserted (or attached) into a parent Groupthe Groupmust have the capability Group.ALLOW_CHILDREN_EXTEND. Why these capabilities have been divided between the Groupand BranchGroupclasses is not quite clear. BranchGroupsare typically used as return values by the methods that build the various components of an application scenegraph. For example, an F1 racing application might define the following abstract class: abstract class F1Object extends Object { BranchGroup m_BranchGroup = null; public F1Object(); protected BranchGroup createBranchGroup(); public getBranchGroup() { if( m_BranchGroup == null ) m_BranchGroup = createBranchGroup(); return m_BranchGroup; } } The F1Objectabstract class forces the application developer to define the createBranchGroupvirtual method in a derived class. The createBranchGroupmethod is responsible for creating a BranchGroup containing the child Nodesrequired to define the Geometryand Behaviorsfor the F1Objectbeing created. This very simple OO design allows an F1Object Managerclass to be defined that maintains a Vector(or Listor Map) of generic F1Objectsand is responsible for their creation and removal from the applications scenegraph. The objects derived from F1Objectare themselves easily reusable because their geometry generation and management routines are all packages with the class itself. The setUserDatamethod (defined in the section on Group Nodes) can be used to track F1Object instances once they have been inserted into the scenegraph. 5.7 OrderedGroup java.lang.Object | +–javax.media.j3d.SceneGraphObject | +–javax.media.j3d.Node | +–javax.media.j3d.Group | +–javax.media.j3d.OrderedGroup The OrderedGroup Nodeis a less commonly used scenegraph element. It allows an application developer to have basic control over the rendering order of the children of a Group Node. 78

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

« Previous PageNext Page »

Powered by Java Web Hosting