Search in sources :

Example 26 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class MD5AnimationLoader method createAnimation.

private MeshAnimationData createAnimation(MD5 md5) {
    List<String> boneNames = Lists.newArrayListWithCapacity(md5.numJoints);
    TIntList boneParents = new TIntArrayList(md5.numJoints);
    for (int i = 0; i < md5.numJoints; ++i) {
        boneNames.add(md5.joints[i].name);
        boneParents.add(md5.joints[i].parent);
    }
    float timePerFrame = 1.0f / md5.frameRate;
    List<MeshAnimationFrame> frames = Lists.newArrayList();
    for (int frameIndex = 0; frameIndex < md5.numFrames; ++frameIndex) {
        MD5Frame frame = md5.frames[frameIndex];
        List<Vector3f> positions = Lists.newArrayListWithExpectedSize(md5.numJoints);
        List<Vector3f> rawRotations = Lists.newArrayListWithExpectedSize(md5.numJoints);
        for (int i = 0; i < md5.numJoints; ++i) {
            positions.add(new Vector3f(md5.baseFramePosition[i]));
            rawRotations.add(new Vector3f(md5.baseFrameOrientation[i]));
        }
        for (int jointIndex = 0; jointIndex < md5.numJoints; ++jointIndex) {
            int compIndex = 0;
            if ((md5.joints[jointIndex].flags & POSITION_X_FLAG) != 0) {
                positions.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & POSITION_Y_FLAG) != 0) {
                positions.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & POSITION_Z_FLAG) != 0) {
                positions.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & ORIENTATION_X_FLAG) != 0) {
                rawRotations.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & ORIENTATION_Y_FLAG) != 0) {
                rawRotations.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & ORIENTATION_Z_FLAG) != 0) {
                rawRotations.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
            }
        }
        List<Quat4f> rotations = rawRotations.stream().map(rot -> MD5ParserCommon.completeQuat4f(rot.x, rot.y, rot.z)).collect(Collectors.toCollection(ArrayList::new));
        // Rotate just the root bone to correct for coordinate system differences
        rotations.set(0, MD5ParserCommon.correctQuat4f(rotations.get(0)));
        positions.set(0, MD5ParserCommon.correctOffset(positions.get(0)));
        frames.add(new MeshAnimationFrame(positions, rotations));
    }
    AABB aabb = AABB.createEncompassing(Arrays.asList(md5.bounds));
    return new MeshAnimationData(boneNames, boneParents, frames, timePerFrame, aabb);
}
Also used : Charsets(com.google.common.base.Charsets) TIntList(gnu.trove.list.TIntList) MeshAnimationFrame(org.terasology.rendering.assets.animation.MeshAnimationFrame) Arrays(java.util.Arrays) TIntArrayList(gnu.trove.list.array.TIntArrayList) Vector3f(org.terasology.math.geom.Vector3f) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) MeshAnimationData(org.terasology.rendering.assets.animation.MeshAnimationData) List(java.util.List) Lists(com.google.common.collect.Lists) Matcher(java.util.regex.Matcher) Quat4f(org.terasology.math.geom.Quat4f) AbstractAssetFileFormat(org.terasology.assets.format.AbstractAssetFileFormat) AssetDataFile(org.terasology.assets.format.AssetDataFile) AABB(org.terasology.math.AABB) RegisterAssetFileFormat(org.terasology.assets.module.annotations.RegisterAssetFileFormat) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) ResourceUrn(org.terasology.assets.ResourceUrn) InputStream(java.io.InputStream) MeshAnimationFrame(org.terasology.rendering.assets.animation.MeshAnimationFrame) TIntArrayList(gnu.trove.list.array.TIntArrayList) Vector3f(org.terasology.math.geom.Vector3f) MeshAnimationData(org.terasology.rendering.assets.animation.MeshAnimationData) TIntList(gnu.trove.list.TIntList) Quat4f(org.terasology.math.geom.Quat4f) AABB(org.terasology.math.AABB)

Example 27 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class MD5ParserCommon method completeQuat4f.

public static Quat4f completeQuat4f(float x, float y, float z) {
    float t = 1.0f - (x * x) - (y * y) - (z * z);
    float w = 0;
    if (t > 0.0f) {
        w = (float) -Math.sqrt(t);
    }
    Quat4f result = new Quat4f(x, y, z, w);
    result.normalize();
    return result;
}
Also used : Quat4f(org.terasology.math.geom.Quat4f)

Example 28 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class Block method setCollision.

/**
 * Set the collision box for the block
 *
 * @param offset The offset to the block's center
 * @param shape  The shape of collision box
 */
public void setCollision(Vector3f offset, CollisionShape shape) {
    collisionShape = shape;
    collisionOffset = offset;
    bounds = shape.getAABB(new Transform(offset, new Quat4f(0, 0, 0, 1), 1.0f));
}
Also used : Transform(org.terasology.math.Transform) Quat4f(org.terasology.math.geom.Quat4f)

Example 29 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class Bone method getLocalPosition.

public Vector3f getLocalPosition() {
    Vector3f pos = new Vector3f(objectSpacePos);
    if (parent != null) {
        pos.sub(parent.getObjectPosition());
        Quat4f inverseParentRot = new Quat4f();
        inverseParentRot.inverse(parent.getObjectRotation());
        inverseParentRot.rotate(pos, pos);
    }
    return pos;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) Quat4f(org.terasology.math.geom.Quat4f)

Example 30 with Quat4f

use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.

