Hi, Java3D guru,
I want to display a Text2D object in front of a transparent
Shape3D object ( I have to set the transparency mode in order to
change its transparency dynamically).
In order to see the text2d is geometrically in front of the
shape3d object, I call setDepthBufferFreezeTransparent(true) on canvas3d.
Although this method call brings the text2d to the front,
another problem is raised, as such the rectangle for mapping Text2D object
is displayed. Is there any way to remove this disgusting rectangle or am I
missing something in the following example code?
Any suggestions are welcome,
thanks,
Dongming Zhang
------------ code example ----------------------
/**
* When the program is started, the text2d object is displayed
* behind the quad object, but its geometry is closer to the viewer.
* If the button is pressed, the setDepthBufferFreezeTransparent is called,
* and the text2d is brought to the front. Further press on the button,
* will flip the text2d object to the back.
*
*
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.interactive.v3d.shape.*;
import com.interactive.v3d.*;
public class Text2DAndTransparency extends Applet implements ActionListener
{
Canvas3D c;
BoundingSphere bounds = new BoundingSphere (new Point3d (0, 0, 0), 100.0);
public void actionPerformed (ActionEvent e) {
c.getView().setDepthBufferFreezeTransparent (
!(c.getView().getDepthBufferFreezeTransparent()));
}
private void enableMouse (TransformGroup objTrans) {
MouseRotate rotation = new MouseRotate ();
rotation.setTransformGroup (objTrans);
objTrans.addChild (rotation);
rotation.setSchedulingBounds (bounds);
MouseZoom zoom = new MouseZoom ();
zoom.setTransformGroup (objTrans);
objTrans.addChild (zoom);
zoom.setSchedulingBounds (bounds);
MouseTranslate trans = new MouseTranslate ();
trans.setTransformGroup (objTrans);
objTrans.addChild (trans);
trans.setSchedulingBounds (bounds);
}
private void enableLight (BranchGroup objRoot) {
Color3f lColor1 = new Color3f(1.0f, 1.0f, 1.0f);
//Vector3f lDir1 = new Vector3f(0.0f, 0.0f, -1.0f);
Vector3f lDir1 = new Vector3f(0.0f, 0.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);
}
private BranchGroup createSceneGraph () {
BranchGroup objRoot = new BranchGroup ();
TransformGroup objTrans = new TransformGroup ();
Transform3D t3d = new Transform3D();
t3d.setScale (0.4f);
objTrans.setTransform (t3d);
objTrans.setCapability (TransformGroup.ALLOW_TRANSFORM_READ);
objTrans.setCapability (TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild (objTrans);
enableMouse(objTrans);
enableLight(objRoot);
// create a quad object and move it to the center
TransformGroup tg = new TransformGroup ();
Shape3D obj = createObject ();
tg.addChild (obj);
objTrans.addChild (tg);
// create text 2d object and position it properly
Text2D text = new Text2D ("RED PLAIN TEXT",
new Color3f (1.0f, 0.0f, 0.0f),
"Serif",
80,
Font.PLAIN);
Transform3D txt3d = new Transform3D();
txt3d.setTranslation (new Vector3f (-0.6f, -0.1f, 0.5@f));
TransformGroup txtTG = new TransformGroup (txt3d);
txtTG.addChild (text);
objTrans.addChild (txtTG);
objRoot.compile ();
return objRoot;
}
public Text2DAndTransparency () {
setLayout (new BorderLayout ());
c = new Canvas3D (null);
add ("Center", c);
add ("South", createControls());
BranchGroup scene = createSceneGraph ();
SimpleUniverse u = new SimpleUniverse (c);
u.getViewingPlatform().setNominalViewingTransform ();
u.addBranchGraph (scene);
}
Panel createControls () {
Panel p = new Panel ();
Button b = new Button ("flip the DepthBufferFreezeTransparency on
Canvas");
b.addActionListener (this);
p.add (b);
return p;
}
public static void main (String[] args) {
Frame f = new Frame ();
f.add (new Text2DAndTransparency());
f.setSize (500, 500);
f.show ();
f.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent E) {
System.exit (0);
}
});
}
Shape3D createObject () {
float[] verts = {
-0.5f, -.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, .5f, 0.0f };
float[] colors = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f };
float[] normals = {
0.4f, 0.1f, 1.0f,
0.0f, 0.3f, 1.0f,
1.0f, 0.5f, 1.0f,
0.8f, 0.0f, .7f };
QuadArray geom = new QuadArray (4, QuadArray.COORDINATES |
QuadArray.NORMALS |
QuadArray.COLOR_4);
geom.setCoordinates (0, verts);
geom.setColors (0, colors);
geom.setNormals (0, normals);
Appearance app = new Appearance ();
app.setCapability
(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
app.setCapability
(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
Material ma = app.getMaterial ();
if (ma == null)
ma = new Material ();
ma.setLightingEnable (true);
ma.setCapability
(Material.ALLOW_COMPONENT_READ);
ma.setCapability
(Material.ALLOW_COMPONENT_WRITE);
if (app.getMaterial () == null)
app.setMaterial (ma);
TransparencyAttributes ta = new TransparencyAttributes();
ta.setTransparency (0.0f);
ta.setTransparencyMode (TransparencyAttributes.FASTEST);
ta.setCapability (TransparencyAttributes.ALLOW_VALUE_READ);
ta.setCapability (TransparencyAttributes.ALLOW_VALUE_WRITE);
app.setTransparencyAttributes (ta);
app.setCapability
(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
app.setCapability
(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
Shape3D shape = new Shape3D (geom,app);
shape.setCapability (Shape3D.ALLOW_APPEARANCE_READ);
shape.setCapability (Shape3D.ALLOW_APPEARANCE_WRITE);
return shape;
}
}
------------------------------------------
Dongming Zhang
Interactive Network Technologies, Inc.
Phone: 713-975-7434 Fax: 713-975-1120
http://www.int.com
Email: [EMAIL PROTECTED]
------------------------------------------