• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 15. CUSTOMIZING BACKGROUND, FOG,... > The Complete Lighting Example

The Complete Lighting Example

Listing 15.3 contains the complete source listing for our lighting examples, allowing you to see how the various snippets of code we've added fit into the context of a complete Java 3D program.

Listing 15.3. VView.java (Example 4)

// Our complete lighting example
// Written by Bernie Roehl, October 1999
//
// CONVERTING TO AN APPLET:
// To convert this application into an applet you must construct
// a standard init() method that contains the code now in the VView
// constructor. From within the init() method you will invoke the VRML
// loader's load(URL) method, as the load(String) method currently used
// by this application can't be used in an applet (a security exception
// is thrown by the applet when loading a VRML world from a String).
//
// To convert to an applet, simply rename the following method:
// public VView(String avatar)
// to:
// public void init()
//
// Next, use the applet's getParameter() method to retrieve the
// name of the VRML file to load and place the resulting string into the
// "avatar" variable. The following code assumes that a PARAM named
// "world" is in the corresponding HTML Web page for this applet:
// String avatar = getParameter("world");
//
// Finally, change the VRML loader's load(String) method so that it
// is passed an absolute URL. For example:
// scene = loader.load(new java.net.URL(getCodeBase(), avatar + ".wrl"));
//
// Visit the Core Web3D web site at http://www.CoreWeb3D.com/ to
// obtain code listings from this book, including an applet version of this
// VView program.
import java.awt.*;
import java.applet.Applet;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.loaders.vrml97.VrmlLoader;
import com.sun.j3d.loaders.Scene;
public class VView extends Applet {
      SimpleUniverse universe;   // the universe
      Scene scene = null;        // the VRML scene
VView(String avatar) {
      setLayout(new BorderLayout());
      Canvas3D canvas = new Canvas3D(null);
      add("Center",canvas);
      universe = new SimpleUniverse(canvas);
      ViewingPlatform viewingPlatform =
          universe.getViewingPlatform();
      TransformGroup vpTransGroup =
          viewingPlatform.getViewPlatformTransform();
      View view = (universe.getViewer()).getView();
      VrmlLoader loader = new VrmlLoader();
      try { scene = loader.load(avatar + ".wrl"); }
      catch (Exception e) {
          System.out.println(
                "Exception loading file from path:"
                + avatar + ".wrl");
          System.exit(1);
      }
      // get the scene group from the loaded VRML scene
      BranchGroup sceneGroup = scene.getSceneGroup();
      sceneGroup.setCapability(
          BranchGroup.ALLOW_DETACH);
      sceneGroup.setCapability(
          BranchGroup.ALLOW_BOUNDS_READ);
      // make the VRML scene live
      universe.addBranchGraph(sceneGroup);
      // find the radius and center of the
      // scene's bounding sphere
      BoundingSphere sceneBounds =
          (BoundingSphere)sceneGroup.getBounds();
      double radius = sceneBounds.getRadius();
      Point3d center = new Point3d();
      sceneBounds.getCenter(center);
      // now move the viewpoint back
      // so we can see the whole scene
      Vector3d temp = new Vector3d(center);
      temp.z += 1.4 * radius
               / Math.tan(view.getFieldOfView() / 2.0);
      // set that viewpoint into the viewing transform
      Transform3D viewTransform = new Transform3D();
      viewTransform.set(temp);
      vpTransGroup.setTransform(viewTransform);
      // increase the size of the sceneBounds,
      // for later use
      sceneBounds.setRadius(temp.z * 2.0);
      // create a new BranchGroup to hold the
      // lights, sounds, background, and fog
      BranchGroup environment = new BranchGroup();
      // add a background to the scene
      Background bg = new Background(
               0.0f, 0.75f, 0.25f);
      bg.setApplicationBounds(sceneBounds);
      environment.addChild(bg);
      // create a new DirectionalLight
      // and add it to the scene
      Vector3f direction = new Vector3f(-1f, -1f, -1f);
      Color3f lightColor = new Color3f(1f, 0f, 0f);
      DirectionalLight dirLight =
      new DirectionalLight(lightColor, direction);
      dirLight.setInfluencingBounds(sceneBounds);
      environment.addChild(dirLight);
      // create a new PointLight
      // and add it to the scene
      lightColor = new Color3f(0f, 0f, 1f);
      Point3f position = new Point3f(-2f, 0f, 0f);
      Point3f attenuation = new Point3f(0.5f, 0f, 0f);
      PointLight pointLight =
            new PointLight(lightColor, position,
                  attenuation);
      pointLight.setInfluencingBounds(sceneBounds);
      environment.addChild(pointLight);
      // create a new SpotLight
      // and add it to the scene
      lightColor = new Color3f(1f, 1f, 0f);
      position = new Point3f(0f, 0f, 2f);
      direction = new Vector3f(0f, 0f, -1f);
      attenuation = new Point3f(0f, 0f, 0.5f);
      SpotLight spotLight =
            new SpotLight(lightColor, position,
                   attenuation, direction, 0.47f, 50f);
      spotLight.setInfluencingBounds(sceneBounds);
      environment.addChild(spotLight);
      // create a new AmbientLight
      // and add it to the scene
      lightColor = new Color3f(0f, 1f, 1f);
      AmbientLight ambLight =
             new AmbientLight(lightColor);
      ambLight.setInfluencingBounds(sceneBounds);
      environment.addChild(ambLight);
      universe.addBranchGraph(environment);
      }
      public static void main(String[] args) {
           new MainFrame(new VView(args[0]), 320, 400);
      }
}


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint