Filters : fixed depth buffer rendering for additionnal passes

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8176 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 14 years ago
parent 90a7917e1a
commit 29dbdc08a4
  1. 872
      engine/src/core/com/jme3/post/Filter.java

@ -1,435 +1,437 @@
/* /*
* Copyright (c) 2009-2010 jMonkeyEngine * Copyright (c) 2009-2010 jMonkeyEngine
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* *
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.post; package com.jme3.post;
import com.jme3.asset.AssetManager; import com.jme3.asset.AssetManager;
import com.jme3.export.InputCapsule; import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter; import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter; import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule; import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable; import com.jme3.export.Savable;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.renderer.Caps; import com.jme3.renderer.Caps;
import com.jme3.renderer.RenderManager; import com.jme3.renderer.RenderManager;
import com.jme3.renderer.Renderer; import com.jme3.renderer.Renderer;
import com.jme3.renderer.ViewPort; import com.jme3.renderer.ViewPort;
import com.jme3.texture.FrameBuffer; import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Image.Format; import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture2D; import com.jme3.texture.Texture2D;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Filters are 2D effects applied to the rendered scene.<br> * Filters are 2D effects applied to the rendered scene.<br>
* The filter is fed with the rendered scene image rendered in an offscreen frame buffer.<br> * The filter is fed with the rendered scene image rendered in an offscreen frame buffer.<br>
* This texture is applied on a fullscreen quad, with a special material.<br> * This texture is applied on a fullscreen quad, with a special material.<br>
* This material uses a shader that aplly the desired effect to the scene texture.<br> * This material uses a shader that aplly the desired effect to the scene texture.<br>
* <br> * <br>
* This class is abstract, any Filter must extend it.<br> * This class is abstract, any Filter must extend it.<br>
* Any filter holds a frameBuffer and a texture<br> * Any filter holds a frameBuffer and a texture<br>
* The getMaterial must return a Material that use a GLSL shader immplementing the desired effect<br> * The getMaterial must return a Material that use a GLSL shader immplementing the desired effect<br>
* *
* @author Rémy Bouquet aka Nehon * @author Rémy Bouquet aka Nehon
*/ */
public abstract class Filter implements Savable { public abstract class Filter implements Savable {
private String name; private String name;
protected Pass defaultPass; protected Pass defaultPass;
protected List<Pass> postRenderPasses; protected List<Pass> postRenderPasses;
protected Material material; protected Material material;
protected boolean enabled = true; protected boolean enabled = true;
protected FilterPostProcessor processor; protected FilterPostProcessor processor;
public Filter(String name) { public Filter(String name) {
this.name = name; this.name = name;
} }
/** /**
* Inner class Pass * Inner class Pass
* Pass are like filters in filters. * Pass are like filters in filters.
* Some filters will need multiple passes before the final render * Some filters will need multiple passes before the final render
*/ */
public class Pass { public class Pass {
protected FrameBuffer renderFrameBuffer; protected FrameBuffer renderFrameBuffer;
protected Texture2D renderedTexture; protected Texture2D renderedTexture;
protected Texture2D depthTexture; protected Texture2D depthTexture;
protected Material passMaterial; protected Material passMaterial;
/** /**
* init the pass called internally * init the pass called internally
* @param renderer * @param renderer
* @param width * @param width
* @param height * @param height
* @param textureFormat * @param textureFormat
* @param depthBufferFormat * @param depthBufferFormat
* @param numSamples * @param numSamples
*/ */
public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat, int numSamples, boolean renderDepth) { public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat, int numSamples, boolean renderDepth) {
Collection<Caps> caps = renderer.getCaps(); Collection<Caps> caps = renderer.getCaps();
if (numSamples > 1 && caps.contains(Caps.FrameBufferMultisample) && caps.contains(Caps.OpenGL31)) { if (numSamples > 1 && caps.contains(Caps.FrameBufferMultisample) && caps.contains(Caps.OpenGL31)) {
renderFrameBuffer = new FrameBuffer(width, height, numSamples); renderFrameBuffer = new FrameBuffer(width, height, numSamples);
renderedTexture = new Texture2D(width, height, numSamples, textureFormat); renderedTexture = new Texture2D(width, height, numSamples, textureFormat);
if(renderDepth){ renderFrameBuffer.setDepthBuffer(depthBufferFormat);
depthTexture = new Texture2D(width, height, numSamples, depthBufferFormat); if (renderDepth) {
renderFrameBuffer.setDepthTexture(depthTexture); depthTexture = new Texture2D(width, height, numSamples, depthBufferFormat);
} renderFrameBuffer.setDepthTexture(depthTexture);
} else { }
renderFrameBuffer = new FrameBuffer(width, height, 1); } else {
renderedTexture = new Texture2D(width, height, textureFormat); renderFrameBuffer = new FrameBuffer(width, height, 1);
if(renderDepth){ renderedTexture = new Texture2D(width, height, textureFormat);
depthTexture = new Texture2D(width, height, depthBufferFormat); renderFrameBuffer.setDepthBuffer(depthBufferFormat);
renderFrameBuffer.setDepthTexture(depthTexture); if (renderDepth) {
} depthTexture = new Texture2D(width, height, depthBufferFormat);
} renderFrameBuffer.setDepthTexture(depthTexture);
}
renderFrameBuffer.setColorTexture(renderedTexture); }
renderFrameBuffer.setDepthBuffer(depthBufferFormat);
renderFrameBuffer.setColorTexture(renderedTexture);
}
/** }
* init the pass called internally
* @param renderer /**
* @param width * init the pass called internally
* @param height * @param renderer
* @param textureFormat * @param width
* @param depthBufferFormat * @param height
*/ * @param textureFormat
public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat) { * @param depthBufferFormat
init(renderer, width, height, textureFormat, depthBufferFormat, 1); */
} public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat) {
init(renderer, width, height, textureFormat, depthBufferFormat, 1);
public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat, int numSamples) { }
init(renderer, width, height, textureFormat, depthBufferFormat, numSamples, false);
} public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat, int numSamples) {
init(renderer, width, height, textureFormat, depthBufferFormat, numSamples, false);
/** }
* init the pass called internally
* @param renderer /**
* @param width * init the pass called internally
* @param height * @param renderer
* @param textureFormat * @param width
* @param depthBufferFormat * @param height
* @param numSample * @param textureFormat
* @param material * @param depthBufferFormat
*/ * @param numSample
public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat, int numSample, Material material) { * @param material
init(renderer, width, height, textureFormat, depthBufferFormat, numSample); */
passMaterial = material; public void init(Renderer renderer, int width, int height, Format textureFormat, Format depthBufferFormat, int numSample, Material material) {
} init(renderer, width, height, textureFormat, depthBufferFormat, numSample);
passMaterial = material;
public boolean requiresSceneAsTexture() { }
return false;
} public boolean requiresSceneAsTexture() {
return false;
public boolean requiresDepthAsTexture() { }
return false;
} public boolean requiresDepthAsTexture() {
return false;
public void beforeRender() { }
}
public void beforeRender() {
public FrameBuffer getRenderFrameBuffer() { }
return renderFrameBuffer;
} public FrameBuffer getRenderFrameBuffer() {
return renderFrameBuffer;
public void setRenderFrameBuffer(FrameBuffer renderFrameBuffer) { }
this.renderFrameBuffer = renderFrameBuffer;
} public void setRenderFrameBuffer(FrameBuffer renderFrameBuffer) {
this.renderFrameBuffer = renderFrameBuffer;
public Texture2D getDepthTexture() { }
return depthTexture;
} public Texture2D getDepthTexture() {
return depthTexture;
public Texture2D getRenderedTexture() { }
return renderedTexture;
} public Texture2D getRenderedTexture() {
return renderedTexture;
public void setRenderedTexture(Texture2D renderedTexture) { }
this.renderedTexture = renderedTexture;
} public void setRenderedTexture(Texture2D renderedTexture) {
this.renderedTexture = renderedTexture;
public Material getPassMaterial() { }
return passMaterial;
} public Material getPassMaterial() {
return passMaterial;
public void setPassMaterial(Material passMaterial) { }
this.passMaterial = passMaterial;
} public void setPassMaterial(Material passMaterial) {
this.passMaterial = passMaterial;
public void cleanup(Renderer r) { }
}
} public void cleanup(Renderer r) {
}
/** }
* returns the default pass texture format
* @return /**
*/ * returns the default pass texture format
protected Format getDefaultPassTextureFormat() { * @return
return Format.RGBA8; */
} protected Format getDefaultPassTextureFormat() {
return Format.RGBA8;
/** }
* returns the default pass depth format
* @return /**
*/ * returns the default pass depth format
protected Format getDefaultPassDepthFormat() { * @return
return Format.Depth; */
} protected Format getDefaultPassDepthFormat() {
return Format.Depth;
/** }
* contruct a Filter
*/ /**
protected Filter() { * contruct a Filter
this("filter"); */
} protected Filter() {
this("filter");
/** }
*
* initialize this filter /**
* use InitFilter for overriding filter initialization *
* @param manager the assetManager * initialize this filter
* @param renderManager the renderManager * use InitFilter for overriding filter initialization
* @param vp the viewport * @param manager the assetManager
* @param w the width * @param renderManager the renderManager
* @param h the height * @param vp the viewport
*/ * @param w the width
protected final void init(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) { * @param h the height
// cleanup(renderManager.getRenderer()); */
defaultPass = new Pass(); protected final void init(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
defaultPass.init(renderManager.getRenderer(), w, h, getDefaultPassTextureFormat(), getDefaultPassDepthFormat()); // cleanup(renderManager.getRenderer());
initFilter(manager, renderManager, vp, w, h); defaultPass = new Pass();
} defaultPass.init(renderManager.getRenderer(), w, h, getDefaultPassTextureFormat(), getDefaultPassDepthFormat());
initFilter(manager, renderManager, vp, w, h);
/** }
* cleanup this filter
* @param r /**
*/ * cleanup this filter
protected final void cleanup(Renderer r) { * @param r
processor = null; */
if (defaultPass != null) { protected final void cleanup(Renderer r) {
defaultPass.cleanup(r); processor = null;
} if (defaultPass != null) {
if (postRenderPasses != null) { defaultPass.cleanup(r);
for (Iterator<Pass> it = postRenderPasses.iterator(); it.hasNext();) { }
Pass pass = it.next(); if (postRenderPasses != null) {
pass.cleanup(r); for (Iterator<Pass> it = postRenderPasses.iterator(); it.hasNext();) {
} Pass pass = it.next();
} pass.cleanup(r);
cleanUpFilter(r); }
} }
cleanUpFilter(r);
/** }
* Initialization of sub classes filters
* This method is called once when the filter is added to the FilterPostProcessor /**
* It should contain Material initializations and extra passes initialization * Initialization of sub classes filters
* @param manager the assetManager * This method is called once when the filter is added to the FilterPostProcessor
* @param renderManager the renderManager * It should contain Material initializations and extra passes initialization
* @param vp the viewPort where this filter is rendered * @param manager the assetManager
* @param w the width of the filter * @param renderManager the renderManager
* @param h the height of the filter * @param vp the viewPort where this filter is rendered
*/ * @param w the width of the filter
protected abstract void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h); * @param h the height of the filter
*/
/** protected abstract void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h);
* override this method if you have some cleanup to do
* @param r the renderer /**
*/ * override this method if you have some cleanup to do
protected void cleanUpFilter(Renderer r) { * @param r the renderer
} */
protected void cleanUpFilter(Renderer r) {
; }
/** ;
* Must return the material used for this filter.
* this method is called every frame. /**
* * Must return the material used for this filter.
* @return the material used for this filter. * this method is called every frame.
*/ *
protected abstract Material getMaterial(); * @return the material used for this filter.
*/
/** protected abstract Material getMaterial();
* Override this method if you want to make a pre pass, before the actual rendering of the frame
* @param renderManager /**
* @param viewPort * Override this method if you want to make a pre pass, before the actual rendering of the frame
*/ * @param renderManager
protected void postQueue(RenderManager renderManager, ViewPort viewPort) { * @param viewPort
} */
protected void postQueue(RenderManager renderManager, ViewPort viewPort) {
/** }
* Override this method if you want to modify parameters according to tpf before the rendering of the frame.
* This is usefull for animated filters /**
* Also it can be the place to render pre passes * Override this method if you want to modify parameters according to tpf before the rendering of the frame.
* @param tpf the time used to render the previous frame * This is usefull for animated filters
*/ * Also it can be the place to render pre passes
protected void preFrame(float tpf) { * @param tpf the time used to render the previous frame
} */
protected void preFrame(float tpf) {
/** }
* Override this method if you want to make a pass just after the frame has been rendered and just before the filter rendering
* @param renderManager /**
* @param viewPort * Override this method if you want to make a pass just after the frame has been rendered and just before the filter rendering
* @param prevFilterBuffer * @param renderManager
* @param sceneBuffer * @param viewPort
*/ * @param prevFilterBuffer
protected void postFrame(RenderManager renderManager, ViewPort viewPort, FrameBuffer prevFilterBuffer, FrameBuffer sceneBuffer) { * @param sceneBuffer
} */
protected void postFrame(RenderManager renderManager, ViewPort viewPort, FrameBuffer prevFilterBuffer, FrameBuffer sceneBuffer) {
/** }
* Override this method if you want to save extra properties when the filter is saved else only basic properties of the filter will be saved
* This method should always begin by super.write(ex); /**
* @param ex * Override this method if you want to save extra properties when the filter is saved else only basic properties of the filter will be saved
* @throws IOException * This method should always begin by super.write(ex);
*/ * @param ex
public void write(JmeExporter ex) throws IOException { * @throws IOException
OutputCapsule oc = ex.getCapsule(this); */
oc.write(name, "name", ""); public void write(JmeExporter ex) throws IOException {
oc.write(enabled, "enabled", true); OutputCapsule oc = ex.getCapsule(this);
} oc.write(name, "name", "");
oc.write(enabled, "enabled", true);
/** }
* Override this method if you want to load extra properties when the filter
* is loaded else only basic properties of the filter will be loaded /**
* This method should always begin by super.read(im); * Override this method if you want to load extra properties when the filter
*/ * is loaded else only basic properties of the filter will be loaded
public void read(JmeImporter im) throws IOException { * This method should always begin by super.read(im);
InputCapsule ic = im.getCapsule(this); */
name = ic.readString("name", ""); public void read(JmeImporter im) throws IOException {
enabled = ic.readBoolean("enabled", true); InputCapsule ic = im.getCapsule(this);
} name = ic.readString("name", "");
enabled = ic.readBoolean("enabled", true);
/** }
* returns the name of the filter
* @return /**
*/ * returns the name of the filter
public String getName() { * @return
return name; */
} public String getName() {
return name;
/** }
* Sets the name of the filter
* @param name /**
*/ * Sets the name of the filter
public void setName(String name) { * @param name
this.name = name; */
} public void setName(String name) {
this.name = name;
/** }
* returns the default pass frame buffer
* @return /**
*/ * returns the default pass frame buffer
protected FrameBuffer getRenderFrameBuffer() { * @return
return defaultPass.renderFrameBuffer; */
} protected FrameBuffer getRenderFrameBuffer() {
return defaultPass.renderFrameBuffer;
/** }
* sets the default pas frame buffer
* @param renderFrameBuffer /**
*/ * sets the default pas frame buffer
protected void setRenderFrameBuffer(FrameBuffer renderFrameBuffer) { * @param renderFrameBuffer
this.defaultPass.renderFrameBuffer = renderFrameBuffer; */
} protected void setRenderFrameBuffer(FrameBuffer renderFrameBuffer) {
this.defaultPass.renderFrameBuffer = renderFrameBuffer;
/** }
* returns the rendered texture of this filter
* @return /**
*/ * returns the rendered texture of this filter
protected Texture2D getRenderedTexture() { * @return
return defaultPass.renderedTexture; */
} protected Texture2D getRenderedTexture() {
return defaultPass.renderedTexture;
/** }
* sets the rendered texture of this filter
* @param renderedTexture /**
*/ * sets the rendered texture of this filter
protected void setRenderedTexture(Texture2D renderedTexture) { * @param renderedTexture
this.defaultPass.renderedTexture = renderedTexture; */
} protected void setRenderedTexture(Texture2D renderedTexture) {
this.defaultPass.renderedTexture = renderedTexture;
/** }
* Override this method and return true if your Filter needs the depth texture
* /**
* @return true if your Filter need the depth texture * Override this method and return true if your Filter needs the depth texture
*/ *
protected boolean isRequiresDepthTexture() { * @return true if your Filter need the depth texture
return false; */
} protected boolean isRequiresDepthTexture() {
return false;
/** }
* Override this method and return false if your Filter does not need the scene texture
* /**
* @return false if your Filter does not need the scene texture * Override this method and return false if your Filter does not need the scene texture
*/ *
protected boolean isRequiresSceneTexture() { * @return false if your Filter does not need the scene texture
return true; */
} protected boolean isRequiresSceneTexture() {
return true;
/** }
* returns the list of the postRender passes
* @return /**
*/ * returns the list of the postRender passes
protected List<Pass> getPostRenderPasses() { * @return
return postRenderPasses; */
} protected List<Pass> getPostRenderPasses() {
return postRenderPasses;
/** }
* Enable or disable this filter
* @param enabled true to enable /**
*/ * Enable or disable this filter
public void setEnabled(boolean enabled) { * @param enabled true to enable
if (processor != null) { */
processor.setFilterState(this, enabled); public void setEnabled(boolean enabled) {
} else { if (processor != null) {
this.enabled = enabled; processor.setFilterState(this, enabled);
} } else {
} this.enabled = enabled;
}
/** }
* returns ttrue if the filter is enabled
* @return enabled /**
*/ * returns ttrue if the filter is enabled
public boolean isEnabled() { * @return enabled
return enabled; */
} public boolean isEnabled() {
return enabled;
/** }
* sets a reference to the FilterPostProcessor ti which this filter is attached
* @param proc /**
*/ * sets a reference to the FilterPostProcessor ti which this filter is attached
protected void setProcessor(FilterPostProcessor proc) { * @param proc
processor = proc; */
} protected void setProcessor(FilterPostProcessor proc) {
} processor = proc;
}
}

Loading…
Cancel
Save