|
Java 3D Basics
An Introduction |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
Canvas3D object
SimpleUniverse object (which includes
a view branch)
SimpleUniverse
// Get a GraphicsConfiguration object that describes the
// properties of the display/graphics card
graphicsConfiguration = SimpleUniverse.getPreferredConfiguration();
// Construct a 3D rendering engine
canvas3D = new Canvas3D(graphicsConfiguration);
// Construct a SimpleUniverse object that creates the
// "view" side of the scene graph
simpleUniverse = new SimpleUniverse(canvas3D);
// Get the ViewingPlatform so that we can set the
// properties of the "view" side of the scene graph
view = simpleUniverse.getViewingPlatform();
// Move the projection plane back along the z-axis
// so that the x interval [-1, 1] can be completely seen
view.setNominalViewingTransform();
Window
LayoutManager
ApplicationWindow3D
ApplicationWindow3D Class ApplicationWindow3D window;
BranchGroup scene;
SimpleUniverse universe;
// Create an ApplicationWindow3D (for convenience)
window = new ApplicationWindow3D();
// Get the SimpleUniverse associated with the window
universe = window.getSimpleUniverse();
SceneGraphObject
Node - The abstract parent of
all Group and LeafNodes
NodeComponent - The abstract parent for
ColoringAttributes, Geometry,
Material, and Texture
Descendants of the Node Class
Node Class // Create a BranchGroup containing a simple cube
scene = new BranchGroup();
scene.addChild(new Background(0.0f, 0.0f, 0.0f));
// Add to the scene
scene.addChild(CubeFactory.createColorCube());
// Compile the scene
scene.compile();
// Add the scene to the SimpleUniverse
universe.addBranchGraph(scene);
Descendants of the NodeComponent Class
Point2f and Point2d
Point3f and Point3d
Point4f and Point4d
Geometry
PointArray
LineArray
TriangleArray
QuadArray
LineStripArray
TriangleStripArray
TriangleFanArray
// Vertices
private static final float vertices[][] = {{-0.5f,-0.5f,-0.5f},
{ 0.5f,-0.5f,-0.5f},
{ 0.5f, 0.5f,-0.5f},
{-0.5f, 0.5f,-0.5f},
{-0.5f,-0.5f, 0.5f},
{ 0.5f,-0.5f, 0.5f},
{ 0.5f, 0.5f, 0.5f},
{-0.5f, 0.5f, 0.5f}
};
// Faces
private static final int faces[][] = {{4,5,6,7},
{1,2,6,5},
{0,1,5,4},
{0,3,2,1},
{0,4,7,3},
{2,3,7,6}
};
// Face Colors
private static final float colors[][] = {{1.0f,0.0f,0.0f},
{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},
{0.0f,0.0f,1.0f},
{1.0f,0.0f,1.0f},
{0.0f,1.0f,1.0f}
};
/**
* Create a cube with white faces
*
* @return The BranchGroup containing the cube
*/
public static BranchGroup createCube()
{
BranchGroup branchGroup;
QuadArray face;
Shape3D node;
// Create an empty BranchGroup
branchGroup = new BranchGroup();
// Create each face
for (int f=0; f<faces.length; f++)
{
// Create a QuadArray to hold each face
face = new QuadArray(4, GeometryArray.COORDINATES);
// Add the vertices to the QuadArray
for (int v=0; v<faces[f].length; v++)
{
face.setCoordinates(v, vertices[faces[f][v]]);
}
// Create a Node to hold the face
node = new Shape3D(face);
// Add the Node to the BranchGroup
branchGroup.addChild(node);
}
return branchGroup;
}
com.sun.j3d.utils.geometry
Box, Cone, Cylinder,
Sphere
ColorCube
javax.vecmath
Transform3D Class:
javax.media.j3d
TransformGroup Class:
Node that can be
added to the scene graph
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import java3d.*;
import java3d.basics.CubeFactory;
/**
* An application that demonstrates the use of transforms
* in Java3D
* Note: Under MS-Windows with Direct3D this application
* must be run as follows:
*
* java -Dj3d.rend=d3d Rotationriver
*
* because OpenGL is the default rendering technology (and some
* MS-Windows installations do not support OpenGL)
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class RotationDriver
{
/**
* The entry-point of the application
*
* @param args The command line arguments
*/
public static void main(String[] args)
{
ApplicationWindow3D window;
BranchGroup scene;
SimpleUniverse universe;
window = new ApplicationWindow3D();
universe = window.getSimpleUniverse();
scene = new BranchGroup();
// Create the transformations
Transform3D rotationX, rotationY, transform;
rotationX = new Transform3D();
rotationY = new Transform3D();
transform = new Transform3D(); // The identity matrix
// Initialize the rotation matrices
rotationX.rotX(Math.PI/4.0d);
rotationY.rotY(Math.PI/5.0d);
// Concatenate the rotations
transform.mul(rotationX);
transform.mul(rotationY);
// Create a TransformGroup from the
// concatenated rotations
TransformGroup transformGroup;
transformGroup = new TransformGroup(transform);
// Add a geometry Node to the TransformGroup
transformGroup.addChild(CubeFactory.createColorCube());
// Add the TransformGroup to the scene
scene.addChild(transformGroup);
scene.compile();
universe.addBranchGraph(scene);
}
}
The Scene Graph in RotationDriver
WakeupOnAWTEvent Class:
WakeupCondition that is triggered by
either a KeyEvent or a MouseEvent
Behavior class
Behavior object
Behavior object
Behavior to the scene graph
Behavior
package java3d.basics;
import java.awt.event.*;
import java.util.*;
import javax.media.j3d.*;
/**
* A Behavior that changes the rotation of a TransformGroup
* in response to KEY_PRESSED events
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class KeyboardBehavior extends Behavior
{
private double angle;
private Transform3D transform;
private TransformGroup transformGroup; // Not owned
/**
* Explicit Value Constructor
*
* @param tg The TransformGroup to adjust in response to key presses
*/
public KeyboardBehavior(TransformGroup tg)
{
transformGroup = tg;
angle = 0.0;
transform = new Transform3D();
}
/**
* Initialize this Behavior
* (required by Behavior)
*/
public void initialize()
{
wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
/**
* Process a stimulus meant for this Behavior
* (required by Behavior)
*
* @param criteria The triggered wakeup criteria
*/
public void processStimulus(Enumeration criteria)
{
angle += 0.1;
transform.rotY(angle);
transformGroup.setTransform(transform);
wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
}
Behavior
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import java3d.*;
import java3d.basics.CubeFactory;
import java3d.basics.KeyboardBehavior;
/**
* An application that demonstrates the use of interaction
* in Java3D
* Note: Under MS-Windows with Direct3D this application
* must be run as follows:
*
* java -Dj3d.rend=d3d KeyboardDriver
*
* because OpenGL is the default rendering technology (and some
* MS-Windows installations do not support OpenGL)
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class KeyboardDriver
{
/**
* The entry-point of the application
*
* @param args The command line arguments
*/
public static void main(String[] args)
{
ApplicationWindow3D window;
BranchGroup scene;
SimpleUniverse universe;
window = new ApplicationWindow3D();
universe = window.getSimpleUniverse();
scene = new BranchGroup();
// Create the transformations
Transform3D transform;
transform = new Transform3D(); // The identity matrix
// Create a TransformGroup from the
// concatenated rotations
TransformGroup transformGroup;
transformGroup = new TransformGroup(transform);
// Allow the TransformGroup to be modified
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// Add a geometry Node to the TransformGroup
transformGroup.addChild(CubeFactory.createColorCube());
// Add the TransformGroup to the scene
scene.addChild(transformGroup);
// Construct the KeyboardBehavior
KeyboardBehavior keyboard;
keyboard = new KeyboardBehavior(transformGroup);
keyboard.setSchedulingBounds(new BoundingSphere());
// Add the KeyboardBehavior to the scene
scene.addChild(keyboard);
scene.compile();
universe.addBranchGraph(scene);
}
}
The Scene Graph in KeyboardDriver
import com.sun.j3d.loaders.*;
import com.sun.j3d.loaders.objectfile.*;
import com.sun.j3d.utils.behaviors.vp.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java3d.*;
import java3d.basics.CubeFactory;
/**
* An application that displays a cube in Java3D
*
* Note: Under MS-Windows with Direct3D this application
* must be run as follows:
*
* java -Dj3d.rend=d3d LoaderDriver
*
* because OpenGL is the default rendering technology (and some
* MS-Windows installations do not support OpenGL)
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class LoaderDriver
{
private static Color3f GRAY = new Color3f(0.75f, 0.75f, 0.75f);
private static Color3f WHITE = new Color3f(1.00f, 1.00f, 1.00f);
/**
* The entry-point of the application
*
* @param args The command line arguments
*/
public static void main(String[] args)
{
AmbientLight ambientLight;
ApplicationWindow3D window;
DirectionalLight backLight, frontLight;
BranchGroup root;
BoundingSphere bounds;
ObjectFile objectFile;
OrbitBehavior orbitBehavior;
Scene scene;
SimpleUniverse universe;
Transform3D transform;
TransformGroup transformGroup;
Vector3f back, front;
window = new ApplicationWindow3D();
universe = window.getSimpleUniverse();
root = new BranchGroup();
objectFile = new ObjectFile();
objectFile.setFlags(ObjectFile.RESIZE | ObjectFile.TRIANGULATE | ObjectFile.STRIPIFY);
try
{
// Read the .obj file
scene = objectFile.load(args[0]);
root.addChild(scene.getSceneGroup());
// Setup the ambient light
ambientLight = new AmbientLight(GRAY);
bounds = new BoundingSphere (new Point3d (0.0, 0.0, 0.0), 100.0);
ambientLight.setInfluencingBounds(bounds);
root.addChild(ambientLight);
// Set up the directional lights
back = new Vector3f(-1.0f, -1.0f, -1.0f);
backLight = new DirectionalLight(WHITE, back);
backLight.setInfluencingBounds(bounds);
root.addChild(backLight);
front = new Vector3f(1.0f, 1.0f, 1.0f);
frontLight = new DirectionalLight(WHITE, front);
frontLight.setInfluencingBounds(bounds);
root.addChild(frontLight);
// Setup the transform
transformGroup = new TransformGroup();
transform = new Transform3D();
transform.setTranslation(new Vector3f(0f,0f,-5f));
transformGroup.setTransform(transform);
root.addChild(transformGroup);
// Setup the user-interaction behavior so the user
// can click-and-drag to rotate the scene
orbitBehavior = new OrbitBehavior(window.getCanvas3D(),
OrbitBehavior.REVERSE_ALL);
orbitBehavior.setSchedulingBounds(bounds);
universe.getViewingPlatform().setViewPlatformBehavior(orbitBehavior);
}
catch (Exception e)
{
e.printStackTrace();
}
universe.addBranchGraph(root);
}
}