Friday, January 11, 2008

Technical: 3D Transformation

Today I will talk about 3D Transformation and how to create in Java3D.

A 3D transformation is basically moving a 3D point from one location to another. Examples of transformation are Rotations, Scales, Translations, and Reflections. Before we begin, we need to import the following:
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;

The scene graph group node that contains a Transform3D object is an instance of the TransformGroup.

The transformation performed in the example below simply rotates the cube a specified number of degrees about the x and y axis. 

public void createTransformation() 

{

/** Create a 3D transformation that will 

       *   rotate the cube a specified number of degrees on the x & y axis

   */ 

Transform3D transform3D = new Transform3D();

transform3D.rotX(xRotationAngle);

transform3D.rotY(yRotationAngle);

    

/** Create the transform group node and add the transformation

 *   to the node. Add the transformation group to the scene graph.

 */

    TransformGroup transformGroup = new TransformGroup(transform3D);

    sceneGraph.addChild(transformGroup);  


/** Add a colored cube to the transform group node. */

    transformGroup.addChild(new ColorCube(cubeScale));

}
The first three lines of code create a Java3D transformation that will be used to rotate a 3D object about the x-axis and the y-axis. The method used to perform the rotation, rotX() / rotY() accepts a single double precision floating point number that specifies the angle in radians. Adding on, a rotZ() will handle the z-axis.
The next two lines creates a transform group node with the specified 3D transformation. Once this group node has been properly initialized, it is added to the scene graph.

The last line of code simply adds the colored cube to the transform group node. The transform group node now has two children: the 3D transformation object and a colored cube.

And we have our HelloJava3Db. The diagram below shows the resultant output.

No comments:

Post a Comment