001: /*
002: * $RCSfile: VertexAttrTestGLSL.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.shader.StringIO;
049: import javax.media.j3d.*;
050: import java.awt.GraphicsConfiguration;
051: import java.io.IOException;
052: import java.nio.ByteBuffer;
053: import java.nio.ByteOrder;
054: import java.nio.FloatBuffer;
055: import javax.swing.JFrame;
056: import javax.swing.JOptionPane;
057: import javax.vecmath.Color3f;
058: import javax.vecmath.Point3d;
059: import org.jdesktop.j3d.examples.Resources;
060:
061: public class VertexAttrTestGLSL extends javax.swing.JFrame {
062:
063: SimpleUniverse univ = null;
064: BranchGroup scene = null;
065:
066: public BranchGroup createSceneGraph(boolean hasVertexAttrs) {
067:
068: // Bounds for BG and behavior
069: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
070: 0.0, 0.0), 100.0);
071:
072: // Create the root of the branch graph
073: BranchGroup objRoot = new BranchGroup();
074: objRoot.setCapability(BranchGroup.ALLOW_DETACH);
075:
076: // Set up the background
077: Color3f bgColor = new Color3f(0.1f, 0.1f, 0.1f);
078: Background bg = new Background(bgColor);
079: bg.setApplicationBounds(bounds);
080: objRoot.addChild(bg);
081:
082: // Create the TransformGroup node and initialize it to the
083: // identity. Enable the TRANSFORM_WRITE capability so that
084: // our behavior code can modify it at run time. Add it to
085: // the root of the subgraph.
086: TransformGroup objTrans = new TransformGroup();
087: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
088: objRoot.addChild(objTrans);
089:
090: // Create a simple Shape3D node; add it to the scene graph.
091: objTrans.addChild(new MyShape(this , hasVertexAttrs));
092:
093: return objRoot;
094: }
095:
096: private Canvas3D initScene() {
097: GraphicsConfiguration config = SimpleUniverse
098: .getPreferredConfiguration();
099:
100: Canvas3D c = new Canvas3D(config);
101: univ = new SimpleUniverse(c);
102:
103: // Add a ShaderErrorListener
104: univ.addShaderErrorListener(new ShaderErrorListener() {
105: public void errorOccurred(ShaderError error) {
106: error.printVerbose();
107: JOptionPane.showMessageDialog(VertexAttrTestGLSL.this ,
108: error.toString(), "ShaderError",
109: JOptionPane.ERROR_MESSAGE);
110: }
111: });
112:
113: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
114: // This will move the ViewPlatform back a bit so the
115: // objects in the scene can be viewed.
116: viewingPlatform.setNominalViewingTransform();
117:
118: return c;
119: }
120:
121: /**
122: * Creates new form VertexAttrTestGLSL
123: */
124: public VertexAttrTestGLSL() {
125: // Initialize the GUI components
126: initComponents();
127:
128: // Create the scene and add the Canvas3D to the drawing panel
129: Canvas3D c = initScene();
130: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
131: }
132:
133: static class MyShape extends Shape3D {
134: private static String vertexProgName = "glsl_shader/vertexshader.vert";
135:
136: // Coordinate data
137: private static final float[] coords = { 0.0f, 0.0f, 0.0f, 0.5f,
138: 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, };
139:
140: private static final int[] sizes = { 1, 3 };
141: private static final float[] weights = { 0.45f, 0.15f, 0.95f, };
142: private static final float[] temps = { 1.0f, 0.5f, 0.5f, 0.5f,
143: 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, };
144:
145: private static final String[] vaNames = { "weight",
146: "temperature" };
147:
148: J3DBuffer createDirectFloatBuffer(float[] arr) {
149: ByteOrder order = ByteOrder.nativeOrder();
150:
151: FloatBuffer nioBuf = ByteBuffer.allocateDirect(
152: arr.length * 4).order(order).asFloatBuffer();
153: nioBuf.put(arr);
154: return new J3DBuffer(nioBuf);
155: }
156:
157: MyShape(JFrame frame, boolean hasVertexAttrs) {
158:
159: int vertexFormat = GeometryArray.COORDINATES;
160: int vertexAttrCount = 0;
161: int[] vertexAttrSizes = null;
162: String[] vertexAttrNames = null;
163: String[] shaderAttrNames = null;
164:
165: if (hasVertexAttrs) {
166: vertexFormat |= GeometryArray.VERTEX_ATTRIBUTES;
167: vertexAttrCount = vaNames.length;
168: vertexAttrSizes = sizes;
169: vertexAttrNames = vaNames;
170: }
171:
172: TriangleArray tri = new TriangleArray(6, vertexFormat, 0,
173: null, vertexAttrCount, vertexAttrSizes);
174: tri.setValidVertexCount(3);
175: tri.setCoordinates(0, coords);
176:
177: if (hasVertexAttrs) {
178: tri.setVertexAttrs(0, 0, weights);
179: tri.setVertexAttrs(1, 0, temps);
180:
181: String vertexProgram = null;
182: try {
183: vertexProgram = StringIO.readFully(Resources
184: .getResource(vertexProgName));
185: } catch (IOException e) {
186: throw new RuntimeException(e);
187: }
188:
189: Shader[] shaders = new Shader[1];
190: shaders[0] = new SourceCodeShader(
191: Shader.SHADING_LANGUAGE_GLSL,
192: Shader.SHADER_TYPE_VERTEX, vertexProgram);
193:
194: ShaderProgram shaderProgram = new GLSLShaderProgram();
195: shaderProgram.setShaders(shaders);
196: shaderProgram.setVertexAttrNames(vertexAttrNames);
197: shaderProgram.setShaderAttrNames(shaderAttrNames);
198:
199: ShaderAppearance app = new ShaderAppearance();
200: app.setShaderProgram(shaderProgram);
201:
202: this .setGeometry(tri);
203:
204: this .setAppearance(app);
205: } else {
206: this .setGeometry(tri);
207: this .setAppearance(new Appearance());
208: }
209: }
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: java.awt.GridBagConstraints gridBagConstraints;
222:
223: mainPanel = new javax.swing.JPanel();
224: guiPanel = new javax.swing.JPanel();
225: vertexCheckBoxPanel = new javax.swing.JPanel();
226: jPanel1 = new javax.swing.JPanel();
227: jSeparator1 = new javax.swing.JSeparator();
228: jSeparator2 = new javax.swing.JSeparator();
229: jPanel2 = new javax.swing.JPanel();
230: vertexAttrsBox = new javax.swing.JCheckBox();
231: geometryPanel = new javax.swing.JPanel();
232: createButton = new javax.swing.JButton();
233: destroyButton = new javax.swing.JButton();
234: drawingPanel = new javax.swing.JPanel();
235: jMenuBar1 = new javax.swing.JMenuBar();
236: fileMenu = new javax.swing.JMenu();
237: exitMenuItem = new javax.swing.JMenuItem();
238:
239: setTitle("VertexAttrTestGLSL");
240: addWindowListener(new java.awt.event.WindowAdapter() {
241: public void windowClosing(java.awt.event.WindowEvent evt) {
242: exitForm(evt);
243: }
244: });
245:
246: mainPanel.setLayout(new java.awt.BorderLayout());
247:
248: guiPanel.setLayout(new java.awt.GridBagLayout());
249:
250: guiPanel.setBorder(javax.swing.BorderFactory
251: .createLineBorder(new java.awt.Color(0, 0, 0)));
252: vertexCheckBoxPanel.setLayout(new java.awt.GridBagLayout());
253:
254: vertexCheckBoxPanel
255: .setBorder(javax.swing.BorderFactory
256: .createTitledBorder(
257: null,
258: "vertexFormat",
259: javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
260: javax.swing.border.TitledBorder.DEFAULT_POSITION,
261: new java.awt.Font("Lucida Sans", 0, 10)));
262: jPanel1.setLayout(new java.awt.GridBagLayout());
263:
264: jSeparator1.setPreferredSize(new java.awt.Dimension(0, 4));
265: gridBagConstraints = new java.awt.GridBagConstraints();
266: gridBagConstraints.gridx = 0;
267: gridBagConstraints.gridy = 1;
268: jPanel1.add(jSeparator1, gridBagConstraints);
269:
270: jSeparator2.setPreferredSize(new java.awt.Dimension(0, 4));
271: gridBagConstraints = new java.awt.GridBagConstraints();
272: gridBagConstraints.gridx = 0;
273: gridBagConstraints.gridy = 3;
274: jPanel1.add(jSeparator2, gridBagConstraints);
275:
276: gridBagConstraints = new java.awt.GridBagConstraints();
277: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
278: vertexCheckBoxPanel.add(jPanel1, gridBagConstraints);
279:
280: jPanel2.setLayout(new java.awt.GridBagLayout());
281:
282: vertexAttrsBox.setSelected(true);
283: vertexAttrsBox.setText("VertexAttrs");
284: gridBagConstraints = new java.awt.GridBagConstraints();
285: gridBagConstraints.gridx = 0;
286: gridBagConstraints.gridy = 4;
287: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
288: jPanel2.add(vertexAttrsBox, gridBagConstraints);
289:
290: gridBagConstraints = new java.awt.GridBagConstraints();
291: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
292: vertexCheckBoxPanel.add(jPanel2, gridBagConstraints);
293:
294: gridBagConstraints = new java.awt.GridBagConstraints();
295: gridBagConstraints.gridy = 0;
296: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
297: guiPanel.add(vertexCheckBoxPanel, gridBagConstraints);
298:
299: geometryPanel.setLayout(new java.awt.GridBagLayout());
300:
301: createButton.setText("Create Geometry");
302: createButton
303: .addActionListener(new java.awt.event.ActionListener() {
304: public void actionPerformed(
305: java.awt.event.ActionEvent evt) {
306: createButtonActionPerformed(evt);
307: }
308: });
309:
310: gridBagConstraints = new java.awt.GridBagConstraints();
311: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
312: geometryPanel.add(createButton, gridBagConstraints);
313:
314: destroyButton.setText("Destroy Geometry");
315: destroyButton
316: .addActionListener(new java.awt.event.ActionListener() {
317: public void actionPerformed(
318: java.awt.event.ActionEvent evt) {
319: destroyButtonActionPerformed(evt);
320: }
321: });
322:
323: gridBagConstraints = new java.awt.GridBagConstraints();
324: gridBagConstraints.gridx = 0;
325: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
326: gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
327: geometryPanel.add(destroyButton, gridBagConstraints);
328:
329: gridBagConstraints = new java.awt.GridBagConstraints();
330: gridBagConstraints.gridy = 0;
331: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
332: guiPanel.add(geometryPanel, gridBagConstraints);
333:
334: mainPanel.add(guiPanel, java.awt.BorderLayout.NORTH);
335:
336: drawingPanel.setLayout(new java.awt.BorderLayout());
337:
338: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
339: mainPanel.add(drawingPanel, java.awt.BorderLayout.CENTER);
340:
341: getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
342:
343: fileMenu.setText("File");
344: exitMenuItem.setText("Exit");
345: exitMenuItem
346: .addActionListener(new java.awt.event.ActionListener() {
347: public void actionPerformed(
348: java.awt.event.ActionEvent evt) {
349: exitMenuItemActionPerformed(evt);
350: }
351: });
352:
353: fileMenu.add(exitMenuItem);
354:
355: jMenuBar1.add(fileMenu);
356:
357: setJMenuBar(jMenuBar1);
358:
359: pack();
360: }// </editor-fold>//GEN-END:initComponents
361:
362: private void destroyButtonActionPerformed(
363: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destroyButtonActionPerformed
364: if (scene != null) {
365: univ.getLocale().removeBranchGraph(scene);
366: scene = null;
367: }
368: }//GEN-LAST:event_destroyButtonActionPerformed
369:
370: private void createButtonActionPerformed(
371: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
372: if (scene == null) {
373: boolean hasVertexAttrs = vertexAttrsBox.isSelected();
374: scene = createSceneGraph(hasVertexAttrs);
375: univ.addBranchGraph(scene);
376: }
377: }//GEN-LAST:event_createButtonActionPerformed
378:
379: private void exitMenuItemActionPerformed(
380: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
381: System.exit(0);
382: }//GEN-LAST:event_exitMenuItemActionPerformed
383:
384: /** Exit the Application */
385: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
386: System.exit(0);
387: }//GEN-LAST:event_exitForm
388:
389: /**
390: * @param args the command line arguments
391: */
392: public static void main(String args[]) {
393: java.awt.EventQueue.invokeLater(new Runnable() {
394: public void run() {
395: new VertexAttrTestGLSL().setVisible(true);
396: }
397: });
398: }
399:
400: // Variables declaration - do not modify//GEN-BEGIN:variables
401: private javax.swing.JButton createButton;
402: private javax.swing.JButton destroyButton;
403: private javax.swing.JPanel drawingPanel;
404: private javax.swing.JMenuItem exitMenuItem;
405: private javax.swing.JMenu fileMenu;
406: private javax.swing.JPanel geometryPanel;
407: private javax.swing.JPanel guiPanel;
408: private javax.swing.JMenuBar jMenuBar1;
409: private javax.swing.JPanel jPanel1;
410: private javax.swing.JPanel jPanel2;
411: private javax.swing.JSeparator jSeparator1;
412: private javax.swing.JSeparator jSeparator2;
413: private javax.swing.JPanel mainPanel;
414: private javax.swing.JCheckBox vertexAttrsBox;
415: private javax.swing.JPanel vertexCheckBoxPanel;
416: // End of variables declaration//GEN-END:variables
417:
418: }
|