Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java 3D API, Sound
James Atlas
July 31, 2008
Review
• J2EE
July 31, 2008
James Atlas - CISC370
2
Today
• Java 3D
• Java Media Framework (Sound)
July 31, 2008
James Atlas - CISC370
3
What is Java 3D?
• Java 3D is:
Standard extension to the Java 2 JDK
An interface for writing programs to display and
interact with 3D graphics
Provides a collection of high-level constructs for
creating and manipulating 3D geometry and
structures for rendering that geometry
It is now a community source project developed
on java.net (https://java3d.dev.java.net/)
July 31, 2008
James Atlas - CISC370
4
What can Java 3D do?
• Java 3D includes a rich and extensible feature set
for
Building shapes
Composing behaviors
Interacting with the environment and the user
Controlling rendering details
• Java 3D enables:
Quick development of complex 3D applications
Fast and efficient implementation on a variety of
platforms, from embedded systems to PCs to high-end
workstations
July 31, 2008
James Atlas - CISC370
5
Advantages and Disadvantages
• Advantages
It enables fast Development, so developers do
not need to manipulate rendering details, they
can focus on visual contents
Cross-platform
• Disadvantages
It has lower speed than C++/OpenGL
July 31, 2008
James Atlas - CISC370
6
Online Java 3D Resources
• Homepage of Java 3D
https://java3d.dev.java.net/
• Java 3D API Documentation, Examples and Downloads
https://java3d.dev.java.net/binary-builds.html
• Java 3D Tutorial (Chapter 0-7, highly recommended)
http://java.sun.com/developer/onlineTraining/java3d/index.html
• Java 3D interest group mail archive (searchable):
http://archives.java.sun.com/archives/java3d-interest.html
• The Java 3D Community Site
http://www.j3d.org/
July 31, 2008
James Atlas - CISC370
7
Java3D API useful packages
• Core classes
javax.media.j3d package, it includes the lowest-level classes
necessary in Java 3D programming
• Utility classes
com.sun.j3d.utils package, it is convenient and powerful additions
to the core
It has 4 major categories: content loaders, scene graph
construction aids, geometry classes, and convenience utilities
• Abstract Windowing Toolkit (AWT)
java.awt package, it creates a window to display the rendering
• Vector math classes
javax.vecmath package, it defines vector math classes for points,
vectors, matrices, and other mathematical objects
July 31, 2008
James Atlas - CISC370
8
Virtual Universe and Scene Graph
• A Java 3D program describes a virtual
universe, which is to be rendered
• A virtual universe is created from a scene
graph
• The scene graph is assembled from objects
to define the geometry, sound, lights, location,
orientation, and appearance of visual and
audio objects
July 31, 2008
James Atlas - CISC370
9
How to Build a Scene Graph
• A scene graph is composed of nodes and
arcs. A node is a data element, and arc is a
relationship between data elements
• The nodes in the scene graph are the
instances of Java 3D classes. The arcs
represent the two kinds of relationship
between the Java 3D instances
July 31, 2008
James Atlas - CISC370
10
Two relationships
• Parent-child relationship
A parent node can have any number of children but only
one parent
A child node can have one parent and no children
• Reference relationship
A reference associates a NodeComponent object with a
scene graph Node. NodeComponent objects define the
Geometry, Appearance, Material and Texture attributes
used to render the visual objects
For a single node, it can have many references
July 31, 2008
James Atlas - CISC370
11
A Scene Graph Example
July 31, 2008
James Atlas - CISC370
12
Concepts
• Each scene graph has a single Virtual Universe
• The Virtual Universe object has a Locale object
•
•
(landmark used to determine the location of visual
objects). It is the root of multiple sub-graphs
A BranchGroup object is the root of a sub-graph
(branch graph)
Two kinds of sub-graph
view branch graph
• viewing parameters (viewing location and direction)
content branch graph
• Geometry, Appearance, Material, Texture, …
July 31, 2008
James Atlas - CISC370
13
SceneGraphObject class
• SceneGraphObject is the superclass
for nearly every Core and Utility Java
3D class. It has two subclasses:
Node
• Group
BranchGroup, TransformGroup
• Leaf
Shape3D, Light, Behavior, and Sound
(have no children but may reference
NodeComponents)
NodeComponent
• Geometry, Appearance, Material,
Texture, … (may be referenced by A
Leaf)
July 31, 2008
James Atlas - CISC370
14
Some Key Classes
• Node and NodeComponent
superclasses for all the items that are added to a scene graph
• BranchGroup
A Container for other objects in the scene
Every item in the scene graph is either a BranchGroup, or is a child of a
BranchGroup
A BrachGroup can be a child of another BranchGroup
• Shape3D
Contains information about an object in the scene
Contains references to Geometry, Appearance, Material, Texture, …
• Appearance
Defines the way a shape appears in the scene, including texture, and attributes for
many other settings
• TransformGroup
Allows translations, scales and rotations to be preformed
• Geometry
Superclass for all classes that allow you to add form to a shape
Subclasses: LineArray, TriangleArray, ....
July 31, 2008
James Atlas - CISC370
15
Shape
• A Shape is composed of Geometry and
Appearance:
Shape
Geometry
July 31, 2008
Appearance
James Atlas - CISC370
16
Geometry
• There are several primitive Geometry
classes available:
Box
Cylinder
Cone
Sphere
Text2D
July 31, 2008
James Atlas - CISC370
17
Appearance
•
•
•
•
Color
Texture
Material
Other attributes (point, polygon, rendering,
etc)
July 31, 2008
James Atlas - CISC370
18
A Recipe for Writing Java 3D
Programs
• 1. Create a Canvas3D object
• 2. Create a VirtualUniverse object
• 3. Create a Locale object, attaching it to the
•
VirtualUniverse object
4. Construct a view branch graph
a. Create a View object
b. Create a ViewPlatform object
c. Create a PhysicalBody object
d. Create a PhysicalEnvironment object
e. Attach ViewPlatform, PhysicalBody, PhysicalEnvironment,
and Canvas3D objects to View object
• 5. Construct content branch graph(s)
• 6. Compile branch graph(s)
• 7. Insert subgraphs into the Locale
July 31, 2008
James Atlas - CISC370
19
The Simplest Recipe for Writing Java
3D Programs
•
•
•
•
1. Create a SimpleUniverse object
2. Construct content branch
3. Insert content branch into the SimpleUniverse
4. Optionally set nominal transformation (a
normalization transform so you can see the whole
scene)
• Example Hello3d
July 31, 2008
James Atlas - CISC370
20
The SimpleUniverse Class
• The SimpleUniverse object constructor creates a
•
•
scene graph including VirtualUniverse and Locale
objects, and a complete view branch graph
The view branch graph created by SimpleUniverse
uses instances of ViewingPlatform and Viewer
convenience classes in place of the core classes
used to create the view branch graph
It significantly reduces the time and effort needed to
create the view branch graph
July 31, 2008
James Atlas - CISC370
21
Other Features
•
•
•
•
•
•
•
Appearance
Defining your own shapes
Transformations
Lighting
Textures
Animation
Interaction
July 31, 2008
James Atlas - CISC370
22
Appearance
Color3f ambient = new Color3f(0.5f, 0.5f, 0.5f);
Color3f emissive = new Color3f(0.0f, 0.0f, 0.5f);
Color3f diffuse = new Color3f(0.0f, 0.0f, 1.0f);
Color3f specular = new Color3f(1.0f, 1.0f, 1.0f);
float shininess = 20.0f;
Appearance shinyBlueApp = new Appearance();
Material
shinyBlueMat = new Material(ambient, emissive,
diffuse, specular, shininess);
shinyBlueApp.setMaterial(shinyBlueMat);
bg.addChild(new Cylinder(0.5f, 4.0f, shinyBlueApp));
July 31, 2008
James Atlas - CISC370
23
Define your own shape types
• Use inheritance to extend Shape3d
Use QuadArrays to define faces, coordinates, etc.
p1 = new Point3d(-30,0,45);
p2 = new Point3d(30,20,45);
QuadArray polygon = new QuadArray(4,
QuadArray.COORDINATES|QuadArray.NORMALS);
polygon.setCoordinate(0, p1);
polygon.setCoordinate(1, p2);
July 31, 2008
James Atlas - CISC370
24
Transformations
• All the usual suspects…
rotation
translation
scaling
• Anything that is a child node (in the scene graph) of
•
a TransformGroup is affected by that
TransformGroup’s Transforms!
TransformDemo.java
July 31, 2008
James Atlas - CISC370
25
Transformation
//set up the transformation
Transform3D trans = new Transform3D();
trans.setTranslation(new Vector3f(-3.0f,0.0f,0.0f));
trans.setScale(new Vector3d(4.0,0.25,4.0));
trans.setRotation(new
AxisAngle4d(1.0,1.0,0.0,Math.PI/2.0));
//set up a group to use the transformation
TransformGroup tg = new TransformGroup(trans);
bg.addChild(tg);
tg.addChild(new Cylinder(0.5f, 4.0f, shinyBlueApp));
July 31, 2008
James Atlas - CISC370
26
Nesting groups
• You can add Shapes directly to the
BranchGroup
• Or you can add them to a TransformGroup
and make that a child of the BranchGroup…
• You can nest the TransformGroups
July 31, 2008
James Atlas - CISC370
27
Translation
trans.setTranslation(new Vector3f(-3.0f,0.0f,0.0f));
July 31, 2008
James Atlas - CISC370
28
Scaling
trans.setScale(new Vector3d(4.0,0.25,4.0));
July 31, 2008
James Atlas - CISC370
29
Rotation
trans.set(new AxisAngle4d(1.0,1.0,0.0,Math.PI/2.0));
July 31, 2008
James Atlas - CISC370
30
Lighting
protected BranchGroup createContentBranch(){
BranchGroup bg = new BranchGroup();
.
.
.
addLights(bg);
return bg;
}
protected void addLights(BranchGroup b){
.
.
.
}
July 31, 2008
James Atlas - CISC370
31
Ambient Light
protected void addLights(BranchGroup bg){
BoundingSphere bounds = new BoundingSphere(new
Point3d(0.0,0.0,0.0),100.0);
Color3f ambientColour = new Color3f(0.2f,0.2f,0.2f);
AmbientLight ambLight= new AmbientLight(ambientColour);
ambLight.setInfluencingBounds(bounds);
bg.addChild(ambLight);
}
July 31, 2008
James Atlas - CISC370
32
Directional Light
protected void addLights(BranchGroup bg){
Color3f dirColour = new Color3f(1.0f,1.0f,1.0f);
Vector3f lightDir = new Vector3f(-1.0f,-1.0f,-1.0f);
DirectionalLight dirLight= new
DirectionalLight(dirColour,lightDir);
dirLight.setInfluencingBounds(bounds);
bg.addChild(dirLight);
}
July 31, 2008
James Atlas - CISC370
33
Other lights
• Point lights
• Spot lights
July 31, 2008
James Atlas - CISC370
34
Textures
TextureLoader tl = new TextureLoader("./resources/wood.jpg", null);
Texture texture = tl.getTexture();
texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor( new Color4f( 0.0f, 1.0f, 0.0f, 0.0f ) );
// Set up the texture attributes
// could be REPLACE, BLEND or DECAL instead of MODULATE
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.REPLACE);
Appearance ap = new Appearance();
ap.setTexture(texture);
ap.setTextureAttributes(texAttr);
//set up the material
ap.setMaterial(new Material());
// Create a ball to demonstrate textures
int primflags = Primitive.GENERATE_NORMALS +
Primitive.GENERATE_TEXTURE_COORDS;
Sphere sphere = new Sphere(0.5f, primflags, ap);
group.addChild(sphere);
July 31, 2008
James Atlas - CISC370
35
Animation
// Create the TransformGroup node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at run time. Add it to
// the root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// Create a simple Shape3D node; add it to the scene graph.
objTrans.addChild(new ColorCube(0.4));
// Create a new Behavior object that will perform the
// desired operation on the specified transform and add
// it into the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, 4000);
RotationInterpolator rotator = new RotationInterpolator(
rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objRoot.addChild(rotator);
July 31, 2008
James Atlas - CISC370
36
Interaction
Canvas3D c = new Canvas3D(config);
c.addKeyListener(this);
c.addMouseListener(this);
frame.getContentPane().add("Center", c);
..
public void keyPressed(KeyEvent e) {
// Invoked when a key has been pressed.
if (e.getKeyChar() == 's') {
xloc = xloc + .1f;
}
if (e.getKeyChar() == 'a') {
xloc = xloc - .1f;
}
trans.setTranslation(new Vector3f(xloc, height, 0.0f));
objTrans.setTransform(trans);
}
July 31, 2008
James Atlas - CISC370
37
Java 3D Game example
• https://java3d.dev.java.net/applets/FourByFo
ur.html
July 31, 2008
James Atlas - CISC370
38
Java Sound
July 31, 2008
James Atlas - CISC370
39
Java Sound Preliminaries
File Formats Supported
• .au or .snd : usually stores
8-bit m-law encoded samples,
but can also store 8 or 16 bit
linear samples
• .aif : usually stores 8 or 16
bit linear encoded samples
• .wav :Can store 8 or 16 bit
samples using linear or m-law
encoded samples
• .midi : follows the midi data
format
Note: The file header indicates
the actual format
July 31, 2008
Frames and Frame Rates
• Sample Frame
Stores all the samples taken at
an instant of time
# of bytes in a frame = # of
bytes in a sample X number of
channels
• Frame Rate
The number of frames per
second of sound
In most cases frame rate is
same as sample rate
In compressed sound, the
frame rate will be less than
sample rate.
James Atlas - CISC370
40
Java Sound API
Line
Mixer
SourceDataLine
July 31, 2008
DataLine
TargetDataLine
James Atlas - CISC370
Port
Clip
41
Java Sound API
System Resources
• Audio Line: Any resource
that is a source or
destination of sampled
sound data
A line can encapsulate
several channels
Example: input/output ports
on the sound card
Lines have controls (gain
and pan control)
Other Sound Sources
• A file or more generally a
URL
Terminology:
• A source data line is a source
•
for a mixer, not a source for
you; you write to it
A target data line is the output
from the mixer; your read from it
• Mixer: Receives input from
one or more source data
lines and outputs the result
of combining the input to an
output line called a target
data line
July 31, 2008
Source Data Lines
Target Data Lines
Sound i/p
Sound i/p
Sound i/p
James Atlas - CISC370
MIXER
Sound o/p
Sound o/p
42
Java Sound API (…contd)
Packages:
AudioStreamInput class
•
•
•
•
• Represents a stream that is
javax.sound.sampled
javax.sound.midi
javax.sound.sampled.spi
javax.sound.sampled.midi
The AudioSystem class
• Establish whether a
particular resource is
available
• Get a ref. to the object that
encapsulates the resource
• Call methods to operate the
resource
July 31, 2008
a source of sampled sound
data with a specific format
• You can create an
AudioStreamInput object
from a local sound file, from
another input stream or a
URL
• You can
Read data from it
Write its contents to an
output stream
Convert its format
James Atlas - CISC370
43
Resource Descriptor Classes
The Line, DataLine, Mixer and Port interface definitions each
include an inner class with the name Info. Objects of these
class types encapsulate data specifying an object of the
corresponding type
Specifies a Line
object by its Class
Line.Info
Mixer.Info
Specifies a Mixer
object by its name,
vendor version and
description
July 31, 2008
DataLine.Info
Specifies a DataLine by
the audio formats to be
supported, the buffer
size limits, and the Class
of the data line
James Atlas - CISC370
Port.Info
Specifies a Port object by
its Class, its name, and
whether it is a source or
target line
44