Class Diagrams for Lathe3D


Chapter 17. Using a Lathe to Make Shapes

I've run up against the lack of useful shapes in Java 3D many times: there's only so much that can be done with the Box, Cone, Cylinder, and Sphere classes. One way of getting past this limitation is to use Java 3D's GeometryArray (or one of its subclasses) to specify a geometry in terms of separate arrays of positional coordinates, colors, normals, and texture coordinates. This is pretty daunting, and I like to avoid it if possible.

An alternative to this low-level shape building is to follow the approach outlined in Chapter 16: create the shape in a 3D modeling package and load it into Java 3D. The drawback is that you need to learn the modeling software. Most of these packages have so many bells and whistles that it's hard to get what you need done.

This chapter describes a compromise between the complexity of Java 3D GeometryArrays and the loading of ready-made models. The edge of a shape is defined in terms of straight lines and simple curves. This edge (or lathe curve) is rotated around the y-axis to create a 3D volume, called a lathe shape. This approach, often called surface or sweep revolution, is packaged up in a LatheShape3D class.

Color or texture information can be added easily. A lathe shape is pink by default, but this can be changed to a different color or texture. In both cases, the shape will reflect light (i.e., a shape's faces always have normals). A color is defined using two Color3f objects: one for ambient illumination (typically a dark hue) and the other for diffuse lighting.

A mix of two colors generally makes the shape look more realistic than using just one.


A texture is wrapped around the shape starting from the middle of the shape's back (the side facing away from the camera position), continuing counterclockwise around the front, and ending back where it started. The texture is stretched in the y-direction, ensuring it covers the shape vertically.

Figure 17-1 shows a selection of LatheShape3D objects.

Figure 17-1. LatheShape3D objects


LatheShape3D was designed with Chapter 20 in mind, where I will describe how to animate an articulated moveable figure. LatheShape3D is ideal for creating body parts, such as arms, legs, and a torso.


This chapter illustrates the following features:


A shape's lathe curve employs Hermite curves

Hermite curves are used to represent the curve sequences inside a lathe curve. I chose Hermite curves since they're simple to specify and (after making a few assumptions about the shape) can be generated automatically.


A lathe shape is created using surface revolution

A QuadArray shape is made by revolving a lathe curve around the y-axis. This was low-level shape creation, but done by the code rather than myself (the way I like it).


Texture coordinate calculation

The calculation of texture coordinates (s, t) is based on the shape's (x, y, z) coordinates, without using a Java 3D TexCoordGeneration object (TexCoordGeneration was introduced in Chapter 16). It's possible to automate these calculations after making some simple assumptions about how the texture should be wrapped over a shape.


Normals calculated with Java 3D utilities

Normals are generated for the quadrilaterals (quads) in the QuadArray with the aid of Java 3D's GeometryInfo and NormalGenerator classes. This allows the shape to reflect light without having to do the hard work of generating the normals.


Subclassing of shapes

LatheShape3D can be subclassed to modify the surface revolution. In other words, the 3D volume doesn't need to be created solely from a circular rotation around the y-axis, and the path can be elliptical or stranger (as you'll see).



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net