Tutorial 2 - Basic Geometry

This tutorial is based on tutorial 1 and hence it'll only detail updates to that tutorial. We'll be adding some geometry to the scene and updating the view so we can actually see it.

Display3D.java

    // add the square to the view
    scene.addChild(new Square());

    setupView();
The lines above are added to the constructor. The first one adds our new object, a Square, to the scene. We'll go to define the Square class next. The next line calls a simple method to setup the view so we'll be able to see the square.

    /**
     * Setup our view so we are able to see the elements
     * in the scene
     */
    public void setupView() {
        // the transform describes how we should modify the view 
        Transform3D viewTransform = new Transform3D();
        viewTransform.setTranslation(new Vector3d(0,0,8));
        
        // and then setup the view for our universe
        universe.getViewingPlatform().getViewPlatformTransform().setTransform(viewTransform);
    }
The first thing introduced here is the Transform3D object. A transform is essentially a 4x4 matrix used in 3d graphics to manipulate other bits of data (including views and objects). Java 3D is great in this manner in that it provides a transform object which has a set of method to support setting it. In our case we'd just like to translate the view back along the Z axis. So we set the transform up to do a translation using the Vector (0,0,8). This will move our view back 8 units (meters) along the Z axis. This will leave us looking at the origin (where we'll put our square) from 8 units away. The last line of the method finally assigns our newly created transform to the view.

Ok, on to the new class "Square", our first piece of geometry.

public class Square extends BranchGroup {
    
    /** 
     * Creates a new instance of Square 
     */
    public Square() {
        QuadArray array = new QuadArray(4,QuadArray.COORDINATES);
        array.setCoordinate(0,new Point3d(-1,-1,0));
        array.setCoordinate(1,new Point3d(1,-1,0));
        array.setCoordinate(2,new Point3d(1,1,0));
        array.setCoordinate(3,new Point3d(-1,1,0));
        
        addChild(new Shape3D(array));
    }
    
}
And thats it. This create a simple square that can be added to the view . Note how the class extends BranchGroup. BranchGroup, as mentioned in tutorial 1, is an element of the Java3D scene graph tree. This makes it possible for us to add this object to the scene in our display.

To create a square we use a QuadArray. In this case our array simply contains coordinates, however it can contain many other attributes (color, texture coordinates). Also in this case we use a quad array since we want a single square, however there are many different types of array for different types of geometry.

We add specify that the array we contain 4 coordinates. We then define each coordinate with calls to setCoordinate.

The final thing we do here is add another scene graph element. Since this a branch group we can add child elements. In this case we add a "Shape3D". The shape 3d node is the basic element of all scenes and models. It brings together a set of geometry and with a common appearance. We'll look at appearance nodes in the next couple of tutorials. In this case we'll just use the default appearance, white.

And thats it, adding this few changes and compiling will present you with a white square on the screen. Although this doesn't seem very 3d its actually a complete model in 3d space.

Get the source code
Back to the tutorials page