001: /*
002: * $RCSfile: SphereCg.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.4 $
041: * $Date: 2007/02/09 17:21:33 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.cg_shader;
046:
047: import com.sun.j3d.utils.geometry.Sphere;
048: import com.sun.j3d.utils.shader.StringIO;
049: import com.sun.j3d.utils.universe.*;
050: import javax.media.j3d.*;
051: import javax.vecmath.*;
052: import java.awt.GraphicsConfiguration;
053: import java.io.*;
054: import javax.swing.JOptionPane;
055: import org.jdesktop.j3d.examples.Resources;
056:
057: /**
058: * Simple Java 3D example program with programmable shader.
059: */
060: public class SphereCg extends javax.swing.JFrame {
061:
062: // Constants for type of light to use
063: private static final int DIRECTIONAL_LIGHT = 0;
064: private static final int POINT_LIGHT = 1;
065: private static final int SPOT_LIGHT = 2;
066:
067: // Flag indicates type of lights: directional, point, or spot
068: // lights. This flag is set based on command line argument
069: private static int lightType = DIRECTIONAL_LIGHT;
070: private SimpleUniverse univ = null;
071: private BranchGroup scene = null;
072:
073: public BranchGroup createSceneGraph() {
074: Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
075: Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
076: Color3f objColor = new Color3f(0.6f, 0.6f, 0.6f);
077: Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
078: Color3f lColor2 = new Color3f(0.0f, 1.0f, 0.0f);
079: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
080: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
081:
082: Transform3D t;
083:
084: // Create the root of the branch graph
085: BranchGroup objRoot = new BranchGroup();
086:
087: // Create a Transformgroup to scale all objects so they
088: // appear in the scene.
089: TransformGroup objScale = new TransformGroup();
090: Transform3D t3d = new Transform3D();
091: t3d.setScale(0.4);
092: objScale.setTransform(t3d);
093: objRoot.addChild(objScale);
094:
095: // Create a bounds for the background and lights
096: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
097: 0.0, 0.0), 100.0);
098:
099: // Set up the background
100: Background bg = new Background(bgColor);
101: bg.setApplicationBounds(bounds);
102: objScale.addChild(bg);
103:
104: // Create a Sphere object, generate one copy of the sphere,
105: // and add it into the scene graph.
106: ShaderAppearance a = new ShaderAppearance();
107: Material m = new Material(objColor, eColor, objColor, sColor,
108: 100.0f);
109: m.setLightingEnable(true);
110: String vertexProgram = null;
111: String fragmentProgram = null;
112: try {
113: vertexProgram = StringIO.readFully(Resources
114: .getResource("cg_shader/simple_vp.cg"));
115: fragmentProgram = StringIO.readFully(Resources
116: .getResource("cg_shader/simple_fp.cg"));
117: } catch (IOException e) {
118: e.printStackTrace();
119: System.exit(1);
120: }
121: Shader[] shaders = new Shader[2];
122: shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_CG,
123: Shader.SHADER_TYPE_VERTEX, vertexProgram);
124: shaders[1] = new SourceCodeShader(Shader.SHADING_LANGUAGE_CG,
125: Shader.SHADER_TYPE_FRAGMENT, fragmentProgram);
126: ShaderProgram shaderProgram = new CgShaderProgram();
127: shaderProgram.setShaders(shaders);
128:
129: a.setShaderProgram(shaderProgram);
130: a.setMaterial(m);
131: Sphere sph = new Sphere(1.0f, Sphere.GENERATE_NORMALS, 200, a);
132: objScale.addChild(sph);
133:
134: // Create the transform group node for the each light and initialize
135: // it to the identity. Enable the TRANSFORM_WRITE capability so that
136: // our behavior code can modify it at runtime. Add them to the root
137: // of the subgraph.
138: TransformGroup l1RotTrans = new TransformGroup();
139: l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
140: objScale.addChild(l1RotTrans);
141:
142: TransformGroup l2RotTrans = new TransformGroup();
143: l2RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
144: objScale.addChild(l2RotTrans);
145:
146: // Create transformations for the positional lights
147: t = new Transform3D();
148: Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
149: t.set(lPos1);
150: TransformGroup l1Trans = new TransformGroup(t);
151: l1RotTrans.addChild(l1Trans);
152:
153: t = new Transform3D();
154: Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
155: t.set(lPos2);
156: TransformGroup l2Trans = new TransformGroup(t);
157: l2RotTrans.addChild(l2Trans);
158:
159: // Create Geometry for point lights
160: ColoringAttributes caL1 = new ColoringAttributes();
161: ColoringAttributes caL2 = new ColoringAttributes();
162: caL1.setColor(lColor1);
163: caL2.setColor(lColor2);
164: Appearance appL1 = new Appearance();
165: Appearance appL2 = new Appearance();
166: appL1.setColoringAttributes(caL1);
167: appL2.setColoringAttributes(caL2);
168: l1Trans.addChild(new Sphere(0.05f, appL1));
169: l2Trans.addChild(new Sphere(0.05f, appL2));
170:
171: // Create lights
172: AmbientLight aLgt = new AmbientLight(alColor);
173:
174: Light lgt1 = null;
175: Light lgt2 = null;
176:
177: Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
178: Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
179: Vector3f lDirect1 = new Vector3f(lPos1);
180: Vector3f lDirect2 = new Vector3f(lPos2);
181: lDirect1.negate();
182: lDirect2.negate();
183:
184: switch (lightType) {
185: case DIRECTIONAL_LIGHT:
186: lgt1 = new DirectionalLight(lColor1, lDirect1);
187: lgt2 = new DirectionalLight(lColor2, lDirect2);
188: break;
189: case POINT_LIGHT:
190: lgt1 = new PointLight(lColor1, lPoint, atten);
191: lgt2 = new PointLight(lColor2, lPoint, atten);
192: break;
193: case SPOT_LIGHT:
194: lgt1 = new SpotLight(lColor1, lPoint, atten, lDirect1,
195: 25.0f * (float) Math.PI / 180.0f, 10.0f);
196: lgt2 = new SpotLight(lColor2, lPoint, atten, lDirect2,
197: 25.0f * (float) Math.PI / 180.0f, 10.0f);
198: break;
199: }
200:
201: // Set the influencing bounds
202: aLgt.setInfluencingBounds(bounds);
203: lgt1.setInfluencingBounds(bounds);
204: lgt2.setInfluencingBounds(bounds);
205:
206: // Add the lights into the scene graph
207: objScale.addChild(aLgt);
208: l1Trans.addChild(lgt1);
209: l2Trans.addChild(lgt2);
210:
211: // Create a new Behavior object that will perform the desired
212: // operation on the specified transform object and add it into the
213: // scene graph.
214: Transform3D yAxis = new Transform3D();
215: Alpha rotor1Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
216: 0, 4000, 0, 0, 0, 0, 0);
217: RotationInterpolator rotator1 = new RotationInterpolator(
218: rotor1Alpha, l1RotTrans, yAxis, 0.0f,
219: (float) Math.PI * 2.0f);
220: rotator1.setSchedulingBounds(bounds);
221: l1RotTrans.addChild(rotator1);
222:
223: // Create a new Behavior object that will perform the desired
224: // operation on the specified transform object and add it into the
225: // scene graph.
226: Alpha rotor2Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
227: 0, 1000, 0, 0, 0, 0, 0);
228: RotationInterpolator rotator2 = new RotationInterpolator(
229: rotor2Alpha, l2RotTrans, yAxis, 0.0f, 0.0f);
230: bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
231: rotator2.setSchedulingBounds(bounds);
232: l2RotTrans.addChild(rotator2);
233:
234: // Create a position interpolator and attach it to the view
235: // platform
236: TransformGroup vpTrans = univ.getViewingPlatform()
237: .getViewPlatformTransform();
238: Transform3D axisOfTranslation = new Transform3D();
239: Alpha transAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
240: | Alpha.DECREASING_ENABLE, 0, 0, 5000, 0, 0, 5000, 0, 0);
241: axisOfTranslation.rotY(-Math.PI / 2.0);
242: PositionInterpolator translator = new PositionInterpolator(
243: transAlpha, vpTrans, axisOfTranslation, 2.0f, 3.5f);
244: translator.setSchedulingBounds(bounds);
245: objScale.addChild(translator);
246:
247: // Let Java 3D perform optimizations on this scene graph.
248: objRoot.compile();
249:
250: return objRoot;
251: }
252:
253: private Canvas3D createUniverse() {
254: // Get the preferred graphics configuration for the default screen
255: GraphicsConfiguration config = SimpleUniverse
256: .getPreferredConfiguration();
257:
258: // Create a Canvas3D using the preferred configuration
259: Canvas3D canvas3d = new Canvas3D(config);
260:
261: // Create simple universe with view branch
262: univ = new SimpleUniverse(canvas3d);
263: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
264: 0.0, 0.0), 100.0);
265:
266: // Add a ShaderErrorListener
267: univ.addShaderErrorListener(new ShaderErrorListener() {
268: public void errorOccurred(ShaderError error) {
269: error.printVerbose();
270: JOptionPane.showMessageDialog(SphereCg.this , error
271: .toString(), "ShaderError",
272: JOptionPane.ERROR_MESSAGE);
273: }
274: });
275:
276: // This will move the ViewPlatform back a bit so the
277: // objects in the scene can be viewed.
278: univ.getViewingPlatform().setNominalViewingTransform();
279:
280: // Ensure at least 5 msec per frame (i.e., < 200Hz)
281: univ.getViewer().getView().setMinimumFrameCycleTime(5);
282:
283: return canvas3d;
284: }
285:
286: /**
287: * Creates new form SphereCg
288: */
289: public SphereCg() {
290:
291: // Initialize the GUI components
292: initComponents();
293:
294: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
295: Canvas3D c = createUniverse();
296: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
297:
298: // Create the content branch and add it to the universe
299: scene = createSceneGraph();
300: univ.addBranchGraph(scene);
301: }
302:
303: // ----------------------------------------------------------------
304:
305: /** This method is called from within the constructor to
306: * initialize the form.
307: * WARNING: Do NOT modify this code. The content of this method is
308: * always regenerated by the Form Editor.
309: */
310: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
311: private void initComponents() {
312: drawingPanel = new javax.swing.JPanel();
313:
314: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
315: setTitle("SphereCg");
316: drawingPanel.setLayout(new java.awt.BorderLayout());
317:
318: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
319: getContentPane()
320: .add(drawingPanel, java.awt.BorderLayout.CENTER);
321:
322: pack();
323: }// </editor-fold>//GEN-END:initComponents
324:
325: /**
326: * @param args the command line arguments
327: */
328: public static void main(final String args[]) {
329: java.awt.EventQueue.invokeLater(new Runnable() {
330: public void run() {
331: SphereCg sphereCg = new SphereCg();
332: sphereCg.setVisible(true);
333: }
334: });
335: }
336:
337: // Variables declaration - do not modify//GEN-BEGIN:variables
338: private javax.swing.JPanel drawingPanel;
339: // End of variables declaration//GEN-END:variables
340:
341: }
|