159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * To change this template, choose Tools | Templates
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * and open the template in the editor.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.water;
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Plane;
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.post.SceneProcessor;
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.Camera;
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.RenderManager;
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.ViewPort;
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.queue.RenderQueue;
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.FrameBuffer;
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Reflection Processor
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Used to render the reflected scene in an off view port
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class ReflectionProcessor implements SceneProcessor {
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private RenderManager rm;
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private ViewPort vp;
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Camera reflectionCam;
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private FrameBuffer reflectionBuffer;
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Plane reflectionClipPlane;
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Creates a ReflectionProcessor
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param reflectionCam the cam to use for reflection
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param reflectionBuffer the FrameBuffer to render to
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param reflectionClipPlane the clipping plane
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public ReflectionProcessor(Camera reflectionCam, FrameBuffer reflectionBuffer, Plane reflectionClipPlane) {
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.reflectionCam = reflectionCam;
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.reflectionBuffer = reflectionBuffer;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.reflectionClipPlane = reflectionClipPlane;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void initialize(RenderManager rm, ViewPort vp) {
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.rm = rm;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.vp = vp;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void reshape(ViewPort vp, int w, int h) {
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public boolean isInitialized() {
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return rm != null;
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void preFrame(float tpf) {
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void postQueue(RenderQueue rq) {
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //we need special treatement for the sky because it must not be clipped
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rm.getRenderer().setFrameBuffer(reflectionBuffer);
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        reflectionCam.setProjectionMatrix(null);
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rm.setCamera(reflectionCam, false);
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rm.getRenderer().clearBuffers(true, true, true);
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //Rendering the sky whithout clipping
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rm.getRenderer().setDepthRange(1, 1);
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        vp.getQueue().renderQueue(RenderQueue.Bucket.Sky, rm, reflectionCam, true);
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rm.getRenderer().setDepthRange(0, 1);
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //setting the clip plane to the cam
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        reflectionCam.setClipPlane(reflectionClipPlane, Plane.Side.Positive);//,1
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rm.setCamera(reflectionCam, false);
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void postFrame(FrameBuffer out) {
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void cleanup() {
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Internal use only<br>
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the frame buffer
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public FrameBuffer getReflectionBuffer() {
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return reflectionBuffer;
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Internal use only<br>
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * sets the frame buffer
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param reflectionBuffer
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setReflectionBuffer(FrameBuffer reflectionBuffer) {
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.reflectionBuffer = reflectionBuffer;
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the reflection cam
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Camera getReflectionCam() {
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return reflectionCam;
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * sets the reflection cam
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param reflectionCam
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setReflectionCam(Camera reflectionCam) {
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.reflectionCam = reflectionCam;
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the reflection clip plane
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Plane getReflectionClipPlane() {
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return reflectionClipPlane;
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Sets the reflection clip plane
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param reflectionClipPlane
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setReflectionClipPlane(Plane reflectionClipPlane) {
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.reflectionClipPlane = reflectionClipPlane;
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
126