001: /*
002: * $RCSfile: VertexAttrTestCg.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.4 $
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.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 VertexAttrTestCg 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:
102: univ = new SimpleUniverse(c);
103:
104: // Add a ShaderErrorListener
105: univ.addShaderErrorListener(new ShaderErrorListener() {
106: public void errorOccurred(ShaderError error) {
107: error.printVerbose();
108: JOptionPane.showMessageDialog(VertexAttrTestCg.this ,
109: error.toString(), "ShaderError",
110: JOptionPane.ERROR_MESSAGE);
111: }
112: });
113:
114: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
115: // This will move the ViewPlatform back a bit so the
116: // objects in the scene can be viewed.
117: viewingPlatform.setNominalViewingTransform();
118:
119: return c;
120: }
121:
122: /**
123: * Creates new form VertexAttrTestCg
124: */
125: public VertexAttrTestCg() {
126: // Initialize the GUI components
127: initComponents();
128:
129: // Create the scene and add the Canvas3D to the drawing panel
130: Canvas3D c = initScene();
131: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
132: }
133:
134: static class MyShape extends Shape3D {
135: // Coordinate data
136: private static final float[] coords = { 0.0f, 0.0f, 0.0f, 0.5f,
137: 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, };
138:
139: private static final int[] sizes = { 1, 3 };
140: private static final float[] weights = { 0.45f, 0.15f, 0.95f, };
141: private static final float[] temps = { 1.0f, 0.5f, 0.5f, 0.5f,
142: 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, };
143:
144: private static final String[] vaNames = { "weight",
145: "temperature" };
146:
147: J3DBuffer createDirectFloatBuffer(float[] arr) {
148: ByteOrder order = ByteOrder.nativeOrder();
149:
150: FloatBuffer nioBuf = ByteBuffer.allocateDirect(
151: arr.length * 4).order(order).asFloatBuffer();
152: nioBuf.put(arr);
153: return new J3DBuffer(nioBuf);
154: }
155:
156: MyShape(JFrame frame, boolean hasVertexAttrs) {
157:
158: int vertexFormat = GeometryArray.COORDINATES;
159: int vertexAttrCount = 0;
160: int[] vertexAttrSizes = null;
161: String[] vertexAttrNames = null;
162: String[] shaderAttrNames = null;
163:
164: if (hasVertexAttrs) {
165: vertexFormat |= GeometryArray.VERTEX_ATTRIBUTES;
166: vertexAttrCount = vaNames.length;
167: vertexAttrSizes = sizes;
168: vertexAttrNames = vaNames;
169: }
170:
171: TriangleArray tri = new TriangleArray(6, vertexFormat, 0,
172: null, vertexAttrCount, vertexAttrSizes);
173: tri.setValidVertexCount(3);
174: tri.setCoordinates(0, coords);
175:
176: if (hasVertexAttrs) {
177: tri.setVertexAttrs(0, 0, weights);
178: tri.setVertexAttrs(1, 0, temps);
179:
180: String vertexProgram = null;
181: try {
182: vertexProgram = StringIO
183: .readFully(Resources
184: .getResource("cg_shader/vertexshader_vp.cg"));
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_CG,
192: Shader.SHADER_TYPE_VERTEX, vertexProgram);
193: ShaderProgram shaderProgram = new CgShaderProgram();
194: shaderProgram.setShaders(shaders);
195: shaderProgram.setVertexAttrNames(vertexAttrNames);
196:
197: ShaderAppearance app = new ShaderAppearance();
198: app.setShaderProgram(shaderProgram);
199:
200: this .setGeometry(tri);
201: this .setAppearance(app);
202:
203: } else {
204: this .setGeometry(tri);
205: this .setAppearance(new Appearance());
206: }
207: }
208: }
209:
210: // ----------------------------------------------------------------
211:
212: /** This method is called from within the constructor to
213: * initialize the form.
214: * WARNING: Do NOT modify this code. The content of this method is
215: * always regenerated by the Form Editor.
216: */
217: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
218: private void initComponents() {
219: java.awt.GridBagConstraints gridBagConstraints;
220:
221: mainPanel = new javax.swing.JPanel();
222: guiPanel = new javax.swing.JPanel();
223: vertexCheckBoxPanel = new javax.swing.JPanel();
224: jPanel1 = new javax.swing.JPanel();
225: jSeparator1 = new javax.swing.JSeparator();
226: jSeparator2 = new javax.swing.JSeparator();
227: jPanel2 = new javax.swing.JPanel();
228: vertexAttrsBox = new javax.swing.JCheckBox();
229: geometryPanel = new javax.swing.JPanel();
230: createButton = new javax.swing.JButton();
231: destroyButton = new javax.swing.JButton();
232: drawingPanel = new javax.swing.JPanel();
233: jMenuBar1 = new javax.swing.JMenuBar();
234: fileMenu = new javax.swing.JMenu();
235: exitMenuItem = new javax.swing.JMenuItem();
236:
237: setTitle("Vertex Attribute Test");
238: addWindowListener(new java.awt.event.WindowAdapter() {
239: public void windowClosing(java.awt.event.WindowEvent evt) {
240: exitForm(evt);
241: }
242: });
243:
244: mainPanel.setLayout(new java.awt.BorderLayout());
245:
246: guiPanel.setLayout(new java.awt.GridBagLayout());
247:
248: guiPanel.setBorder(javax.swing.BorderFactory
249: .createLineBorder(new java.awt.Color(0, 0, 0)));
250: vertexCheckBoxPanel.setLayout(new java.awt.GridBagLayout());
251:
252: vertexCheckBoxPanel
253: .setBorder(javax.swing.BorderFactory
254: .createTitledBorder(
255: null,
256: "vertexFormat",
257: javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
258: javax.swing.border.TitledBorder.DEFAULT_POSITION,
259: new java.awt.Font("Lucida Sans", 0, 10)));
260: jPanel1.setLayout(new java.awt.GridBagLayout());
261:
262: jSeparator1.setPreferredSize(new java.awt.Dimension(0, 4));
263: gridBagConstraints = new java.awt.GridBagConstraints();
264: gridBagConstraints.gridx = 0;
265: gridBagConstraints.gridy = 1;
266: jPanel1.add(jSeparator1, gridBagConstraints);
267:
268: jSeparator2.setPreferredSize(new java.awt.Dimension(0, 4));
269: gridBagConstraints = new java.awt.GridBagConstraints();
270: gridBagConstraints.gridx = 0;
271: gridBagConstraints.gridy = 3;
272: jPanel1.add(jSeparator2, gridBagConstraints);
273:
274: gridBagConstraints = new java.awt.GridBagConstraints();
275: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
276: vertexCheckBoxPanel.add(jPanel1, gridBagConstraints);
277:
278: jPanel2.setLayout(new java.awt.GridBagLayout());
279:
280: vertexAttrsBox.setSelected(true);
281: vertexAttrsBox.setText("VertexAttrs");
282: gridBagConstraints = new java.awt.GridBagConstraints();
283: gridBagConstraints.gridx = 0;
284: gridBagConstraints.gridy = 4;
285: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
286: jPanel2.add(vertexAttrsBox, gridBagConstraints);
287:
288: gridBagConstraints = new java.awt.GridBagConstraints();
289: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
290: vertexCheckBoxPanel.add(jPanel2, gridBagConstraints);
291:
292: gridBagConstraints = new java.awt.GridBagConstraints();
293: gridBagConstraints.gridy = 0;
294: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
295: guiPanel.add(vertexCheckBoxPanel, gridBagConstraints);
296:
297: geometryPanel.setLayout(new java.awt.GridBagLayout());
298:
299: createButton.setText("Create Geometry");
300: createButton
301: .addActionListener(new java.awt.event.ActionListener() {
302: public void actionPerformed(
303: java.awt.event.ActionEvent evt) {
304: createButtonActionPerformed(evt);
305: }
306: });
307:
308: gridBagConstraints = new java.awt.GridBagConstraints();
309: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
310: geometryPanel.add(createButton, gridBagConstraints);
311:
312: destroyButton.setText("Destroy Geometry");
313: destroyButton
314: .addActionListener(new java.awt.event.ActionListener() {
315: public void actionPerformed(
316: java.awt.event.ActionEvent evt) {
317: destroyButtonActionPerformed(evt);
318: }
319: });
320:
321: gridBagConstraints = new java.awt.GridBagConstraints();
322: gridBagConstraints.gridx = 0;
323: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
324: gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
325: geometryPanel.add(destroyButton, gridBagConstraints);
326:
327: gridBagConstraints = new java.awt.GridBagConstraints();
328: gridBagConstraints.gridy = 0;
329: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
330: guiPanel.add(geometryPanel, gridBagConstraints);
331:
332: mainPanel.add(guiPanel, java.awt.BorderLayout.NORTH);
333:
334: drawingPanel.setLayout(new java.awt.BorderLayout());
335:
336: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
337: mainPanel.add(drawingPanel, java.awt.BorderLayout.CENTER);
338:
339: getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
340:
341: fileMenu.setText("File");
342: exitMenuItem.setText("Exit");
343: exitMenuItem
344: .addActionListener(new java.awt.event.ActionListener() {
345: public void actionPerformed(
346: java.awt.event.ActionEvent evt) {
347: exitMenuItemActionPerformed(evt);
348: }
349: });
350:
351: fileMenu.add(exitMenuItem);
352:
353: jMenuBar1.add(fileMenu);
354:
355: setJMenuBar(jMenuBar1);
356:
357: pack();
358: }// </editor-fold>//GEN-END:initComponents
359:
360: private void destroyButtonActionPerformed(
361: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destroyButtonActionPerformed
362: if (scene != null) {
363: univ.getLocale().removeBranchGraph(scene);
364: scene = null;
365: }
366: }//GEN-LAST:event_destroyButtonActionPerformed
367:
368: private void createButtonActionPerformed(
369: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
370: if (scene == null) {
371: boolean hasVertexAttrs = vertexAttrsBox.isSelected();
372: scene = createSceneGraph(hasVertexAttrs);
373: univ.addBranchGraph(scene);
374: }
375: }//GEN-LAST:event_createButtonActionPerformed
376:
377: private void exitMenuItemActionPerformed(
378: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
379: System.exit(0);
380: }//GEN-LAST:event_exitMenuItemActionPerformed
381:
382: /** Exit the Application */
383: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
384: System.exit(0);
385: }//GEN-LAST:event_exitForm
386:
387: /**
388: * @param args the command line arguments
389: */
390: public static void main(String args[]) {
391: java.awt.EventQueue.invokeLater(new Runnable() {
392: public void run() {
393: new VertexAttrTestCg().setVisible(true);
394: }
395: });
396: }
397:
398: // Variables declaration - do not modify//GEN-BEGIN:variables
399: private javax.swing.JButton createButton;
400: private javax.swing.JButton destroyButton;
401: private javax.swing.JPanel drawingPanel;
402: private javax.swing.JMenuItem exitMenuItem;
403: private javax.swing.JMenu fileMenu;
404: private javax.swing.JPanel geometryPanel;
405: private javax.swing.JPanel guiPanel;
406: private javax.swing.JMenuBar jMenuBar1;
407: private javax.swing.JPanel jPanel1;
408: private javax.swing.JPanel jPanel2;
409: private javax.swing.JSeparator jSeparator1;
410: private javax.swing.JSeparator jSeparator2;
411: private javax.swing.JPanel mainPanel;
412: private javax.swing.JCheckBox vertexAttrsBox;
413: private javax.swing.JPanel vertexCheckBoxPanel;
414: // End of variables declaration//GEN-END:variables
415:
416: }
|