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 TransparencyTester {
35
36 private Node node;
37 public TransformGroup tGroup;
38 private Canvas3D canvas3d;
39 private BranchGroup bg;
40
41 /* subclass of PickBehavior which changes transparency attributes of picked
42 geometries */
43 private TransparencyPickBehavior pickBeh;
44
45 /***
46 *
47 */
48 public TransparencyTester() {
49 assembleGraph();
50 addGeometry(7.0f,0.0f,-20.0f);
51 addGeometry(-3.0f,0.0f,-13.0f);
52 addGeometry(-5.0f,0.0f,-17.0f);
53 addGeometry(10.0f,0.0f,-21.0f);
54 System.out.println("Transparency Tester:\nClick on a ColorCube to change its TransparecyAttributes");
55 }
56
57
58 /***
59 * Creates the java 3D basic structure.
60 */
61 public void assembleGraph() {
62 JFrame frame = new JFrame();
63 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
64
65 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
66 canvas3d = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration( template ));
67 SimpleUniverse su = new SimpleUniverse(canvas3d);
68 su.getViewingPlatform().setNominalViewingTransform();
69
70 tGroup = new TransformGroup();
71 tGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);
72 tGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
73 tGroup.setCapability(Node.ENABLE_PICK_REPORTING);
74 tGroup.setCapability(Node.ALLOW_BOUNDS_READ);
75 tGroup.setPickable(true);
76
77 Transform3D tempT3D = new Transform3D();
78 tempT3D.set(new Vector3d(-10.0 ,0.0 , 10.0));
79
80 bg = new BranchGroup();
81 bg.setCapability(Group.ALLOW_CHILDREN_READ);
82 bg.setCapability(Group.ALLOW_CHILDREN_WRITE);
83 bg.setCapability(Group.ALLOW_CHILDREN_EXTEND);
84 bg.setCapability(Group.ENABLE_PICK_REPORTING);
85 bg.setCapability(Node.ALLOW_BOUNDS_READ);
86 bg.setCapability(BranchGroup.ALLOW_DETACH);
87 bg.setPickable(true);
88
89 TransformGroup vpTrans = su.getViewingPlatform().getViewPlatformTransform();
90 KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(vpTrans);
91 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),1000);
92 Background back = new Background(new Color3f(0.5f, 0.5f, 0.5f));
93 back.setApplicationBounds(bounds);
94 bg.addChild(back);
95
96 keynav.setSchedulingBounds(bounds);
97 tGroup.addChild(keynav);
98 Color3f white = new Color3f(1.0f,1.0f,1.0f);
99 Point3f center = new Point3f(0.0f, 0.0f, 0.0f);
100 Point3f attenuation = new Point3f(0.1f,0.1f,0.1f);
101 PointLight light = new PointLight(true, white, center, attenuation);
102 tGroup.addChild(light);
103 frame.getContentPane().add(canvas3d);
104 frame.setSize(300,300);
105 frame.setVisible(true);
106 bg.addChild(tGroup);
107
108 BoundingSphere pBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10000.0);
109 pickBeh = new TransparencyPickBehavior(canvas3d,bg,pBounds);
110 pickBeh.setSchedulingBounds(pBounds);
111 bg.addChild(pickBeh);
112
113 su.getLocale().addBranchGraph(bg);
114 }
115
116
117
118 void addGeometry(float x, float y, float z){
119 Shape3D shape = (Shape3D) new ColorCube(1.0f);
120 shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
121 shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
122 shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
123 shape.setCapability(Node.ALLOW_BOUNDS_READ);
124 shape.setCapability(Shape3D.ALLOW_COLLISION_BOUNDS_READ);
125 shape.setCapability(Node.ENABLE_PICK_REPORTING);
126 Enumeration enum = shape.getAllGeometries();
127 while(enum.hasMoreElements()){
128 ((Geometry)enum.nextElement()).setCapability(Geometry.ALLOW_INTERSECT);
129 }
130 shape.setPickable(true);
131 TransformGroup tg = new TransformGroup();
132 tg.setCapability(Node.ENABLE_PICK_REPORTING);
133 tg.setCapability(Node.ALLOW_BOUNDS_READ);
134 tg.setCapability(Group.ALLOW_CHILDREN_READ);
135 tg.setCapability(Group.ALLOW_COLLISION_BOUNDS_READ);
136 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
137 tg.setPickable(true);
138 Transform3D t3d = new Transform3D();
139 t3d.setTranslation(new Vector3f(x,y,z));
140 tg.setTransform(t3d);
141 tg.addChild(shape);
142 this.add(tg);
143 TransparencyAttributes transparencyAttributes = new TransparencyAttributes(TransparencyAttributes.NONE, 0.8f);
144 transparencyAttributes.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
145 transparencyAttributes.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
146 transparencyAttributes.setCapability(TransparencyAttributes.ALLOW_VALUE_READ);
147 transparencyAttributes.setCapability(TransparencyAttributes.ALLOW_MODE_READ);
148 Appearance app = new Appearance();
149 app.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
150 app.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
151 app.setTransparencyAttributes(transparencyAttributes);
152 shape.setAppearance(app);
153 }
154
155 /***
156 * Adds a node to the scene graph
157 */
158 public void add(Node n){
159 BranchGroup bG = new BranchGroup();
160 bG.addChild(n);
161 tGroup.addChild(bG);
162 }
163
164
165 public static void main(String[] args) {
166 TransparencyTester transparencyTester1 = new TransparencyTester();
167 }
168
169 /* Pick behavior that simply changes the TransparencyAttributes of the picked geometry */
170 class TransparencyPickBehavior extends PickMouseBehavior{
171 public TransparencyPickBehavior(Canvas3D canvas, BranchGroup rootBG, Bounds bounds){
172 super(canvas,rootBG,bounds);
173 }
174
175 public void initialize(){
176 super.initialize();
177 }
178
179 public void updateScene(int xpos, int ypos){
180 pickCanvas.setShapeLocation(xpos, ypos);
181 System.out.println("Updating scene");
182 PickResult pr;
183 try{
184 pr = pickCanvas.pickClosest();
185 }catch(CapabilityNotSetException ex){
186 ex.printStackTrace();
187 return;
188 }
189 if(pr == null) return;
190 Shape3D shape = (Shape3D) pr.getNode(PickResult.SHAPE3D);
191 if(shape == null) return;
192 /* */
193 TransparencyAttributes att = shape.getAppearance().getTransparencyAttributes();
194 int tMode = att.getTransparencyMode();
195 if(tMode == TransparencyAttributes.NONE){
196 att.setTransparencyMode(TransparencyAttributes.FASTEST);
197 att.setTransparency(0.5f);
198 }else{
199 att.setTransparencyMode(TransparencyAttributes.NONE);
200 att.setTransparency(0.0f);
201 }
202 }
203 }
204
205 }
This page was automatically generated by Maven