Hi, guru,
As the subject indicates, the display is not updated
when the Material.setLightingEnable() is called unless
it is forced to refresh the display, for example, rotate
the object or iconize the frame. The following piece
of code illustrates the problem. Can anybody know why?
Thanks,
Dongming Zhang
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.*;
public class LightRefresh extends Applet implements ActionListener {
Material ma;
public void actionPerformed (ActionEvent e) {
ma.setLightingEnable (!(ma.getLightingEnable()));
}
private BranchGroup createSceneGraph () {
BranchGroup objRoot = new BranchGroup ();
BoundingSphere bounds = new BoundingSphere (new Point3d (0, 0, 0),
100.0);
TransformGroup objTrans = new TransformGroup ();
objTrans.setCapability (TransformGroup.ALLOW_TRANSFORM_READ);
objTrans.setCapability (TransformGroup.ALLOW_TRANSFORM_WRITE);
MouseRotate rotation = new MouseRotate ();
rotation.setTransformGroup (objTrans);
objTrans.addChild (rotation);
rotation.setSchedulingBounds (bounds);
objRoot.addChild (objTrans);
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
//Vector3f lDir1 = new Vector3f(-1.0f, -1.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);
TransformGroup tg = new TransformGroup ();
Shape3D obj = createObject ();
tg.addChild (obj);
objTrans.addChild (tg);
objRoot.compile ();
return objRoot;
}
Shape3D createObject () {
float[] verts = {
-0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.5f,
0.5f, 0.0f, 0.0f };
float[] colors = {
1.0f, .0f, 1.0f,
1.0f, .0f, 1.0f,
1.0f, .0f, 1.0f,
1.0f, .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_3);
geom.setCoordinates (0, verts);
geom.setColors (0, colors);
geom.setNormals (0, normals);
Appearance app = new Appearance ();
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);
Shape3D shape = new Shape3D (geom,app);
return shape;
}
public LightRefresh () {
setLayout (new BorderLayout ());
Canvas3D 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 lighting flag in material");
b.addActionListener (this);
p.add (b);
return p;
}
public static void main (String[] args) {
new MainFrame (new LightRefresh (), 500, 500);
}
}
------------------------------------------
Dongming Zhang
Interactive Network Technologies, Inc.
Phone: 713-975-7434 Fax: 713-975-1120
http://www.int.com
Email: [EMAIL PROTECTED]
------------------------------------------
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/