use of com.jme3.math.Matrix4f in project jmonkeyengine by jMonkeyEngine.
the class BIHTree method collideWithBoundingVolume.
private int collideWithBoundingVolume(BoundingVolume bv, Matrix4f worldMatrix, CollisionResults results) {
BoundingBox bbox;
if (bv instanceof BoundingSphere) {
BoundingSphere sphere = (BoundingSphere) bv;
bbox = new BoundingBox(bv.getCenter().clone(), sphere.getRadius(), sphere.getRadius(), sphere.getRadius());
} else if (bv instanceof BoundingBox) {
bbox = new BoundingBox((BoundingBox) bv);
} else {
throw new UnsupportedCollisionException("BoundingVolume:" + bv);
}
bbox.transform(worldMatrix.invert(), bbox);
return root.intersectWhere(bv, bbox, worldMatrix, this, results);
}
use of com.jme3.math.Matrix4f in project jmonkeyengine by jMonkeyEngine.
the class PssmShadowUtil method computeZFar.
/**
* Compute the Zfar in the model vieuw to adjust the Zfar distance for the splits calculation
*/
public static float computeZFar(GeometryList occ, GeometryList recv, Camera cam) {
Matrix4f mat = cam.getViewMatrix();
BoundingBox bbOcc = ShadowUtil.computeUnionBound(occ, mat);
BoundingBox bbRecv = ShadowUtil.computeUnionBound(recv, mat);
return min(max(bbOcc.getZExtent() - bbOcc.getCenter().z, bbRecv.getZExtent() - bbRecv.getCenter().z), cam.getFrustumFar());
}
use of com.jme3.math.Matrix4f in project jmonkeyengine by jMonkeyEngine.
the class ShadowUtil method computeBoundForPoints.
/**
* Compute bounds from an array of points
* @param pts
* @param mat
* @return
*/
public static BoundingBox computeBoundForPoints(Vector3f[] pts, Matrix4f mat) {
Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
TempVars vars = TempVars.get();
Vector3f temp = vars.vect1;
for (int i = 0; i < pts.length; i++) {
float w = mat.multProj(pts[i], temp);
temp.x /= w;
temp.y /= w;
// Why was this commented out?
temp.z /= w;
min.minLocal(temp);
max.maxLocal(temp);
}
vars.release();
Vector3f center = min.add(max).multLocal(0.5f);
Vector3f extent = max.subtract(min).multLocal(0.5f);
//Nehon 08/18/2010 : Added an offset to the extend to avoid banding artifacts when the frustum are aligned
return new BoundingBox(center, extent.x + 2.0f, extent.y + 2.0f, extent.z + 2.5f);
}
use of com.jme3.math.Matrix4f in project jmonkeyengine by jMonkeyEngine.
the class ShadowUtil method computeUnionBound.
/**
* Compute bounds of a geomList
* @param list
* @param mat
* @return
*/
public static BoundingBox computeUnionBound(GeometryList list, Matrix4f mat) {
BoundingBox bbox = new BoundingBox();
TempVars tempv = TempVars.get();
for (int i = 0; i < list.size(); i++) {
BoundingVolume vol = list.get(i).getWorldBound();
BoundingVolume store = vol.transform(mat, tempv.bbox);
//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);
}
}
tempv.release();
return bbox;
}
use of com.jme3.math.Matrix4f in project jmonkeyengine by jMonkeyEngine.
the class ShadowUtil method updateShadowCamera.
/**
* 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);
}
Aggregations