Java/3D/Sphere Ball
Содержание
Appearance Bounds
/*
* @(#)AlternateAppearanceBoundsTest.java 1.10 02/10/21 13:34:15
*
* Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: -
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. - Redistribution in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed,licensed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.j3d.AlternateAppearance;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingLeaf;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Group;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class AlternateAppearanceBoundsTest extends JApplet implements
ActionListener {
Material mat1, altMat;
Appearance app, otherApp;
JComboBox altAppMaterialColor;
JComboBox appMaterialColor;
JCheckBox useBoundingLeaf;
JCheckBox override;
JComboBox boundsType;
private Group content1 = null;
AlternateAppearance altApp;
Shape3D[] shapes1;
boolean boundingLeafOn = false;
// Globally used colors
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
Color3f[] colors = { white, red, green, blue };
private Bounds worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
1000.0); // Extent
private Bounds smallBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
0.25); // Extent
private Bounds tinyBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
0.05); // Extent
private BoundingLeaf leafBounds = null;
private int currentBounds = 2;
private Bounds[] allBounds = { tinyBounds, smallBounds, worldBounds };
DirectionalLight light1 = null;
// Get the current bounding leaf position
private int currentPosition = 0;
// Point3f pos = (Point3f)positions[currentPosition].value;
private SimpleUniverse u = null;
public AlternateAppearanceBoundsTest() {
}
public void init() {
Container contentPane = getContentPane();
Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
contentPane.add("Center", c);
BranchGroup scene = createSceneGraph();
// SimpleUniverse is a Convenience Utility class
u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
// Create GUI
JPanel p = new JPanel();
BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
p.add(createBoundsPanel());
p.add(createMaterialPanel());
p.setLayout(boxlayout);
contentPane.add("South", p);
}
public void destroy() {
u.cleanup();
}
BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
// Create an alternate appearance
otherApp = new Appearance();
altMat = new Material();
altMat.setCapability(Material.ALLOW_COMPONENT_WRITE);
altMat.setDiffuseColor(new Color3f(0.0f, 1.0f, 0.0f));
otherApp.setMaterial(altMat);
altApp = new AlternateAppearance();
altApp.setAppearance(otherApp);
altApp.setCapability(AlternateAppearance.ALLOW_BOUNDS_WRITE);
altApp
.setCapability(AlternateAppearance.ALLOW_INFLUENCING_BOUNDS_WRITE);
altApp.setInfluencingBounds(worldBounds);
objRoot.addChild(altApp);
// Build foreground geometry
Appearance app1 = new Appearance();
mat1 = new Material();
mat1.setCapability(Material.ALLOW_COMPONENT_WRITE);
mat1.setDiffuseColor(new Color3f(1.0f, 0.0f, 0.0f));
app1.setMaterial(mat1);
content1 = new SphereGroup(0.05f, // radius of spheres
0.15f, // x spacing
0.15f, // y spacing
5, // number of spheres in X
5, // number of spheres in Y
app1, // appearance
true); // alt app override = true
objRoot.addChild(content1);
shapes1 = ((SphereGroup) content1).getShapes();
// Add lights
light1 = new DirectionalLight();
light1.setEnable(true);
light1.setColor(new Color3f(0.2f, 0.2f, 0.2f));
light1.setDirection(new Vector3f(1.0f, 0.0f, -1.0f));
light1.setInfluencingBounds(worldBounds);
light1.setCapability(DirectionalLight.ALLOW_INFLUENCING_BOUNDS_WRITE);
light1.setCapability(DirectionalLight.ALLOW_BOUNDS_WRITE);
objRoot.addChild(light1);
// Add an ambient light to dimly illuminate the rest of
// the shapes in the scene to help illustrate that the
// directional lights are being scoped... otherwise it looks
// like we"re just removing shapes from the scene
AmbientLight ambient = new AmbientLight();
ambient.setEnable(true);
ambient.setColor(new Color3f(1.0f, 1.0f, 1.0f));
ambient.setInfluencingBounds(worldBounds);
objRoot.addChild(ambient);
// Define a bounding leaf
leafBounds = new BoundingLeaf(allBounds[currentBounds]);
leafBounds.setCapability(BoundingLeaf.ALLOW_REGION_WRITE);
objRoot.addChild(leafBounds);
if (boundingLeafOn) {
altApp.setInfluencingBoundingLeaf(leafBounds);
} else {
altApp.setInfluencingBounds(allBounds[currentBounds]);
}
return objRoot;
}
JPanel createBoundsPanel() {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("Scopes"));
String boundsValues[] = { "Tiny Bounds", "Small Bounds", "Big Bounds" };
boundsType = new JComboBox(boundsValues);
boundsType.addActionListener(this);
boundsType.setSelectedIndex(2);
panel.add(new JLabel("Bounds"));
panel.add(boundsType);
useBoundingLeaf = new JCheckBox("Enable BoundingLeaf", boundingLeafOn);
useBoundingLeaf.addActionListener(this);
panel.add(useBoundingLeaf);
override = new JCheckBox("Enable App Override", false);
override.addActionListener(this);
panel.add(override);
return panel;
}
JPanel createMaterialPanel() {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("Appearance Attributes"));
String colorVals[] = { "WHITE", "RED", "GREEN", "BLUE" };
altAppMaterialColor = new JComboBox(colorVals);
altAppMaterialColor.addActionListener(this);
altAppMaterialColor.setSelectedIndex(2);
panel.add(new JLabel("Alternate Appearance MaterialColor"));
panel.add(altAppMaterialColor);
appMaterialColor = new JComboBox(colorVals);
appMaterialColor.addActionListener(this);
appMaterialColor.setSelectedIndex(1);
panel.add(new JLabel("Normal Appearance MaterialColor"));
panel.add(appMaterialColor);
return panel;
}
public void actionPerformed(ActionEvent e) {
int i;
Object target = e.getSource();
if (target == altAppMaterialColor) {
altMat.setDiffuseColor(colors[altAppMaterialColor
.getSelectedIndex()]);
} else if (target == useBoundingLeaf) {
boundingLeafOn = useBoundingLeaf.isSelected();
if (boundingLeafOn) {
leafBounds.setRegion(allBounds[currentBounds]);
altApp.setInfluencingBoundingLeaf(leafBounds);
} else {
altApp.setInfluencingBoundingLeaf(null);
altApp.setInfluencingBounds(allBounds[currentBounds]);
}
} else if (target == boundsType) {
currentBounds = boundsType.getSelectedIndex();
if (boundingLeafOn) {
leafBounds.setRegion(allBounds[currentBounds]);
altApp.setInfluencingBoundingLeaf(leafBounds);
} else {
altApp.setInfluencingBoundingLeaf(null);
altApp.setInfluencingBounds(allBounds[currentBounds]);
}
} else if (target == override) {
for (i = 0; i < shapes1.length; i++)
shapes1[i].setAppearanceOverrideEnable(override.isSelected());
} else if (target == appMaterialColor) {
mat1.setDiffuseColor(colors[appMaterialColor.getSelectedIndex()]);
}
}
public static void main(String[] args) {
Frame frame = new MainFrame(new AlternateAppearanceBoundsTest(), 800,
800);
}
}
/*
* @(#)SphereGroup.java 1.7 02/04/01 15:04:02
*
* Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed,licensed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
class SphereGroup extends Group {
Shape3D[] shapes;
int numShapes = 0;
// Constructors
public SphereGroup() {
// radius x,y spacing x,y count appearance
this(0.25f, 0.75f, 0.75f, 5, 5, null, false);
}
public SphereGroup(Appearance app) {
// radius x,y spacing x,y count appearance
this(0.25f, 0.75f, 0.75f, 5, 5, app, false);
}
public SphereGroup(float radius, float xSpacing, float ySpacing,
int xCount, int yCount, boolean overrideflag) {
this(radius, xSpacing, ySpacing, xCount, yCount, null, overrideflag);
}
public SphereGroup(float radius, float xSpacing, float ySpacing,
int xCount, int yCount, Appearance app, boolean overrideflag) {
if (app == null) {
app = new Appearance();
Material material = new Material();
material.setDiffuseColor(new Color3f(0.8f, 0.8f, 0.8f));
material.setSpecularColor(new Color3f(0.0f, 0.0f, 0.0f));
material.setShininess(0.0f);
app.setMaterial(material);
}
double xStart = -xSpacing * (double) (xCount - 1) / 2.0;
double yStart = -ySpacing * (double) (yCount - 1) / 2.0;
Sphere sphere = null;
TransformGroup trans = null;
Transform3D t3d = new Transform3D();
Vector3d vec = new Vector3d();
double x, y = yStart, z = 0.0;
shapes = new Shape3D[xCount * yCount];
for (int i = 0; i < yCount; i++) {
x = xStart;
for (int j = 0; j < xCount; j++) {
vec.set(x, y, z);
t3d.setTranslation(vec);
trans = new TransformGroup(t3d);
addChild(trans);
sphere = new Sphere(radius, // sphere radius
Primitive.GENERATE_NORMALS, // generate normals
16, // 16 divisions radially
app); // it"s appearance
trans.addChild(sphere);
x += xSpacing;
shapes[numShapes] = sphere.getShape();
if (overrideflag)
shapes[numShapes]
.setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_WRITE);
numShapes++;
}
y += ySpacing;
}
}
Shape3D[] getShapes() {
return shapes;
}
}
A red sphere using the Sphere utility class
/*
Essential Java 3D Fast
Ian Palmer
Publisher: Springer-Verlag
ISBN: 1-85233-394-4
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.PointLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Sphere;
/**
* This builds a red sphere using the Sphere utility class and adds lights so
* that you can see it shape. It creates a material for the sphere, creates an
* ambient light and a one directional light.
*
* @author I.J.Palmer
* @version 1.0
*/
public class SimpleSphere extends Frame implements ActionListener {
protected Canvas3D myCanvas3D = new Canvas3D(null);
protected Button myButton = new Button("Exit");
/**
* This function builds the view branch of the scene graph. It creates a
* branch group and then creates the necessary view elements to give a
* useful view of our content.
*
* @param c
* Canvas3D that will display the view
* @return BranchGroup that is the root of the view elements
*/
protected BranchGroup buildViewBranch(Canvas3D c) {
BranchGroup viewBranch = new BranchGroup();
Transform3D viewXfm = new Transform3D();
viewXfm.set(new Vector3f(0.0f, 0.0f, 10.0f));
TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
ViewPlatform myViewPlatform = new ViewPlatform();
PhysicalBody myBody = new PhysicalBody();
PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
viewXfmGroup.addChild(myViewPlatform);
viewBranch.addChild(viewXfmGroup);
View myView = new View();
myView.addCanvas3D(c);
myView.attachViewPlatform(myViewPlatform);
myView.setPhysicalBody(myBody);
myView.setPhysicalEnvironment(myEnvironment);
return viewBranch;
}
protected void addLights(BranchGroup b) {
// Create a bounds for the lights
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
// Set up the global lights
Color3f lightColour1 = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f lightDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
Color3f lightColour2 = new Color3f(1.0f, 1.0f, 1.0f);
Point3f lightPosition2 = new Point3f(3.0f, 3.0f, 3.0f);
Point3f lightAtten2 = new Point3f(0.0f, 0.0f, 1.0f);
Vector3f lightDir2 = new Vector3f(-1.0f, -1.0f, -1.0f);
Color3f ambientColour = new Color3f(0.2f, 0.2f, 0.2f);
AmbientLight ambientLight1 = new AmbientLight(ambientColour);
ambientLight1.setInfluencingBounds(bounds);
DirectionalLight light1 = new DirectionalLight(lightColour1, lightDir1);
light1.setInfluencingBounds(bounds);
PointLight light2 = new PointLight(lightColour2, lightPosition2,
lightAtten2);
light2.setInfluencingBounds(bounds);
b.addChild(ambientLight1);
b.addChild(light1);
//b.addChild(light2);
}
protected BranchGroup buildContentBranch() {
BranchGroup contentBranch = new BranchGroup();
Transform3D rotateCube = new Transform3D();
rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0));
// rotateCube.set(new AxisAngle4d(1.0,0.0,0.0,Math.PI/2.0));
TransformGroup rotationGroup = new TransformGroup(rotateCube);
contentBranch.addChild(rotationGroup);
Appearance app = new Appearance();
Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
float shininess = 20.0f;
app.setMaterial(new Material(ambientColour, emissiveColour,
diffuseColour, specularColour, shininess));
rotationGroup.addChild(new Sphere(2.0f, Sphere.GENERATE_NORMALS, 120,
app));
addLights(contentBranch);
return contentBranch;
}
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}
public SimpleSphere() {
VirtualUniverse myUniverse = new VirtualUniverse();
Locale myLocale = new Locale(myUniverse);
myLocale.addBranchGraph(buildViewBranch(myCanvas3D));
myLocale.addBranchGraph(buildContentBranch());
setTitle("SimpleWorld");
setSize(400, 400);
setLayout(new BorderLayout());
add("Center", myCanvas3D);
add("South", myButton);
myButton.addActionListener(this);
setVisible(true);
}
public static void main(String[] args) {
SimpleSphere sw = new SimpleSphere();
}
}
Illustrates how to display a ball lit by a red light
/*
The Joy of Java 3D
by Greg Hopkins
Copyright Copyright 2001
*/
/*
Lighting up the World. The way the light falls on an object provides us with the
shading that helps us see shapes in three dimensions
The next example illustrates how to display a ball lit by a red light:
The sphere we created is white (the default), it appears red because of the colored
light. Since it is a DirectionalLight, we also have to specify how far the light
shines and in what direction. In the example, the light shines for 100 meters from
the origin and the direction is to the right, down and into the screen (this is defined
by the vector: 4.0 right, -7.0 down, and -12.0 into the screen).
You can also create an AmbientLight which will produce a directionless light, or a
SpotLight if you want to focus on a particular part of your scene. A combination of a
strong directional light and a weaker ambient light gives a natural-looking appearance to
your scene. Java 3D lights do not produce shadows.
*/
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class Ball {
public Ball() {
// Create the universe
SimpleUniverse universe = new SimpleUniverse();
// Create a structure to contain objects
BranchGroup group = new BranchGroup();
// Create a ball and add it to the group of objects
Sphere sphere = new Sphere(0.5f);
group.addChild(sphere);
// Create a red light that shines for 100m from the origin
Color3f light1Color = new Color3f(1.8f, 0.1f, 0.1f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
// look towards the ball
universe.getViewingPlatform().setNominalViewingTransform();
// add the group of objects to the Universe
universe.addBranchGraph(group);
}
public static void main(String[] args) {
new Ball();
}
}
Sphere Sample
/*
* LitSphereApp.java 1.0 99/04/12
*
* Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear facility.
* Licensee represents and warrants that it will not use or redistribute the
* Software for such purposes.
*/
/*
*
* This application (or a version of it) generated one or more of the images in
* Chapter 6 of Getting Started with the Java 3D API. The Java 3D Turtorial.
*
* See http://www.sun.ru/desktop/java3d/collateral for more information.
*
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class LitSphereApp extends Applet {
Appearance createAppearance() {
Appearance appear = new Appearance();
Material material = new Material();
// material.setDiffuseColor(0.0f, 0.0f, 1.0f);
material.setShininess(50.0f);
// make modifications to default material properties
appear.setMaterial(material);
// ColoringAttributes colorAtt = new ColoringAttributes();
// colorAtt.setShadeModel(ColoringAttributes.SHADE_FLAT);
// appear.setColoringAttributes(colorAtt);
return appear;
}
BranchGroup createScene() {
BranchGroup scene = new BranchGroup();
scene.addChild(new Sphere(0.9f, Sphere.GENERATE_NORMALS,
createAppearance()));
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
scene.addChild(lightA);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f direction1 = new Vector3f(-1.0f, -1.0f, -0.5f);
direction1.normalize();
lightD1.setDirection(direction1);
lightD1.setColor(new Color3f(0.0f, 0.0f, 1.0f));
scene.addChild(lightD1);
DirectionalLight lightD2 = new DirectionalLight();
lightD2.setInfluencingBounds(new BoundingSphere());
Vector3f direction2 = new Vector3f(1.0f, -1.0f, -0.5f);
direction2.normalize();
lightD2.setDirection(direction2);
lightD2.setColor(new Color3f(1.0f, 0.0f, 0.0f));
scene.addChild(lightD2);
Background bg = new Background();
bg.setColor(1.0f, 1.0f, 1.0f);
bg.setApplicationBounds(new BoundingSphere());
scene.addChild(bg);
return scene;
}
public LitSphereApp() {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
BranchGroup scene = createScene();
scene.rupile();
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String argv[]) {
new MainFrame(new LitSphereApp(), 256, 256);
}
}
The scene is of nine spheres and one light source
/*
* MaterialApp.java 1.0 99/04/13
*
* Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear facility.
* Licensee represents and warrants that it will not use or redistribute the
* Software for such purposes.
*/
/*
* This application illustates differences in setting the material properties to
* different shininess values for visual objects.
*
* The scene is of nine spheres and one light source.
*
* This application (or a version of it) generated one or more of the images in
* Chapter 6 of Getting Started with the Java 3D API. The Java 3D Turtorial.
*
* See http://www.sun.ru/desktop/java3d/collateral for more information.
*
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class MaterialApp extends Applet {
TransformGroup createTG(float x, float y, float z) {
Vector3f position = new Vector3f(x, y, z);
Transform3D translate = new Transform3D();
translate.set(position);
TransformGroup trans1 = new TransformGroup(translate);
return trans1;
}
Appearance createMatAppear(Color3f dColor, Color3f sColor, float shine) {
Appearance appear = new Appearance();
Material material = new Material();
material.setDiffuseColor(dColor);
material.setSpecularColor(sColor);
material.setShininess(shine);
appear.setMaterial(material);
return appear;
}
public MaterialApp() {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
BranchGroup scene = new BranchGroup();
TransformGroup trans11 = createTG(-0.7f, 0.7f, -0.5f);
scene.addChild(trans11);
trans11.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 0.0f)));
TransformGroup trans12 = createTG(0.0f, 0.7f, -0.5f);
scene.addChild(trans12);
trans12.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1.0f)));
TransformGroup trans13 = createTG(0.7f, 0.7f, -0.5f);
scene.addChild(trans13);
trans13.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 2.0f)));
TransformGroup trans21 = createTG(-0.7f, 0.0f, -0.5f);
scene.addChild(trans21);
trans21.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 5.0f)));
TransformGroup trans22 = createTG(0.0f, 0.0f, -0.5f);
scene.addChild(trans22);
trans22.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 10.0f)));
TransformGroup trans23 = createTG(0.7f, 0.0f, -0.5f);
scene.addChild(trans23);
trans23.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 20.0f)));
TransformGroup trans31 = createTG(-0.7f, -0.7f, -0.5f);
scene.addChild(trans31);
trans31.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 50.0f)));
TransformGroup trans32 = createTG(0.0f, -0.7f, -0.5f);
scene.addChild(trans32);
trans32.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 100.0f)));
TransformGroup trans33 = createTG(0.7f, -0.7f, -0.5f);
scene.addChild(trans33);
trans33.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
scene.addChild(lightA);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f direction = new Vector3f(-1.0f, -1.0f, -1.0f);
direction.normalize();
lightD1.setDirection(direction);
lightD1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
scene.addChild(lightD1);
Background background = new Background();
background.setApplicationBounds(new BoundingSphere());
background.setColor(1.0f, 1.0f, 1.0f);
scene.addChild(background);
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
// setLocalEyeViewing
u.getViewer().getView().setLocalEyeLightingEnable(true);
u.addBranchGraph(scene);
}
public static void main(String argv[]) {
new MainFrame(new MaterialApp(), 256, 256);
}
}