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