Understanding 3D Graphics (in Java)
Three-dimensional scene modeling is inescapably complex because there are many considerations we must take into account beyond just including coordinate values of the third dimension. Objects are constructed with various combinations of plane and curved surface geometry. Graphics packages provide convenient routines for displaying 3D components. However, the reason for geometric transformations in three-dimensional modeling is more complicated than with 2D because we have many more parameters to select when specifying how a 3D scene is to be mapped to a display device. The article will take up some key ideas and try to explicate the intricacies involved before one considers jumping into 3D programming.
Coordinates and 3D Display
Figure 1: The Cartesian coordinate system
If you are familiar with the Cartesian coordinate system of XY reference, 3D coordinates just add another dimension, called the Z axis, and can be visualized by posturing your thumb (right or left), index, and middle finger in three different directions, each representing the X,Y, and Z axis, respectively; or, you can call them the height, width, and depth of an object's reference. If the left hand is used, it is called left-handed coordinate space. And, if the right hand is used, it is called right-handed coordinate space.
Now, to obtain a display of a 3D scene that has been modeled in world coordinates, we need to set up a coordinate reference for the camera/eye. This coordinate reference defines the position and orientation for the viewing angle. Object reference are then transferred to the camera reference coordinates and projected on to the display plane.
Figure 2: The 3D (world) coordinate system
- The world coordinate system establishes the global reference frame for all other specified coordinate system.
- Object space is the coordinate space associated with a particular object.
- Camera space is the coordinate space associated with an observer.
Let's draw a relation among the different coordinate spaces. The virtual three-dimensional worlds have their own local coordinate space and define their own origin and axes. The model or the object also has its own origin and axes decided by the person who created it. Because the camera/eye has its own coordinate system, called local coordinate space, when we rotate the camera/eye around any principal axes, the rotation takes place around its local coordinates. However, if we translate/move the camera/eye to a different location, we move its world coordinates.
Figure 3: The local coordinate space system
|Note: The default coordinate system of a JavaFX 3D scene uses the +y axis pointing down, +x axis pointing towards the right side of the screen, and the +z axis towards the viewer, as shown in Figure 4.|
Figure 4: The default coordinate system of a JavaFX 3D scene
Three-dimensional Object Representation
There are different types of objects represented in a graphics scene, from complex trees, leaves, to simple objects like cube, cylinder, and other geometrical shapes. As a result, there is no one method to describe objects that will include all characteristics of these different materials. The newly introduced 3D library in JavaFX 8 provides some of the basic 3D shapes such as Box, Cylinder, MeshView, and Sphere under the javafx.scene.shape package. TriangleMesh, derived from the javafx.scene.shape.Mesh abstract class, is of specific importance in modeling complex 3D graphical object. Triangle meshes are basically a subset of polygon meshes and they are of special interest because of their simple nature. A straightforward way to represent a triangle mesh is to use an array of triangles. However, although simple and adequate it may seem for a trivial application, the term "mesh" implicitly defines a complex network of triangles and implies a degree of connectivity between adjacent triangles. As a result to create a triangle mesh, we need
- Vertices: A triangle has three vertices, shared by other triangles in the series
- Edges: The connection between vertices, also shared by other triangles
- Faces: It represent the surface of the triangles
There are many ways to actually represent it in a computer. One of the simplest techniques is illustrated in Figure 5.
Figure 5: One way to represent vertices, edges, and faces
|Vertex Table||Edge Table||Surface Table|
|E1: V1, V2
E2: V1, V3
E3: V2, V3
E4: V2, V4
E5: V3, V4
E6: V3, V5
E7: V4, V5
E8: V4, V6
E9: V5, V6
E10: V5, V7
E11: V6, V7
E13: V7, V8
|S1: E1, E2, E3
S2: E3, E4, E5
S3: E5, E6, E7
S4: E7, E8, E9
S5:E9, E10, E11
S6: E11, E12, E13
This simplistic idea would provide a hint on how the vertex format is represented while working with a triangle mesh in JavaFX.
Three-dimensional transformation is basically the same as 2D transformation except the technique has been extended by including consideration for z-coordinates. JavaFX supports all standard 3D transformation such as: rotation, scaling, shearing, translation, and affine transformation. They are standard for all graphics packages. The idea that we like to explicate here is the subtle difference between transforming an object vis-à-vis transforming coordinate space. A graphical object, although transformed, actually moves the point to a different location in the same coordinate space. If we transform the coordinate space, the point of the object remains the same but, in fact, we are expressing their location using a different coordinate space. Both the operation produces the same result because transforming an object by a certain amount is equivalent to transforming the coordinate space by the opposite amount. However, this conceptual distinction is useful for us to think about transforming the object in some cases and transforming the coordinate space in other classes.
These concepts only scratched the surface; however, they are essential to begin with 3D programming in any programming language, especially JavaFX. There is a whole lot of mathematics involved if you lay your foot deep down and want to start everything from scratch. However, graphics packages save the day and a programmer can concentrate on the work at hand and be productive. 3D programming in JavaFX is still in its infancy, but it's a promising child. Unless the internal working procedures are grasped, jumping into 3D programming will be a futile attempt and one may feel overwhelmed by the API structure and its complexity.