February 15, 2010

Java3D Example

« Easymock example | Main | Configure Log4j (log4j.xml) »

Java3D is a specification for generation and manipulation of three-dimensional objects. It is based on the scene graph concept, which relies on tree-oriented structures. So you have an intuitive feasibility to realize 3D scenes in the object-oriented world of the Java programming language. The basic objects of Java3D are:

  • BranchGroup: Defines a group of 3D-based shapes, transformations, lights, appearances, ... Could be a child of another BranchGroup-object
  • TransformGroup: Defines a transformation how rotation, translation and scaling.
  • Shape3D: geometrical 3D objects (Box, ColorCube, Cylinder,...).
  • Behavior: Root interface for handling input devices how mouse, keyboard, joystick.
  • Light: All possible light types (ambient, direct, ...).
The following video shows the implemented Java3D example.

The example above describes a scene graph, which consists of 10x10x10 boxes. It is based on the following scene graph.

package org.developers.blog.graph3d;

import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.universe.*;
import java.awt.Color;

import javax.media.j3d.*;

import javax.vecmath.*;

/**
 * Hello world!
 *
 */
public class Java3DExample {

    private SimpleUniverse universe;
    private Appearance cubeAppearance;
    private BranchGroup rootGroup;
    private BoundingSphere bounds;

    public void init() {
        createUniverse();
        createAppearance();
        createCubeOfCubes();
        createLights();
        createBehaviourInteractors();
        universe.getViewingPlatform().setNominalViewingTransform();

        // add the cube group of objects to SimpleUnvirse object
        universe.addBranchGraph(rootGroup);
    }

    private void createUniverse() {
        universe = new SimpleUniverse();
        rootGroup = new BranchGroup();
        bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
        
    }

    private void createAppearance() {
        cubeAppearance = new Appearance();
        Color3f ambientColour = new Color3f();
        ambientColour.set(Color.GRAY);
        Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
        Color3f diffuseColour = new Color3f();
        diffuseColour.set(Color.GRAY);

        float shininess = 20.0f;
        cubeAppearance.setMaterial(new Material(ambientColour, emissiveColour,
                diffuseColour, specularColour, shininess));
    }

    private void createCubeOfCubes() {
        // build up a cube of 10 cube for each axis (x, y, z)
        for (float x = -.5f; x <= .5f; x = x + .1f) {
            for (float y = -.5f; y <= .5f; y += .1f) {
                for (float z = -.5f; z <= .5f; z += .1f) {
                    Box box = new Box(0.02f, 0.02f, 0.02f, cubeAppearance);
                    TransformGroup tg = new TransformGroup();
                    Transform3D transform = new Transform3D();
                    Vector3f vector = new Vector3f(x, y, z);
                    transform.setTranslation(vector);
                    tg.setTransform(transform);
                    tg.addChild(box);
                    rootGroup.addChild(tg);
                }
            }

        }
    }

    private void createLights() {
        Color3f ambientLightColour = new Color3f(0.9f, 0.9f, 0.9f);
        AmbientLight ambientLight = new AmbientLight(ambientLightColour);
        ambientLight.setInfluencingBounds(bounds);
        Color3f directionLightColour = new Color3f(1.0f, 1.0f, 1.0f);
        Vector3f directionLightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
        DirectionalLight directionLight = new DirectionalLight(directionLightColour, directionLightDir);
        directionLight.setInfluencingBounds(bounds);
        rootGroup.addChild(ambientLight);
        rootGroup.addChild(directionLight);
    }

    private void createBehaviourInteractors() {
        TransformGroup viewTransformGroup =
                universe.getViewingPlatform().getViewPlatformTransform();

        KeyNavigatorBehavior keyInteractor =
                new KeyNavigatorBehavior(viewTransformGroup);

        BoundingSphere movingBounds = new BoundingSphere(new Point3d(0.0, 0.0,
        0.0), 100.0);
        keyInteractor.setSchedulingBounds(movingBounds);
        rootGroup.addChild(keyInteractor);

        MouseRotate behavior = new MouseRotate();
        behavior.setTransformGroup(viewTransformGroup);
        rootGroup.addChild(behavior);
        behavior.setSchedulingBounds(bounds);
    }

    public static void main(String[] args) {
        (new Java3DExample()).init();
    }
}

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 12:31 AM in Java

 

[Trackback URL for this entry]

Comment: Derrick at Fr, 12 Mrz 1:39 PM

Very nice! Java3D depends on openGL for hardware acceleration, right?

Comment: milos at Fr, 12 Mrz 5:45 PM

simple first example but it is FINE TO POPULARIZE Java3D. It is easy to use. Oracle-Sun could encourage it more.

You can choose if you want OpenGL or DirectX renderer I think. I have not play with Java3D for three years.

Comment: Rafael at Fr, 12 Mrz 5:52 PM

On Linux platforms Java3D use OpenGL. On Windows platforms you can use OpenGL and DirectX-Versions.

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« February »
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28