001: /*
002: * $RCSfile: AlternateAppearanceBoundsTest.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:31 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.alternate_appearance;
046:
047: import java.applet.Applet;
048: import java.awt.*;
049: import java.awt.event.*;
050: import com.sun.j3d.utils.applet.MainFrame;
051: import com.sun.j3d.utils.geometry.*;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import javax.swing.*;
056: import javax.swing.event.*;
057: import javax.swing.border.*;
058: import com.sun.j3d.utils.behaviors.mouse.*;
059:
060: public class AlternateAppearanceBoundsTest extends JApplet implements
061: ActionListener {
062:
063: Material mat1, altMat;
064: Appearance app, otherApp;
065: JComboBox altAppMaterialColor;
066: JComboBox appMaterialColor;
067: JCheckBox useBoundingLeaf;
068: JCheckBox override;
069: JComboBox boundsType;
070: private Group content1 = null;
071: AlternateAppearance altApp;
072: Shape3D[] shapes1;
073: boolean boundingLeafOn = false;
074: // Globally used colors
075: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
076: Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
077: Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
078: Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
079: Color3f[] colors = { white, red, green, blue };
080:
081: private Bounds worldBounds = new BoundingSphere(new Point3d(0.0,
082: 0.0, 0.0), // Center
083: 1000.0); // Extent
084: private Bounds smallBounds = new BoundingSphere(new Point3d(0.0,
085: 0.0, 0.0), // Center
086: 0.25); // Extent
087: private Bounds tinyBounds = new BoundingSphere(new Point3d(0.0,
088: 0.0, 0.0), // Center
089: 0.05); // Extent
090: private BoundingLeaf leafBounds = null;
091: private int currentBounds = 2;
092:
093: private Bounds[] allBounds = { tinyBounds, smallBounds, worldBounds };
094:
095: DirectionalLight light1 = null;
096:
097: // Get the current bounding leaf position
098: private int currentPosition = 0;
099: // Point3f pos = (Point3f)positions[currentPosition].value;
100:
101: private SimpleUniverse u = null;
102:
103: public AlternateAppearanceBoundsTest() {
104: }
105:
106: public void init() {
107: Container contentPane = getContentPane();
108:
109: Canvas3D c = new Canvas3D(SimpleUniverse
110: .getPreferredConfiguration());
111: contentPane.add("Center", c);
112:
113: BranchGroup scene = createSceneGraph();
114: // SimpleUniverse is a Convenience Utility class
115: u = new SimpleUniverse(c);
116:
117: // This will move the ViewPlatform back a bit so the
118: // objects in the scene can be viewed.
119: u.getViewingPlatform().setNominalViewingTransform();
120: u.addBranchGraph(scene);
121:
122: // Create GUI
123: JPanel p = new JPanel();
124: BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
125: p.add(createBoundsPanel());
126: p.add(createMaterialPanel());
127: p.setLayout(boxlayout);
128:
129: contentPane.add("South", p);
130: }
131:
132: public void destroy() {
133: u.cleanup();
134: }
135:
136: BranchGroup createSceneGraph() {
137: BranchGroup objRoot = new BranchGroup();
138:
139: // Create an alternate appearance
140: otherApp = new Appearance();
141: altMat = new Material();
142: altMat.setCapability(Material.ALLOW_COMPONENT_WRITE);
143: altMat.setDiffuseColor(new Color3f(0.0f, 1.0f, 0.0f));
144: otherApp.setMaterial(altMat);
145:
146: altApp = new AlternateAppearance();
147: altApp.setAppearance(otherApp);
148: altApp.setCapability(AlternateAppearance.ALLOW_BOUNDS_WRITE);
149: altApp
150: .setCapability(AlternateAppearance.ALLOW_INFLUENCING_BOUNDS_WRITE);
151: altApp.setInfluencingBounds(worldBounds);
152: objRoot.addChild(altApp);
153:
154: // Build foreground geometry
155: Appearance app1 = new Appearance();
156: mat1 = new Material();
157: mat1.setCapability(Material.ALLOW_COMPONENT_WRITE);
158: mat1.setDiffuseColor(new Color3f(1.0f, 0.0f, 0.0f));
159: app1.setMaterial(mat1);
160: content1 = new SphereGroup(0.05f, // radius of spheres
161: 0.15f, // x spacing
162: 0.15f, // y spacing
163: 5, // number of spheres in X
164: 5, // number of spheres in Y
165: app1, // appearance
166: true); // alt app override = true
167: objRoot.addChild(content1);
168: shapes1 = ((SphereGroup) content1).getShapes();
169:
170: // Add lights
171: light1 = new DirectionalLight();
172: light1.setEnable(true);
173: light1.setColor(new Color3f(0.2f, 0.2f, 0.2f));
174: light1.setDirection(new Vector3f(1.0f, 0.0f, -1.0f));
175: light1.setInfluencingBounds(worldBounds);
176: light1
177: .setCapability(DirectionalLight.ALLOW_INFLUENCING_BOUNDS_WRITE);
178: light1.setCapability(DirectionalLight.ALLOW_BOUNDS_WRITE);
179: objRoot.addChild(light1);
180:
181: // Add an ambient light to dimly illuminate the rest of
182: // the shapes in the scene to help illustrate that the
183: // directional lights are being scoped... otherwise it looks
184: // like we're just removing shapes from the scene
185: AmbientLight ambient = new AmbientLight();
186: ambient.setEnable(true);
187: ambient.setColor(new Color3f(1.0f, 1.0f, 1.0f));
188: ambient.setInfluencingBounds(worldBounds);
189: objRoot.addChild(ambient);
190:
191: // Define a bounding leaf
192: leafBounds = new BoundingLeaf(allBounds[currentBounds]);
193: leafBounds.setCapability(BoundingLeaf.ALLOW_REGION_WRITE);
194: objRoot.addChild(leafBounds);
195: if (boundingLeafOn) {
196: altApp.setInfluencingBoundingLeaf(leafBounds);
197: } else {
198: altApp.setInfluencingBounds(allBounds[currentBounds]);
199: }
200:
201: return objRoot;
202: }
203:
204: JPanel createBoundsPanel() {
205: JPanel panel = new JPanel();
206: panel.setBorder(new TitledBorder("Scopes"));
207:
208: String boundsValues[] = { "Tiny Bounds", "Small Bounds",
209: "Big Bounds" };
210:
211: boundsType = new JComboBox(boundsValues);
212: boundsType.addActionListener(this );
213: boundsType.setSelectedIndex(2);
214: panel.add(new JLabel("Bounds"));
215: panel.add(boundsType);
216:
217: useBoundingLeaf = new JCheckBox("Enable BoundingLeaf",
218: boundingLeafOn);
219: useBoundingLeaf.addActionListener(this );
220: panel.add(useBoundingLeaf);
221:
222: override = new JCheckBox("Enable App Override", false);
223: override.addActionListener(this );
224: panel.add(override);
225:
226: return panel;
227:
228: }
229:
230: JPanel createMaterialPanel() {
231: JPanel panel = new JPanel();
232: panel.setBorder(new TitledBorder("Appearance Attributes"));
233:
234: String colorVals[] = { "WHITE", "RED", "GREEN", "BLUE" };
235:
236: altAppMaterialColor = new JComboBox(colorVals);
237: altAppMaterialColor.addActionListener(this );
238: altAppMaterialColor.setSelectedIndex(2);
239: panel.add(new JLabel("Alternate Appearance MaterialColor"));
240: panel.add(altAppMaterialColor);
241:
242: appMaterialColor = new JComboBox(colorVals);
243: appMaterialColor.addActionListener(this );
244: appMaterialColor.setSelectedIndex(1);
245: panel.add(new JLabel("Normal Appearance MaterialColor"));
246: panel.add(appMaterialColor);
247:
248: return panel;
249:
250: }
251:
252: public void actionPerformed(ActionEvent e) {
253: int i;
254:
255: Object target = e.getSource();
256: if (target == altAppMaterialColor) {
257: altMat.setDiffuseColor(colors[altAppMaterialColor
258: .getSelectedIndex()]);
259: } else if (target == useBoundingLeaf) {
260: boundingLeafOn = useBoundingLeaf.isSelected();
261: if (boundingLeafOn) {
262: leafBounds.setRegion(allBounds[currentBounds]);
263: altApp.setInfluencingBoundingLeaf(leafBounds);
264: } else {
265: altApp.setInfluencingBoundingLeaf(null);
266: altApp.setInfluencingBounds(allBounds[currentBounds]);
267: }
268:
269: } else if (target == boundsType) {
270: currentBounds = boundsType.getSelectedIndex();
271: if (boundingLeafOn) {
272: leafBounds.setRegion(allBounds[currentBounds]);
273: altApp.setInfluencingBoundingLeaf(leafBounds);
274: } else {
275: altApp.setInfluencingBoundingLeaf(null);
276: altApp.setInfluencingBounds(allBounds[currentBounds]);
277: }
278:
279: } else if (target == override) {
280: for (i = 0; i < shapes1.length; i++)
281: shapes1[i].setAppearanceOverrideEnable(override
282: .isSelected());
283: } else if (target == appMaterialColor) {
284: mat1.setDiffuseColor(colors[appMaterialColor
285: .getSelectedIndex()]);
286: }
287:
288: }
289:
290: public static void main(String[] args) {
291: Frame frame = new MainFrame(
292: new AlternateAppearanceBoundsTest(), 800, 800);
293: }
294:
295: }
|