Implemented proper frustum culling for the shadow post pass.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9979 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 12 years ago
parent 116c7161f7
commit 78d1754df1
  1. 29
      engine/src/core/com/jme3/shadow/AbstractShadowRenderer.java
  2. 12
      engine/src/core/com/jme3/shadow/DirectionalLightShadowRenderer.java
  3. 11
      engine/src/core/com/jme3/shadow/PointLightShadowRenderer.java
  4. 98
      engine/src/core/com/jme3/shadow/ShadowUtil.java
  5. 13
      engine/src/core/com/jme3/shadow/SpotLightShadowRenderer.java

@ -43,6 +43,7 @@ import com.jme3.renderer.RenderManager;
import com.jme3.renderer.Renderer;
import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.GeometryList;
import com.jme3.renderer.queue.OpaqueComparator;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
@ -76,7 +77,6 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
protected Material preshadowMat;
protected Material postshadowMat;
protected Matrix4f[] lightViewProjectionsMatrices;
protected boolean noOccluders = false;
protected AssetManager assetManager;
protected boolean debug = false;
protected float edgesThickness = 1.0f;
@ -91,6 +91,9 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
//flags to know when to change params in the materials
//a list of material of the post shadow queue geometries.
protected List<Material> matCache = new ArrayList<Material>();
protected GeometryList sceneReceivers;
protected GeometryList lightReceivers = new GeometryList(new OpaqueComparator());
protected GeometryList shadowMapOccluders = new GeometryList(new OpaqueComparator());
/**
* Create an abstract shadow renderer, this is to be called in extending classes
@ -300,7 +303,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
* @param sceneReceivers the recievers of the whole scene
* @return
*/
protected abstract GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers);
protected abstract GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers,GeometryList shadowMapOccluders);
/**
* return the shadow camera to use for rendering the shadow map according the given index
@ -319,8 +322,8 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
@SuppressWarnings("fallthrough")
public void postQueue(RenderQueue rq) {
GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast);
GeometryList receivers = rq.getShadowQueueContent(ShadowMode.Receive);
if (receivers.size() == 0 || occluders.size() == 0) {
sceneReceivers = rq.getShadowQueueContent(ShadowMode.Receive);
if (sceneReceivers.size() == 0 || occluders.size() == 0) {
return;
}
@ -335,7 +338,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
if (debugfrustums) {
doDisplayFrustumDebug(shadowMapIndex);
}
renderShadowMap(shadowMapIndex, occluders, receivers);
renderShadowMap(shadowMapIndex, occluders, sceneReceivers);
}
@ -352,7 +355,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
}
protected void renderShadowMap(int shadowMapIndex, GeometryList occluders, GeometryList receivers) {
GeometryList mapOccluders = getOccludersToRender(shadowMapIndex, occluders, receivers);
shadowMapOccluders = getOccludersToRender(shadowMapIndex, occluders, receivers,shadowMapOccluders);
Camera shadowCam = getShadowCam(shadowMapIndex);
//saving light view projection matrix for this split
@ -363,7 +366,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
renderManager.getRenderer().clearBuffers(false, true, false);
// render shadow casters to shadow map
viewPort.getQueue().renderShadowQueue(mapOccluders, renderManager, shadowCam, true);
viewPort.getQueue().renderShadowQueue(shadowMapOccluders, renderManager, shadowCam, true);
}
boolean debugfrustums = false;
@ -394,12 +397,17 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
debug = true;
}
abstract GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers);
public void postFrame(FrameBuffer out) {
if (debug) {
displayShadowMap(renderManager.getRenderer());
}
if (!noOccluders) {
lightReceivers = getReceivers(sceneReceivers, lightReceivers);
if (lightReceivers.size()!=0) {
//setting params to recieving geometry list
setMatParams();
@ -413,7 +421,10 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
renderManager.setForcedTechnique(postTechniqueName);
//rendering the post shadow pass
viewPort.getQueue().renderShadowQueue(ShadowMode.Receive, renderManager, cam, flushQueues);
viewPort.getQueue().renderShadowQueue(lightReceivers, renderManager, cam, true);
if(flushQueues){
sceneReceivers.clear();
}
//resetting renderManager settings
renderManager.setForcedTechnique(null);

@ -59,7 +59,6 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
protected float zFarOverride = 0;
protected Camera shadowCam;
protected ColorRGBA splits;
protected GeometryList splitOccluders = new GeometryList(new OpaqueComparator());
protected float[] splitsArray;
protected DirectionalLight light;
protected Vector3f[] points = new Vector3f[8];
@ -145,15 +144,20 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
}
@Override
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers) {
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
// update frustum points based on current camera and split
ShadowUtil.updateFrustumPoints(viewPort.getCamera(), splitsArray[shadowMapIndex], splitsArray[shadowMapIndex + 1], 1.0f, points);
//Updating shadow cam with curent split frustra
ShadowUtil.updateShadowCamera(sceneOccluders, sceneReceivers, shadowCam, points, splitOccluders);
ShadowUtil.updateShadowCamera(sceneOccluders, sceneReceivers, shadowCam, points, shadowMapOccluders);
return splitOccluders;
return shadowMapOccluders;
}
@Override
GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
return sceneReceivers;
}
@Override

@ -51,7 +51,6 @@ public class PointLightShadowRenderer extends AbstractShadowRenderer {
public static final int CAM_NUMBER = 6;
protected PointLight light;
protected Camera[] shadowCams;
protected GeometryList shadowMapOccluders = new GeometryList(new OpaqueComparator());
private Geometry[] frustums = null;
/**
@ -104,11 +103,17 @@ public class PointLightShadowRenderer extends AbstractShadowRenderer {
}
@Override
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers) {
ShadowUtil.getOccludersInCamFrustum(sceneOccluders, shadowCams[shadowMapIndex], shadowMapOccluders);
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
ShadowUtil.getGeometriesInCamFrustum(sceneOccluders, shadowCams[shadowMapIndex], shadowMapOccluders);
return shadowMapOccluders;
}
@Override
GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
ShadowUtil.getGeometriesInLightRadius(sceneReceivers, shadowCams, lightReceivers);
return lightReceivers;
}
@Override
protected Camera getShadowCam(int shadowMapIndex) {
return shadowCams[shadowMapIndex];

@ -50,17 +50,17 @@ import java.util.List;
/**
* Includes various useful shadow mapping functions.
*
* @see
* <ul>
* <li><a href="http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/">http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/</a></li>
* <li><a href="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html">http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html</a></li>
* </ul>
* for more info.
* @see <ul> <li><a
* href="http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/">http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/</a></li>
* <li><a
* href="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html">http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html</a></li>
* </ul> for more info.
*/
public class ShadowUtil {
/**
* Updates a points arrays with the frustum corners of the provided camera.
*
* @param viewCam
* @param points
*/
@ -81,8 +81,8 @@ public class ShadowUtil {
/**
* Updates the points array to contain the frustum corners of the given
* camera. The nearOverride and farOverride variables can be used
* to override the camera's near/far values with own values.
* camera. The nearOverride and farOverride variables can be used to
* override the camera's near/far values with own values.
*
* TODO: Reduce creation of new vectors
*
@ -205,6 +205,7 @@ public class ShadowUtil {
/**
* Computes the bounds of multiple bounding volumes
*
* @param bv
* @return
*/
@ -219,6 +220,7 @@ public class ShadowUtil {
/**
* Compute bounds from an array of points
*
* @param pts
* @param transform
* @return
@ -269,8 +271,8 @@ public class ShadowUtil {
}
/**
* Updates the shadow camera to properly contain the given
* points (which contain the eye camera frustum corners)
* Updates the shadow camera to properly contain the given points (which
* contain the eye camera frustum corners)
*
* @param shadowCam
* @param points
@ -324,9 +326,8 @@ public class ShadowUtil {
}
/**
* Updates the shadow camera to properly contain the given
* points (which contain the eye camera frustum corners) and the
* shadow occluder objects.
* Updates the shadow camera to properly contain the given points (which
* contain the eye camera frustum corners) and the shadow occluder objects.
*
* @param occluders
* @param receivers
@ -341,9 +342,8 @@ public class ShadowUtil {
}
/**
* Updates the shadow camera to properly contain the given
* points (which contain the eye camera frustum corners) and the
* shadow occluder objects.
* Updates the shadow camera to properly contain the given points (which
* contain the eye camera frustum corners) and the shadow occluder objects.
*
* @param occluders
* @param shadowCam
@ -492,26 +492,58 @@ public class ShadowUtil {
}
/**
* Updates the shadow camera to properly contain the given
* points (which contain the eye camera frustum corners) and the
* shadow occluder objects.
/**
* Populates the outputGeometryList with the geometry of the
* inputGeomtryList that are in the frustum of the given camera
*
* @param occluders
* @param shadowCam
* @param points
* @param inputGeometryList The list containing all geometry to check
* against the camera frustum
* @param camera the camera to check geometries against
* @param outputGeometryList the list of all geometries that are in the
* camera frustum
*/
public static void getOccludersInCamFrustum(GeometryList occluders,
Camera shadowCam,
GeometryList splitOccluders) {
for (int i = 0; i < occluders.size(); i++) {
Geometry g = occluders.get(i);
int planeState = shadowCam.getPlaneState();
shadowCam.setPlaneState(0);
if (shadowCam.contains(g.getWorldBound()) != Camera.FrustumIntersect.Outside) {
splitOccluders.add(g);
public static void getGeometriesInCamFrustum(GeometryList inputGeometryList,
Camera camera,
GeometryList outputGeometryList) {
for (int i = 0; i < inputGeometryList.size(); i++) {
Geometry g = inputGeometryList.get(i);
int planeState = camera.getPlaneState();
camera.setPlaneState(0);
if (camera.contains(g.getWorldBound()) != Camera.FrustumIntersect.Outside) {
outputGeometryList.add(g);
}
camera.setPlaneState(planeState);
}
}
/**
* Populates the outputGeometryList with the geometry of the
* inputGeomtryList that are in the radius of a light.
* The array of camera must be an array of 6 cameara initialized so they represent the light viewspace of a pointlight
*
* @param inputGeometryList The list containing all geometry to check
* against the camera frustum
* @param cameras the camera array to check geometries against
* @param outputGeometryList the list of all geometries that are in the
* camera frustum
*/
public static void getGeometriesInLightRadius(GeometryList inputGeometryList,
Camera[] cameras,
GeometryList outputGeometryList) {
for (int i = 0; i < inputGeometryList.size(); i++) {
Geometry g = inputGeometryList.get(i);
boolean inFrustum = false;
for (int j = 0; j < cameras.length && inFrustum == false; j++) {
Camera camera = cameras[j];
int planeState = camera.getPlaneState();
camera.setPlaneState(0);
inFrustum = camera.contains(g.getWorldBound()) != Camera.FrustumIntersect.Outside;
camera.setPlaneState(planeState);
}
if (inFrustum) {
outputGeometryList.add(g);
}
shadowCam.setPlaneState(planeState);
}
}

@ -60,7 +60,6 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer {
protected float zFarOverride = 0;
protected Camera shadowCam;
protected GeometryList mapOccluders = new GeometryList(new OpaqueComparator());
protected SpotLight light;
protected Vector3f[] points = new Vector3f[8];
//Holding the info for fading shadows in the far distance
@ -125,9 +124,15 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer {
}
@Override
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers) {
ShadowUtil.getOccludersInCamFrustum(sceneOccluders, shadowCam, mapOccluders);
return mapOccluders;
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
ShadowUtil.getGeometriesInCamFrustum(sceneOccluders, shadowCam, shadowMapOccluders);
return shadowMapOccluders;
}
@Override
GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
ShadowUtil.getGeometriesInCamFrustum(sceneReceivers, shadowCam, lightReceivers);
return lightReceivers;
}
@Override

Loading…
Cancel
Save