001: /*
002: * $RCSfile: IntersectTest.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:48 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.picking;
046:
047: import java.applet.Applet;
048: import java.awt.BorderLayout;
049: import java.awt.event.*;
050: import java.awt.GraphicsConfiguration;
051: import com.sun.j3d.utils.applet.MainFrame;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import com.sun.j3d.utils.behaviors.keyboard.*;
056:
057: public class IntersectTest extends Applet {
058:
059: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
060: 0.0), 1000.0);
061:
062: private SimpleUniverse u = null;
063:
064: public BranchGroup createSceneGraph() {
065:
066: // Create the root of the branch graph
067: BranchGroup objRoot = new BranchGroup();
068:
069: // Set up the ambient light
070: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
071: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
072: ambientLightNode.setInfluencingBounds(bounds);
073: objRoot.addChild(ambientLightNode);
074:
075: // Set up the directional lights
076: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
077: Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
078: Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
079: Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);
080:
081: DirectionalLight light1 = new DirectionalLight(light1Color,
082: light1Direction);
083: light1.setInfluencingBounds(bounds);
084: objRoot.addChild(light1);
085:
086: DirectionalLight light2 = new DirectionalLight(light2Color,
087: light2Direction);
088: light2.setInfluencingBounds(bounds);
089: objRoot.addChild(light2);
090:
091: Transform3D t3 = new Transform3D();
092:
093: // Shapes
094: for (int x = 0; x < 3; x++) {
095: for (int y = 0; y < 3; y++) {
096: for (int z = 0; z < 3; z++) {
097: t3.setTranslation(new Vector3d(-4 + x * 4.0, -4 + y
098: * 4.0, -20 - z * 4.0));
099: TransformGroup objTrans = new TransformGroup(t3);
100:
101: objRoot.addChild(objTrans);
102:
103: // Create a simple shape leaf node, add it to the scene graph.
104: GeometryArray geom = null;
105:
106: if (((x + y + z) % 2) == 0) {
107: geom = new RandomColorCube();
108: } else {
109: geom = new RandomColorTetrahedron();
110: }
111:
112: Shape3D shape = new Shape3D(geom);
113:
114: objTrans.addChild(shape);
115: }
116: }
117: }
118:
119: // Lines
120: Point3f[] verts = { new Point3f(-2.0f, 0.0f, 0.0f),
121: new Point3f(2.0f, 0.0f, 0.0f) };
122: Color3f grey = new Color3f(0.7f, 0.7f, 0.7f);
123: Color3f[] colors = { grey, grey };
124:
125: for (int y = 0; y < 5; y++) {
126: for (int z = 0; z < 5; z++) {
127: t3.setTranslation(new Vector3d(7.0, -4 + y * 2.0, -20.0
128: - z * 2.0));
129: TransformGroup objTrans = new TransformGroup(t3);
130:
131: objRoot.addChild(objTrans);
132:
133: LineArray la = new LineArray(verts.length,
134: LineArray.COORDINATES | LineArray.COLOR_3);
135: la.setCoordinates(0, verts);
136: la.setColors(0, colors);
137:
138: Shape3D shape = new Shape3D();
139: shape.setGeometry(la);
140:
141: objTrans.addChild(shape);
142: }
143: }
144:
145: // Points
146: for (double x = -2.0; x <= 2.0; x += 1.0) {
147: for (double y = -2.0; y <= 2.0; y += 1.0) {
148: for (double z = -2.0; z <= 2.0; z += 1.0) {
149: t3.setTranslation(new Vector3d(-10.0 + 2.0 * x,
150: 0.0 + 2.0 * y, -20.0 + 2.0 * z));
151: TransformGroup objTrans = new TransformGroup(t3);
152:
153: objRoot.addChild(objTrans);
154:
155: PointArray pa = new PointArray(1,
156: PointArray.COORDINATES | PointArray.COLOR_3);
157:
158: pa.setCoordinate(0, new Point3d(0.0, 0.0, 0.0));
159: pa.setColor(0, grey);
160:
161: Shape3D shape = new Shape3D();
162: shape.setGeometry(pa);
163:
164: objTrans.addChild(shape);
165: }
166: }
167: }
168:
169: return objRoot;
170: }
171:
172: public IntersectTest() {
173: }
174:
175: public void init() {
176: setLayout(new BorderLayout());
177:
178: GraphicsConfiguration config = SimpleUniverse
179: .getPreferredConfiguration();
180:
181: Canvas3D c = new Canvas3D(config);
182: add("Center", c);
183:
184: // Create a simple scene and attach it to the virtual universe
185: BranchGroup scene = createSceneGraph();
186: u = new SimpleUniverse(c);
187:
188: // Add picking behavior
189: IntersectInfoBehavior behavior = new IntersectInfoBehavior(c,
190: scene, 0.05f);
191: behavior.setSchedulingBounds(bounds);
192: scene.addChild(behavior);
193:
194: TransformGroup vpTrans = u.getViewingPlatform()
195: .getViewPlatformTransform();
196:
197: KeyNavigatorBehavior keybehavior = new KeyNavigatorBehavior(
198: vpTrans);
199: keybehavior.setSchedulingBounds(bounds);
200: scene.addChild(keybehavior);
201: scene.setCapability(Group.ALLOW_CHILDREN_EXTEND);
202: scene.compile();
203: u.addBranchGraph(scene);
204:
205: View view = u.getViewer().getView();
206: view.setBackClipDistance(100000);
207:
208: }
209:
210: public void destroy() {
211: u.cleanup();
212: }
213:
214: //
215: // The following allows IntersectTest to be run as an application
216: // as well as an applet
217: //
218: public static void main(String[] args) {
219: String s = "\n\nIntersectTest:\n-----------\n";
220: s += "Pick with the mouse over the primitives\n";
221: s += "- A sphere will be placed to indicate the picked point.\n";
222: s += "If color information is available, the sphere will change color to reflect\n";
223: s += "the interpolated color.\n";
224: s += "- Other spheres will be placed to show the vertices of the selected polygon\n";
225: s += "- Information will be displayed about the picking operation\n\n\n";
226:
227: System.out.println(s);
228:
229: new MainFrame(new IntersectTest(), 640, 640);
230: }
231: }
|