Vector Math Library

The Vector Math Library is a required library for creating Java 3D objects. The Vector Math Library is packaged separately so that other Java APIs or non-3D applications might make use of it.

The main purpose of the Vector Math Library is to provide methods for matrix and vector mathematics as well as methods for dealing with color, positions, and volumes.

A brief review of the methods is provided here.

Tuples

Tuples store two, three, and four element values that are used to represent points, coordinates, and vectors. For example, a color can be stored in the tuple with three floating-point values (one each for red, green, and blue). The Java 3D class for a color specified in this way is Color3f. Note that there are tuples for representing bytes, double precision, and floating point values.

Matrix Objects

As we saw previously, matrix operations are a fundamental part of 3D graphics. Most applications won't need access to the matrices directly; however, there are many situations in which matrix manipulation is the only avenue available to achieve a particular transformation. The Vector Math Library contains a basic set of matrix objects that can be used to perform matrix operations (listed in Table 11.8).

Table 11.8. Matrix Classes
Matrix Object Description
Matrix3f Single-precision, floating-point 3x3 matrix
Matrix3d Double-precision, floating-point 3x3 matrix
Matrix4f Single-precision, floating-point 4x4 matrix
Matrix4d Double-precision, floating-point 4x4 matrix
Gmatrix Double-precision NxM matrix; can be dynamically resized

For many projects, it simply isn't necessary to directly use any of the objects in the Vector Math Library. Indeed, TransformGroup has several utility methods for rotation, translation, and scaling. (You have seen these in action already.) However, in many cases these aren't sufficient to achieve the desired effect, and moreover, many 3D programmers would prefer to perform their own matrix operations.

Regardless, the Vector Math Library is useful in many situations and worth understanding. Listing 11.15 shows a solution to the problem of scaling an object along a single dimension using matrix operations.

Listing 11.15 MatrixExampleJ3D.java
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
//import javax.vecmath.Matrix3d;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;

public class MatrixExamplesJ3D extends Applet {
  VirtualUniverse universe;
  Locale locale;
  TransformGroup vpTrans;
  View view;
  Bounds bounds;
  TransformGroup geoTG;
  float sval=.1f;

    public BranchGroup createSceneGraph() {
        // Create the root of the branch graph; this will be returned
        Appearance app = new Appearance();
        BranchGroup objRoot = new BranchGroup();
        geoTG = new TransformGroup();
        geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    geoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

        Box box = new Box(1.f,1.f,1.f, app);

        objRoot.addChild(geoTG);
        MouseRotate behavior = new MouseRotate(geoTG);

        geoTG.addChild(behavior);
        behavior.setSchedulingBounds(bounds);
        geoTG.addChild(box);

    return objRoot;
     }

. . .
    public void scaleBox() {
        sval=sval+0.08f;

        Transform3D currentTran = new Transform3D();
        geoTG.getTransform(currentTran);

        Matrix3f xmat = new Matrix3f();
        currentTran.get(xmat);

       /* System.out.println("before " + xmat.m00 + " " + xmat.m01 + " " + xmat.m02 + " "
                         + xmat.m10 + " " + xmat.m11 + " " + xmat.m12 + " "
                         + xmat.m20 + " " + xmat.m21 + " " + xmat.m22);
       */
        Matrix3f newmat = new Matrix3f();
        newmat.setIdentity();

        newmat.setElement(1,1,sval);

        xmat.mul(newmat);

        /* System.out.println("after " + xmat.m00 + " " + xmat.m01 + " " + xmat.m02 + " "
                         + xmat.m10 + " " + xmat.m11 + " " + xmat.m12 + " "
                         + xmat.m20 + " " + xmat.m21 + " " + xmat.m22);
        */
        currentTran.set(xmat);

         geoTG.setTransform(currentTran);


    }
}
. . .

 class KeyHandler implements KeyListener {
    MatrixExamplesJ3D me;

     public KeyHandler(MatrixExamplesJ3D me) {
         this.me = me;
     }
     public void keyReleased(java.awt.event.KeyEvent p1) {
     }

     public void keyPressed(java.awt.event.KeyEvent p1) {
        me.scaleBox();

     }

     public void keyTyped(java.awt.event.KeyEvent p1) {
     }

}

Using the matrix notation mij, we can see one way in which we gain access to the different elements of the matrix. For example, one of the constructors for a Matrix3f object is

public Matrix3f(float m00, float m01, float m02,
                float m10, float m11, float m12
                float m20, float m21, float m22)

It is also possible to use the setElement() and getElement() methods to get and set individual elements directly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset