RenderToTexture class

Using RTT is not easy for me. :cry:

It’s an convenience class for RTT.

By default, RTT is updated per 1/30 sec.



the use case

[java]

// create

rtt = new RenderToTexture(“RTT”, 512, 512, Format.RGB8, 0, new Camera(512, 512), renderManager);



// add offscreen scenes

rtt.attachOffscreenScene(scene);



// add texture to real scene

material.setColorTexture(rtt);

[/java]





[java]

public class RenderToTexture extends Texture2D implements SceneProcessor {

private RenderManager renderManager;

private ViewPort viewport;

private boolean enabled = true;

private transient float time;

private transient float updateTime = 1 / 30f;



private ByteBuffer cpuBuf;



private LinkedList<Spatial> offscreenScenes;



/**

  • @param name Pre ViewPort’s name
  • @param width
  • @param height
  • @param renderManager

    */

    public RenderToTexture(String name, int width, int height, RenderManager renderManager) {

    this(name, width, height, Format.RGBA8, 1, new Camera(width, height), renderManager);

    }

    /**
  • @param name
  •        Pre ViewPort's name<br />
    
  • @param width
  •        Texture width<br />
    
  • @param height
  •        Texture height<br />
    
  • @param format
  •        image format<br />
    
  • @param sample
  • @param cam
  • @param renderManager

    */

    public RenderToTexture(String name, int width, int height, Format format, int sample, Camera camera, RenderManager renderManager) {

    MemoryTracer.trace(this);

    setMinFilter(Texture.MinFilter.Trilinear);

    init(name, format, sample, camera, renderManager);

    }



    private void init(String viewPortName, Format format, int sample, Camera camera, RenderManager renderManager) {

    this.renderManager = renderManager;

    final int width = camera.getWidth();

    final int height = camera.getHeight();

    viewport = renderManager.createPreView(viewPortName, camera);

    viewport.setClearEnabled(true);

    viewport.setBackgroundColor(ColorUtil.TRANSPARENT_RGBA);

    // setup framebuffer’s texture

    Image image = new Image(format, width, height, null);

    setImage(image);



    FrameBuffer frameBuffer = new FrameBuffer(width, height, sample);

    frameBuffer.setDepthBuffer(Format.Depth);

    frameBuffer.setColorTexture(this);



    viewport.setOutputFrameBuffer(frameBuffer);

    }



    public ViewPort getViewPort() {

    return viewport;

    }



    /**
  • set Rendering scene

    *
  • @param target

    */

    public void setScene(final Spatial… target) {

    viewport.clearScenes();

    attachScene(target);

    }



    /**
  • add scenes which is updated elsewhere

    *
  • @param target
  • @author mulova

    */

    public void attachScene(final Spatial… target) {

    for (Spatial spatial : target) {

    viewport.attachScene(spatial);

    }

    }



    /**
  • add scenes which is not updated elsewhere

    *
  • @param target
  • @author mulova

    */

    public void attachOffscreenScene(final Spatial… target) {

    if (offscreenScenes == null) {

    offscreenScenes = new LinkedList<Spatial>();

    viewport.addProcessor(this);

    }

    for (Spatial s : target) {

    offscreenScenes.add(s);

    viewport.attachScene(s);

    }

    }



    public boolean hasScene(final Spatial target) {

    return viewport.getScenes().contains(target);

    }



    public Camera getCamera() {

    return viewport.getCamera();

    }



    public void setBackgroundColor(final ReadOnlyColorRGBA bg) {

    viewport.setBackgroundColor(bg);

    }



    public void setRefreshTime(final float time) {

    updateTime = time;

    }



    public void setEnabled(final boolean enable) {

    viewport.setEnabled(enable);

    }



    /**
  • update framebuffer even if disabled

    */

    public void takeSnapshot() {

    time = updateTime;

    }



    public void reset() {

    viewport.clearScenes();

    setEnabled(false);

    }



    /**
  • dispose native resources

    */

    public void dispose() {

    reset();

    setEnabled(false);

    }



    /**
  • @param bufferedImage
  •        [out] buffer to store FrameBuffer snapshot. if null, create a new BufferedImage<br />
    
  • @return bufferedImage.

    */

    public BufferedImage getBufferedImage(BufferedImage bufferedImage) {

    final int width = getCamera().getWidth();

    final int height = getCamera().getHeight();

    if (cpuBuf == null) {

    cpuBuf = BufferUtils.createByteBuffer(width * height * 4);

    }

    if (bufferedImage == null) {

    bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);

    }

    cpuBuf.clear();

    renderManager.getRenderer().readFrameBuffer(viewport.getOutputFrameBuffer(), cpuBuf);



    synchronized (bufferedImage) {

    Screenshots.convertScreenShot(cpuBuf, bufferedImage);

    }

    return bufferedImage;

    }



    /**
  • setup framebuffer to use renderbuffer this is faster for gpu -> cpu copies

    */

    public void useRenderBuffer() {

    FrameBuffer fb = getViewPort().getOutputFrameBuffer();

    fb.setDepthBuffer(Format.Depth);

    fb.setColorBuffer(Format.RGBA8);

    }



    @Override

    public void initialize(RenderManager rm, ViewPort vp) {

    }



    @Override

    public void reshape(ViewPort vp, int w, int h) {

    }



    @Override

    public boolean isInitialized() {

    return false;

    }



    @Override

    public void preFrame(float tpf) {

    for (Spatial s : offscreenScenes) {

    s.updateLogicalState(tpf);

    s.updateGeometricState();

    }

    time += tpf;

    if (time >= updateTime) {

    time = 0;

    viewport.setEnabled(enabled);

    } else {

    viewport.setEnabled(false);

    }

    }



    @Override

    public void postQueue(RenderQueue rq) {

    }



    @Override

    public void postFrame(FrameBuffer out) {

    viewport.setEnabled(enabled);

    }



    @Override

    public void cleanup() {

    renderManager.removePreView(viewport);

    }



    @Override

    public void read(JmeImporter importer) throws IOException {

    super.read(importer);

    InputCapsule in = importer.getCapsule(this);

    int sample = in.readInt("sample", 0);

    Camera camera = (Camera) in.readSavable("camera", null);

    String name = in.readString("viewPortName", "RenderToTexture");

    init(name, Format.RGB8, sample, camera, Global.getRenderManager());

    updateTime = in.readFloat("updateTime", 1 / 30f);

    enabled = in.readBoolean("enabled", true);

    }



    @Override

    public void write(JmeExporter exporter) throws IOException {

    super.write(exporter);

    OutputCapsule out = exporter.getCapsule(this);

    out.write(viewport.getCamera(), "camera", null);

    out.write(viewport.getOutputFrameBuffer().getSamples(), "sample", 0);

    out.write(viewport.getName(), "viewPortName", null);

    out.write(updateTime, "updateTime", 1 / 30f);

    out.write(enabled, "enabled", true);

    }

    }

    [/java]