/** * Updates the shadow camera to properly contain the given points (which contain the eye camera * frustum corners) * * @param shadowCam * @param points */ public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) { boolean ortho = shadowCam.isParallelProjection(); shadowCam.setProjectionMatrix(null); if (ortho) { shadowCam.setFrustum(-1, 1, -1, 1, 1, -1); } else { shadowCam.setFrustumPerspective(45, 1, 1, 150); } Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix(); Matrix4f projMatrix = shadowCam.getProjectionMatrix(); BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix); TempVars vars = TempVars.get(); Vector3f splitMin = splitBB.getMin(vars.vect1); Vector3f splitMax = splitBB.getMax(vars.vect2); // splitMin.z = 0; // Create the crop matrix. float scaleX, scaleY, scaleZ; float offsetX, offsetY, offsetZ; scaleX = 2.0f / (splitMax.x - splitMin.x); scaleY = 2.0f / (splitMax.y - splitMin.y); offsetX = -0.5f * (splitMax.x + splitMin.x) * scaleX; offsetY = -0.5f * (splitMax.y + splitMin.y) * scaleY; scaleZ = 1.0f / (splitMax.z - splitMin.z); offsetZ = -splitMin.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); }
/** * 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); }
/** Computes which points are inside the hull */ private Mesh buildPreviewMesh() { long start = System.currentTimeMillis(); // First get the bounding box. updateWorldBound(); BoundingBox bound = (BoundingBox) getWorldBound(); Vector3f maxBound = bound.getMax(null); Vector3f originPoint = bound.getMin(null); originPoint.x = Math.min(originPoint.x, -maxBound.x); originPoint.y = Math.min(originPoint.y, -maxBound.y); originPoint.z = Math.min(originPoint.z, -maxBound.z); // Thread Pool ForkJoinPool pool = new ForkJoinPool(); // Create an octree from the data OctreeNode octree = new OctreeNode(originPoint, maxBound); OctreeConstructionTask dcOctreeTask = new OctreeConstructionTask(octree, primitives, 3, 6); pool.invoke(dcOctreeTask); // Contour the octree. AdaptiveDualContouringTask adaptiveTask = new AdaptiveDualContouringTask(octree, primitives); pool.invoke(adaptiveTask); // Retrieve computed data. ArrayList<Vector3f> verticesList = dcOctreeTask.getVertices(); ArrayList<Vector3i> triangles = adaptiveTask.getTriangles(); int numberOfVerticesBefore = verticesList.size(); int numberOfTrianglesBefore = triangles.size(); // Compute normals both from data and triangles. Vector3f normals[] = MeshUtils.facetedNormalsFromFaces( triangles, verticesList, primitives, (float) Math.toRadians(10)); // Drop the triangles to an array. int index = 0; int[] triangleList = new int[3 * triangles.size()]; for (Vector3i v : triangles) { triangleList[index++] = v.x; triangleList[index++] = v.y; triangleList[index++] = v.z; } // Finally, make the mesh itself: Mesh mesh = new Mesh(); mesh.setBuffer( Type.Position, 3, BufferUtils.createFloatBuffer(verticesList.toArray(new Vector3f[0]))); mesh.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(triangleList)); mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals)); mesh.updateBound(); mesh.setStatic(); long timeTaken = System.currentTimeMillis() - start; System.out.println( String.format( "%d Vertices, %d Triangles in %d Milliseconds", verticesList.size(), triangles.size(), timeTaken)); return mesh; }