001: /*
002: * $RCSfile: PureImmediateStereo.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:51 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.pure_immediate;
046:
047: import java.util.*;
048: import com.sun.j3d.utils.geometry.*;
049: import com.sun.j3d.utils.universe.*;
050: import javax.media.j3d.*;
051: import javax.vecmath.*;
052: import java.awt.GraphicsConfiguration;
053: import java.awt.GraphicsEnvironment;
054:
055: /**
056: * Pure immediate mode stereo example program for stereo. In pure
057: * immediate mode, the renderer must be stopped on the Canvas being
058: * rendered into. In our example, this is done immediately after the
059: * canvas is created. A separate thread is started up to do the
060: * immediate mode rendering.
061: */
062:
063: public class PureImmediateStereo extends javax.swing.JFrame implements
064: Runnable {
065:
066: private SimpleUniverse univ = null;
067: private BranchGroup scene = null;
068:
069: // Set this to true when the graphics card use shared z buffer
070: // in stereo mode.
071: public static String defaultSharedStereoZbuffer = Boolean.TRUE
072: .toString();
073:
074: private boolean sharedStereoZbuffer;
075: private boolean stereoSupport;
076: private Canvas3D canvas;
077: private GraphicsContext3D gc;
078: private Shape3D leftConeBody, rightConeBody;
079: private Shape3D leftConeCap, rightConeCap;
080: private Transform3D cmt = new Transform3D();
081: private Vector3f leftTrans, rightTrans;
082:
083: // One rotation (2*PI radians) every 6 seconds
084: private Alpha rotAlpha = new Alpha(-1, 6000);
085: private double angle;
086:
087: // Compute data which is common for both
088: // left and right eye
089: void computeSharedData() {
090: // Compute angle of rotation based on alpha value
091: angle = rotAlpha.value() * 2.0 * Math.PI;
092: cmt.rotY(angle);
093: }
094:
095: // Render the geometry in right eye
096: void renderLeft() {
097: cmt.setTranslation(leftTrans);
098: gc.setModelTransform(cmt);
099:
100: if (sharedStereoZbuffer) {
101: // Graphics card shared same z buffer in stereo mode,
102: // in this case we have to explicitly clearing both
103: // frame buffers.
104: gc.clear();
105: }
106: gc.draw(leftConeBody);
107: gc.draw(leftConeCap);
108: }
109:
110: // Render the geometry for right eye
111: void renderRight() {
112: cmt.setTranslation(rightTrans);
113: gc.setModelTransform(cmt);
114:
115: if (sharedStereoZbuffer) {
116: // Graphics card shared same z buffer in stereo mode,
117: // in this case we have to explicitly clearing both
118: // frame buffers.
119: gc.clear();
120: }
121: gc.draw(rightConeBody);
122: gc.draw(rightConeCap);
123: }
124:
125: //
126: // Run method for our immediate mode rendering thread.
127: //
128: public void run() {
129: // Set up Graphics context
130: gc = canvas.getGraphicsContext3D();
131:
132: // We always need to set this for PureImmediate
133: // stereo mode
134: gc.setBufferOverride(true);
135:
136: Color3f lightColor = new Color3f(1, 1, 1);
137: Vector3f lightDir = new Vector3f(0, 0, -1);
138: DirectionalLight light = new DirectionalLight(lightColor,
139: lightDir);
140:
141: gc.addLight(light);
142:
143: Appearance redApp = new Appearance();
144: Appearance greenApp = new Appearance();
145: Color3f ambientColor = new Color3f(0, 0, 0);
146: Color3f emissiveColor = new Color3f(0, 0, 0);
147: Color3f diffuseColor = new Color3f(1, 0, 0);
148: Color3f specularColor = new Color3f(1, 1, 1);
149: redApp.setMaterial(new Material(ambientColor, emissiveColor,
150: diffuseColor, specularColor, 5));
151: diffuseColor = new Color3f(0, 1, 0);
152:
153: greenApp.setMaterial(new Material(ambientColor, emissiveColor,
154: diffuseColor, specularColor, 5));
155:
156: // Set up geometry
157: Cone leftCone = new Cone(0.4f, 0.6f,
158: Primitive.GENERATE_NORMALS, redApp);
159: Cone rightCone = new Cone(0.4f, 0.6f,
160: Primitive.GENERATE_NORMALS, greenApp);
161: leftConeBody = leftCone.getShape(Cone.BODY);
162: leftConeCap = leftCone.getShape(Cone.CAP);
163:
164: rightConeBody = rightCone.getShape(Cone.BODY);
165: rightConeCap = rightCone.getShape(Cone.CAP);
166: leftTrans = new Vector3f(-0.6f, 0, 0);
167: rightTrans = new Vector3f(0.6f, 0, 0);
168:
169: while (true) {
170: // compute data which is can be used
171: // for both left and right eye
172: computeSharedData();
173:
174: if (stereoSupport) {
175: if (!sharedStereoZbuffer) {
176: gc.setStereoMode(GraphicsContext3D.STEREO_BOTH);
177: // This clear both left and right buffers, we
178: // must set STEREO_BOTH before it. Otherwise
179: // it only clear LEFT or RIGHT buffer unless
180: // this is invoke twice for each buffer.
181: gc.clear();
182: }
183:
184: gc.setStereoMode(GraphicsContext3D.STEREO_LEFT);
185: renderLeft();
186:
187: gc.setStereoMode(GraphicsContext3D.STEREO_RIGHT);
188: renderRight();
189: } else {
190: gc.clear();
191: renderLeft();
192: }
193:
194: // This swap both left and right buffers so
195: // there is no need to set STEREO_BOTH before it
196: canvas.swap();
197:
198: // Be polite to other threads !
199: Thread.yield();
200: }
201: }
202:
203: private void createUniverse() {
204: // Preferred to use Stereo
205: GraphicsConfigTemplate3D gct = new GraphicsConfigTemplate3D();
206: gct.setStereo(GraphicsConfigTemplate3D.PREFERRED);
207:
208: GraphicsConfiguration config = GraphicsEnvironment
209: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
210: .getBestConfiguration(gct);
211:
212: canvas = new Canvas3D(config);
213: Map map = canvas.queryProperties();
214:
215: stereoSupport = canvas.getStereoAvailable();
216:
217: if (stereoSupport) {
218: System.out
219: .println("This machine support stereo, you should see a red cone on the left and green cone on the right.");
220: // User can overide the above default behavior using
221: // java3d property.
222: String str = System.getProperty("j3d.sharedstereozbuffer",
223: defaultSharedStereoZbuffer);
224: sharedStereoZbuffer = (new Boolean(str)).booleanValue();
225: } else {
226: System.out
227: .println("Stereo is not support, you should only see the left red cone.");
228: }
229:
230: if (!canvas.getDoubleBufferAvailable()) {
231: System.out.println("Double buffer is not support !");
232: }
233:
234: // we must stop the Renderer in PureImmediate mode
235: canvas.stopRenderer();
236:
237: // Create simple universe with view branch
238: univ = new SimpleUniverse(canvas);
239:
240: // This will move the ViewPlatform back a bit so the
241: // objects in the scene can be viewed.
242: univ.getViewingPlatform().setNominalViewingTransform();
243:
244: // Ensure at least 5 msec per frame (i.e., < 200Hz)
245: univ.getViewer().getView().setMinimumFrameCycleTime(5);
246:
247: // Start a new thread that will continuously render
248: (new Thread(this )).start();
249: }
250:
251: /**
252: * Creates new form PureImmediateStereo
253: */
254: public PureImmediateStereo() {
255: // Initialize the GUI components
256: initComponents();
257:
258: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
259: createUniverse();
260: drawingPanel.add(canvas, java.awt.BorderLayout.CENTER);
261: }
262:
263: // ----------------------------------------------------------------
264:
265: /** This method is called from within the constructor to
266: * initialize the form.
267: * WARNING: Do NOT modify this code. The content of this method is
268: * always regenerated by the Form Editor.
269: */
270: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
271: private void initComponents() {
272: drawingPanel = new javax.swing.JPanel();
273:
274: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
275: setTitle("PureImmediateStereo");
276: drawingPanel.setLayout(new java.awt.BorderLayout());
277:
278: drawingPanel.setPreferredSize(new java.awt.Dimension(512, 256));
279: getContentPane()
280: .add(drawingPanel, java.awt.BorderLayout.CENTER);
281:
282: pack();
283: }// </editor-fold>//GEN-END:initComponents
284:
285: /**
286: * @param args the command line arguments
287: */
288: public static void main(String args[]) {
289: java.awt.EventQueue.invokeLater(new Runnable() {
290: public void run() {
291: new PureImmediateStereo().setVisible(true);
292: }
293: });
294: }
295:
296: // Variables declaration - do not modify//GEN-BEGIN:variables
297: private javax.swing.JPanel drawingPanel;
298: // End of variables declaration//GEN-END:variables
299:
300: }
|