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/package-summary.html">> camera3d;
21
22 import javax.media.j3d.Shape3D;
23 import javax.media.j3d.*;
24 import com.sun.j3d.*;
25 import com.sun.j3d.utils.universe.*;
26 import com.sun.j3d.utils.behaviors.keyboard.*;
27 import javax.swing.*;
28 import javax.vecmath.*;
29 import java.awt.*;
30
31 /***
32 * Emulates a cursor in the 3D world.
33 *
34 * @author Fabio Roberto de Miranda
35 * @version 1.0
36 */
37 class Cursor3D extends Shape3D {
38 private LineArray lineArray;
39 private byte[] colorArray = new byte[3];
40 private float[][] coordinates = {{0,0,1},
41 {0,0,-1},
42 {0,1,0},
43 {0,-1,0},
44 {1,0,0},
45 {-1,0,0},
46 {-0.5f,0.0f,-0.5f},
47 {0.5f,0.0f,-0.5f},
48 {0.5f,0.0f,-0.5f},
49 {0.5f,0.0f,0.5f},
50 {0.5f,0.0f,0.5f},
51 {-0.5f,0.0f,0.5f},
52 {-0.5f,0.0f,0.5f},
53 {-0.5f,0.0f,-0.5f},
54 {0.5f,0.5f,0.0f},
55 {0.5f,-0.5f,0.0f},
56 {0.5f,-0.5f,0.0f},
57 {-0.5f,-0.5f,0.0f},
58 {-0.5f,-0.5f,0.0f},
59 {-0.5f,0.5f,0.0f},
60 {-0.5f,0.5f,0.0f},
61 { 0.5f,0.5f,0.0f}
62 };
63
64 private Color3f blue;
65 private Appearance appearance;
66 private LineAttributes lineAttributes;
67
68
69 Cursor3D() {
70 super();
71 lineArray = new LineArray(coordinates.length, LineArray.COORDINATES| LineArray.COLOR_3);
72 colorArray[0] = (byte)180;
73 colorArray[1] = (byte)180;
74 colorArray[2] = (byte)254;
75
76 // blue = new Color3f(0.0f, 0.4f, 1.0f);
77 blue = new Color3f(0.0f, 0.0f, 0.0f);
78 appearance = new Appearance();
79 lineAttributes = new LineAttributes();
80 lineAttributes.setLineWidth(5);
81 appearance.setLineAttributes(lineAttributes);
82
83 for (int i=0; i<coordinates.length; i++){
84 //lineArray.setColor(i, colorArray);
85 lineArray.setColor(i, blue);
86 lineArray.setCoordinate(i, coordinates[i]);
87 }
88
89 lineArray.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
90 lineArray.setCapability(GeometryArray.ALLOW_COUNT_READ);
91
92
93 this.setGeometry(lineArray);
94 this.setAppearance(appearance);
95 /*
96 this.setCapability(Shape3D.ALLOW_GEOMETRY_READ
97 |Shape3D.ALLOW_GEOMETRY_WRITE
98 |Shape3D.ALLOW_APPEARANCE_READ
99 |Shape3D.ALLOW_GEOMETRY_WRITE);
100 */
101 this.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
102 this.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
103 this.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
104 this.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
105 this.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
106
107 }
108
109 /***
110 * Sets line width of cursor geometry.
111 */
112 void setLineWidth(float width){
113 this.getAppearance().getLineAttributes().setLineWidth(width);
114 }
115
116 /***
117 * Sets color of cursor geometry.
118 */
119 void setColor(byte red, byte green, byte blue){
120 colorArray[0] = red;
121 colorArray[1] = green;
122 colorArray[2] = blue;
123 for (int i=0; i<6; i++){
124 lineArray.setColor(i, colorArray);
125 }
126
127 }
128
129 /***
130 * Tests the behavior of this class.
131 */
132 public static void main(String[] args) {
133 JFrame frame = new JFrame();
134 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
135 Canvas3D canvas3d = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration( template ));
136 SimpleUniverse su = new SimpleUniverse(canvas3d);
137 TransformGroup tGroup = new TransformGroup();
138 Cursor3D cursor = new Cursor3D();
139 tGroup.addChild(cursor);
140 BranchGroup bg = new BranchGroup();
141 TransformGroup vpTrans = su.getViewingPlatform().getViewPlatformTransform();
142 KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(vpTrans);
143 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),1000);
144 keynav.setSchedulingBounds(bounds);
145 tGroup.addChild(keynav);
146 Color3f white = new Color3f(1.0f,1.0f,1.0f);
147 Point3f center = new Point3f(0.0f, 0.0f, 0.0f);
148 Point3f attenuation = new Point3f(0.1f,0.1f,0.1f);
149 PointLight light = new PointLight(true, white, center, attenuation);
150 tGroup.addChild(light);
151
152 frame.getContentPane().add(canvas3d);
153 frame.setSize(300,300);
154 frame.setVisible(true);
155 bg.addChild(tGroup);
156 su.getLocale().addBranchGraph(bg);
157
158 }
159
160
161 }
This page was automatically generated by Maven