ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Killer Game Programming in Java: A 3D Checkerboard, Part 1
Pages: 1, 2, 3, 4

Coloring the Spheres

The Java 3D Material component controls what color a shape exhibits when lit by different kinds of lights:

    Material mat = new Material(ambientColor, emissiveColor,
                                diffuseColor, specularColor, shininess);

The ambient color argument specifies the shape's color when lit by ambient light: this gives the object a uniform color. The emissive color contributes the color that the shape produces (as for a light bulb); frequently, this argument is set to black (equivalent to off). The diffuse color is the color of the object when lit, with its intensity depending on the angle the light beams make with the shape's surface.

TIP: The diffuse and ambient colors are often set to be the same, which matches the way real-world objects are colored when lit.

The intensity of the specular color parameter is related to how much the shape reflects from its shiny areas. This is combined with the shininess argument, which controls the size of the reflective highlights.

TIP: The specular color is often set to white, matching the specular color produced by most objects in the real world.

In Checkers3D, there are two directional lights, which create two shiny patches on the top of the floating sphere (see Figure 15-1). The floor tiles are unlit since their color is set in the shape's geometry (more on this later in the chapter).

The code in floatingSphere( ) that handles the sphere's appearance is shown here:

    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f blue = new Color3f(0.3f, 0.3f, 0.8f);
    Color3f specular = new Color3f(0.9f, 0.9f, 0.9f); // near white

    Material blueMat= new Material(blue, black, blue, specular, 25.0f);

    Appearance blueApp = new Appearance( );

Positioning the Spheres

Positioning a shape is almost always done by placing its scene graph node below a TransformGroup (see the sphere Group in Figure 15-3). A TransformGroup can be used to position, rotate, and scale the nodes which lie beneath it, with the transformations defined with Java 3D Transform3D objects:

    Transform3D t3d = new Transform3D( );
    t3d.set( new Vector3f(0,4,0));     // place at (0,4,0)
    TransformGroup tg = new TransformGroup(t3d);
    tg.addChild(new Sphere(2.0f, blueApp));
           // set the sphere's radius and appearance
           // and its normals by default

The set( ) method positions the sphere's center at (0, 4, 0) and resets any previous rotations or scalings. set( ) can be used to scale and rotate while resetting the other transformations. The methods setTranslation( ), setScale( ), and setRotation( ) only affect the given transformation.

Unlike some 3D drawing packages, the y-axis in Java 3D is in the vertical direction, while the ground is being defined by the XZ plane, as shown in Figure 15-4.

The position of the sphere is Checkers3D is set to be (0, 4, 0), which places its center four units above the XZ plane.

Figure 15-4
Figure 15-4. Axes in Java 3D

View catalog information for Killer Game Programming in Java

Return to ONJava.com.