Search in sources :

Example 1 with Quaternion4d

use of io.xol.chunkstories.api.math.Quaternion4d in project chunkstories by Hugobros3.

the class BVHAnimation method main.

public static void main(String[] a) throws FileNotFoundException {
    BVHAnimation test = new BVHAnimation(new FileInputStream(new File("res/animations/human/human.bvh")));
    System.out.println(test.toString());
    float rotX = (float) (Math.random() * 2.0 * Math.PI);
    float rotY = (float) (Math.random() * 2.0 * Math.PI);
    float rotZ = (float) (Math.random() * 2.0 * Math.PI);
    Quaternion4d quaternionXLower = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotX);
    Quaternion4d quaternionYLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotY);
    Quaternion4d quaternionZLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZ);
    Quaternion4d total = new Quaternion4d(quaternionXLower);
    total = total.mult(quaternionYLower);
    total.normalize();
    total = total.mult(quaternionZLower);
    Matrix4f matrix = new Matrix4f();
    // Apply rotations
    matrix.rotate(rotX, new Vector3f(1, 0, 0));
    matrix.rotate(rotY, new Vector3f(0, 1, 0));
    matrix.rotate(rotZ, new Vector3f(0, 0, 1));
    Matrix4f mX = new Matrix4f();
    Matrix4f mY = new Matrix4f();
    Matrix4f mZ = new Matrix4f();
    mX.rotate(rotX, new Vector3f(1, 0, 0));
    mY.rotate(rotY, new Vector3f(0, 1, 0));
    mZ.rotate(rotZ, new Vector3f(0, 0, 1));
    /*System.out.println("Old:\n"+matrix);
		System.out.println("New:\n"+Matrix4f.mul(Matrix4f.mul(mX, mY, null), mZ, null));
		
		System.out.println("mX:\n"+mX);
		System.out.println("mY:\n"+mY);
		System.out.println("mZ:\n"+mZ);*/
    // System.out.println(quaternionXLower);
    // System.out.println(quaternionYLower);
    // System.out.println(quaternionZLower);
    mX = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotX).toMatrix4f();
    mY = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotY).toMatrix4f();
    mZ = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZ).toMatrix4f();
    // System.out.println("Old:\n"+matrix);
    // System.out.println("New:\n"+Matrix4f.mul(Matrix4f.mul(mX, mY, null), mZ, null));
    // System.out.println("mX:\n"+mX);
    // System.out.println("mY:\n"+mY);
    // System.out.println("mZ:\n"+mZ);
    // total = Matrix4f.invert(total, null);
    // System.out.println("Qml:\n"+total.toMatrix4f());
    // System.out.println("Inv:\n"+Matrix4f.invert(total.toMatrix4f(), null));
    // Swaps Y and Z axises arround
    // matrix = new Matrix4f();
    System.out.println(matrix);
    System.out.println("");
    Matrix4f blender2ingame = new Matrix4f();
    blender2ingame.m11(0);
    blender2ingame.m22(0);
    blender2ingame.m12(1);
    blender2ingame.m21(1);
    // Rotate the matrix first to apply the transformation in blender space
    blender2ingame.mul(matrix, matrix);
    // Matrix4f.mul(blender2ingame, matrix, matrix);
    // Mirror it
    Matrix4f mirror = new Matrix4f();
    mirror.m22(-1);
    mirror.mul(matrix, matrix);
    // Matrix4f.mul(mirror, matrix, matrix);
    // Rotate again after so it's back the correct way arround
    matrix.mul(blender2ingame);
    // Matrix4f.mul(matrix, blender2ingame, matrix);
    matrix.mul(mirror);
    // Matrix4f.mul(matrix, mirror, matrix);
    System.out.println(matrix);
}
Also used : Quaternion4d(io.xol.chunkstories.api.math.Quaternion4d) Matrix4f(org.joml.Matrix4f) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with Quaternion4d

use of io.xol.chunkstories.api.math.Quaternion4d in project chunkstories by Hugobros3.

the class BVHTreeBone method getTransformationMatrixInterpolatedInternal.

private Matrix4f getTransformationMatrixInterpolatedInternal(int frameLower, int frameUpper, double t) {
    // Read rotation data from where it is
    float rotXLower;
    float rotYLower;
    float rotZLower;
    if (channels == 6) {
        rotXLower = toRad(animationData[frameLower][3]);
        rotYLower = toRad(animationData[frameLower][4]);
        rotZLower = toRad(animationData[frameLower][5]);
    } else {
        rotXLower = toRad(animationData[frameLower][0]);
        rotYLower = toRad(animationData[frameLower][1]);
        rotZLower = toRad(animationData[frameLower][2]);
    }
    float rotXUpper;
    float rotYUpper;
    float rotZUpper;
    if (channels == 6) {
        rotXUpper = toRad(animationData[frameUpper][3]);
        rotYUpper = toRad(animationData[frameUpper][4]);
        rotZUpper = toRad(animationData[frameUpper][5]);
    } else {
        rotXUpper = toRad(animationData[frameUpper][0]);
        rotYUpper = toRad(animationData[frameUpper][1]);
        rotZUpper = toRad(animationData[frameUpper][2]);
    }
    Quaternion4d quaternionXLower = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotXLower);
    Quaternion4d quaternionYLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotYLower);
    Quaternion4d quaternionZLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZLower);
    Quaternion4d totalLower = quaternionXLower.mult(quaternionYLower).mult(quaternionZLower);
    Quaternion4d quaternionXUpper = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotXUpper);
    Quaternion4d quaternionYUpper = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotYUpper);
    Quaternion4d quaternionZUpper = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZUpper);
    Quaternion4d totalUpper = (quaternionXUpper.mult(quaternionYUpper)).mult(quaternionZUpper);
    Quaternion4d total = Quaternion4d.slerp(totalLower, totalUpper, t);
    Matrix4f matrix = total.toMatrix4f();
    // Apply transformations
    if (channels == 6) {
        matrix.m30(matrix.m30() + Math2.mix(animationData[frameLower][0], animationData[frameUpper][0], t));
        matrix.m31(matrix.m31() + Math2.mix(animationData[frameLower][1], animationData[frameUpper][1], t));
        matrix.m32(matrix.m32() + Math2.mix(animationData[frameLower][2], animationData[frameUpper][2], t));
    } else // TODO check on that, I'm not sure if you should apply both when possible
    {
        matrix.m30(matrix.m30() + offset.x());
        matrix.m31(matrix.m31() + offset.y());
        matrix.m32(matrix.m32() + offset.z());
    }
    return BVHAnimation.transformBlenderBVHExportToChunkStoriesWorldSpace(matrix);
}
Also used : Quaternion4d(io.xol.chunkstories.api.math.Quaternion4d) Matrix4f(org.joml.Matrix4f) Vector3d(org.joml.Vector3d)

Aggregations

Quaternion4d (io.xol.chunkstories.api.math.Quaternion4d)2 Matrix4f (org.joml.Matrix4f)2 Vector3d (org.joml.Vector3d)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Vector3f (org.joml.Vector3f)1