Search in sources :

Example 41 with Matrix4f

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

the class TestOpenCLLibraries method simpleInitApp.

@Override
public void simpleInitApp() {
    BitmapFont fnt = assetManager.loadFont("Interface/Fonts/Default.fnt");
    Context clContext = context.getOpenCLContext();
    if (clContext == null) {
        BitmapText txt = new BitmapText(fnt);
        txt.setText("No OpenCL Context created!\nSee output log for details.");
        txt.setLocalTranslation(5, settings.getHeight() - 5, 0);
        guiNode.attachChild(txt);
        return;
    }
    CommandQueue clQueue = clContext.createQueue(clContext.getDevices().get(0));
    StringBuilder str = new StringBuilder();
    str.append("OpenCL Context created:\n  Platform: ").append(clContext.getDevices().get(0).getPlatform().getName()).append("\n  Devices: ").append(clContext.getDevices());
    str.append("\nTests:");
    str.append("\n  Random numbers: ").append(testRandom(clContext, clQueue));
    str.append("\n  Matrix3f: ").append(testMatrix3f(clContext, clQueue));
    str.append("\n  Matrix4f: ").append(testMatrix4f(clContext, clQueue));
    clQueue.release();
    BitmapText txt1 = new BitmapText(fnt);
    txt1.setText(str.toString());
    txt1.setLocalTranslation(5, settings.getHeight() - 5, 0);
    guiNode.attachChild(txt1);
    flyCam.setEnabled(false);
    inputManager.setCursorVisible(true);
}
Also used : BitmapText(com.jme3.font.BitmapText) BitmapFont(com.jme3.font.BitmapFont)

Example 42 with Matrix4f

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

the class FbxLoader method applyBindPoses.

/**
     * Copies the bind poses from FBX BindPose objects to FBX nodes.
     * Must be called prior to {@link #updateWorldTransforms()}.
     */
private void applyBindPoses() {
    for (FbxBindPose bindPose : bindPoses) {
        Map<FbxId, Matrix4f> bindPoseData = bindPose.getJmeObject();
        logger.log(Level.INFO, "Applying {0} bind poses", bindPoseData.size());
        for (Map.Entry<FbxId, Matrix4f> entry : bindPoseData.entrySet()) {
            FbxObject obj = objectMap.get(entry.getKey());
            if (obj instanceof FbxNode) {
                FbxNode node = (FbxNode) obj;
                node.setWorldBindPose(entry.getValue());
            } else {
                logger.log(Level.WARNING, "Bind pose can only be applied to FBX nodes. Ignoring.");
            }
        }
    }
}
Also used : FbxObject(com.jme3.scene.plugins.fbx.obj.FbxObject) FbxNode(com.jme3.scene.plugins.fbx.node.FbxNode) Matrix4f(com.jme3.math.Matrix4f) FbxBindPose(com.jme3.scene.plugins.fbx.anim.FbxBindPose) HashMap(java.util.HashMap) Map(java.util.Map) FbxId(com.jme3.scene.plugins.fbx.file.FbxId)

Example 43 with Matrix4f

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

the class ObjectHelper method getMatrix.

/**
     * This method returns the matrix of a given name for the given structure.
     * It takes up axis into consideration.
     * 
     * The method that moves the matrix from Z-up axis to Y-up axis space is as follows:
     * - load the matrix directly from blender (it has the Z-up axis orientation)
     * - switch the second and third rows in the matrix
     * - switch the second and third column in the matrix
     * - multiply the values in the third row by -1
     * - multiply the values in the third column by -1
     * 
     * The result matrix is now in Y-up axis orientation.
     * The procedure was discovered by experimenting but it looks like it's working :)
     * The previous procedure transformet the loaded matrix into component (loc, rot, scale),
     * switched several values and pu the back into the matrix.
     * It worked fine until models with negative scale are used.
     * The current method is not touched by that flaw.
     * 
     * @param structure
     *            the structure with matrix data
     * @param matrixName
     *            the name of the matrix
     * @param fixUpAxis
     *            tells if the Y axis is a UP axis
     * @param store
     *            the matrix where the result will pe placed
     * @return the required matrix
     */
@SuppressWarnings("unchecked")
private Matrix4f getMatrix(Structure structure, String matrixName, boolean fixUpAxis, Matrix4f store) {
    DynamicArray<Number> obmat = (DynamicArray<Number>) structure.getFieldValue(matrixName);
    // the matrix must be square
    int rowAndColumnSize = Math.abs((int) Math.sqrt(obmat.getTotalSize()));
    for (int i = 0; i < rowAndColumnSize; ++i) {
        for (int j = 0; j < rowAndColumnSize; ++j) {
            float value = obmat.get(j, i).floatValue();
            if (Math.abs(value) <= FastMath.FLT_EPSILON) {
                value = 0;
            }
            store.set(i, j, value);
        }
    }
    if (fixUpAxis) {
        // first switch the second and third row
        for (int i = 0; i < 4; ++i) {
            float temp = store.get(1, i);
            store.set(1, i, store.get(2, i));
            store.set(2, i, temp);
        }
        // then switch the second and third column
        for (int i = 0; i < 4; ++i) {
            float temp = store.get(i, 1);
            store.set(i, 1, store.get(i, 2));
            store.set(i, 2, temp);
        }
        // multiply the values in the third row by -1
        store.m20 *= -1;
        store.m21 *= -1;
        store.m22 *= -1;
        store.m23 *= -1;
        // multiply the values in the third column by -1
        store.m02 *= -1;
        store.m12 *= -1;
        store.m22 *= -1;
        store.m32 *= -1;
    }
    return store;
}
Also used : DynamicArray(com.jme3.scene.plugins.blender.file.DynamicArray) CullHint(com.jme3.scene.Spatial.CullHint)

