001: /*
002: * $RCSfile: ObjLoadGLSL.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/02/09 17:21:40 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.glsl_shader;
046:
047: import com.sun.j3d.loaders.objectfile.ObjectFile;
048: import com.sun.j3d.loaders.ParsingErrorException;
049: import com.sun.j3d.loaders.IncorrectFormatException;
050: import com.sun.j3d.loaders.Scene;
051: import com.sun.j3d.utils.shader.StringIO;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import java.awt.GraphicsConfiguration;
056: import java.net.MalformedURLException;
057: import java.util.Enumeration;
058: import java.io.*;
059: import java.net.URL;
060: import com.sun.j3d.utils.behaviors.vp.*;
061: import java.io.FileNotFoundException;
062: import javax.swing.JOptionPane;
063: import org.jdesktop.j3d.examples.Resources;
064:
065: /**
066: * Simple Java 3D example program to display an .obj object with shader programs.
067: */
068: public class ObjLoadGLSL extends javax.swing.JFrame {
069:
070: private String shaderName = "polkadot3d";
071: private boolean spin = false;
072: private boolean noTriangulate = false;
073: private boolean noStripify = false;
074: private double creaseAngle = 60.0;
075: private URL filename = null;
076:
077: private SimpleUniverse univ = null;
078: private BranchGroup scene = null;
079:
080: public BranchGroup createSceneGraph() {
081: // Create the root of the branch graph
082: BranchGroup objRoot = new BranchGroup();
083:
084: // Create a Transformgroup to scale all objects so they
085: // appear in the scene.
086: TransformGroup objScale = new TransformGroup();
087: Transform3D t3d = new Transform3D();
088: t3d.setScale(0.7);
089: objScale.setTransform(t3d);
090: objRoot.addChild(objScale);
091:
092: // Create the transform group node and initialize it to the
093: // identity. Enable the TRANSFORM_WRITE capability so that
094: // our behavior code can modify it at runtime. Add it to the
095: // root of the subgraph.
096: TransformGroup objTrans = new TransformGroup();
097: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
098: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
099: objScale.addChild(objTrans);
100:
101: int flags = ObjectFile.RESIZE;
102: if (!noTriangulate)
103: flags |= ObjectFile.TRIANGULATE;
104: if (!noStripify)
105: flags |= ObjectFile.STRIPIFY;
106: ObjectFile f = new ObjectFile(flags, (float) (creaseAngle
107: * Math.PI / 180.0));
108: Scene s = null;
109: try {
110: s = f.load(filename);
111: } catch (FileNotFoundException e) {
112: System.err.println(e);
113: System.exit(1);
114: } catch (ParsingErrorException e) {
115: System.err.println(e);
116: System.exit(1);
117: } catch (IncorrectFormatException e) {
118: System.err.println(e);
119: System.exit(1);
120: }
121:
122: // Set vertex and fragment shader program for all Shape3D nodes in scene
123: String vertexProgram = null;
124: String fragmentProgram = null;
125: try {
126: vertexProgram = StringIO
127: .readFully(Resources.getResource("glsl_shader/"
128: + shaderName + ".vert"));
129: fragmentProgram = StringIO
130: .readFully(Resources.getResource("glsl_shader/"
131: + shaderName + ".frag"));
132: } catch (IOException e) {
133: throw new RuntimeException(e);
134: }
135: Shader[] shaders = new Shader[2];
136: shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL,
137: Shader.SHADER_TYPE_VERTEX, vertexProgram);
138: shaders[1] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL,
139: Shader.SHADER_TYPE_FRAGMENT, fragmentProgram);
140: ShaderProgram shaderProgram = new GLSLShaderProgram();
141: shaderProgram.setShaders(shaders);
142: setShaderProgram(s.getSceneGroup(), shaderProgram);
143:
144: objTrans.addChild(s.getSceneGroup());
145:
146: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
147: 0.0, 0.0), 100.0);
148:
149: if (spin) {
150: Transform3D yAxis = new Transform3D();
151: Alpha rotationAlpha = new Alpha(-1,
152: Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
153:
154: RotationInterpolator rotator = new RotationInterpolator(
155: rotationAlpha, objTrans, yAxis, 0.0f,
156: (float) Math.PI * 2.0f);
157: rotator.setSchedulingBounds(bounds);
158: objTrans.addChild(rotator);
159: }
160:
161: // Set up the background
162: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
163: Background bgNode = new Background(bgColor);
164: bgNode.setApplicationBounds(bounds);
165: objRoot.addChild(bgNode);
166:
167: return objRoot;
168: }
169:
170: private Canvas3D createUniverse() {
171: // Get the preferred graphics configuration for the default screen
172: GraphicsConfiguration config = SimpleUniverse
173: .getPreferredConfiguration();
174:
175: // Create a Canvas3D using the preferred configuration
176: Canvas3D canvas3d = new Canvas3D(config);
177:
178: // Create simple universe with view branch
179: univ = new SimpleUniverse(canvas3d);
180: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
181: 0.0, 0.0), 100.0);
182:
183: // Add a ShaderErrorListener
184: univ.addShaderErrorListener(new ShaderErrorListener() {
185: public void errorOccurred(ShaderError error) {
186: error.printVerbose();
187: JOptionPane.showMessageDialog(ObjLoadGLSL.this , error
188: .toString(), "ShaderError",
189: JOptionPane.ERROR_MESSAGE);
190: }
191: });
192:
193: // add mouse behaviors to the ViewingPlatform
194: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
195:
196: PlatformGeometry pg = new PlatformGeometry();
197:
198: // Set up the ambient light
199: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
200: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
201: ambientLightNode.setInfluencingBounds(bounds);
202: pg.addChild(ambientLightNode);
203:
204: // Set up the directional lights
205: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
206: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
207: Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
208: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
209:
210: DirectionalLight light1 = new DirectionalLight(light1Color,
211: light1Direction);
212: light1.setInfluencingBounds(bounds);
213: pg.addChild(light1);
214:
215: DirectionalLight light2 = new DirectionalLight(light2Color,
216: light2Direction);
217: light2.setInfluencingBounds(bounds);
218: pg.addChild(light2);
219:
220: viewingPlatform.setPlatformGeometry(pg);
221:
222: // This will move the ViewPlatform back a bit so the
223: // objects in the scene can be viewed.
224: viewingPlatform.setNominalViewingTransform();
225:
226: if (!spin) {
227: OrbitBehavior orbit = new OrbitBehavior(canvas3d,
228: OrbitBehavior.REVERSE_ALL);
229: orbit.setSchedulingBounds(bounds);
230: viewingPlatform.setViewPlatformBehavior(orbit);
231: }
232:
233: // Ensure at least 5 msec per frame (i.e., < 200Hz)
234: univ.getViewer().getView().setMinimumFrameCycleTime(5);
235:
236: return canvas3d;
237: }
238:
239: private void usage() {
240: System.out
241: .println("Usage: java ObjLoadGLSL [-s] [-S shaderName] [-n] [-t] [-c degrees] <.obj file>");
242: System.out.println(" -s Spin (no user interaction)");
243: System.out
244: .println(" -S Set shader name (default is 'simple')");
245: System.out.println(" -n No triangulation");
246: System.out.println(" -t No stripification");
247: System.out
248: .println(" -c Set crease angle for normal generation (default is 60 without");
249: System.out
250: .println(" smoothing group info, otherwise 180 within smoothing groups)");
251: System.exit(0);
252: } // End of usage
253:
254: // Set shader program for all nodes in specified branch graph
255: private void setShaderProgram(BranchGroup g,
256: ShaderProgram shaderProgram) {
257: ShaderAppearance myApp = new ShaderAppearance();
258: Material mat = new Material();
259: myApp.setShaderProgram(shaderProgram);
260: myApp.setMaterial(mat);
261: setShaderProgram(g, myApp);
262: }
263:
264: // Recursively set shader program for all children of specified group
265: private void setShaderProgram(Group g, ShaderAppearance myApp) {
266:
267: Enumeration e = g.getAllChildren();
268: while (e.hasMoreElements()) {
269: Node n = (Node) (e.nextElement());
270: if (n instanceof Group) {
271: setShaderProgram((Group) n, myApp);
272: } else if (n instanceof Shape3D) {
273: Shape3D s = (Shape3D) n;
274: s.setAppearance(myApp);
275: }
276: }
277: }
278:
279: /**
280: * Creates new form ObjLoadGLSL
281: */
282: public ObjLoadGLSL(String args[]) {
283: if (args.length != 0) {
284: for (int i = 0; i < args.length; i++) {
285: if (args[i].startsWith("-")) {
286: if (args[i].equals("-s")) {
287: spin = true;
288: } else if (args[i].equals("-n")) {
289: noTriangulate = true;
290: } else if (args[i].equals("-t")) {
291: noStripify = true;
292: } else if (args[i].equals("-c")) {
293: if (i < args.length - 1) {
294: creaseAngle = (new Double(args[++i]))
295: .doubleValue();
296: } else
297: usage();
298: } else if (args[i].equals("-S")) {
299: if (i < args.length - 1) {
300: shaderName = args[++i];
301: } else
302: usage();
303: } else {
304: usage();
305: }
306: } else {
307: try {
308: if ((args[i].indexOf("file:") == 0)
309: || (args[i].indexOf("http") == 0)) {
310: filename = new URL(args[i]);
311: } else if (args[i].charAt(0) != '/') {
312: filename = new URL("file:./" + args[i]);
313: } else {
314: filename = new URL("file:" + args[i]);
315: }
316: } catch (MalformedURLException e) {
317: System.err.println(e);
318: System.exit(1);
319: }
320: }
321: }
322: }
323:
324: if (filename == null) {
325: filename = Resources
326: .getResource("resources/geometry/galleon.obj");
327: if (filename == null) {
328: System.err
329: .println("resources/geometry/galleon.obj not found");
330: System.exit(1);
331: }
332: }
333:
334: // Initialize the GUI components
335: initComponents();
336:
337: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
338: Canvas3D c = createUniverse();
339: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
340:
341: // Create the content branch and add it to the universe
342: scene = createSceneGraph();
343: univ.addBranchGraph(scene);
344: }
345:
346: // ----------------------------------------------------------------
347:
348: /** This method is called from within the constructor to
349: * initialize the form.
350: * WARNING: Do NOT modify this code. The content of this method is
351: * always regenerated by the Form Editor.
352: */
353: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
354: private void initComponents() {
355: drawingPanel = new javax.swing.JPanel();
356:
357: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
358: setTitle("ObjLoadGLSL");
359: drawingPanel.setLayout(new java.awt.BorderLayout());
360:
361: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
362: getContentPane()
363: .add(drawingPanel, java.awt.BorderLayout.CENTER);
364:
365: pack();
366: }// </editor-fold>//GEN-END:initComponents
367:
368: /**
369: * @param args the command line arguments
370: */
371: public static void main(final String args[]) {
372: java.awt.EventQueue.invokeLater(new Runnable() {
373: public void run() {
374: ObjLoadGLSL objLoadGLSL = new ObjLoadGLSL(args);
375: objLoadGLSL.setVisible(true);
376: }
377: });
378: }
379:
380: // Variables declaration - do not modify//GEN-BEGIN:variables
381: private javax.swing.JPanel drawingPanel;
382: // End of variables declaration//GEN-END:variables
383:
384: }
|