2014-11-05 12:15:14 +01:00
|
|
|
/*
|
2014-11-05 20:15:12 -05:00
|
|
|
* Copyright (c) 2009-2014 jMonkeyEngine
|
2014-11-05 12:15:14 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package jme3test.renderer;
|
|
|
|
|
|
|
|
import com.jme3.app.SimpleApplication;
|
2014-11-05 20:15:12 -05:00
|
|
|
import com.jme3.input.KeyInput;
|
|
|
|
import com.jme3.input.controls.ActionListener;
|
|
|
|
import com.jme3.input.controls.KeyTrigger;
|
2014-11-05 12:15:14 +01:00
|
|
|
import com.jme3.material.Material;
|
2014-11-05 20:15:12 -05:00
|
|
|
import com.jme3.material.RenderState;
|
2014-11-05 12:15:14 +01:00
|
|
|
import com.jme3.renderer.RenderManager;
|
2014-11-05 20:15:12 -05:00
|
|
|
import com.jme3.renderer.Renderer;
|
2014-11-05 12:15:14 +01:00
|
|
|
import com.jme3.renderer.ViewPort;
|
|
|
|
import com.jme3.scene.Geometry;
|
2014-11-05 20:15:12 -05:00
|
|
|
import com.jme3.scene.Node;
|
2014-11-05 12:15:14 +01:00
|
|
|
import com.jme3.scene.control.AbstractControl;
|
2014-11-05 20:15:12 -05:00
|
|
|
import com.jme3.scene.shape.Sphere;
|
2014-11-05 12:15:14 +01:00
|
|
|
import com.jme3.texture.FrameBuffer;
|
|
|
|
import com.jme3.texture.Image.Format;
|
|
|
|
import com.jme3.texture.Texture2D;
|
2014-11-05 20:15:12 -05:00
|
|
|
import com.jme3.ui.Picture;
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
public class TestDepthStencil extends SimpleApplication {
|
2014-11-05 12:15:14 +01:00
|
|
|
|
|
|
|
private boolean enableStencil = false;
|
2014-11-05 20:15:12 -05:00
|
|
|
|
|
|
|
private Node fbNode = new Node("Framebuffer Node");
|
|
|
|
private FrameBuffer fb;
|
2014-11-05 12:15:14 +01:00
|
|
|
|
|
|
|
public static void main(String[] args){
|
|
|
|
TestDepthStencil app = new TestDepthStencil();
|
2014-11-05 20:15:12 -05:00
|
|
|
app.start();
|
2014-11-05 12:15:14 +01:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
@Override
|
|
|
|
public void simpleInitApp() {
|
|
|
|
int w = settings.getWidth();
|
|
|
|
int h = settings.getHeight();
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
//setup framebuffer
|
|
|
|
fb = new FrameBuffer(w, h, 1);
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
Texture2D fbTex = new Texture2D(w, h, Format.RGB8);
|
|
|
|
fb.setDepthBuffer(Format.Depth24Stencil8);
|
|
|
|
fb.setColorTexture(fbTex);
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
// setup framebuffer's scene
|
|
|
|
Sphere sphMesh = new Sphere(20, 20, 1);
|
|
|
|
Material solidColor = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
final Geometry sphere = new Geometry("sphere", sphMesh);
|
|
|
|
sphere.setMaterial(solidColor);
|
|
|
|
fbNode.attachChild(sphere);
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
sphere.addControl(new AbstractControl() {
|
2014-11-05 12:15:14 +01:00
|
|
|
@Override
|
|
|
|
protected void controlUpdate(float tpf) {
|
2014-11-05 20:15:12 -05:00
|
|
|
Material mat = sphere.getMaterial();
|
|
|
|
mat.getAdditionalRenderState().setStencil(enableStencil,
|
|
|
|
RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep,
|
|
|
|
RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep,
|
|
|
|
RenderState.TestFunction.Never, RenderState.TestFunction.Never
|
2014-11-05 12:15:14 +01:00
|
|
|
//TestFunction.Always, TestFunction.Always
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void controlRender(RenderManager rm, ViewPort vp) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
//setup main scene
|
|
|
|
Picture p = new Picture("Picture");
|
|
|
|
p.setPosition(0, 0);
|
|
|
|
p.setWidth(w);
|
|
|
|
p.setHeight(h);
|
|
|
|
p.setTexture(assetManager, fbTex, false);
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
rootNode.attachChild(p);
|
|
|
|
|
|
|
|
inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
|
|
|
|
ActionListener acl = new ActionListener() {
|
|
|
|
public void onAction(String name, boolean keyPressed, float tpf) {
|
|
|
|
if (name.equals("toggle") && keyPressed) {
|
|
|
|
if (enableStencil) {
|
|
|
|
enableStencil = false;
|
|
|
|
System.out.println("Stencil Enabled (model should be hidden)");
|
|
|
|
} else {
|
|
|
|
enableStencil = true;
|
|
|
|
System.out.println("Stencil Disabled (model should be visible)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
inputManager.addListener(acl, "toggle");
|
|
|
|
|
|
|
|
System.out.println("Press space to toggle stencil");
|
2014-11-05 12:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void simpleUpdate(float tpf){
|
2014-11-05 20:15:12 -05:00
|
|
|
fbNode.updateLogicalState(tpf);
|
|
|
|
fbNode.updateGeometricState();
|
2014-11-05 12:15:14 +01:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
@Override
|
|
|
|
public void simpleRender(RenderManager rm){
|
|
|
|
Renderer r = rm.getRenderer();
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
//do FBO rendering
|
|
|
|
r.setFrameBuffer(fb);
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
rm.setCamera(cam, false); // FBO uses current camera
|
|
|
|
r.clearBuffers(true, true, true);
|
|
|
|
rm.renderScene(fbNode, viewPort);
|
|
|
|
rm.flushQueue(viewPort);
|
2014-11-05 12:15:14 +01:00
|
|
|
|
2014-11-05 20:15:12 -05:00
|
|
|
//go back to default rendering and let
|
|
|
|
//SimpleApplication render the default scene
|
|
|
|
r.setFrameBuffer(null);
|
2014-11-05 12:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|