Example 44 with Matrix4f

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

the class BIHTree method collideWithRay.

private int collideWithRay(Ray r, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) {
    TempVars vars = TempVars.get();
    try {
        CollisionResults boundResults = vars.collisionResults;
        boundResults.clear();
        worldBound.collideWith(r, boundResults);
        if (boundResults.size() > 0) {
            float tMin = boundResults.getClosestCollision().getDistance();
            float tMax = boundResults.getFarthestCollision().getDistance();
            if (tMax <= 0) {
                tMax = Float.POSITIVE_INFINITY;
            } else if (tMin == tMax) {
                tMin = 0;
            }
            if (tMin <= 0) {
                tMin = 0;
            }
            if (r.getLimit() < Float.POSITIVE_INFINITY) {
                tMax = Math.min(tMax, r.getLimit());
                if (tMin > tMax) {
                    return 0;
                }
            }
            //            return root.intersectBrute(r, worldMatrix, this, tMin, tMax, results);
            return root.intersectWhere(r, worldMatrix, this, tMin, tMax, results);
        }
        return 0;
    } finally {
        vars.release();
    }
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) TempVars(com.jme3.util.TempVars)

Example 45 with Matrix4f

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

the class BIHNode method intersectWhere.

public final int intersectWhere(Collidable col, BoundingBox box, Matrix4f worldMatrix, BIHTree tree, CollisionResults results) {
    TempVars vars = TempVars.get();
    ArrayList<BIHStackData> stack = vars.bihStack;
    stack.clear();
    float[] minExts = { box.getCenter().x - box.getXExtent(), box.getCenter().y - box.getYExtent(), box.getCenter().z - box.getZExtent() };
    float[] maxExts = { box.getCenter().x + box.getXExtent(), box.getCenter().y + box.getYExtent(), box.getCenter().z + box.getZExtent() };
    stack.add(new BIHStackData(this, 0, 0));
    Triangle t = new Triangle();
    int cols = 0;
    stackloop: while (stack.size() > 0) {
        BIHNode node = stack.remove(stack.size() - 1).node;
        while (node.axis != 3) {
            int a = node.axis;
            float maxExt = maxExts[a];
            float minExt = minExts[a];
            if (node.leftPlane < node.rightPlane) {
                // if the box is in that gap, we stop there
                if (minExt > node.leftPlane && maxExt < node.rightPlane) {
                    continue stackloop;
                }
            }
            if (maxExt < node.rightPlane) {
                node = node.left;
            } else if (minExt > node.leftPlane) {
                node = node.right;
            } else {
                stack.add(new BIHStackData(node.right, 0, 0));
                node = node.left;
            }
        //                if (maxExt < node.leftPlane
        //                 && maxExt < node.rightPlane){
        //                    node = node.left;
        //                }else if (minExt > node.leftPlane
        //                       && minExt > node.rightPlane){
        //                    node = node.right;
        //                }else{
        //                }
        }
        for (int i = node.leftIndex; i <= node.rightIndex; i++) {
            tree.getTriangle(i, t.get1(), t.get2(), t.get3());
            if (worldMatrix != null) {
                worldMatrix.mult(t.get1(), t.get1());
                worldMatrix.mult(t.get2(), t.get2());
                worldMatrix.mult(t.get3(), t.get3());
            }
            int added = col.collideWith(t, results);
            if (added > 0) {
                int index = tree.getTriangleIndex(i);
                int start = results.size() - added;
                for (int j = start; j < results.size(); j++) {
                    CollisionResult cr = results.getCollisionDirect(j);
                    cr.setTriangleIndex(index);
                }
                cols += added;
            }
        }
    }
    vars.release();
    return cols;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Triangle(com.jme3.math.Triangle) TempVars(com.jme3.util.TempVars)

Aggregations

Matrix4f (com.jme3.math.Matrix4f)36 TempVars (com.jme3.util.TempVars)24 Vector3f (com.jme3.math.Vector3f)15 FloatBuffer (java.nio.FloatBuffer)7 BoundingBox (com.jme3.bounding.BoundingBox)6 Transform (com.jme3.math.Transform)6 Bone (com.jme3.animation.Bone)4 CollisionResult (com.jme3.collision.CollisionResult)3 Spatial (com.jme3.scene.Spatial)3 BoundingVolume (com.jme3.bounding.BoundingVolume)2 Material (com.jme3.material.Material)2 Quaternion (com.jme3.math.Quaternion)2 Geometry (com.jme3.scene.Geometry)2 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)2 Structure (com.jme3.scene.plugins.blender.file.Structure)2 FbxId (com.jme3.scene.plugins.fbx.file.FbxId)2 FrameBuffer (com.jme3.texture.FrameBuffer)2 Texture2D (com.jme3.texture.Texture2D)2 Picture (com.jme3.ui.Picture)2 ByteBuffer (java.nio.ByteBuffer)2