Utilisation de l'Immediate mode.
/*
* @(#)PureImmediate.java 1.7 98/04/09 15:33:37
*
* Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
/**
* Pure immediate mode example program. In pure immediate mode, the
* renderer must be stopped on the Canvas being rendered into. In our
* example, this is done immediately after the canvas is created. A
* separate thread is started up to do the immediate mode rendering.
*/
public class PureImmediate extends Applet implements Runnable {
private Canvas3D canvas;
private Thread t = null;
private GraphicsContext3D gc = null;
private Geometry cube = null;
private long startTime = 0L;
private Transform3D cmt = new Transform3D();
// 1/6 revolution (60 degrees) of rotation per second
private static final double rotationPerSecond = (2.0 * Math.PI) / 6.0;
//
// Renders a single frame by clearing the canvas, drawing the
// geometry, and swapping the draw and display buffer.
//
public void render() {
if (gc == null) {
// Set up Graphics context
gc = canvas.getGraphicsContext3D();
gc.setAppearance(new Appearance());
// Set up geometry
cube = new ColorCube(0.4).getGeometry();
}
// Get current system time
long currTime = System.currentTimeMillis();
if (startTime == 0)
startTime = currTime;
// Compute angle of rotation based on elapsed time since startup
long elapsedTime = currTime - startTime;
double angle = (double)elapsedTime / 1000.0 * rotationPerSecond;
cmt.rotY(angle);
// Render the geometry for this frame
gc.clear();
gc.setModelTransform(cmt);
gc.draw(cube);
canvas.swap();
}
//
// Run method for our immediate mode rendering thread.
//
public void run() {
System.out.println("PureImmediate.run: starting main loop");
while (true) {
render();
Thread.yield();
}
}
//
//Applet init routine (called by browser or by MainFrame)
//
public void init() {
setLayout(new BorderLayout());
canvas = new Canvas3D(null);
canvas.stopRenderer();
add("Center", canvas);
// Create a simple scene and attach it to the virtual universe
SimpleUniverse u = new SimpleUniverse(canvas);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
// Start a new thread that will continuously render
t = new Thread(this);
t.start();
}
//
// The following allows PureImmediate to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new PureImmediate(), 256, 256);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr