Lighting the Scene
One ambient and two directional lights are added to the scene by
lightScene( ). An ambient light reaches every corner of the world, illuminating everything equally.
Color3f white = new Color3f(1.0f, 1.0f, 1.0f); // Set up the ambient light AmbientLight ambientLightNode = new AmbientLight(white); ambientLightNode.setInfluencingBounds(bounds); sceneBG.addChild(ambientLightNode);
The color of the light is set, the ambient source is created along with bounds and added to the scene. The
Color3f( ) constructor takes Red/Green/Blue values between
1.0f being "full-on").
A directional light mimics a light from a distant source, hitting the surfaces of objects from a specified direction. The main difference from an ambient light is the requirement for a direction vector.
Vector3f light1Direction = new Vector3f(-1.0f, -1.0f, -1.0f); // left, down, backwards DirectionalLight light1 = new DirectionalLight(white, light1Direction); light1.setInfluencingBounds(bounds); sceneBG.addChild(light1);
Figure 15-3. Partial scene graph for Checkers3D
The direction is the vector between (0, 0, 0) and (-1, -1, -1); the light can be imagined to be multiple parallel lines with that direction, originating at infinity.
Point and spot lights are the other forms of Java 3D lighting. Point lights position the light in space, emitting in all directions. Spot lights are focused point lights, aimed in a particular direction.
The Scene's Background
A background for a scene can be specified as a constant color (as shown here), a static image, or a texture-mapped geometry such as a sphere:
Background back = new Background( ); back.setApplicationBounds( bounds ); back.setColor(0.17f, 0.65f, 0.92f); // sky color sceneBG.addChild( back );
Sphere is a utility class from Java 3D's
com.sun.j3d.utils.geometry package, a subclass of the
Primitive class, which is a
Group node with a
Shape3D child (see Figure 15-3). Its geometry is stored in a Java 3D
TriangleStripArray, which specifies the sphere as an array of connected triangles. I don't have to adjust this geometry, but the sphere's appearance and position do require changes.
Appearance node is a container for references of to much information, including coloring, line, point, polygon, rendering, transparency, and texture attributes.
ColouringAttributes fixes the color of a shape and is unaffected by scene lighting. For a shape requiring interaction between color and light, the
Material component is employed. For light to affect a shape's color, three conditions must be met:
The shape's geometry must include normals.
Appearancenode must have a
Materialcomponent must have enabled lighting with
Sphere class can automatically creates normals, so the first condition is easily satisfied.