001: /*
002: * $RCSfile: PureImmediate.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 com.sun.j3d.utils.universe.*;
048: import com.sun.j3d.utils.geometry.ColorCube;
049: import javax.media.j3d.*;
050: import java.awt.GraphicsConfiguration;
051:
052: /**
053: * Pure immediate mode example program. In pure immediate mode, the
054: * renderer must be stopped on the Canvas being rendered into. In our
055: * example, this is done immediately after the canvas is created. A
056: * separate thread is started up to do the immediate mode rendering.
057: */
058: public class PureImmediate extends javax.swing.JFrame implements
059: Runnable {
060:
061: private SimpleUniverse univ = null;
062: private BranchGroup scene = null;
063:
064: private Canvas3D canvas;
065: private GraphicsContext3D gc = null;
066: private Geometry cube = null;
067: private Transform3D cmt = new Transform3D();
068:
069: // One rotation (2*PI radians) every 6 seconds
070: private Alpha rotAlpha = new Alpha(-1, 6000);
071:
072: //
073: // Renders a single frame by clearing the canvas, drawing the
074: // geometry, and swapping the draw and display buffer.
075: //
076: public void render() {
077: if (gc == null) {
078: // Set up Graphics context
079: gc = canvas.getGraphicsContext3D();
080: gc.setAppearance(new Appearance());
081:
082: // Set up geometry
083: cube = new ColorCube(0.4).getGeometry();
084: }
085:
086: // Compute angle of rotation based on alpha value
087: double angle = rotAlpha.value() * 2.0 * Math.PI;
088: cmt.rotY(angle);
089:
090: // Render the geometry for this frame
091: gc.clear();
092: gc.setModelTransform(cmt);
093: gc.draw(cube);
094: canvas.swap();
095: }
096:
097: //
098: // Run method for our immediate mode rendering thread.
099: //
100: public void run() {
101: System.out.println("PureImmediate.run: starting main loop");
102: while (true) {
103: render();
104: Thread.yield();
105: }
106: }
107:
108: private void createUniverse() {
109: // Get the preferred graphics configuration for the default screen
110: GraphicsConfiguration config = SimpleUniverse
111: .getPreferredConfiguration();
112:
113: // Create a Canvas3D using the preferred configuration
114: canvas = new Canvas3D(config);
115: canvas.stopRenderer();
116: // Create simple universe with view branch
117: univ = new SimpleUniverse(canvas);
118:
119: // This will move the ViewPlatform back a bit so the
120: // objects in the scene can be viewed.
121: univ.getViewingPlatform().setNominalViewingTransform();
122:
123: // Ensure at least 5 msec per frame (i.e., < 200Hz)
124: univ.getViewer().getView().setMinimumFrameCycleTime(5);
125: }
126:
127: /**
128: * Creates new form PureImmediate
129: */
130: public PureImmediate() {
131: // Initialize the GUI components
132: initComponents();
133:
134: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
135: createUniverse();
136: drawingPanel.add(canvas, java.awt.BorderLayout.CENTER);
137:
138: // Start a new thread that will continuously render
139: new Thread(this ).start();
140: }
141:
142: // ----------------------------------------------------------------
143:
144: /** This method is called from within the constructor to
145: * initialize the form.
146: * WARNING: Do NOT modify this code. The content of this method is
147: * always regenerated by the Form Editor.
148: */
149: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
150: private void initComponents() {
151: drawingPanel = new javax.swing.JPanel();
152:
153: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
154: setTitle("PureImmediate");
155: drawingPanel.setLayout(new java.awt.BorderLayout());
156:
157: drawingPanel.setPreferredSize(new java.awt.Dimension(250, 250));
158: getContentPane()
159: .add(drawingPanel, java.awt.BorderLayout.CENTER);
160:
161: pack();
162: }// </editor-fold>//GEN-END:initComponents
163:
164: /**
165: * @param args the command line arguments
166: */
167: public static void main(String args[]) {
168: java.awt.EventQueue.invokeLater(new Runnable() {
169: public void run() {
170: new PureImmediate().setVisible(true);
171: }
172: });
173: }
174:
175: // Variables declaration - do not modify//GEN-BEGIN:variables
176: private javax.swing.JPanel drawingPanel;
177: // End of variables declaration//GEN-END:variables
178:
179: }
|