Friday, January 4, 2008

Technical: Our First Java3D

Following the Java3D tutorial at http://www.java3d.org/starting.htm, we compiled and executed our first Java3D program. This program is swee.. short and simple, and show you the basic steps needed to display 3D objects.

To compile: javac HelloJava3Da.java

To run: java HelloJava3Da

* Mac is good. All Java3D classes are already included in the Mac OS X Java Build. No worries on additional setup. Too bad for windows. 

import com.sun.j3d.utils.universe.SimpleUniverse;

import com.sun.j3d.utils.geometry.ColorCube;

import javax.media.j3d.BranchGroup;


public class HelloJava3Da

{

public HelloJava3Da()

{

// Create a virtual universe to contain your scene.

SimpleUniverse universe = new SimpleUniverse();

// Create a data structure to contain a group of objects

BranchGroup group = new BranchGroup();

// Add an object to the group   

group.addChild(new ColorCube(0.3));

// Position the viewer so that they are looking at the object

universe.getViewingPlatform().setNominalViewingTransform();

// Add the group of objects to the universe

universe.addBranchGraph(group);

}

public static void main( String[] args )

{

new HelloJava3Da();

    }


l

At the Hello3d() constructor, you will see the five lines that perform each of the commented steps. The program displays a glowing cube, the viewer is looking directly at the red face of the cube, so what you actually see is a red square on a black background.

No comments: