> >From: Dongming Zhang <[EMAIL PROTECTED]>
> >
> >Anybody knows or experienced how to render a scene graph
> >in mixed immediate mode?
Attached below is are two examples which ways of using mixed immediate mode.
Both draw a "rubber band" line in immediate mode as the main mouse button is
dragged over the J3D scene.
MixedImmediate uses double buffered immediate mode. It inquires the current
image on the canvas and then uses that as the background for the immediate mode
rendering. The result is that the immediate mode rendering is very cleanly
rendered on top of the J3D scene, at the expense of reading the raster and
drawing the scene as an image.
MixedImmediateFront uses single buffered immediate mode. The rubber band line
is drawn and undrawn (drawn in the background color) in the front buffer. To
render to the front buffer the canvas is switched to single buffered mode when
starting immediate mode. This mechanism doesn't work in J3D 1.1.1, but it is
fixed in 1.1.2.
One other way of using immediate mode is to draw your immediate mode primitives
in the Canvas3D.postRender() method. This would allow you to draw your
primitives on top of the J3D scene but before the buffers are swapped (the same
effect should be possible by adding putting the geometry into the J3D scene
graph).
Doug Gehringer
Sun Microsystems
/*
* %Z%%M% %I% %E% %U%
*
* Copyright (c) 1996-1999 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.*;
import java.awt.*;
import java.awt.image.DataBufferInt;
import java.awt.image.BufferedImage;
import java.util.Vector;
import java.util.Enumeration;
import java.text.NumberFormat;
import java.lang.Thread;
/**
* An example of mixing immediate mode rendering with normal rendering. The
* "HelloUniverse" spinning cube is draw until there is a mouse button pressed.
* Then a rubber-band line is drawn in immediate mode in front of the existing
* image using a Background created from the original image. This technique is
* only useful when drawing an image of the screen is faster than redrawing
* the screen and when the immediate mode rendering can be done on top of a
* static image of the screen.
*/
public class MixedImmediate extends Applet {
Canvas3D canvas;
GraphicsContext3D gc = null;
LineArray line = null;
Transform3D sphereTransform = new Transform3D();
WakeupCriterion[] mouseEvents;
WakeupOr mouseCriterion;
Point3d coords[] = new Point3d[2];
Point3d baseMouse = new Point3d();
Point3d curMouse = new Point3d();
Transform3D ipToVworld = new Transform3D();
boolean immMode = false;
Vector inputQueue = new Vector();
BufferedImage bImage = null;
ImageComponent2D canvasImageComponent;
Raster canvasImage;
Background canvasBackground;
boolean gotBase = false;
NumberFormat nf;
long startTime;
int reportTime = 10;
int numImmRenders = 0;
long bgTime = 0;
long totalTime = 0;
// set to true to see timing info
static final boolean timing = false;
public void setBase(int x, int y) {
canvas.getPixelLocationInImagePlate(x, y, baseMouse);
canvas.getImagePlateToVworld(ipToVworld);
ipToVworld.transform(baseMouse);
gotBase = true;
}
public void render(int x, int y) {
canvas.getPixelLocationInImagePlate(x, y, curMouse);
canvas.getImagePlateToVworld(ipToVworld);
ipToVworld.transform(curMouse);
line = new LineArray(2, LineArray.COORDINATES);
coords[0] = baseMouse;
coords[1] = curMouse;
line.setCoordinates(0, coords);
// Render the geometry for this frame
long start = 0, bg = 0;
if (timing) {
start = System.currentTimeMillis();
}
gc.clear();
//gc.draw(canvasImage); // faster to draw image using Background
if (timing) {
bg = System.currentTimeMillis();
}
gc.draw(line);
canvas.swap();
if (timing) {
long total = System.currentTimeMillis();
bgTime += bg - start;
totalTime += total - start;
numImmRenders++;
}
}
//
//Applet init routine (called by browser or by MainFrame)
//
public void init() {
setLayout(new BorderLayout());
if (timing) {
resetTiming();
}
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
canvas = new CallbackCanvas3D(null);
add("Center", canvas);
// set up the graphics context
gc = canvas.getGraphicsContext3D();
gc.setAppearance(new Appearance());
// 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();
BranchGroup scene = createSceneGraph();
u.addBranchGraph(scene);
BranchGroup behaviorRoot = new BranchGroup();
behaviorRoot.addChild(new Callback());
u.addBranchGraph(behaviorRoot);
}
void initImmMode() {
canvas.stopRenderer();
Dimension canvasSize = canvas.getSize();
if ((canvasImageComponent == null) ||
(canvasSize.width != canvasImageComponent.getWidth()) ||
(canvasSize.height != canvasImageComponent.getHeight())) {
canvasImageComponent = new ImageComponent2D(
ImageComponent.FORMAT_RGB, canvasSize.width,
canvasSize.height);
canvasImage = new Raster(new Point3f(),
Raster.RASTER_COLOR, 0, 0, canvasSize.width,
canvasSize.height, canvasImageComponent, null);
}
gc.readRaster(canvasImage);
canvasBackground = new Background(canvasImageComponent);
gc.setBackground(canvasBackground);
}
void finishImmMode() {
canvas.startRenderer();
}
// called when we get a "mouse up" event and end imm mode
void clearRender() {
gotBase = false;
}
void processImmEvent() {
MouseEvent mevent = (MouseEvent) inputQueue.remove(0);
if (!gotBase) {
setBase(mevent.getX(), mevent.getY());
} else {
render(mevent.getX(), mevent.getY());
}
}
public class CallbackCanvas3D extends Canvas3D {
CallbackCanvas3D(GraphicsConfiguration config) {
super(config);
}
public void postSwap() {
if (immMode) {
initImmMode();
if (timing) {
resetTiming();
}
while (immMode == true) {
while (!inputQueue.isEmpty()) {
processImmEvent();
}
Thread.yield(); // allow others to run, but spin for best
// response time, could also put a small
// sleep here
}
if (timing) {
reportTiming();
}
clearRender();
finishImmMode();
}
}
}
// Behavior thread methods to start and end imm mode
// TODO: need some kind of lock on immMode?
void startImmMode() {
if (immMode != true) {
immMode = true;
// If we didn't have a behavior which was continously animating
// we would need to enable a wakeOnElapsedFrames(0) here to ensure
// that Canvas3D.postSwap() get called
}
}
void endImmMode() {
immMode = false;
}
public class Callback extends Behavior {
public void initialize() {
mouseEvents = new WakeupCriterion[3];
mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
mouseCriterion = new WakeupOr(mouseEvents);
wakeupOn (mouseCriterion);
setSchedulingBounds(new BoundingSphere(new Point3d(),1000));
}
public void processStimulus (Enumeration criteria) {
WakeupCriterion wakeup;
AWTEvent[] event;
int id;
int dx, dy;
while (criteria.hasMoreElements()) {
wakeup = (WakeupCriterion) criteria.nextElement();
if (wakeup instanceof WakeupOnAWTEvent) {
event = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
for (int i=0; i<event.length; i++) {
MouseEvent mevent = (MouseEvent) event[i];
if (!mevent.isMetaDown() &&
!mevent.isAltDown()){
id = event[i].getID();
switch (id) {
case MouseEvent.MOUSE_PRESSED:
startImmMode();
// fall through to add event to queue
case MouseEvent.MOUSE_DRAGGED:
inputQueue.add(mevent);
break;
case MouseEvent.MOUSE_RELEASED:
//System.out.println("start renderer");
endImmMode();
break;
}
}
}
}
}
wakeupOn (mouseCriterion);
}
}
void resetTiming() {
startTime = System.currentTimeMillis();
numImmRenders = 0;
bgTime = 0;
totalTime = 0;
}
void reportTiming() {
long endTime = System.currentTimeMillis();
double elapsed = (endTime - startTime) / 1000.0;
System.out.println(numImmRenders + " renders in " +
nf.format(elapsed) +
" sec, " + nf.format(numImmRenders / elapsed) +
" renders/sec, per-render: bg: " +
nf.format((float) bgTime / numImmRenders) +
"ms total: " +
nf.format((float) totalTime / numImmRenders) +
"ms");
}
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// Create a simple shape leaf node, add it to the scene graph.
objTrans.addChild(new ColorCube(0.4));
// Create a new Behavior object that will perform the desired
// operation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
// Have Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
//
// The following allows MixedImmediate to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new MixedImmediate(), 600, 600);
}
}
/*
* @(#)MixedImmediateFront.java 1.2 99/03/29 12:50:59
*
* Copyright (c) 1996-1999 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.*;
import java.awt.*;
import java.awt.image.DataBufferInt;
import java.awt.image.BufferedImage;
import java.util.Enumeration;
import java.util.Vector;
import java.text.NumberFormat;
import java.lang.Thread;
/**
* An example of mixing immediate mode rendering with normal rendering. The
* "HelloUniverse" spinning cube is draw until there is a mouse button pressed.
* Then a rubber-band line is drawn in immediate mode in front of the existing
* image by making updates to the front buffer. The previous lines (if any)
* are "undrawn" by drawing them in black (someday this could use XOR). This
* mechanism is faster than using a Background image, but it has the display
* artifacts of "undrawing".
*/
public class MixedImmediateFront extends Applet {
Canvas3D canvas;
GraphicsContext3D gc = null;
LineArray line = null;
Transform3D sphereTransform = new Transform3D();
boolean immMode;
Vector inputQueue = new Vector();
WakeupCriterion[] mouseEvents;
WakeupOr mouseCriterion;
boolean rendererRunning = true;
Point3d coords[] = new Point3d[2];
Point3d baseMouse = new Point3d();
Point3d curMouse = new Point3d();
Transform3D ipToVworld = new Transform3D();
boolean gotBase = false;
Appearance blackAppearance;
Appearance whiteAppearance;
static final boolean debug = false;
// only works in single buffer mode right now
static final boolean singleBuffer = false;
// Imm mode render methods, called from Renderer thread
public void setBase(int x, int y) {
canvas.getPixelLocationInImagePlate(x, y, baseMouse);
canvas.getImagePlateToVworld(ipToVworld);
ipToVworld.transform(baseMouse);
gotBase = true;
}
public void render(int x, int y) {
if (line != null) {
if (debug) {
System.err.println("undraw");
}
gc.setAppearance(blackAppearance);
gc.draw(line);
}
canvas.getPixelLocationInImagePlate(x, y, curMouse);
canvas.getImagePlateToVworld(ipToVworld);
ipToVworld.transform(curMouse);
line = new LineArray(2, LineArray.COORDINATES);
coords[0] = baseMouse;
coords[1] = curMouse;
line.setCoordinates(0, coords);
// Render the geometry for this frame
if (debug) {
System.err.println("draw");
}
gc.setAppearance(whiteAppearance);
gc.draw(line);
}
public void clearRender() {
// clean up for the next time
gotBase = false;
line = null;
}
// Imm mode start/event/stop methods called from postSwap method by
// Renderer thread
void initImmMode() {
canvas.stopRenderer();
canvas.setDoubleBufferEnable(false);
}
void finishImmMode() {
canvas.setDoubleBufferEnable(true);
canvas.startRenderer();
}
void processImmEvent() {
MouseEvent mevent = (MouseEvent) inputQueue.remove(0);
int id = mevent.getID();
switch (id) {
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_DRAGGED:
if (!gotBase) {
setBase(mevent.getX(), mevent.getY());
} else {
render(mevent.getX(), mevent.getY());
}
break;
}
}
public class CallbackCanvas3D extends Canvas3D {
CallbackCanvas3D(GraphicsConfiguration config) {
super(config);
}
public void postSwap() {
if (immMode) {
initImmMode();
while (immMode == true) {
while (!inputQueue.isEmpty()) {
processImmEvent();
}
Thread.yield(); // allow others to run, but spin for best
// response time, could also put a small
// sleep here
}
clearRender();
finishImmMode();
}
}
}
// Behavior thread methods to start and end imm mode
// TODO: need some kind of lock on immMode?
void startImmMode() {
if (immMode != true) {
immMode = true;
// If we didn't have a behavior which was continously animating
// we would need to enable a wakeOnElapsedFrames(0) here to ensure
// that Canvas3D.postSwap() get called
}
}
void endImmMode() {
immMode = false;
}
public class Callback extends Behavior {
public void initialize() {
mouseEvents = new WakeupCriterion[3];
mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
mouseCriterion = new WakeupOr(mouseEvents);
wakeupOn (mouseCriterion);
setSchedulingBounds(new BoundingSphere(new Point3d(),1000));
}
public void processStimulus (Enumeration criteria) {
WakeupCriterion wakeup;
AWTEvent[] event;
int id;
int dx, dy;
while (criteria.hasMoreElements()) {
wakeup = (WakeupCriterion) criteria.nextElement();
if (wakeup instanceof WakeupOnAWTEvent) {
event = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
for (int i=0; i<event.length; i++) {
MouseEvent mevent = (MouseEvent) event[i];
if (!mevent.isMetaDown() &&
!mevent.isAltDown()){
id = event[i].getID();
switch (id) {
case MouseEvent.MOUSE_PRESSED:
startImmMode();
case MouseEvent.MOUSE_DRAGGED:
inputQueue.add(mevent);
break;
case MouseEvent.MOUSE_RELEASED:
endImmMode();
break;
}
}
}
}
}
wakeupOn (mouseCriterion);
}
}
//
//Applet init routine (called by browser or by MainFrame)
//
public void init() {
setLayout(new BorderLayout());
canvas = new CallbackCanvas3D(null);
if (singleBuffer) {
canvas.setDoubleBufferEnable(false);
}
add("Center", canvas);
// set up the graphics context
gc = canvas.getGraphicsContext3D();
// set up the appearances
RenderingAttributes ra = new RenderingAttributes();
ra.setDepthBufferEnable(false);
whiteAppearance = new Appearance();
whiteAppearance.setRenderingAttributes(ra);
ColoringAttributes blackColor = new ColoringAttributes(0.0f, 0.0f, 0.0f,
ColoringAttributes.SHADE_FLAT);
blackAppearance = new Appearance();
blackAppearance.setColoringAttributes(blackColor);
blackAppearance.setRenderingAttributes(ra);
// 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();
BranchGroup scene = createSceneGraph();
u.addBranchGraph(scene);
BranchGroup behaviorRoot = new BranchGroup();
behaviorRoot.addChild(new Callback());
u.addBranchGraph(behaviorRoot);
}
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// Create a simple shape leaf node, add it to the scene graph.
objTrans.addChild(new ColorCube(0.4));
// Create a new Behavior object that will perform the desired
// operation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
// Have Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
//
// The following allows MixedImmediateFront to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new MixedImmediateFront(), 600, 600);
}
}