Search in sources :

Example 26 with Matrix

use of com.jme3.scene.plugins.blender.math.Matrix in project jmonkeyengine by jMonkeyEngine.

the class Matrix4f method fillFloatBuffer.

/**
     * <code>fillFloatBuffer</code> fills a FloatBuffer object with the matrix
     * data.
     * 
     * @param fb
     *            the buffer to fill, starting at current position. Must have
     *            room for 16 more floats.
     * @param columnMajor
     *            if true, this buffer should be filled with column major data,
     *            otherwise it will be filled row major.
     * @return matrix data as a FloatBuffer. (position is advanced by 16 and any
     *         limit set is not changed).
     */
public FloatBuffer fillFloatBuffer(FloatBuffer fb, boolean columnMajor) {
    //        if (columnMajor) {
    //            fb.put(m00).put(m10).put(m20).put(m30);
    //            fb.put(m01).put(m11).put(m21).put(m31);
    //            fb.put(m02).put(m12).put(m22).put(m32);
    //            fb.put(m03).put(m13).put(m23).put(m33);
    //        } else {
    //            fb.put(m00).put(m01).put(m02).put(m03);
    //            fb.put(m10).put(m11).put(m12).put(m13);
    //            fb.put(m20).put(m21).put(m22).put(m23);
    //            fb.put(m30).put(m31).put(m32).put(m33);
    //        }
    TempVars vars = TempVars.get();
    fillFloatArray(vars.matrixWrite, columnMajor);
    fb.put(vars.matrixWrite, 0, 16);
    vars.release();
    return fb;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 27 with Matrix

use of com.jme3.scene.plugins.blender.math.Matrix in project jmonkeyengine by jMonkeyEngine.

the class TestOpenCLLibraries method testMatrix4f.

private boolean testMatrix4f(Context clContext, CommandQueue clQueue) {
    try {
        String code = "" + "#import \"Common/OpenCL/Matrix4f.clh\"\n" + "\n" + "__kernel void TestMatrix4f_1(mat4 m1, __global char* result)\n" + "{\n" + "  mat4 id = mat4Identity();\n" + "  mat4 m1Inv = mat4Invert(m1);\n" + "  mat4 m1Res = mat4Mult(m1, m1Inv);\n" + "  result[0] = mat4Equals(id, m1Res, 0.0001f) ? 1 : 0;\n" + "}\n" + "\n" + "__kernel void TestMatrix4f_2(mat4 m1, float d, mat4 m2, mat4 m3, __global char* result)\n" + "{\n" + "  float d2 = mat4Determinant(m1);\n" + "  result[0] = fabs(d - d2) < 0.0001f ? 1 : 0;\n" + "  mat4 res = mat4Transpose(m1);\n" + "  result[1] = mat4Equals(res, m2, 0.0001f) ? 1 : 0;\n" + "  res = mat4Adjoint(m1);\n" + "  result[2] = mat4Equals(res, m3, 0.0001f) ? 1 : 0;\n" + "}\n";
        Program program = clContext.createProgramFromSourceCodeWithDependencies(code, assetManager);
        program.build();
        com.jme3.opencl.Buffer buffer = clContext.createBuffer(3);
        Random rand = new Random(1561);
        Kernel testMatrix4fKernel1 = program.createKernel("TestMatrix4f_1");
        Matrix4f m1 = new Matrix4f();
        do {
            for (int i = 0; i < 4; ++i) {
                for (int j = 0; j < 4; ++j) {
                    m1.set(i, j, rand.nextFloat() * 20 - 10);
                }
            }
        } while (FastMath.abs(m1.determinant()) < 0.00001f);
        testMatrix4fKernel1.Run1NoEvent(clQueue, new Kernel.WorkSize(1), m1, buffer);
        ByteBuffer bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix inversion failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix4fKernel1.release();
        Kernel testMatrix4fKernel2 = program.createKernel("TestMatrix4f_2");
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                m1.set(i, j, rand.nextFloat() * 20 - 10);
            }
        }
        testMatrix4fKernel2.Run1NoEvent(clQueue, new Kernel.WorkSize(1), m1, m1.determinant(), m1.transpose(), m1.adjoint(), buffer);
        bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix determinant computation failed");
            return false;
        }
        if (bb.get() == 0) {
            LOG.severe("Matrix transposing failed");
            return false;
        }
        if (bb.get() == 0) {
            LOG.severe("Matrix adjoint computation failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix4fKernel2.release();
        buffer.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "matrix4f test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "matrix4f test failed with:", ex);
        return false;
    }
    return true;
}
Also used : com.jme3.opencl(com.jme3.opencl) Matrix4f(com.jme3.math.Matrix4f) Random(java.util.Random)

