O'Reilly logo

Killer Game Programming in Java by Andrew Davison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Scene Graph Creation

The scene graph is created by the constructor for WrapCheckers3D:

    public WrapCheckers3D()
    {
      // initialization code

      GraphicsConfiguration config =
                 SimpleUniverse.getPreferredConfiguration();
      Canvas3D canvas3D = new Canvas3D(config);
      add("Center", canvas3D);
      canvas3D.setFocusable(true);     // give focus to the canvas
      canvas3D.requestFocus();

      su = new SimpleUniverse(canvas3D);

      createSceneGraph();
      initUserPosition();        // set user's viewpoint
      orbitControls(canvas3D);   // controls for moving the viewpoint

      su.addBranchGraph( sceneBG );
    }

The Canvas3D object is initialized with a configuration obtained from getPreferredConfiguration(); this method queries the hardware for rendering information. Some older Java 3D programs don't bother initializing a GraphicsConfiguration object, using null as the argument to the Canvas3D constructor instead. This is bad programming practice.

canvas3D is given focus so keyboard events will be sent to behaviors in the scene graph. Behaviors are often triggered by key presses and releases, but they may be triggered by timers, frame changes, and events generated by Java 3D internally. There aren't any behaviors in Checkers3D, so it's not necessary to set the focus. I've left these lines in since they're needed in almost every other program we'll consider.

The su SimpleUniverse object creates a standard view branch graph and the VirtualUniverse and Locale nodes of the scene graph. createSceneGraph() sets up the lighting, the sky background, the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required