001: /*
002: * $RCSfile: SphereGLSL.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.5 $
041: * $Date: 2007/02/09 17:21:41 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.glsl_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 SphereGLSL 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("glsl_shader/simple.vert"));
115: fragmentProgram = StringIO.readFully(Resources
116: .getResource("glsl_shader/simple.frag"));
117: } catch (IOException e) {
118: throw new RuntimeException(e);
119: }
120: Shader[] shaders = new Shader[2];
121: shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL,
122: Shader.SHADER_TYPE_VERTEX, vertexProgram);
123: shaders[1] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL,
124: Shader.SHADER_TYPE_FRAGMENT, fragmentProgram);
125: ShaderProgram shaderProgram = new GLSLShaderProgram();
126: shaderProgram.setShaders(shaders);
127:
128: a.setShaderProgram(shaderProgram);
129: a.setMaterial(m);
130: Sphere sph = new Sphere(1.0f, Sphere.GENERATE_NORMALS, 200, a);
131: objScale.addChild(sph);
132:
133: // Create the transform group node for the each light and initialize
134: // it to the identity. Enable the TRANSFORM_WRITE capability so that
135: // our behavior code can modify it at runtime. Add them to the root
136: // of the subgraph.
137: TransformGroup l1RotTrans = new TransformGroup();
138: l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
139: objScale.addChild(l1RotTrans);
140:
141: TransformGroup l2RotTrans = new TransformGroup();
142: l2RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
143: objScale.addChild(l2RotTrans);
144:
145: // Create transformations for the positional lights
146: t = new Transform3D();
147: Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
148: t.set(lPos1);
149: TransformGroup l1Trans = new TransformGroup(t);
150: l1RotTrans.addChild(l1Trans);
151:
152: t = new Transform3D();
153: Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
154: t.set(lPos2);
155: TransformGroup l2Trans = new TransformGroup(t);
156: l2RotTrans.addChild(l2Trans);
157:
158: // Create Geometry for point lights
159: ColoringAttributes caL1 = new ColoringAttributes();
160: ColoringAttributes caL2 = new ColoringAttributes();
161: caL1.setColor(lColor1);
162: caL2.setColor(lColor2);
163: Appearance appL1 = new Appearance();
164: Appearance appL2 = new Appearance();
165: appL1.setColoringAttributes(caL1);
166: appL2.setColoringAttributes(caL2);
167: l1Trans.addChild(new Sphere(0.05f, appL1));
168: l2Trans.addChild(new Sphere(0.05f, appL2));
169:
170: // Create lights
171: AmbientLight aLgt = new AmbientLight(alColor);
172:
173: Light lgt1 = null;
174: Light lgt2 = null;
175:
176: Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
177: Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
178: Vector3f lDirect1 = new Vector3f(lPos1);
179: Vector3f lDirect2 = new Vector3f(lPos2);
180: lDirect1.negate();
181: lDirect2.negate();
182:
183: switch (lightType) {
184: case DIRECTIONAL_LIGHT:
185: lgt1 = new DirectionalLight(lColor1, lDirect1);
186: lgt2 = new DirectionalLight(lColor2, lDirect2);
187: break;
188: case POINT_LIGHT:
189: lgt1 = new PointLight(lColor1, lPoint, atten);
190: lgt2 = new PointLight(lColor2, lPoint, atten);
191: break;
192: case SPOT_LIGHT:
193: lgt1 = new SpotLight(lColor1, lPoint, atten, lDirect1,
194: 25.0f * (float) Math.PI / 180.0f, 10.0f);
195: lgt2 = new SpotLight(lColor2, lPoint, atten, lDirect2,
196: 25.0f * (float) Math.PI / 180.0f, 10.0f);
197: break;
198: }
199:
200: // Set the influencing bounds
201: aLgt.setInfluencingBounds(bounds);
202: lgt1.setInfluencingBounds(bounds);
203: lgt2.setInfluencingBounds(bounds);
204:
205: // Add the lights into the scene graph
206: objScale.addChild(aLgt);
207: l1Trans.addChild(lgt1);
208: l2Trans.addChild(lgt2);
209:
210: // Create a new Behavior object that will perform the desired
211: // operation on the specified transform object and add it into the
212: // scene graph.
213: Transform3D yAxis = new Transform3D();
214: Alpha rotor1Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
215: 0, 4000, 0, 0, 0, 0, 0);
216: RotationInterpolator rotator1 = new RotationInterpolator(
217: rotor1Alpha, l1RotTrans, yAxis, 0.0f,
218: (float) Math.PI * 2.0f);
219: rotator1.setSchedulingBounds(bounds);
220: l1RotTrans.addChild(rotator1);
221:
222: // Create a new Behavior object that will perform the desired
223: // operation on the specified transform object and add it into the
224: // scene graph.
225: Alpha rotor2Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
226: 0, 1000, 0, 0, 0, 0, 0);
227: RotationInterpolator rotator2 = new RotationInterpolator(
228: rotor2Alpha, l2RotTrans, yAxis, 0.0f, 0.0f);
229: bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
230: rotator2.setSchedulingBounds(bounds);
231: l2RotTrans.addChild(rotator2);
232:
233: // Create a position interpolator and attach it to the view
234: // platform
235: TransformGroup vpTrans = univ.getViewingPlatform()
236: .getViewPlatformTransform();
237: Transform3D axisOfTranslation = new Transform3D();
238: Alpha transAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
239: | Alpha.DECREASING_ENABLE, 0, 0, 5000, 0, 0, 5000, 0, 0);
240: axisOfTranslation.rotY(-Math.PI / 2.0);
241: PositionInterpolator translator = new PositionInterpolator(
242: transAlpha, vpTrans, axisOfTranslation, 2.0f, 3.5f);
243: translator.setSchedulingBounds(bounds);
244: objScale.addChild(translator);
245:
246: // Let Java 3D perform optimizations on this scene graph.
247: objRoot.compile();
248:
249: return objRoot;
250: }
251:
252: private Canvas3D createUniverse() {
253: // Get the preferred graphics configuration for the default screen
254: GraphicsConfiguration config = SimpleUniverse
255: .getPreferredConfiguration();
256:
257: // Create a Canvas3D using the preferred configuration
258: Canvas3D canvas3d = new Canvas3D(config);
259:
260: // Create simple universe with view branch
261: univ = new SimpleUniverse(canvas3d);
262: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
263: 0.0, 0.0), 100.0);
264:
265: // Add a ShaderErrorListener
266: univ.addShaderErrorListener(new ShaderErrorListener() {
267: public void errorOccurred(ShaderError error) {
268: error.printVerbose();
269: JOptionPane.showMessageDialog(SphereGLSL.this , error
270: .toString(), "ShaderError",
271: JOptionPane.ERROR_MESSAGE);
272: }
273: });
274:
275: // This will move the ViewPlatform back a bit so the
276: // objects in the scene can be viewed.
277: univ.getViewingPlatform().setNominalViewingTransform();
278:
279: // Ensure at least 5 msec per frame (i.e., < 200Hz)
280: univ.getViewer().getView().setMinimumFrameCycleTime(5);
281:
282: return canvas3d;
283: }
284:
285: /**
286: * Creates new form SphereGLSL
287: */
288: public SphereGLSL() {
289:
290: // Initialize the GUI components
291: initComponents();
292:
293: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
294: Canvas3D c = createUniverse();
295: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
296:
297: // Create the content branch and add it to the universe
298: scene = createSceneGraph();
299: univ.addBranchGraph(scene);
300: }
301:
302: // ----------------------------------------------------------------
303:
304: /** This method is called from within the constructor to
305: * initialize the form.
306: * WARNING: Do NOT modify this code. The content of this method is
307: * always regenerated by the Form Editor.
308: */
309: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
310: private void initComponents() {
311: drawingPanel = new javax.swing.JPanel();
312:
313: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
314: setTitle("SphereGLSL");
315: drawingPanel.setLayout(new java.awt.BorderLayout());
316:
317: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
318: getContentPane()
319: .add(drawingPanel, java.awt.BorderLayout.CENTER);
320:
321: pack();
322: }// </editor-fold>//GEN-END:initComponents
323:
324: /**
325: * @param args the command line arguments
326: */
327: public static void main(final String args[]) {
328: java.awt.EventQueue.invokeLater(new Runnable() {
329: public void run() {
330: SphereGLSL sphereGLSL = new SphereGLSL();
331: sphereGLSL.setVisible(true);
332: }
333: });
334: }
335:
336: // Variables declaration - do not modify//GEN-BEGIN:variables
337: private javax.swing.JPanel drawingPanel;
338: // End of variables declaration//GEN-END:variables
339:
340: }
|