Implemented proper frustum culling for the shadow post pass.
git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9979 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
116c7161f7
commit
78d1754df1
@ -43,6 +43,7 @@ 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.renderer.queue.GeometryList;
|
import com.jme3.renderer.queue.GeometryList;
|
||||||
|
import com.jme3.renderer.queue.OpaqueComparator;
|
||||||
import com.jme3.renderer.queue.RenderQueue;
|
import com.jme3.renderer.queue.RenderQueue;
|
||||||
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
|
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
|
||||||
import com.jme3.scene.Geometry;
|
import com.jme3.scene.Geometry;
|
||||||
@ -76,7 +77,6 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
|
|||||||
protected Material preshadowMat;
|
protected Material preshadowMat;
|
||||||
protected Material postshadowMat;
|
protected Material postshadowMat;
|
||||||
protected Matrix4f[] lightViewProjectionsMatrices;
|
protected Matrix4f[] lightViewProjectionsMatrices;
|
||||||
protected boolean noOccluders = false;
|
|
||||||
protected AssetManager assetManager;
|
protected AssetManager assetManager;
|
||||||
protected boolean debug = false;
|
protected boolean debug = false;
|
||||||
protected float edgesThickness = 1.0f;
|
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
|
//flags to know when to change params in the materials
|
||||||
//a list of material of the post shadow queue geometries.
|
//a list of material of the post shadow queue geometries.
|
||||||
protected List<Material> matCache = new ArrayList<Material>();
|
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
|
* 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
|
* @param sceneReceivers the recievers of the whole scene
|
||||||
* @return
|
* @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
|
* 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")
|
@SuppressWarnings("fallthrough")
|
||||||
public void postQueue(RenderQueue rq) {
|
public void postQueue(RenderQueue rq) {
|
||||||
GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast);
|
GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast);
|
||||||
GeometryList receivers = rq.getShadowQueueContent(ShadowMode.Receive);
|
sceneReceivers = rq.getShadowQueueContent(ShadowMode.Receive);
|
||||||
if (receivers.size() == 0 || occluders.size() == 0) {
|
if (sceneReceivers.size() == 0 || occluders.size() == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,7 +338,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
|
|||||||
if (debugfrustums) {
|
if (debugfrustums) {
|
||||||
doDisplayFrustumDebug(shadowMapIndex);
|
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) {
|
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);
|
Camera shadowCam = getShadowCam(shadowMapIndex);
|
||||||
|
|
||||||
//saving light view projection matrix for this split
|
//saving light view projection matrix for this split
|
||||||
@ -363,7 +366,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
|
|||||||
renderManager.getRenderer().clearBuffers(false, true, false);
|
renderManager.getRenderer().clearBuffers(false, true, false);
|
||||||
|
|
||||||
// render shadow casters to shadow map
|
// render shadow casters to shadow map
|
||||||
viewPort.getQueue().renderShadowQueue(mapOccluders, renderManager, shadowCam, true);
|
viewPort.getQueue().renderShadowQueue(shadowMapOccluders, renderManager, shadowCam, true);
|
||||||
}
|
}
|
||||||
boolean debugfrustums = false;
|
boolean debugfrustums = false;
|
||||||
|
|
||||||
@ -394,12 +397,17 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
|
|||||||
debug = true;
|
debug = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers);
|
||||||
|
|
||||||
public void postFrame(FrameBuffer out) {
|
public void postFrame(FrameBuffer out) {
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
displayShadowMap(renderManager.getRenderer());
|
displayShadowMap(renderManager.getRenderer());
|
||||||
}
|
}
|
||||||
if (!noOccluders) {
|
|
||||||
|
lightReceivers = getReceivers(sceneReceivers, lightReceivers);
|
||||||
|
|
||||||
|
if (lightReceivers.size()!=0) {
|
||||||
//setting params to recieving geometry list
|
//setting params to recieving geometry list
|
||||||
setMatParams();
|
setMatParams();
|
||||||
|
|
||||||
@ -413,7 +421,10 @@ public abstract class AbstractShadowRenderer implements SceneProcessor {
|
|||||||
renderManager.setForcedTechnique(postTechniqueName);
|
renderManager.setForcedTechnique(postTechniqueName);
|
||||||
|
|
||||||
//rendering the post shadow pass
|
//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
|
//resetting renderManager settings
|
||||||
renderManager.setForcedTechnique(null);
|
renderManager.setForcedTechnique(null);
|
||||||
|
@ -59,7 +59,6 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
|
|||||||
protected float zFarOverride = 0;
|
protected float zFarOverride = 0;
|
||||||
protected Camera shadowCam;
|
protected Camera shadowCam;
|
||||||
protected ColorRGBA splits;
|
protected ColorRGBA splits;
|
||||||
protected GeometryList splitOccluders = new GeometryList(new OpaqueComparator());
|
|
||||||
protected float[] splitsArray;
|
protected float[] splitsArray;
|
||||||
protected DirectionalLight light;
|
protected DirectionalLight light;
|
||||||
protected Vector3f[] points = new Vector3f[8];
|
protected Vector3f[] points = new Vector3f[8];
|
||||||
@ -145,15 +144,20 @@ public class DirectionalLightShadowRenderer extends AbstractShadowRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
// update frustum points based on current camera and split
|
||||||
ShadowUtil.updateFrustumPoints(viewPort.getCamera(), splitsArray[shadowMapIndex], splitsArray[shadowMapIndex + 1], 1.0f, points);
|
ShadowUtil.updateFrustumPoints(viewPort.getCamera(), splitsArray[shadowMapIndex], splitsArray[shadowMapIndex + 1], 1.0f, points);
|
||||||
|
|
||||||
//Updating shadow cam with curent split frustra
|
//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
|
@Override
|
||||||
|
@ -51,7 +51,6 @@ public class PointLightShadowRenderer extends AbstractShadowRenderer {
|
|||||||
public static final int CAM_NUMBER = 6;
|
public static final int CAM_NUMBER = 6;
|
||||||
protected PointLight light;
|
protected PointLight light;
|
||||||
protected Camera[] shadowCams;
|
protected Camera[] shadowCams;
|
||||||
protected GeometryList shadowMapOccluders = new GeometryList(new OpaqueComparator());
|
|
||||||
private Geometry[] frustums = null;
|
private Geometry[] frustums = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -104,11 +103,17 @@ public class PointLightShadowRenderer extends AbstractShadowRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers) {
|
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
|
||||||
ShadowUtil.getOccludersInCamFrustum(sceneOccluders, shadowCams[shadowMapIndex], shadowMapOccluders);
|
ShadowUtil.getGeometriesInCamFrustum(sceneOccluders, shadowCams[shadowMapIndex], shadowMapOccluders);
|
||||||
return shadowMapOccluders;
|
return shadowMapOccluders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
|
||||||
|
ShadowUtil.getGeometriesInLightRadius(sceneReceivers, shadowCams, lightReceivers);
|
||||||
|
return lightReceivers;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Camera getShadowCam(int shadowMapIndex) {
|
protected Camera getShadowCam(int shadowMapIndex) {
|
||||||
return shadowCams[shadowMapIndex];
|
return shadowCams[shadowMapIndex];
|
||||||
|
@ -50,17 +50,17 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Includes various useful shadow mapping functions.
|
* Includes various useful shadow mapping functions.
|
||||||
*
|
*
|
||||||
* @see
|
* @see <ul> <li><a
|
||||||
* <ul>
|
* 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://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/">http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/</a></li>
|
* <li><a
|
||||||
* <li><a href="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html">http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html</a></li>
|
* href="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html">http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html</a></li>
|
||||||
* </ul>
|
* </ul> for more info.
|
||||||
* for more info.
|
|
||||||
*/
|
*/
|
||||||
public class ShadowUtil {
|
public class ShadowUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates a points arrays with the frustum corners of the provided camera.
|
* Updates a points arrays with the frustum corners of the provided camera.
|
||||||
|
*
|
||||||
* @param viewCam
|
* @param viewCam
|
||||||
* @param points
|
* @param points
|
||||||
*/
|
*/
|
||||||
@ -81,8 +81,8 @@ public class ShadowUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the points array to contain the frustum corners of the given
|
* Updates the points array to contain the frustum corners of the given
|
||||||
* camera. The nearOverride and farOverride variables can be used
|
* camera. The nearOverride and farOverride variables can be used to
|
||||||
* to override the camera's near/far values with own values.
|
* override the camera's near/far values with own values.
|
||||||
*
|
*
|
||||||
* TODO: Reduce creation of new vectors
|
* TODO: Reduce creation of new vectors
|
||||||
*
|
*
|
||||||
@ -205,6 +205,7 @@ public class ShadowUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the bounds of multiple bounding volumes
|
* Computes the bounds of multiple bounding volumes
|
||||||
|
*
|
||||||
* @param bv
|
* @param bv
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -219,6 +220,7 @@ public class ShadowUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute bounds from an array of points
|
* Compute bounds from an array of points
|
||||||
|
*
|
||||||
* @param pts
|
* @param pts
|
||||||
* @param transform
|
* @param transform
|
||||||
* @return
|
* @return
|
||||||
@ -269,8 +271,8 @@ public class ShadowUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the shadow camera to properly contain the given
|
* Updates the shadow camera to properly contain the given points (which
|
||||||
* points (which contain the eye camera frustum corners)
|
* contain the eye camera frustum corners)
|
||||||
*
|
*
|
||||||
* @param shadowCam
|
* @param shadowCam
|
||||||
* @param points
|
* @param points
|
||||||
@ -324,9 +326,8 @@ public class ShadowUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the shadow camera to properly contain the given
|
* Updates the shadow camera to properly contain the given points (which
|
||||||
* points (which contain the eye camera frustum corners) and the
|
* contain the eye camera frustum corners) and the shadow occluder objects.
|
||||||
* shadow occluder objects.
|
|
||||||
*
|
*
|
||||||
* @param occluders
|
* @param occluders
|
||||||
* @param receivers
|
* @param receivers
|
||||||
@ -341,9 +342,8 @@ public class ShadowUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the shadow camera to properly contain the given
|
* Updates the shadow camera to properly contain the given points (which
|
||||||
* points (which contain the eye camera frustum corners) and the
|
* contain the eye camera frustum corners) and the shadow occluder objects.
|
||||||
* shadow occluder objects.
|
|
||||||
*
|
*
|
||||||
* @param occluders
|
* @param occluders
|
||||||
* @param shadowCam
|
* @param shadowCam
|
||||||
@ -492,26 +492,58 @@ public class ShadowUtil {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the shadow camera to properly contain the given
|
* Populates the outputGeometryList with the geometry of the
|
||||||
* points (which contain the eye camera frustum corners) and the
|
* inputGeomtryList that are in the frustum of the given camera
|
||||||
* shadow occluder objects.
|
|
||||||
*
|
*
|
||||||
* @param occluders
|
* @param inputGeometryList The list containing all geometry to check
|
||||||
* @param shadowCam
|
* against the camera frustum
|
||||||
* @param points
|
* @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,
|
public static void getGeometriesInCamFrustum(GeometryList inputGeometryList,
|
||||||
Camera shadowCam,
|
Camera camera,
|
||||||
GeometryList splitOccluders) {
|
GeometryList outputGeometryList) {
|
||||||
for (int i = 0; i < occluders.size(); i++) {
|
for (int i = 0; i < inputGeometryList.size(); i++) {
|
||||||
Geometry g = occluders.get(i);
|
Geometry g = inputGeometryList.get(i);
|
||||||
int planeState = shadowCam.getPlaneState();
|
int planeState = camera.getPlaneState();
|
||||||
shadowCam.setPlaneState(0);
|
camera.setPlaneState(0);
|
||||||
if (shadowCam.contains(g.getWorldBound()) != Camera.FrustumIntersect.Outside) {
|
if (camera.contains(g.getWorldBound()) != Camera.FrustumIntersect.Outside) {
|
||||||
splitOccluders.add(g);
|
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 float zFarOverride = 0;
|
||||||
protected Camera shadowCam;
|
protected Camera shadowCam;
|
||||||
protected GeometryList mapOccluders = new GeometryList(new OpaqueComparator());
|
|
||||||
protected SpotLight light;
|
protected SpotLight light;
|
||||||
protected Vector3f[] points = new Vector3f[8];
|
protected Vector3f[] points = new Vector3f[8];
|
||||||
//Holding the info for fading shadows in the far distance
|
//Holding the info for fading shadows in the far distance
|
||||||
@ -125,9 +124,15 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers) {
|
protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
|
||||||
ShadowUtil.getOccludersInCamFrustum(sceneOccluders, shadowCam, mapOccluders);
|
ShadowUtil.getGeometriesInCamFrustum(sceneOccluders, shadowCam, shadowMapOccluders);
|
||||||
return mapOccluders;
|
return shadowMapOccluders;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
|
||||||
|
ShadowUtil.getGeometriesInCamFrustum(sceneReceivers, shadowCam, lightReceivers);
|
||||||
|
return lightReceivers;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user