001: /*
002: * $RCSfile: SamplerTestCg.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:33 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.cg_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 SamplerTestCg 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 = "cg_shader/multitex_fp.cg";
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_CG,
102: Shader.SHADER_TYPE_FRAGMENT, fragmentProgram);
103: final String[] shaderAttrNames = { "cloudFactor" };
104: final Object[] shaderAttrValues = { new Float(0.6f), };
105: ShaderProgram shaderProgram = new CgShaderProgram();
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: // Setup texture coordinate generation
124: Vector4f plane0S = new Vector4f(3.0f, 1.5f, 0.3f, 0.0f);
125: Vector4f plane0T = new Vector4f(1.0f, 2.5f, 0.24f, 0.0f);
126: TexCoordGeneration tcg0 = new TexCoordGeneration(
127: TexCoordGeneration.OBJECT_LINEAR,
128: TexCoordGeneration.TEXTURE_COORDINATE_2, plane0S,
129: plane0T);
130:
131: // Setup texture coordinate generation
132: TexCoordGeneration tcg1 = new TexCoordGeneration(
133: TexCoordGeneration.SPHERE_MAP,
134: TexCoordGeneration.TEXTURE_COORDINATE_2);
135:
136: // Put the textures in unit 0,1
137: TextureUnitState[] tus = new TextureUnitState[2];
138: tus[CLOUD] = new TextureUnitState();
139: tus[CLOUD].setTexture(cloudTex);
140: tus[CLOUD].setTexCoordGeneration(tcg0);
141: tus[EARTH] = new TextureUnitState();
142: tus[EARTH].setTexture(earthTex);
143: tus[EARTH].setTexCoordGeneration(tcg1);
144: app.setTextureUnitState(tus);
145:
146: // Create a Sphere object using the shader appearance,
147: // and add it into the scene graph.
148: Sphere sph = new Sphere(0.4f, Sphere.GENERATE_NORMALS, 30, app);
149: objTrans.addChild(sph);
150:
151: // Create a new Behavior object that will perform the
152: // desired operation on the specified transform and add
153: // it into the scene graph.
154: Transform3D yAxis = new Transform3D();
155: Alpha rotationAlpha = new Alpha(-1, 4000);
156:
157: RotationInterpolator rotator = new RotationInterpolator(
158: rotationAlpha, objTrans, yAxis, 0.0f,
159: (float) Math.PI * 2.0f);
160: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
161: 0.0, 0.0), 100.0);
162: rotator.setSchedulingBounds(bounds);
163: objRoot.addChild(rotator);
164:
165: // Have Java 3D perform optimizations on this scene graph.
166: //objRoot.compile();
167:
168: return objRoot;
169: }
170:
171: private Canvas3D initScene() {
172: GraphicsConfiguration config = SimpleUniverse
173: .getPreferredConfiguration();
174:
175: Canvas3D c = new Canvas3D(config);
176:
177: BranchGroup scene = createSceneGraph();
178: univ = new SimpleUniverse(c);
179:
180: // Add a ShaderErrorListener
181: univ.addShaderErrorListener(new ShaderErrorListener() {
182: public void errorOccurred(ShaderError error) {
183: error.printVerbose();
184: JOptionPane.showMessageDialog(SamplerTestCg.this , error
185: .toString(), "ShaderError",
186: JOptionPane.ERROR_MESSAGE);
187: }
188: });
189:
190: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
191: // This will move the ViewPlatform back a bit so the
192: // objects in the scene can be viewed.
193: viewingPlatform.setNominalViewingTransform();
194:
195: univ.addBranchGraph(scene);
196:
197: return c;
198: }
199:
200: /**
201: * Creates new form SamplerTestCg
202: */
203: public SamplerTestCg() {
204: // Initialize the GUI components
205: initComponents();
206:
207: // Create the scene and add the Canvas3D to the drawing panel
208: Canvas3D c = initScene();
209: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
210: }
211:
212: // ----------------------------------------------------------------
213:
214: /** This method is called from within the constructor to
215: * initialize the form.
216: * WARNING: Do NOT modify this code. The content of this method is
217: * always regenerated by the Form Editor.
218: */
219: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
220: private void initComponents() {
221: drawingPanel = new javax.swing.JPanel();
222:
223: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
224: setTitle("SamplerTestCg");
225: drawingPanel.setLayout(new java.awt.BorderLayout());
226:
227: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
228: getContentPane()
229: .add(drawingPanel, java.awt.BorderLayout.CENTER);
230:
231: pack();
232: }// </editor-fold>//GEN-END:initComponents
233:
234: /**
235: * @param args the command line arguments
236: */
237: public static void main(String args[]) {
238: java.awt.EventQueue.invokeLater(new Runnable() {
239: public void run() {
240: new SamplerTestCg().setVisible(true);
241: }
242: });
243: }
244:
245: // Variables declaration - do not modify//GEN-BEGIN:variables
246: private javax.swing.JPanel drawingPanel;
247: // End of variables declaration//GEN-END:variables
248:
249: }
|