Example 28 with Matrix

use of com.jme3.scene.plugins.blender.math.Matrix in project jmonkeyengine by jMonkeyEngine.

the class TestOpenCLLibraries method testMatrix3f.

private boolean testMatrix3f(Context clContext, CommandQueue clQueue) {
    try {
        String code = "" + "#import \"Common/OpenCL/Matrix3f.clh\"\n" + "\n" + "__kernel void TestMatrix3f_1(__global char* result)\n" + "{\n" + "  mat3 id = mat3Identity();\n" + "  mat3 m1 = mat3FromRows( (float3)(23,-3,10.4f), (float3)(5,-8,2.22f), (float3)(-1,0,34) );\n" + "  mat3 m1Inv = mat3Invert(m1);\n" + "  mat3 m1Res = mat3Mult(m1, m1Inv);\n" + "  result[0] = mat3Equals(id, m1Res, 0.0001f) ? 1 : 0;\n" + "}\n" + "\n" + "__kernel void TestMatrix3f_2(mat3 m1, float a, mat3 m2, mat3 mRes, __global char* result)\n" + "{\n" + "  mat3 m = mat3Transpose(m1);\n" + "  m = mat3Add(mat3Scale(m, a), m2);\n" + "  result[0] = mat3Equals(mRes, m, 0.01f) ? 1 : 0;\n" + "}\n";
        Program program = clContext.createProgramFromSourceCodeWithDependencies(code, assetManager);
        program.build();
        com.jme3.opencl.Buffer buffer = clContext.createBuffer(1);
        Kernel testMatrix3fKernel1 = program.createKernel("TestMatrix3f_1");
        testMatrix3fKernel1.Run1NoEvent(clQueue, new Kernel.WorkSize(1), buffer);
        ByteBuffer bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix inversion failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix3fKernel1.release();
        Kernel testMatrix3fKernel2 = program.createKernel("TestMatrix3f_2");
        Matrix3f m1 = new Matrix3f(13.24f, -0.234f, 42, 83.23f, -34.2f, 3.2f, 0.25f, -42, 7.64f);
        Matrix3f m2 = new Matrix3f(-5.2f, 0.757f, 2.01f, 12.0f, -6, 2, 0.01f, 9, 2.255f);
        Matrix3f mRes = new Matrix3f(-31.68f, -165.703f, 1.51f, 12.468f, 62.4f, 86, -83.99f, 2.6f, -13.025f);
        testMatrix3fKernel2.Run1NoEvent(clQueue, new Kernel.WorkSize(1), m1, -2.0f, m2, mRes, buffer);
        bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix add, multiply, transpose failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix3fKernel2.release();
        buffer.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "matrix3f test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "matrix3f test failed with:", ex);
        return false;
    }
    return true;
}
Also used : com.jme3.opencl(com.jme3.opencl) Matrix3f(com.jme3.math.Matrix3f)

Aggregations

TempVars (com.jme3.util.TempVars)12 Matrix4f (com.jme3.math.Matrix4f)10 Vector3f (com.jme3.math.Vector3f)5 Bone (com.jme3.animation.Bone)3 Camera (com.jme3.renderer.Camera)3 Spatial (com.jme3.scene.Spatial)3 BoundingBox (com.jme3.bounding.BoundingBox)2 com.jme3.opencl (com.jme3.opencl)2 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)2 Uniform (com.jme3.shader.Uniform)2 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Matrix (android.graphics.Matrix)1 Skeleton (com.jme3.animation.Skeleton)1 TextureKey (com.jme3.asset.TextureKey)1 BoundingSphere (com.jme3.bounding.BoundingSphere)1 BoundingVolume (com.jme3.bounding.BoundingVolume)1 JoyAxisEvent (com.jme3.input.event.JoyAxisEvent)1 DirectionalLight (com.jme3.light.DirectionalLight)1 Light (com.jme3.light.Light)1