public void postFrame(FrameBuffer out) { if (debug) { displayShadowMap(renderManager.getRenderer()); } lightReceivers = getReceivers(sceneReceivers, lightReceivers); if (lightReceivers.size() != 0) { // setting params to recieving geometry list setMatParams(); Camera cam = viewPort.getCamera(); // some materials in the scene does not have a post shadow technique so we're using the fall // back material if (needsfallBackMaterial) { renderManager.setForcedMaterial(postshadowMat); } // forcing the post shadow technique and render state renderManager.setForcedTechnique(postTechniqueName); // rendering the post shadow pass viewPort.getQueue().renderShadowQueue(lightReceivers, renderManager, cam, true); if (flushQueues) { sceneReceivers.clear(); } // resetting renderManager settings renderManager.setForcedTechnique(null); renderManager.setForcedMaterial(null); renderManager.setCamera(cam, false); } }
@SuppressWarnings("fallthrough") public void postQueue(RenderQueue rq) { GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast); sceneReceivers = rq.getShadowQueueContent(ShadowMode.Receive); if (sceneReceivers.size() == 0 || occluders.size() == 0) { return; } updateShadowCams(viewPort.getCamera()); Renderer r = renderManager.getRenderer(); renderManager.setForcedMaterial(preshadowMat); renderManager.setForcedTechnique("PreShadow"); for (int shadowMapIndex = 0; shadowMapIndex < nbShadowMaps; shadowMapIndex++) { if (debugfrustums) { doDisplayFrustumDebug(shadowMapIndex); } renderShadowMap(shadowMapIndex, occluders, sceneReceivers); } debugfrustums = false; if (flushQueues) { occluders.clear(); } // restore setting for future rendering r.setFrameBuffer(viewPort.getOutputFrameBuffer()); renderManager.setForcedMaterial(null); renderManager.setForcedTechnique(null); renderManager.setCamera(viewPort.getCamera(), false); }
/** * Populates the outputGeometryList with the geometry of the inputGeomtryList that are in the * frustum of the given camera * * @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 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); } }
/** * Compute bounds of a geomList * * @param list * @param transform * @return */ public static BoundingBox computeUnionBound(GeometryList list, Transform transform) { BoundingBox bbox = new BoundingBox(); for (int i = 0; i < list.size(); i++) { BoundingVolume vol = list.get(i).getWorldBound(); BoundingVolume newVol = vol.transform(transform); // Nehon : prevent NaN and infinity values to screw the final bounding box if (!Float.isNaN(newVol.getCenter().x) && !Float.isInfinite(newVol.getCenter().x)) { bbox.mergeLocal(newVol); } } return bbox; }
/** * Compute bounds of a geomList * * @param list * @param mat * @return */ public static BoundingBox computeUnionBound(GeometryList list, Matrix4f mat) { BoundingBox bbox = new BoundingBox(); BoundingVolume store = null; for (int i = 0; i < list.size(); i++) { BoundingVolume vol = list.get(i).getWorldBound(); store = vol.clone().transform(mat, null); // Nehon : prevent NaN and infinity values to screw the final bounding box if (!Float.isNaN(store.getCenter().x) && !Float.isInfinite(store.getCenter().x)) { bbox.mergeLocal(store); } } return bbox; }
public void postQueue(RenderQueue rq) { GeometryList occluders = rq.getShadowQueueContent(ShadowMode.Cast); if (occluders.size() == 0) { noOccluders = true; return; } else { noOccluders = false; } GeometryList receivers = rq.getShadowQueueContent(ShadowMode.Receive); // update frustum points based on current camera Camera viewCam = viewPort.getCamera(); ShadowUtil.updateFrustumPoints( viewCam, viewCam.getFrustumNear(), viewCam.getFrustumFar(), 1.0f, points); Vector3f frustaCenter = new Vector3f(); for (Vector3f point : points) { frustaCenter.addLocal(point); } frustaCenter.multLocal(1f / 8f); // update light direction shadowCam.setProjectionMatrix(null); shadowCam.setParallelProjection(true); // shadowCam.setFrustumPerspective(45, 1, 1, 20); shadowCam.lookAtDirection(direction, Vector3f.UNIT_Y); shadowCam.update(); shadowCam.setLocation(frustaCenter); shadowCam.update(); shadowCam.updateViewProjection(); // render shadow casters to shadow map ShadowUtil.updateShadowCamera(occluders, receivers, shadowCam, points); Renderer r = renderManager.getRenderer(); renderManager.setCamera(shadowCam, false); renderManager.setForcedMaterial(preshadowMat); r.setFrameBuffer(shadowFB); r.clearBuffers(false, true, false); viewPort.getQueue().renderShadowQueue(ShadowMode.Cast, renderManager, shadowCam, true); r.setFrameBuffer(viewPort.getOutputFrameBuffer()); renderManager.setForcedMaterial(null); renderManager.setCamera(viewCam, false); }
/** * 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); } } }
private void setMatParams() { GeometryList l = viewPort.getQueue().getShadowQueueContent(ShadowMode.Receive); // iteration throught all the geometries of the list to gather the materials matCache.clear(); for (int i = 0; i < l.size(); i++) { Material mat = l.get(i).getMaterial(); // checking if the material has the post technique and adding it to the material cache if (mat.getMaterialDef().getTechniqueDef(postTechniqueName) != null) { if (!matCache.contains(mat)) { matCache.add(mat); } } else { needsfallBackMaterial = true; } } // iterating through the mat cache and setting the parameters for (Material mat : matCache) { mat.setFloat("ShadowMapSize", shadowMapSize); for (int j = 0; j < nbShadowMaps; j++) { mat.setMatrix4("LightViewProjectionMatrix" + j, lightViewProjectionsMatrices[j]); } for (int j = 0; j < nbShadowMaps; j++) { mat.setTexture("ShadowMap" + j, shadowMaps[j]); } mat.setBoolean("HardwareShadows", shadowCompareMode == CompareMode.Hardware); mat.setInt("FilterMode", edgeFilteringMode.getMaterialParamValue()); mat.setFloat("PCFEdge", edgesThickness); mat.setFloat("ShadowIntensity", shadowIntensity); setMaterialParameters(mat); } // At least one material of the receiving geoms does not support the post shadow techniques // so we fall back to the forced material solution (transparent shadows won't be supported for // these objects) if (needsfallBackMaterial) { setPostShadowParams(); } }
/** * 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 * @param points */ public static void updateShadowCamera( GeometryList occluders, GeometryList receivers, Camera shadowCam, Vector3f[] points, GeometryList splitOccluders) { boolean ortho = shadowCam.isParallelProjection(); shadowCam.setProjectionMatrix(null); if (ortho) { shadowCam.setFrustum(-1, 1, -1, 1, 1, -1); } // create transform to rotate points to viewspace Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix(); BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix); ArrayList<BoundingVolume> visRecvList = new ArrayList<BoundingVolume>(); for (int i = 0; i < receivers.size(); i++) { // convert bounding box to light's viewproj space Geometry receiver = receivers.get(i); BoundingVolume bv = receiver.getWorldBound(); BoundingVolume recvBox = bv.transform(viewProjMatrix, null); if (splitBB.intersects(recvBox)) { visRecvList.add(recvBox); } } ArrayList<BoundingVolume> visOccList = new ArrayList<BoundingVolume>(); for (int i = 0; i < occluders.size(); i++) { // convert bounding box to light's viewproj space Geometry occluder = occluders.get(i); BoundingVolume bv = occluder.getWorldBound(); BoundingVolume occBox = bv.transform(viewProjMatrix, null); boolean intersects = splitBB.intersects(occBox); if (!intersects && occBox instanceof BoundingBox) { BoundingBox occBB = (BoundingBox) occBox; // Kirill 01/10/2011 // Extend the occluder further into the frustum // This fixes shadow dissapearing issues when // the caster itself is not in the view camera // but its shadow is in the camera // The number is in world units occBB.setZExtent(occBB.getZExtent() + 50); occBB.setCenter(occBB.getCenter().addLocal(0, 0, 25)); if (splitBB.intersects(occBB)) { // To prevent extending the depth range too much // We return the bound to its former shape // Before adding it occBB.setZExtent(occBB.getZExtent() - 50); occBB.setCenter(occBB.getCenter().subtractLocal(0, 0, 25)); visOccList.add(occBox); if (splitOccluders != null) { splitOccluders.add(occluder); } } } else if (intersects) { visOccList.add(occBox); if (splitOccluders != null) { splitOccluders.add(occluder); } } } BoundingBox casterBB = computeUnionBound(visOccList); BoundingBox receiverBB = computeUnionBound(visRecvList); // Nehon 08/18/2010 this is to avoid shadow bleeding when the ground is set to only receive // shadows if (visOccList.size() != visRecvList.size()) { casterBB.setXExtent(casterBB.getXExtent() + 2.0f); casterBB.setYExtent(casterBB.getYExtent() + 2.0f); casterBB.setZExtent(casterBB.getZExtent() + 2.0f); } TempVars vars = TempVars.get(); Vector3f casterMin = casterBB.getMin(vars.vect1); Vector3f casterMax = casterBB.getMax(vars.vect2); Vector3f receiverMin = receiverBB.getMin(vars.vect3); Vector3f receiverMax = receiverBB.getMax(vars.vect4); Vector3f splitMin = splitBB.getMin(vars.vect5); Vector3f splitMax = splitBB.getMax(vars.vect6); splitMin.z = 0; // if (!ortho) { // shadowCam.setFrustumPerspective(45, 1, 1, splitMax.z); // } Matrix4f projMatrix = shadowCam.getProjectionMatrix(); Vector3f cropMin = vars.vect7; Vector3f cropMax = vars.vect8; // IMPORTANT: Special handling for Z values cropMin.x = max(max(casterMin.x, receiverMin.x), splitMin.x); cropMax.x = min(min(casterMax.x, receiverMax.x), splitMax.x); cropMin.y = max(max(casterMin.y, receiverMin.y), splitMin.y); cropMax.y = min(min(casterMax.y, receiverMax.y), splitMax.y); cropMin.z = min(casterMin.z, splitMin.z); cropMax.z = min(receiverMax.z, splitMax.z); // Create the crop matrix. float scaleX, scaleY, scaleZ; float offsetX, offsetY, offsetZ; scaleX = (2.0f) / (cropMax.x - cropMin.x); scaleY = (2.0f) / (cropMax.y - cropMin.y); offsetX = -0.5f * (cropMax.x + cropMin.x) * scaleX; offsetY = -0.5f * (cropMax.y + cropMin.y) * scaleY; scaleZ = 1.0f / (cropMax.z - cropMin.z); offsetZ = -cropMin.z * scaleZ; Matrix4f cropMatrix = vars.tempMat4; cropMatrix.set( scaleX, 0f, 0f, offsetX, 0f, scaleY, 0f, offsetY, 0f, 0f, scaleZ, offsetZ, 0f, 0f, 0f, 1f); Matrix4f result = new Matrix4f(); result.set(cropMatrix); result.multLocal(projMatrix); vars.release(); shadowCam.setProjectionMatrix(result); }