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;
21
22 import javax.media.j3d.*;
23 import com.sun.j3d.*;
24 import com.sun.j3d.utils.universe.*;
25 import com.sun.j3d.utils.behaviors.keyboard.*;
26 import javax.swing.*;
27 import javax.vecmath.*;
28 import java.awt.*;
29
30 /***
31 * This class assembles an 1-unit spaced grid, intended to be used as a helper.
32 * The grid covers by now negative z and positive x space.
33 *
34 * @author Fábio Roberto de Miranda
35 */
36 public class HelpingGrid extends TransformGroup {
37
38 Shape3D shape;
39 LineArray lineArray;
40 int xGridLines = 100;
41 int zGridLines = 100;
42 float xUnit = 1;
43 float zUnit = 1;
44 double[][] xCoordinates;
45 double[][] zCoordinates;
46 Color3f gridColor;
47
48 public HelpingGrid(Transform3D t, int xGridLines, int zGridLines, Color3f color){
49 this(t, xGridLines, zGridLines);
50 this.gridColor = color;
51 }
52
53 public HelpingGrid(Transform3D t, int xGridLines, int zGridLines) {
54 super(t);
55 this.xGridLines = xGridLines;
56 this.zGridLines = zGridLines;
57 shape = new Shape3D();
58 xCoordinates = new double[2*xGridLines][];
59 zCoordinates = new double[2*zGridLines][];
60 lineArray = new LineArray((2*xGridLines + 2*zGridLines), LineArray.COORDINATES| LineArray.COLOR_3);
61 gridColor = new Color3f(0.0f,0.0f,0.0f);
62
63 for (int i=0; i<xCoordinates.length; i++){
64 xCoordinates[i] = new double[3];
65 }
66 for (int i=0; i<zCoordinates.length; i++){
67 zCoordinates[i] = new double[3];
68 }
69
70 for (int i=0;i<(xGridLines);i=i+2) {
71 xCoordinates[i][0] = xUnit*i;
72 xCoordinates[i][1] = 0.0;
73 xCoordinates[i][2] = 0.0;
74 xCoordinates[i+1][0] = xUnit*i;
75 xCoordinates[i+1][1] = 0.0;
76 xCoordinates[i+1][2] = -xUnit*zGridLines;
77 }
78
79 for (int i=0;i<(zGridLines);i=i+2) {
80 zCoordinates[i][0] = 0.0;
81 zCoordinates[i][1] = 0.0;
82 zCoordinates[i][2] = -zUnit*i;
83 zCoordinates[i+1][0] = zUnit*xGridLines;
84 zCoordinates[i+1][1] = 0.0;
85 zCoordinates[i+1][2] = -zUnit*i;
86 }
87 for (int i=0;i<xCoordinates.length;i++ ) {
88 lineArray.setCoordinate(i, xCoordinates[i]);
89 lineArray.setColor(i, gridColor);
90 }
91 for (int i=xCoordinates.length; i <(xCoordinates.length + zCoordinates.length); i++){
92 lineArray.setCoordinate(i, zCoordinates[i-xCoordinates.length]);
93 lineArray.setColor(i, gridColor);
94 }
95 shape.setGeometry(lineArray);
96 this.addChild(shape);
97
98 this.setPickable(false);
99 this.clearCapability(Node.ENABLE_PICK_REPORTING);
100 }
101
102 public HelpingGrid(Transform3D t) {
103 super(t);
104 shape = new Shape3D();
105 xCoordinates = new double[2*xGridLines][];
106 zCoordinates = new double[2*zGridLines][];
107 lineArray = new LineArray((2*xGridLines + 2*zGridLines), LineArray.COORDINATES| LineArray.COLOR_3);
108 Color3f gridColor = new Color3f(0.0f,0.0f,0.0f);
109
110 for (int i=0; i<xCoordinates.length; i++){
111 xCoordinates[i] = new double[3];
112 }
113 for (int i=0; i<zCoordinates.length; i++){
114 zCoordinates[i] = new double[3];
115 }
116
117 for (int i=0;i<(xGridLines);i=i+2) {
118 xCoordinates[i][0] = xUnit*i;
119 xCoordinates[i][1] = 0.0;
120 xCoordinates[i][2] = 0.0;
121 xCoordinates[i+1][0] = xUnit*i;
122 xCoordinates[i+1][1] = 0.0;
123 xCoordinates[i+1][2] = -xUnit*zGridLines;
124 }
125
126 for (int i=0;i<(zGridLines);i=i+2) {
127 zCoordinates[i][0] = 0.0;
128 zCoordinates[i][1] = 0.0;
129 zCoordinates[i][2] = -zUnit*i;
130 zCoordinates[i+1][0] = zUnit*xGridLines;
131 zCoordinates[i+1][1] = 0.0;
132 zCoordinates[i+1][2] = -zUnit*i;
133 }
134 for (int i=0;i<xCoordinates.length;i++ ) {
135 lineArray.setCoordinate(i, xCoordinates[i]);
136 lineArray.setColor(i, gridColor);
137 }
138 for (int i=xCoordinates.length; i <(xCoordinates.length + zCoordinates.length); i++){
139 lineArray.setCoordinate(i, zCoordinates[i-xCoordinates.length]);
140 lineArray.setColor(i, gridColor);
141 }
142 shape.setGeometry(lineArray);
143
144 Appearance appearance = new Appearance();
145 /*
146 * Linhas comentadas porque a grid estava aparecendo na frente de alguns objetos de que não deveria aparecer
147 * 26/04/2002 - Fábio Miranda
148 TransparencyAttributes transparencyAttributes = new TransparencyAttributes(TransparencyAttributes.FASTEST, 0.3f);
149 appearance.setTransparencyAttributes(transparencyAttributes);
150 */
151 shape.setAppearance(appearance);
152
153 this.setPickable(false);
154 this.clearCapability(Node.ENABLE_PICK_REPORTING);
155
156 this.addChild(shape);
157
158 }
159
160 public static void main(String[] args) {
161 JFrame frame = new JFrame();
162 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
163 Canvas3D canvas3d = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration( template ));
164 SimpleUniverse su = new SimpleUniverse(canvas3d);
165 TransformGroup tGroup = new TransformGroup();
166 Transform3D t3D = new Transform3D();
167 HelpingGrid grid = new HelpingGrid(t3D);
168 tGroup.addChild(grid);
169 BranchGroup bg = new BranchGroup();
170 TransformGroup vpTrans = su.getViewingPlatform().getViewPlatformTransform();
171 KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(vpTrans);
172 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),1000);
173 keynav.setSchedulingBounds(bounds);
174 tGroup.addChild(keynav);
175 Color3f white = new Color3f(1.0f,1.0f,1.0f);
176 Point3f center = new Point3f(0.0f, 0.0f, 0.0f);
177 Point3f attenuation = new Point3f(0.1f,0.1f,0.1f);
178 PointLight light = new PointLight(true, white, center, attenuation);
179 tGroup.addChild(light);
180 //Background background = new Background(1.0f, 1.0f, 1.0f);
181 //background.setApplicationBounds(bounds);
182 //bg.addChild(background);
183 /*
184 vpTrans.setCapability(Group.ALLOW_CHILDREN_READ);
185 vpTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);
186 vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
187 vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
188 */
189 frame.getContentPane().add(canvas3d);
190 frame.setSize(300,300);
191 frame.setVisible(true);
192 bg.addChild(tGroup);
193 su.getLocale().addBranchGraph(bg);
194
195 }
196
197 }
This page was automatically generated by Maven