Adding an Image to the ViewpointThe call to gunHand( ) inside initUserControls( ) hides the creation of a TexturedPlane object just in front of and below the user's viewpoint: private PlatformGeometry gunHand( ) { PlatformGeometry pg = new PlatformGeometry( ); // define a square of sides 0.2f, facing along the z-axis Point3f p1 = new Point3f(-0.1f, -0.3f, -0.7f); Point3f p2 = new Point3f(0.1f, -0.3f, -0.7f); Point3f p3 = new Point3f(0.1f, -0.1f, -0.7f); Point3f p4 = new Point3f(-0.1f, -0.1f, -0.7f); TexturedPlane tp = new TexturedPlane(p1, p2, p3, p4, GUN_PIC); pg.addChild( tp ); return pg; } The Java 3D PlatformGeometry is nothing more than a detachable BranchGroup (confirmed by looking at its source code in java3d-utils-src.jar). It's added to ViewPlatform's transformGroup by a call to ViewingPlatform's setPlatformGeometry( ). My TexturedPlane class is a Shape3D holding a four-point QuadArray; the constructor is called with the points and a transparent GIF that will be pasted onto the quad's front face. TexturedPlane is a simplification of the ImagesSeries class of the last chapter, which lays a sequence of GIFs over a quad to exhibit an animation. The most complicated aspect of using TexturedPlane is determining its scene coordinates. I used trial and error until the image appeared at the bottom edge of the screen. It's helpful to remember that the (x, y) coordinate pair (0, 0) corresponds to the middle of the canvas, and that negative z-coordinates are farther into the scene. For example, look at the following points: Point3f p1 = new Point3f(-0.1f, -0.1f, -1.7f); Point3f p2 = new Point3f(0.1f, -0.1f, -1.7f); Point3f p3 = new Point3f(0.1f, 0.1f, -1.7f); Point3f p4 = new Point3f(-0.1f, 0.1f, -1.7f); These points cause the gun-in-hand image to be located (as shown in Figure 24-5) in the center of the screen, farther into the scene. Figure 24-5. Gun-in-hand at a new initial locationAn alternative way of placing the image is to use Java 3D's Viewer and ViewerAvatar. A fragment of code illustrates the approach: TexturedPlane tp = new TexturedPlane(p1, p2, p3, p4, GUN_PIC); ViewerAvatar va = new ViewerAvatar( ); va.addChild( tp ); Viewer viewer = su.getViewer( ); viewer.setAvatar(va); ViewerAvatar plays the same role as PlatformGeometry: it's a subclass of BranchGroup. setAvatar( ) connects it to the TRansformGroup above the ViewPlatform node.
Why Use a GIF?Since any shape can be attached to the viewpoint, why choose a QuadArray acting as a surface for a transparent GIF? One reason is efficiency because it would require a complex shape to represent a hand and a gun, together with suitable coloring and textures. By contrast, the GIF is only 5 KB. Another advantage is that occlusion is less likely to happen. Occlusion occurs when the image at the viewpoint intersects with a shape in the scene and is partially hidden. Since the GIF is flat, the viewpoint must move right up to a shape before occlusion occurs.
|