the class OpenVRStereoCamera method updateMatrices.

@Override
public void updateMatrices(float fov) {
    prevViewProjectionMatrix.set(viewProjectionMatrix);
    org.joml.Matrix4f leftEyeProjection = vrProvider.getState().getEyeProjectionMatrix(0);
    org.joml.Matrix4f rightEyeProjection = vrProvider.getState().getEyeProjectionMatrix(1);
    org.joml.Matrix4f leftEyePose = vrProvider.getState().getEyePose(0);
    org.joml.Matrix4f rightEyePose = vrProvider.getState().getEyePose(1);
    float halfIPD = (float) Math.sqrt(Math.pow(leftEyePose.m30() - rightEyePose.m30(), 2) + Math.pow(leftEyePose.m31() - rightEyePose.m31(), 2) + Math.pow(leftEyePose.m32() - rightEyePose.m32(), 2)) / 2.0f;
    // set camera orientation
    Vector4f vecQuaternion = OpenVRUtil.convertToQuaternion(leftEyePose);
    Quaternionf quaternion = new Quaternionf(vecQuaternion.x, vecQuaternion.y, vecQuaternion.z, vecQuaternion.w);
    setOrientation(new Quat4f(quaternion.x, quaternion.y, quaternion.z, quaternion.w));
    // view matrix is inverse of pose matrix
    leftEyePose = leftEyePose.invert();
    rightEyePose = rightEyePose.invert();
    if (Math.sqrt(Math.pow(leftEyePose.m30(), 2) + Math.pow(leftEyePose.m31(), 2) + Math.pow(leftEyePose.m32(), 2)) < 0.25) {
        return;
    }
    jomlMatrix4f(leftEyeProjection, projectionMatrixLeftEye);
    jomlMatrix4f(rightEyeProjection, projectionMatrixRightEye);
    projectionMatrix = projectionMatrixLeftEye;
    jomlMatrix4f(leftEyePose, viewMatrixLeftEye);
    jomlMatrix4f(rightEyePose, viewMatrixRightEye);
    viewMatrix = viewMatrixLeftEye;
    normViewMatrix = viewMatrixLeftEye;
    reflectionMatrix.setRow(0, 1.0f, 0.0f, 0.0f, 0.0f);
    reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 2f * (-position.y + 32f));
    reflectionMatrix.setRow(2, 0.0f, 0.0f, 1.0f, 0.0f);
    reflectionMatrix.setRow(3, 0.0f, 0.0f, 0.0f, 1.0f);
    viewMatrixReflected.mul(viewMatrix, reflectionMatrix);
    reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 0.0f);
    normViewMatrixReflected.mul(normViewMatrix, reflectionMatrix);
    viewTranslationLeftEye.setIdentity();
    viewTranslationLeftEye.setTranslation(new Vector3f(halfIPD, 0.0f, 0.0f));
    viewTranslationRightEye.setIdentity();
    viewTranslationRightEye.setTranslation(new Vector3f(-halfIPD, 0.0f, 0.0f));
    viewMatrixReflectedLeftEye.mul(viewMatrixReflected, viewTranslationLeftEye);
    viewMatrixReflectedRightEye.mul(viewMatrixReflected, viewTranslationRightEye);
    viewProjectionMatrixLeftEye = MatrixUtils.calcViewProjectionMatrix(viewMatrixLeftEye, projectionMatrixLeftEye);
    viewProjectionMatrixRightEye = MatrixUtils.calcViewProjectionMatrix(viewMatrixRightEye, projectionMatrixRightEye);
    inverseViewProjectionMatrixLeftEye = new Matrix4f(viewProjectionMatrixLeftEye);
    inverseViewProjectionMatrixRightEye = new Matrix4f(viewProjectionMatrixRightEye);
    inverseViewProjectionMatrixLeftEye.invert(viewProjectionMatrixLeftEye);
    inverseViewProjectionMatrixRightEye.invert(viewProjectionMatrixRightEye);
    inverseProjectionMatrixLeftEye = new Matrix4f(projectionMatrixLeftEye);
    inverseProjectionMatrixRightEye = new Matrix4f(projectionMatrixRightEye);
    inverseProjectionMatrixLeftEye.invert(projectionMatrixLeftEye);
    inverseProjectionMatrixRightEye.invert(projectionMatrixRightEye);
    updateFrustum();
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) Vector4f(org.joml.Vector4f) Vector3f(org.terasology.math.geom.Vector3f) Quaternionf(org.joml.Quaternionf) Quat4f(org.terasology.math.geom.Quat4f)

Aggregations

Quat4f (org.terasology.math.geom.Quat4f)50 Vector3f (org.terasology.math.geom.Vector3f)33 LocationComponent (org.terasology.logic.location.LocationComponent)16 EntityRef (org.terasology.entitySystem.entity.EntityRef)14 Matrix4f (org.terasology.math.geom.Matrix4f)9 ClientComponent (org.terasology.network.ClientComponent)7 BaseQuat4f (org.terasology.math.geom.BaseQuat4f)6 FloatBuffer (java.nio.FloatBuffer)4 Test (org.junit.Test)4 BaseVector3f (org.terasology.math.geom.BaseVector3f)4 ArrayList (java.util.ArrayList)3 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)3 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)3 CharacterComponent (org.terasology.logic.characters.CharacterComponent)3 Lists (com.google.common.collect.Lists)2 TIntList (gnu.trove.list.TIntList)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Arrays (java.util.Arrays)2