001: /*
002: * $RCSfile: BackgroundGeometry.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.6 $
041: * $Date: 2007/04/24 18:55:58 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.background;
046:
047: import com.sun.j3d.utils.image.TextureLoader;
048: import com.sun.j3d.utils.behaviors.mouse.*;
049: import com.sun.j3d.utils.geometry.*;
050: import com.sun.j3d.utils.universe.*;
051: import javax.media.j3d.*;
052: import javax.vecmath.*;
053: import java.awt.GraphicsConfiguration;
054: import org.jdesktop.j3d.examples.Resources;
055:
056: public class BackgroundGeometry extends javax.swing.JFrame {
057:
058: private SimpleUniverse univ = null;
059: private BranchGroup scene = null;
060: private java.net.URL bgImage = null;
061: private BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
062: 0.0, 0.0), 100.0);
063:
064: public BranchGroup createSceneGraph() {
065:
066: // Create the root of the branch graph
067: BranchGroup objRoot = new BranchGroup();
068:
069: // Create a Transformgroup to scale all objects so they
070: // appear in the scene.
071: TransformGroup objScale = new TransformGroup();
072: Transform3D t3d = new Transform3D();
073: t3d.setScale(0.4);
074: objScale.setTransform(t3d);
075: objRoot.addChild(objScale);
076:
077: // Create the transform group node and initialize it to the
078: // identity. Enable the TRANSFORM_WRITE capability so that
079: // our behavior code can modify it at runtime.
080: TransformGroup objTrans = new TransformGroup();
081: objScale.addChild(objTrans);
082:
083: Background bg = new Background();
084: bg.setApplicationBounds(bounds);
085: BranchGroup backGeoBranch = new BranchGroup();
086: Sphere sphereObj = new Sphere(1.0f, Sphere.GENERATE_NORMALS
087: | Sphere.GENERATE_NORMALS_INWARD
088: | Sphere.GENERATE_TEXTURE_COORDS
089: | Sphere.GENERATE_TEXTURE_COORDS_Y_UP, 45);
090: Appearance backgroundApp = sphereObj.getAppearance();
091: backGeoBranch.addChild(sphereObj);
092: bg.setGeometry(backGeoBranch);
093: objTrans.addChild(bg);
094:
095: TextureLoader tex = new TextureLoader(bgImage,
096: new String("RGB"), TextureLoader.BY_REFERENCE
097: | TextureLoader.Y_UP, this );
098: if (tex != null)
099: backgroundApp.setTexture(tex.getTexture());
100:
101: Vector3f tranlation = new Vector3f(2.0f, 0.0f, 0.0f);
102: Transform3D modelTransform = new Transform3D();
103: Transform3D tmpTransform = new Transform3D();
104: double angleInc = Math.PI / 8.0;
105: double angle = 0.0;
106: int numBoxes = 16;
107:
108: float scaleX[] = { 0.1f, 0.2f, 0.2f, 0.3f, 0.2f, 0.1f, 0.2f,
109: 0.3f, 0.1f, 0.3f, 0.2f, 0.3f, 0.1f, 0.3f, 0.2f, 0.3f };
110:
111: float scaleY[] = { 0.3f, 0.4f, 0.3f, 0.4f, 0.3f, 0.4f, 0.3f,
112: 0.4f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 0.4f };
113:
114: float scaleZ[] = { 0.3f, 0.2f, 0.1f, 0.1f, 0.3f, 0.2f, 0.1f,
115: 0.3f, 0.3f, 0.2f, 0.1f, 0.3f, 0.3f, 0.2f, 0.1f, 0.2f };
116:
117: Appearance a1 = new Appearance();
118: Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
119: Color3f sColor = new Color3f(0.5f, 0.5f, 1.0f);
120: Color3f oColor = new Color3f(0.5f, 0.5f, 0.3f);
121:
122: Material m = new Material(oColor, eColor, oColor, sColor,
123: 100.0f);
124: m.setLightingEnable(true);
125: a1.setMaterial(m);
126:
127: for (int i = 0; i < numBoxes; i++, angle += angleInc) {
128: modelTransform.rotY(angle);
129: tmpTransform.set(tranlation);
130: modelTransform.mul(tmpTransform);
131:
132: TransformGroup tgroup = new TransformGroup(modelTransform);
133: objTrans.addChild(tgroup);
134:
135: tgroup.addChild(new Box(scaleX[i], scaleY[i], scaleZ[i],
136: Box.GENERATE_NORMALS, a1));
137: }
138:
139: // Shine it with two lights.
140: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
141: Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
142: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
143: Vector3f lDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
144: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
145: DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
146: lgt1.setInfluencingBounds(bounds);
147: lgt2.setInfluencingBounds(bounds);
148: objScale.addChild(lgt1);
149: objScale.addChild(lgt2);
150:
151: return objRoot;
152: }
153:
154: private Canvas3D createUniverse() {
155: // Get the preferred graphics configuration for the default screen
156: GraphicsConfiguration config = SimpleUniverse
157: .getPreferredConfiguration();
158:
159: // Create a Canvas3D using the preferred configuration
160: Canvas3D c = new Canvas3D(config);
161:
162: // Create simple universe with view branch
163: univ = new SimpleUniverse(c);
164:
165: // This will move the ViewPlatform back a bit so the
166: // objects in the scene can be viewed.
167: univ.getViewingPlatform().setNominalViewingTransform();
168:
169: // Ensure at least 5 msec per frame (i.e., < 200Hz)
170: univ.getViewer().getView().setMinimumFrameCycleTime(5);
171:
172: TransformGroup viewTrans = univ.getViewingPlatform()
173: .getViewPlatformTransform();
174:
175: // Create the rotate behavior node
176: MouseRotate behavior1 = new MouseRotate(viewTrans);
177: scene.addChild(behavior1);
178: behavior1.setSchedulingBounds(bounds);
179:
180: // Create the zoom behavior node
181: MouseZoom behavior2 = new MouseZoom(viewTrans);
182: scene.addChild(behavior2);
183: behavior2.setSchedulingBounds(bounds);
184:
185: // Create the translate behavior node
186: MouseTranslate behavior3 = new MouseTranslate(viewTrans);
187: scene.addChild(behavior3);
188: behavior3.setSchedulingBounds(bounds);
189:
190: return c;
191: }
192:
193: /**
194: * Creates new form BackgroundGeometry
195: */
196: public BackgroundGeometry() {
197:
198: if (bgImage == null) {
199: // the path to the image for an applet
200: bgImage = Resources.getResource("resources/images/bg.jpg");
201: if (bgImage == null) {
202: System.err.println("resources/images/bg.jpg not found");
203: System.exit(1);
204: }
205: }
206:
207: // Initialize the GUI components
208: initComponents();
209:
210: // Create the content branch and add it to the universe
211: scene = createSceneGraph();
212:
213: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
214: Canvas3D c = createUniverse();
215: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
216:
217: // Let Java 3D perform optimizations on this scene graph.
218: scene.compile();
219:
220: univ.addBranchGraph(scene);
221: }
222:
223: // ----------------------------------------------------------------
224:
225: /** This method is called from within the constructor to
226: * initialize the form.
227: * WARNING: Do NOT modify this code. The content of this method is
228: * always regenerated by the Form Editor.
229: */
230: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
231: private void initComponents() {
232: drawingPanel = new javax.swing.JPanel();
233:
234: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
235: setTitle("BackgroundGeometry");
236: drawingPanel.setLayout(new java.awt.BorderLayout());
237:
238: drawingPanel.setOpaque(false);
239: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
240: getContentPane()
241: .add(drawingPanel, java.awt.BorderLayout.CENTER);
242:
243: pack();
244: }// </editor-fold>//GEN-END:initComponents
245:
246: /**
247: * @param args the command line arguments
248: */
249: public static void main(String args[]) {
250: java.awt.EventQueue.invokeLater(new Runnable() {
251: public void run() {
252: new BackgroundGeometry().setVisible(true);
253: }
254: });
255: }
256:
257: // Variables declaration - do not modify//GEN-BEGIN:variables
258: private javax.swing.JPanel drawingPanel;
259: // End of variables declaration//GEN-END:variables
260:
261: }
|