1 /*****************************************************************************
2 * Virtual Mockup for Machine Vision
3 * Copyright (C) 2001-2003 Fabio R. de Miranda, Joćo E. Kogler Jr.,
4 * Carlos S. Santos.
5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP
6 *
7 * Permission is granted to redistribute and/or modify this
8 * software under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html)
16 * for more details.
17 *
18 *****************************************************************************/
19
20 package camera3d.test;
21
22 import camera3d.HelpingGrid;
23 import javax.media.j3d.*;
24 import javax.vecmath.*;
25 import java.util.Enumeration;
26 import java.awt.*;
27 import javax.swing.JFrame;
28 import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
29 import com.sun.j3d.utils.universe.SimpleUniverse;
30 import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
31 import com.sun.j3d.utils.picking.*;
32 import com.sun.j3d.utils.geometry.*;
33
34 public class TransparencyTester2 {
35
36 private Node node;
37 private TransformGroup tGroup;
38 private Canvas3D canvas3d;
39 private BranchGroup bg;
40
41 private TransparencyAttributes opaque = new TransparencyAttributes();
42 private TransparencyAttributes transparent = new TransparencyAttributes(TransparencyAttributes.FASTEST,0.7f);
43
44 /* subclass of PickBehavior which changes transparency attributes of picked
45 geometries */
46 private TransparencyPickBehavior pickBeh;
47
48 /***
49 *
50 */
51 public TransparencyTester2() {
52 assembleGraph();
53 addGeometry(-5.0f,0.0f,-20.0f);
54 addGeometry(-2.0f,0.0f,-20.0f);
55 addGeometry(2.0f,0.0f,-20.0f);
56 addGeometry(5.0f,0.0f,-20.0f);
57 System.out.println("Transparency Tester:\nClick on a ColorCube to change its TransparecyAttributes");
58 }
59
60
61 /***
62 * Creates the java 3D basic structure.
63 */
64 public void assembleGraph() {
65 JFrame frame = new JFrame();
66 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
67
68 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
69 canvas3d = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration( template ));
70 SimpleUniverse su = new SimpleUniverse(canvas3d);
71 su.getViewingPlatform().setNominalViewingTransform();
72
73 tGroup = new TransformGroup();
74 tGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);
75 tGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
76 tGroup.setCapability(Node.ENABLE_PICK_REPORTING);
77 tGroup.setCapability(Node.ALLOW_BOUNDS_READ);
78 tGroup.setPickable(true);
79
80 Transform3D tempT3D = new Transform3D();
81 tempT3D.set(new Vector3d(-10.0 ,0.0 , 10.0));
82
83 bg = new BranchGroup();
84 bg.setCapability(Group.ALLOW_CHILDREN_READ);
85 bg.setCapability(Group.ALLOW_CHILDREN_WRITE);
86 bg.setCapability(Group.ALLOW_CHILDREN_EXTEND);
87 bg.setCapability(Group.ENABLE_PICK_REPORTING);
88 bg.setCapability(Node.ALLOW_BOUNDS_READ);
89 bg.setCapability(BranchGroup.ALLOW_DETACH);
90 bg.setPickable(true);
91
92 TransformGroup vpTrans = su.getViewingPlatform().getViewPlatformTransform();
93 KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(vpTrans);
94 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),1000);
95 Background back = new Background(new Color3f(0.5f, 0.5f, 0.5f));
96 back.setApplicationBounds(bounds);
97 bg.addChild(back);
98
99 keynav.setSchedulingBounds(bounds);
100 tGroup.addChild(keynav);
101 Color3f white = new Color3f(1.0f,1.0f,1.0f);
102 Point3f center = new Point3f(0.0f, 0.0f, 0.0f);
103 Point3f attenuation = new Point3f(0.1f,0.1f,0.1f);
104 PointLight light = new PointLight(true, white, center, attenuation);
105 tGroup.addChild(light);
106 frame.getContentPane().add(canvas3d);
107 frame.setSize(300,300);
108 frame.setVisible(true);
109 bg.addChild(tGroup);
110
111 BoundingSphere pBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10000.0);
112 pickBeh = new TransparencyPickBehavior(canvas3d,bg,pBounds);
113 pickBeh.setSchedulingBounds(pBounds);
114 bg.addChild(pickBeh);
115
116 su.getLocale().addBranchGraph(bg);
117 }
118
119
120
121 void addGeometry(float x, float y, float z){
122 Shape3D shape = (Shape3D) new ColorCube(1.0f);
123 shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
124 shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
125 shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
126 shape.setCapability(Node.ALLOW_BOUNDS_READ);
127 shape.setCapability(Shape3D.ALLOW_COLLISION_BOUNDS_READ);
128 shape.setCapability(Node.ENABLE_PICK_REPORTING);
129 Enumeration enum = shape.getAllGeometries();
130 while(enum.hasMoreElements()){
131 ((Geometry)enum.nextElement()).setCapability(Geometry.ALLOW_INTERSECT);
132 }
133 shape.setPickable(true);
134 TransformGroup tg = new TransformGroup();
135 tg.setCapability(Node.ENABLE_PICK_REPORTING);
136 tg.setCapability(Node.ALLOW_BOUNDS_READ);
137 tg.setCapability(Group.ALLOW_CHILDREN_READ);
138 tg.setCapability(Group.ALLOW_COLLISION_BOUNDS_READ);
139 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
140 tg.setPickable(true);
141 Transform3D t3d = new Transform3D();
142 t3d.setTranslation(new Vector3f(x,y,z));
143 tg.setTransform(t3d);
144 tg.addChild(shape);
145 this.add(tg);
146
147 Appearance app = new Appearance();
148 app.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
149 app.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
150 app.setTransparencyAttributes(opaque);
151 shape.setAppearance(app);
152 }
153
154 /***
155 * Adds a node to the scene graph
156 */
157 public void add(Node n){
158 BranchGroup bG = new BranchGroup();
159 bG.addChild(n);
160 tGroup.addChild(bG);
161 }
162
163
164 public static void main(String[] args) {
165 TransparencyTester2 transparencyTester1 = new TransparencyTester2();
166 }
167
168 /* Pick behavior that simply changes the TransparencyAttributes of the picked geometry */
169 class TransparencyPickBehavior extends PickMouseBehavior{
170 public TransparencyPickBehavior(Canvas3D canvas, BranchGroup rootBG, Bounds bounds){
171 super(canvas,rootBG,bounds);
172 }
173
174 public void initialize(){
175 super.initialize();
176 }
177
178 public void updateScene(int xpos, int ypos){
179 pickCanvas.setShapeLocation(xpos, ypos);
180 System.out.println("Updating scene");
181 PickResult pr;
182 try{
183 pr = pickCanvas.pickClosest();
184 }catch(CapabilityNotSetException ex){
185 ex.printStackTrace();
186 return;
187 }
188 if(pr == null) return;
189 Shape3D shape = (Shape3D) pr.getNode(PickResult.SHAPE3D);
190 if(shape == null) return;
191 /* */
192 TransparencyAttributes att = shape.getAppearance().getTransparencyAttributes();
193 if(att == opaque){
194 shape.getAppearance().setTransparencyAttributes(transparent);
195 }else{
196 shape.getAppearance().setTransparencyAttributes(opaque);
197 }
198 }
199 }
200
201 }
This page was automatically generated by Maven