Paul,

You need to create an offscreen canvas to do offscreen rendering.
Included is an example of how offscreen rendering works.

-Charmaine Lee
Java3D Engineering Team



> MIME-Version: 1.0
> X-Priority: 3 (Normal)
> X-MSMail-Priority: Normal
> Importance: Normal
> X-MimeOLE: Produced By Microsoft MimeOLE V5.00.3018.1300
> Date: Mon, 29 Jan 2001 15:54:33 -0500
> From: Paul Siegel <[EMAIL PROTECTED]>
> Subject: [JAVA3D] Off Screen Rendering
> To: [EMAIL PROTECTED]
>
>
> I know a whole thread just passed by about off screen rendering, and I
> foolishly paid it no heed, not realizing how soon I'd need the info.
> Anyway, I think I'm setting up off screen rendering correctly in my applet,
> but when I draw the off screen buffer to the screen, it just shows up black.
> I've tried changing it to not use an off screen buffer and just add the
> Canvas 3D object to my applet, and I see a spinning cube.  So obviously I'm
> doing something wrong.
>
> Anyway, I'm attaching my code in hopes someone with experience doing this
> sort of thing will give it the once over and tell me what I'm doing wrong.
> Or, if someone has a nice working example I can look at, that would be cool
> too.  Thanks.
>
> Paul
/*
 *
 *    "@(#)SimpleOffScreenTest.java 1.1 99/11/17"
 *
 * 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.awt.*;
import java.applet.*;
import java.awt.image.BufferedImage;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.applet.MainFrame;

public class SimpleOffScreenTest extends Applet {

    static final int WIDTH = 200;
    static final int HEIGHT = 200;

    ImageComponent2D buffer;

    public SimpleOffScreenTest() {
        createOffScreenImage();
    }

    public void paint(Graphics g) {
        g.drawImage(buffer.getImage(), 0, 0, this);
    }

    void createOffScreenImage() {
        Canvas3D c = getOffScreenCanvas();

        SimpleUniverse u = new SimpleUniverse(c);
        u.getViewingPlatform().setNominalViewingTransform();
        BranchGroup bg = createSceneGraph();
        u.addBranchGraph(bg);

        createBufferImage();
        c.setOffScreenBuffer(buffer);
        c.renderOffScreenBuffer();
        c.waitForOffScreenRendering();
    }

    Canvas3D getOffScreenCanvas() {
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D c = new Canvas3D(config, true);
        Screen3D scr = c.getScreen3D();
        scr.setSize(new Dimension(1280, 1024));
        scr.setPhysicalScreenWidth(0.36124);
        scr.setPhysicalScreenHeight(0.28899555);
        return c;
    }

    void createBufferImage() {
        BufferedImage offScreenImg = new BufferedImage(WIDTH, WIDTH ,
                                               BufferedImage.TYPE_INT_ARGB);
        buffer = new ImageComponent2D(ImageComponent.FORMAT_RGBA, offScreenImg);
        buffer.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
    }

    BranchGroup createSceneGraph() {
        BranchGroup objRoot = new BranchGroup();
        objRoot.addChild(new ColorCube(0.4));
        return objRoot;
    }

    public static void main(String argv[]) {
        new MainFrame(new SimpleOffScreenTest(), WIDTH, HEIGHT);
    }
}

Reply via email to