Search in sources :

Example 6 with Transform

use of com.jme3.math.Transform 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) and the shadow occluder objects
     * collected through the traverse of the scene hierarchy
     */
public static void updateShadowCamera(ViewPort viewPort, GeometryList receivers, Camera shadowCam, Vector3f[] points, GeometryList splitOccluders, float shadowMapSize) {
    boolean ortho = shadowCam.isParallelProjection();
    shadowCam.setProjectionMatrix(null);
    if (ortho) {
        shadowCam.setFrustum(-shadowCam.getFrustumFar(), shadowCam.getFrustumFar(), -1, 1, 1, -1);
    }
    // create transform to rotate points to viewspace        
    Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
    BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);
    TempVars vars = TempVars.get();
    BoundingBox casterBB = new BoundingBox();
    BoundingBox receiverBB = new BoundingBox();
    int casterCount = 0, receiverCount = 0;
    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, vars.bbox);
        if (splitBB.intersects(recvBox)) {
            //Nehon : prevent NaN and infinity values to screw the final bounding box
            if (!Float.isNaN(recvBox.getCenter().x) && !Float.isInfinite(recvBox.getCenter().x)) {
                receiverBB.mergeLocal(recvBox);
                receiverCount++;
            }
        }
    }
    // collect splitOccluders through scene recursive traverse
    OccludersExtractor occExt = new OccludersExtractor(viewProjMatrix, casterCount, splitBB, casterBB, splitOccluders, vars);
    for (Spatial scene : viewPort.getScenes()) {
        occExt.addOccluders(scene);
    }
    casterCount = occExt.casterCount;
    //Nehon 08/18/2010 this is to avoid shadow bleeding when the ground is set to only receive shadows
    if (casterCount != receiverCount) {
        casterBB.setXExtent(casterBB.getXExtent() + 2.0f);
        casterBB.setYExtent(casterBB.getYExtent() + 2.0f);
        casterBB.setZExtent(casterBB.getZExtent() + 2.0f);
    }
    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);
    //Shadow map stabilization approximation from shaderX 7
    //from Practical Cascaded Shadow maps adapted to PSSM
    //scale stabilization
    float halfTextureSize = shadowMapSize * 0.5f;
    if (halfTextureSize != 0 && scaleX > 0 && scaleY > 0) {
        float scaleQuantizer = 0.1f;
        scaleX = 1.0f / FastMath.ceil(1.0f / scaleX * scaleQuantizer) * scaleQuantizer;
        scaleY = 1.0f / FastMath.ceil(1.0f / scaleY * scaleQuantizer) * scaleQuantizer;
    }
    offsetX = -0.5f * (cropMax.x + cropMin.x) * scaleX;
    offsetY = -0.5f * (cropMax.y + cropMin.y) * scaleY;
    //offset stabilization
    if (halfTextureSize != 0 && scaleX > 0 && scaleY > 0) {
        offsetX = FastMath.ceil(offsetX * halfTextureSize) / halfTextureSize;
        offsetY = FastMath.ceil(offsetY * halfTextureSize) / halfTextureSize;
    }
    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);
}
Also used : Geometry(com.jme3.scene.Geometry) Matrix4f(com.jme3.math.Matrix4f) Spatial(com.jme3.scene.Spatial) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) BoundingVolume(com.jme3.bounding.BoundingVolume) TempVars(com.jme3.util.TempVars)

Example 7 with Transform

use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.

the class BatchNode method doTransformsTangents.

