001: /*
002: * $RCSfile: TransparencyOrderedGroupTest.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:16:59 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.transparency;
046:
047: import com.sun.j3d.utils.geometry.Box;
048: import com.sun.j3d.utils.scenegraph.transparency.TransparencySortController;
049: import com.sun.j3d.utils.universe.*;
050: import com.sun.j3d.utils.geometry.ColorCube;
051: import java.awt.event.ActionEvent;
052: import java.awt.event.ActionListener;
053: import javax.media.j3d.*;
054: import javax.swing.event.ChangeEvent;
055: import javax.swing.event.ChangeListener;
056: import javax.vecmath.*;
057: import java.awt.GraphicsConfiguration;
058: import org.jdesktop.j3d.utils.scenegraph.transparency.TransparencyOrderController;
059: import org.jdesktop.j3d.utils.scenegraph.transparency.TransparencyOrderedGroup;
060:
061: /**
062: */
063: public class TransparencyOrderedGroupTest extends javax.swing.JFrame {
064:
065: private SimpleUniverse univ = null;
066: private BranchGroup scene = null;
067:
068: public BranchGroup createSceneGraph() {
069: // Create the root of the branch graph
070: BranchGroup objRoot = new BranchGroup();
071:
072: objRoot.addChild(createTest2());
073:
074: return objRoot;
075: }
076:
077: // TOG addChild, insertChild and removeChild tests (non visual)
078: private Group createTest3() {
079: BranchGroup bg = new BranchGroup();
080:
081: TransparencyOrderedGroup tog = new TransparencyOrderedGroup();
082: tog.setCapability(Group.ALLOW_CHILDREN_EXTEND);
083: tog.setCapability(Group.ALLOW_CHILDREN_WRITE);
084: bg.addChild(tog);
085:
086: tog.addChild(createShape(0f, 0f, 0f, 1f, null, "c1"));
087: tog.addChild(createShape(0f, 0f, 0f, 1f, null, "c2"));
088: tog.addChild(createShape(0f, 0f, 0f, 1f, null, "c3"));
089: tog.addChild(createShape(0f, 0f, 0f, 1f, null, "c4"));
090:
091: tog.setChildIndexOrder(new int[] { 3, 2, 1, 0 });
092: univ.addBranchGraph(bg);
093:
094: tog.addChild(createShape(0f, 0f, 0f, 1f, null, "i1"));
095:
096: {
097: int[] io = tog.getChildIndexOrder();
098: for (int i = 0; i < io.length; i++)
099: System.out.println(io[i] + " "
100: + tog.getChild(i).getName());
101: }
102:
103: // tog.removeChild(0);
104: // {
105: // int[] io = tog.getChildIndexOrder();
106: // for(int i=0; i<io.length; i++)
107: // System.out.println(io[i]+" "+tog.getChild(i).getName());
108: // }
109:
110: // tog.removeChild(3);
111: // {
112: // int[] io = tog.getChildIndexOrder();
113: // for(int i=0; i<io.length; i++)
114: // System.out.println(io[i]+" "+tog.getChild(i).getName());
115: // }
116: //
117: // tog.removeChild(0);
118: // {
119: // int[] io = tog.getChildIndexOrder();
120: // for(int i=0; i<io.length; i++)
121: // System.out.println(io[i]+" "+tog.getChild(i).getName());
122: // }
123:
124: return null;
125: }
126:
127: // Rendering test
128: private Group createTest2() {
129: BranchGroup bg = new BranchGroup();
130: BranchGroup bg2 = new BranchGroup();
131:
132: Appearance app = new Appearance();
133: final TransparencyAttributes transparencyAttr = new TransparencyAttributes();
134: transparencyAttr.setTransparency(jSlider1.getValue() / 100.0f);
135: transparencyAttr
136: .setTransparencyMode(TransparencyAttributes.FASTEST);
137: transparencyAttr
138: .setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
139: app.setTransparencyAttributes(transparencyAttr);
140:
141: jSlider1.addChangeListener(new ChangeListener() {
142: public void stateChanged(ChangeEvent e) {
143: transparencyAttr
144: .setTransparency(jSlider1.getValue() / 100.0f);
145: }
146:
147: });
148:
149: Appearance app2 = new Appearance();
150: final TransparencyAttributes transparencyAttr2 = new TransparencyAttributes();
151: transparencyAttr2.setTransparency(0.5f);
152: transparencyAttr2
153: .setTransparencyMode(TransparencyAttributes.FASTEST);
154: transparencyAttr2
155: .setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
156: app2.setTransparencyAttributes(transparencyAttr2);
157: ColoringAttributes colorAttr = new ColoringAttributes();
158: colorAttr.setColor(1f, 0f, 0f);
159: app2.setColoringAttributes(colorAttr);
160:
161: jSlider2.addChangeListener(new ChangeListener() {
162: public void stateChanged(ChangeEvent e) {
163: transparencyAttr2
164: .setTransparency(jSlider2.getValue() / 100.0f);
165: }
166:
167: });
168:
169: Appearance app3 = new Appearance();
170: ColoringAttributes colorAttr2 = new ColoringAttributes();
171: colorAttr2.setColor(0f, 1f, 0f);
172: app3.setColoringAttributes(colorAttr2);
173:
174: Appearance app4 = new Appearance();
175: final TransparencyAttributes transparencyAttr3 = new TransparencyAttributes();
176: transparencyAttr3.setTransparency(0.5f);
177: transparencyAttr3
178: .setTransparencyMode(TransparencyAttributes.FASTEST);
179: transparencyAttr3
180: .setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
181: app4.setTransparencyAttributes(transparencyAttr3);
182: ColoringAttributes colorAttr3 = new ColoringAttributes();
183: colorAttr3.setColor(1f, 1f, 0f);
184: app4.setColoringAttributes(colorAttr3);
185:
186: bg2
187: .addChild(createShape(0.3f, 0.3f, 0.5f, 0.1f, app2,
188: "right"));
189: bg2
190: .addChild(createShape(-0.3f, 0.3f, 0.5f, 0.1f, app2,
191: "left"));
192:
193: bg2
194: .addChild(createShape(0.3f, -0.3f, 0.5f, 0.1f, app3,
195: "solid"));
196:
197: TransparencyOrderedGroup tog2 = new TransparencyOrderedGroup();
198: tog2
199: .addChild(createShape(0.39f, 0.3f, 0.55f, 0.05f, app4,
200: null));
201: tog2.addChild(bg2);
202:
203: final TransparencyOrderedGroup tog = new TransparencyOrderedGroup();
204: tog.addChild(tog2); // Rendered First (behind)
205: tog.addChild(createShape(0f, 0f, 0f, 0.6f, app, "BigBox")); // Rendered Second (in front)
206:
207: tog.setChildIndexOrder(new int[] { 0, 1 });
208: jCheckBox1.addActionListener(new ActionListener() {
209: public void actionPerformed(ActionEvent e) {
210: int[] order = tog.getChildIndexOrder();
211: int tmp = order[0];
212: order[0] = order[1];
213: order[1] = tmp;
214: tog.setChildIndexOrder(order);
215: }
216: });
217:
218: bg.addChild(tog);
219:
220: // Background box
221: Appearance bApp = new Appearance();
222: ColoringAttributes bClr = new ColoringAttributes();
223: bClr.setColor(0f, 0f, 1f);
224: bApp.setColoringAttributes(bClr);
225: bg.addChild(createShape(0f, 0f, -1f, 0.6f, bApp, "background"));
226:
227: return bg;
228: }
229:
230: private Node createShape(float x, float y, float z, float size,
231: Appearance app, String name) {
232: BranchGroup bg = new BranchGroup();
233: bg.setName("bg-" + name);
234: bg.setCapability(BranchGroup.ALLOW_DETACH);
235: Transform3D t3d = new Transform3D();
236: t3d.setTranslation(new Vector3f(x, y, z));
237: TransformGroup tg = new TransformGroup(t3d);
238: tg.setName("tg-" + name);
239: //tg.addChild(new Box(size,size,size, app));
240:
241: TriangleStripArray geom = new TriangleStripArray(4,
242: TriangleStripArray.COORDINATES, new int[] { 4 });
243: float s = size;
244: geom.setCoordinates(0, new float[] { -s, -s, 0f, s, -s, 0f, -s,
245: s, 0f, s, s, 0f });
246:
247: Shape3D shape = new Shape3D(geom, app);
248: shape.setName(name);
249:
250: tg.addChild(shape);
251: bg.addChild(tg);
252:
253: return bg;
254: }
255:
256: private Group createTest1() {
257: TransparencyOrderedGroup tog1 = new TransparencyOrderedGroup();
258: tog1.setName("tog1");
259:
260: TransparencyOrderedGroup tog2 = new TransparencyOrderedGroup();
261: tog2.setName("tog2");
262: BranchGroup bg = new BranchGroup();
263:
264: Shape3D s1 = new Shape3D();
265: s1.setName("s1");
266: Shape3D s2 = new Shape3D();
267: s2.setName("s2");
268: Shape3D s3 = new Shape3D();
269: s3.setName("s3");
270: Shape3D s4 = new Shape3D();
271: s4.setName("s4");
272:
273: BranchGroup bg2 = new BranchGroup();
274:
275: bg2.addChild(s1);
276: tog2.addChild(bg2);
277: tog2.addChild(s2);
278: tog1.addChild(s3);
279: tog1.addChild(bg);
280: tog1.addChild(tog2);
281: bg.addChild(s4);
282:
283: return tog1;
284: }
285:
286: private Canvas3D createUniverse() {
287: // Get the preferred graphics configuration for the default screen
288: GraphicsConfiguration config = SimpleUniverse
289: .getPreferredConfiguration();
290:
291: // Create a Canvas3D using the preferred configuration
292: Canvas3D c = new Canvas3D(config);
293:
294: // Create simple universe with view branch
295: univ = new SimpleUniverse(c);
296:
297: // This will move the ViewPlatform back a bit so the
298: // objects in the scene can be viewed.
299: univ.getViewingPlatform().setNominalViewingTransform();
300:
301: // Ensure at least 5 msec per frame (i.e., < 200Hz)
302: univ.getViewer().getView().setMinimumFrameCycleTime(5);
303:
304: univ.addBranchGraph(new TransparencyOrderController(univ
305: .getViewer().getView()));
306:
307: return c;
308: }
309:
310: /**
311: * Creates new form HelloUniverse
312: */
313: public TransparencyOrderedGroupTest() {
314: // Initialize the GUI components
315: initComponents();
316:
317: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
318: Canvas3D c = createUniverse();
319: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
320:
321: // Create the content branch and add it to the universe
322: scene = createSceneGraph();
323: univ.addBranchGraph(scene);
324: }
325:
326: // ----------------------------------------------------------------
327:
328: /** This method is called from within the constructor to
329: * initialize the form.
330: * WARNING: Do NOT modify this code. The content of this method is
331: * always regenerated by the Form Editor.
332: */
333: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
334: private void initComponents() {
335: java.awt.GridBagConstraints gridBagConstraints;
336:
337: drawingPanel = new javax.swing.JPanel();
338: jPanel1 = new javax.swing.JPanel();
339: jLabel1 = new javax.swing.JLabel();
340: jSlider1 = new javax.swing.JSlider();
341: jLabel2 = new javax.swing.JLabel();
342: jSlider2 = new javax.swing.JSlider();
343: jCheckBox1 = new javax.swing.JCheckBox();
344:
345: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
346: setTitle("HelloUniverse");
347: drawingPanel.setLayout(new java.awt.BorderLayout());
348:
349: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
350: jPanel1.setLayout(new java.awt.GridBagLayout());
351:
352: jLabel1.setText("White Transparency");
353: gridBagConstraints = new java.awt.GridBagConstraints();
354: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
355: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
356: jPanel1.add(jLabel1, gridBagConstraints);
357:
358: jSlider1.setValue(20);
359: jPanel1.add(jSlider1, new java.awt.GridBagConstraints());
360:
361: jLabel2.setText("Red Transparency");
362: gridBagConstraints = new java.awt.GridBagConstraints();
363: gridBagConstraints.gridx = 0;
364: gridBagConstraints.gridy = 1;
365: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
366: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
367: jPanel1.add(jLabel2, gridBagConstraints);
368:
369: gridBagConstraints = new java.awt.GridBagConstraints();
370: gridBagConstraints.gridx = 1;
371: gridBagConstraints.gridy = 1;
372: jPanel1.add(jSlider2, gridBagConstraints);
373:
374: jCheckBox1.setSelected(true);
375: jCheckBox1.setText("White Quad In Front");
376: jCheckBox1.setBorder(javax.swing.BorderFactory
377: .createEmptyBorder(0, 0, 0, 0));
378: jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));
379: gridBagConstraints = new java.awt.GridBagConstraints();
380: gridBagConstraints.gridx = 1;
381: gridBagConstraints.gridy = 2;
382: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
383: jPanel1.add(jCheckBox1, gridBagConstraints);
384:
385: drawingPanel.add(jPanel1, java.awt.BorderLayout.SOUTH);
386:
387: getContentPane()
388: .add(drawingPanel, java.awt.BorderLayout.CENTER);
389:
390: pack();
391: }// </editor-fold>//GEN-END:initComponents
392:
393: /**
394: * @param args the command line arguments
395: */
396: public static void main(String args[]) {
397: java.awt.EventQueue.invokeLater(new Runnable() {
398: public void run() {
399: new TransparencyOrderedGroupTest().setVisible(true);
400: }
401: });
402: }
403:
404: // Variables declaration - do not modify//GEN-BEGIN:variables
405: private javax.swing.JPanel drawingPanel;
406: private javax.swing.JCheckBox jCheckBox1;
407: private javax.swing.JLabel jLabel1;
408: private javax.swing.JLabel jLabel2;
409: private javax.swing.JPanel jPanel1;
410: private javax.swing.JSlider jSlider1;
411: private javax.swing.JSlider jSlider2;
412: // End of variables declaration//GEN-END:variables
413:
414: }
|