001: /*
002: * $RCSfile: SamplerTestGLSL.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.3 $
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.universe.*;
048: import com.sun.j3d.utils.geometry.Sphere;
049: import com.sun.j3d.utils.image.TextureLoader;
050: import com.sun.j3d.utils.shader.StringIO;
051: import javax.media.j3d.*;
052: import javax.vecmath.*;
053: import java.awt.GraphicsConfiguration;
054: import java.io.IOException;
055: import java.net.URL;
056: import javax.swing.JOptionPane;
057: import org.jdesktop.j3d.examples.Resources;
058:
059: public class SamplerTestGLSL extends javax.swing.JFrame {
060:
061: private static String cloudTexName = "resources/images/bg.jpg";
062: private static String earthTexName = "resources/images/earth.jpg";
063: private static String fragmentProgName = "glsl_shader/multitex.frag";
064: private URL cloudURL = null;
065: private URL earthURL = null;
066: private static final int CLOUD = 0;
067: private static final int EARTH = 1;
068:
069: SimpleUniverse univ = null;
070:
071: public BranchGroup createSceneGraph() {
072: // Create the root of the branch graph
073: BranchGroup objRoot = new BranchGroup();
074:
075: // Create the TransformGroup node and initialize it to the
076: // identity. Enable the TRANSFORM_WRITE capability so that
077: // our behavior code can modify it at run time. Add it to
078: // the root of the subgraph.
079: TransformGroup objTrans = new TransformGroup();
080: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
081: objRoot.addChild(objTrans);
082:
083: // Create texture objects
084: cloudURL = Resources.getResource(cloudTexName);
085: Texture cloudTex = new TextureLoader(cloudURL, this )
086: .getTexture();
087: earthURL = Resources.getResource(earthTexName);
088: Texture earthTex = new TextureLoader(earthURL, this )
089: .getTexture();
090:
091: // Create the shader program
092: String vertexProgram = null;
093: String fragmentProgram = null;
094: try {
095: fragmentProgram = StringIO.readFully(Resources
096: .getResource(fragmentProgName));
097: } catch (IOException e) {
098: System.err.println(e);
099: }
100: Shader[] shaders = new Shader[1];
101: shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL,
102: Shader.SHADER_TYPE_FRAGMENT, fragmentProgram);
103: final String[] shaderAttrNames = { "cloudFactor", "cloudTex",
104: "earthTex", };
105: final Object[] shaderAttrValues = { new Float(0.6f),
106: new Integer(0), new Integer(1), };
107: ShaderProgram shaderProgram = new GLSLShaderProgram();
108: shaderProgram.setShaders(shaders);
109: shaderProgram.setShaderAttrNames(shaderAttrNames);
110:
111: // Create the shader attribute set
112: ShaderAttributeSet shaderAttributeSet = new ShaderAttributeSet();
113: for (int i = 0; i < shaderAttrNames.length; i++) {
114: ShaderAttribute shaderAttribute = new ShaderAttributeValue(
115: shaderAttrNames[i], shaderAttrValues[i]);
116: shaderAttributeSet.put(shaderAttribute);
117: }
118:
119: // Create shader appearance to hold the shader program and
120: // shader attributes
121: ShaderAppearance app = new ShaderAppearance();
122: app.setShaderProgram(shaderProgram);
123: app.setShaderAttributeSet(shaderAttributeSet);
124:
125: // Setup texture coordinate generation
126: Vector4f plane0S = new Vector4f(3.0f, 1.5f, 0.3f, 0.0f);
127: Vector4f plane0T = new Vector4f(1.0f, 2.5f, 0.24f, 0.0f);
128: TexCoordGeneration tcg0 = new TexCoordGeneration(
129: TexCoordGeneration.OBJECT_LINEAR,
130: TexCoordGeneration.TEXTURE_COORDINATE_2, plane0S,
131: plane0T);
132:
133: // Setup texture coordinate generation
134: TexCoordGeneration tcg1 = new TexCoordGeneration(
135: TexCoordGeneration.SPHERE_MAP,
136: TexCoordGeneration.TEXTURE_COORDINATE_2);
137:
138: // Put the textures in unit 0,1
139: TextureUnitState[] tus = new TextureUnitState[2];
140: tus[CLOUD] = new TextureUnitState();
141: tus[CLOUD].setTexture(cloudTex);
142: tus[CLOUD].setTexCoordGeneration(tcg0);
143: tus[EARTH] = new TextureUnitState();
144: tus[EARTH].setTexture(earthTex);
145: tus[EARTH].setTexCoordGeneration(tcg1);
146: app.setTextureUnitState(tus);
147:
148: // Create a Sphere object using the shader appearance,
149: // and add it into the scene graph.
150: Sphere sph = new Sphere(0.4f, Sphere.GENERATE_NORMALS, 30, app);
151: objTrans.addChild(sph);
152:
153: // Create a new Behavior object that will perform the
154: // desired operation on the specified transform and add
155: // it into the scene graph.
156: Transform3D yAxis = new Transform3D();
157: Alpha rotationAlpha = new Alpha(-1, 4000);
158:
159: RotationInterpolator rotator = new RotationInterpolator(
160: rotationAlpha, objTrans, yAxis, 0.0f,
161: (float) Math.PI * 2.0f);
162: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
163: 0.0, 0.0), 100.0);
164: rotator.setSchedulingBounds(bounds);
165: objRoot.addChild(rotator);
166:
167: // Have Java 3D perform optimizations on this scene graph.
168: //objRoot.compile();
169:
170: return objRoot;
171: }
172:
173: private Canvas3D initScene() {
174: GraphicsConfiguration config = SimpleUniverse
175: .getPreferredConfiguration();
176:
177: Canvas3D c = new Canvas3D(config);
178:
179: BranchGroup scene = createSceneGraph();
180: univ = new SimpleUniverse(c);
181:
182: // Add a ShaderErrorListener
183: univ.addShaderErrorListener(new ShaderErrorListener() {
184: public void errorOccurred(ShaderError error) {
185: error.printVerbose();
186: JOptionPane.showMessageDialog(SamplerTestGLSL.this ,
187: error.toString(), "ShaderError",
188: JOptionPane.ERROR_MESSAGE);
189: }
190: });
191:
192: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
193: // This will move the ViewPlatform back a bit so the
194: // objects in the scene can be viewed.
195: viewingPlatform.setNominalViewingTransform();
196:
197: univ.addBranchGraph(scene);
198:
199: return c;
200: }
201:
202: /**
203: * Creates new form SamplerTestGLSL
204: */
205: public SamplerTestGLSL() {
206: // Initialize the GUI components
207: initComponents();
208:
209: // Create the scene and add the Canvas3D to the drawing panel
210: Canvas3D c = initScene();
211: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
212: }
213:
214: // ----------------------------------------------------------------
215:
216: /** This method is called from within the constructor to
217: * initialize the form.
218: * WARNING: Do NOT modify this code. The content of this method is
219: * always regenerated by the Form Editor.
220: */
221: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
222: private void initComponents() {
223: drawingPanel = new javax.swing.JPanel();
224:
225: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
226: setTitle("SamplerTestGLSL");
227: drawingPanel.setLayout(new java.awt.BorderLayout());
228:
229: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
230: getContentPane()
231: .add(drawingPanel, java.awt.BorderLayout.CENTER);
232:
233: pack();
234: }// </editor-fold>//GEN-END:initComponents
235:
236: /**
237: * @param args the command line arguments
238: */
239: public static void main(String args[]) {
240: java.awt.EventQueue.invokeLater(new Runnable() {
241: public void run() {
242: new SamplerTestGLSL().setVisible(true);
243: }
244: });
245: }
246:
247: // Variables declaration - do not modify//GEN-BEGIN:variables
248: private javax.swing.JPanel drawingPanel;
249: // End of variables declaration//GEN-END:variables
250:
251: }
|