private void doTransformsTangents(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, FloatBuffer bindBufTangents, FloatBuffer bufPos, FloatBuffer bufNorm, FloatBuffer bufTangents, int start, int end, Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;
    Vector3f tan = vars.vect3;
    int length = (end - start) * 3;
    int tanLength = (end - start) * 4;
    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    int tanOffset = start * 4;
    bindBufPos.rewind();
    bindBufNorm.rewind();
    bindBufTangents.rewind();
    bindBufPos.get(tmpFloat, 0, length);
    bindBufNorm.get(tmpFloatN, 0, length);
    bindBufTangents.get(tmpFloatT, 0, tanLength);
    int index = 0;
    int tanIndex = 0;
    while (index < length) {
        pos.x = tmpFloat[index];
        norm.x = tmpFloatN[index++];
        pos.y = tmpFloat[index];
        norm.y = tmpFloatN[index++];
        pos.z = tmpFloat[index];
        norm.z = tmpFloatN[index];
        tan.x = tmpFloatT[tanIndex++];
        tan.y = tmpFloatT[tanIndex++];
        tan.z = tmpFloatT[tanIndex++];
        transform.mult(pos, pos);
        transform.multNormal(norm, norm);
        transform.multNormal(tan, tan);
        index -= 2;
        tanIndex -= 3;
        tmpFloat[index] = pos.x;
        tmpFloatN[index++] = norm.x;
        tmpFloat[index] = pos.y;
        tmpFloatN[index++] = norm.y;
        tmpFloat[index] = pos.z;
        tmpFloatN[index++] = norm.z;
        tmpFloatT[tanIndex++] = tan.x;
        tmpFloatT[tanIndex++] = tan.y;
        tmpFloatT[tanIndex++] = tan.z;
        //Skipping 4th element of tangent buffer (handedness)
        tanIndex++;
    }
    vars.release();
    bufPos.position(offset);
    //using bulk put as it's faster
    bufPos.put(tmpFloat, 0, length);
    bufNorm.position(offset);
    //using bulk put as it's faster
    bufNorm.put(tmpFloatN, 0, length);
    bufTangents.position(tanOffset);
    //using bulk put as it's faster
    bufTangents.put(tmpFloatT, 0, tanLength);
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 8 with Transform

use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.

the class BatchNode method doTransforms.

private void doTransforms(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, FloatBuffer bufPos, FloatBuffer bufNorm, int start, int end, Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;
    int length = (end - start) * 3;
    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    bindBufPos.rewind();
    bindBufNorm.rewind();
    //bufPos.position(offset);
    //bufNorm.position(offset);
    bindBufPos.get(tmpFloat, 0, length);
    bindBufNorm.get(tmpFloatN, 0, length);
    int index = 0;
    while (index < length) {
        pos.x = tmpFloat[index];
        norm.x = tmpFloatN[index++];
        pos.y = tmpFloat[index];
        norm.y = tmpFloatN[index++];
        pos.z = tmpFloat[index];
        norm.z = tmpFloatN[index];
        transform.mult(pos, pos);
        transform.multNormal(norm, norm);
        index -= 2;
        tmpFloat[index] = pos.x;
        tmpFloatN[index++] = norm.x;
        tmpFloat[index] = pos.y;
        tmpFloatN[index++] = norm.y;
        tmpFloat[index] = pos.z;
        tmpFloatN[index++] = norm.z;
    }
    vars.release();
    bufPos.position(offset);
    //using bulk put as it's faster
    bufPos.put(tmpFloat, 0, length);
    bufNorm.position(offset);
    //using bulk put as it's faster
    bufNorm.put(tmpFloatN, 0, length);
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 9 with Transform

use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.

the class Geometry method computeWorldMatrix.

/**
     * Indicate that the transform of this spatial has changed and that
     * a refresh is required.
     */
// NOTE: Spatial has an identical implementation of this method,
// thus it was commented out.
//    @Override
//    protected void setTransformRefresh() {
//        refreshFlags |= RF_TRANSFORM;
//        setBoundRefresh();
//    }
/**
     * Recomputes the matrix returned by {@link Geometry#getWorldMatrix() }.
     * This will require a localized transform update for this geometry.
     */
public void computeWorldMatrix() {
    // Force a local update of the geometry's transform
    checkDoTransformUpdate();
    // Compute the cached world matrix
    cachedWorldMat.loadIdentity();
    cachedWorldMat.setRotationQuaternion(worldTransform.getRotation());
    cachedWorldMat.setTranslation(worldTransform.getTranslation());
    TempVars vars = TempVars.get();
    Matrix4f scaleMat = vars.tempMat4;
    scaleMat.loadIdentity();
    scaleMat.scale(worldTransform.getScale());
    cachedWorldMat.multLocal(scaleMat);
    vars.release();
}
Also used : Matrix4f(com.jme3.math.Matrix4f) TempVars(com.jme3.util.TempVars)

Example 10 with Transform

use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.

the class Spatial method write.

public void write(JmeExporter ex) throws IOException {
    OutputCapsule capsule = ex.getCapsule(this);
    capsule.write(name, "name", null);
    capsule.write(worldBound, "world_bound", null);
    capsule.write(cullHint, "cull_mode", CullHint.Inherit);
    capsule.write(batchHint, "batch_hint", BatchHint.Inherit);
    capsule.write(queueBucket, "queue", RenderQueue.Bucket.Inherit);
    capsule.write(shadowMode, "shadow_mode", ShadowMode.Inherit);
    capsule.write(localTransform, "transform", Transform.IDENTITY);
    capsule.write(localLights, "lights", null);
    capsule.writeSavableArrayList(new ArrayList(localOverrides), "overrides", null);
    // Shallow clone the controls array to convert its type.
    capsule.writeSavableArrayList(new ArrayList(controls), "controlsList", null);
    capsule.writeStringSavableMap(userData, "user_data", null);
}
Also used : SafeArrayList(com.jme3.util.SafeArrayList)

Aggregations

Vector3f (com.jme3.math.Vector3f)29 Transform (com.jme3.math.Transform)26 TempVars (com.jme3.util.TempVars)24 Quaternion (com.jme3.math.Quaternion)11 Matrix4f (com.jme3.math.Matrix4f)10 Bone (com.jme3.animation.Bone)9 BoundingBox (com.jme3.bounding.BoundingBox)6 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)5 PointLight (com.jme3.light.PointLight)4 Spatial (com.jme3.scene.Spatial)4 FloatBuffer (java.nio.FloatBuffer)4 Transform (com.bulletphysics.linearmath.Transform)3 ChildCollisionShape (com.jme3.bullet.collision.shapes.infos.ChildCollisionShape)3 DirectionalLight (com.jme3.light.DirectionalLight)3 Light (com.jme3.light.Light)3 SpotLight (com.jme3.light.SpotLight)3 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)2 BoneTrack (com.jme3.animation.BoneTrack)2 SpatialTrack (com.jme3.animation.SpatialTrack)2 BoundingVolume (com.jme3.bounding.BoundingVolume)2