|
Hi,
Like Falk Doberman I am having some problems
using transparency.
My program attempts to display a spinning ball
in transparent red and green. I was hoping that I would be able to see a nice 3D
ball with my red/green specs.
Instead I get a mostly green ball with a red
shadow and some cool-looking but unintended disc-shaped shadows appearing and
disappearing within it. I don't understand why.
Could anyone be kind enough to point out where
I'm going wrong?
Greg.
import java.applet.Applet;
import java.awt.BorderLayout; import java.awt.*; import java.awt.event.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.image.TextureLoader; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior; public class Ball extends Applet { TransformGroup vpTrans = null; private BranchGroup
createSceneGraph() {
// Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create a bounds for the background and lights BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); // Set up
the global lights
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f); Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); AmbientLight aLgt = new
AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objRoot.addChild(aLgt); objRoot.addChild(lgt1); // Create objects with a behavior and add them // into the scene graph. TransformGroup spinTg = new TransformGroup(); spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 5000, 0, 0, 0, 0, 0); Alpha rotationAlpha2 = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 5000, 0, 0, 0, 0, 0); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, spinTg, yAxis, 0.0f, (float) Math.PI*2.0f); BoundingSphere bounds2 = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); rotator.setSchedulingBounds(bounds); // TransformGroup spinTg2 = new TransformGroup(); spinTg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); RotationInterpolator rotator2 = new RotationInterpolator(rotationAlpha2, spinTg2, yAxis, 0.0f, (float) Math.PI*2.0f); rotator2.setSchedulingBounds(bounds2); Appearance appearance = createAppearance(); Sphere sphere = new Sphere(0.5f, appearance); Appearance greenAppearance =
createAppearance2();
Sphere greenSphere = new Sphere(0.5f, greenAppearance); Transform3D t = new Transform3D(); t.set(1, new Vector3d(-0.07, 0.0, 0.0)); TransformGroup objTrans = new TransformGroup(t); objTrans.addChild(rotator); objTrans.addChild(spinTg); spinTg.addChild(greenSphere); TransformGroup objTrans2 = new TransformGroup(); objTrans2.addChild(rotator2); objTrans2.addChild(spinTg2); spinTg2.addChild(sphere); objRoot.addChild(objTrans); objRoot.addChild(objTrans2); //objRoot.addChild(sphere);
return objRoot; } private Appearance createAppearance() { Appearance app = new Appearance(); // Set up the transparency
properties
TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparencyMode(ta.BLENDED); ta.setTransparency(0.6f); app.setTransparencyAttributes(ta); // Set up the polygon
attributes
PolygonAttributes pa = new PolygonAttributes(); pa.setCullFace(pa.CULL_NONE); app.setPolygonAttributes(pa); // Set up the material
properties
Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f objColor = new Color3f(1.0f, 0.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, black, 1.0f)); return app;
} private Appearance createAppearance2() { Appearance app = new Appearance(); // Set up the transparency
properties
TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparencyMode(ta.BLENDED); ta.setTransparency(0.6f); app.setTransparencyAttributes(ta); // Set up the polygon
attributes
PolygonAttributes pa = new PolygonAttributes(); pa.setCullFace(pa.CULL_NONE); app.setPolygonAttributes(pa); // Set up the material
properties
Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f objColor = new Color3f(0.0f, 1.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, black, 1.0f)); return app; } public Ball() {
setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c); // Create a simple scene and attach it to
the virtual universe
BranchGroup scene = createSceneGraph(); 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(); vpTrans = u.getViewingPlatform().getViewPlatformTransform(); KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans); keyNavBeh.setSchedulingBounds(new BoundingSphere( new Point3d(),1000.0)); scene.addChild(keyNavBeh); u.addBranchGraph(scene);
} // // The following allows Ball to be run as an application // as well as an applet // public static void main(String[] args) { new MainFrame(new Ball(), 700, 700); } } |
