Search in sources :

Example 16 with Matrix4f

use of org.joml.Matrix4f in project Terasology by MovingBlocks.

the class BulletSweepCallback method checkForStep.

@Override
public boolean checkForStep(Vector3f direction, float stepHeight, float slopeFactor, float checkForwardDistance) {
    boolean moveUpStep;
    boolean hitStep = false;
    float stepSlope = 1f;
    Vector3f lookAheadOffset = new Vector3f(direction.x, direction.y, direction.z);
    lookAheadOffset.y = 0;
    lookAheadOffset.normalize();
    lookAheadOffset.mul(checkForwardDistance);
    Vector3f fromWorld = new Vector3f();
    this.getHitPointWorld(fromWorld);
    fromWorld.y += stepHeight + 0.05f;
    fromWorld.add(lookAheadOffset);
    Vector3f toWorld = new Vector3f();
    this.getHitPointWorld(toWorld);
    toWorld.y -= 0.05f;
    toWorld.add(lookAheadOffset);
    ClosestRayResultCallback rayResult = new ClosestRayResultCallback(fromWorld, toWorld);
    Matrix4f transformFrom = new Matrix4f().setTranslation(fromWorld);
    Matrix4f transformTo = new Matrix4f().setTranslation(toWorld);
    Matrix4f targetTransform = this.getHitCollisionObject().getWorldTransform();
    btDiscreteDynamicsWorld.rayTestSingle(transformFrom, transformTo, this.getHitCollisionObject(), this.getHitCollisionObject().getCollisionShape(), targetTransform, rayResult);
    if (rayResult.hasHit()) {
        hitStep = true;
        Vector3f hitNormal = new Vector3f();
        rayResult.getHitNormalWorld(hitNormal);
        stepSlope = hitNormal.dot(0, 1, 0);
    }
    fromWorld.add(lookAheadOffset);
    toWorld.add(lookAheadOffset);
    rayResult = new ClosestRayResultCallback(fromWorld, toWorld);
    transformFrom = new Matrix4f().setTranslation(fromWorld);
    transformTo = new Matrix4f().setTranslation(toWorld);
    targetTransform = this.getHitCollisionObject().getWorldTransform();
    btDiscreteDynamicsWorld.rayTestSingle(transformFrom, transformTo, this.getHitCollisionObject(), this.getHitCollisionObject().getCollisionShape(), targetTransform, rayResult);
    if (rayResult.hasHit()) {
        hitStep = true;
        Vector3f hitNormal = new Vector3f();
        rayResult.getHitNormalWorld(hitNormal);
        stepSlope = Math.min(stepSlope, hitNormal.dot(0, 1, 0));
    }
    moveUpStep = hitStep && stepSlope >= slopeFactor;
    return moveUpStep;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f) ClosestRayResultCallback(com.badlogic.gdx.physics.bullet.collision.ClosestRayResultCallback)

Example 17 with Matrix4f

use of org.joml.Matrix4f in project Terasology by MovingBlocks.

the class SkeletalMeshData method getVertexNormals.

/**
 * Provides the normals of all vertices of the mesh, transformed based on the transformation matrices of all bones
 *
 * @param boneTransforms A transformation matrix for each bone in the skeletal mesh
 * @return The normals of each vertex
 */
public List<Vector3f> getVertexNormals(List<Matrix4f> boneTransforms) {
    List<Vector3f> results = Lists.newArrayListWithCapacity(getVertexCount());
    for (int i = 0; i < normals.size(); i++) {
        Vector3f norm = new Vector3f(normals.get(i));
        Matrix4f skinMat = new Matrix4f().m00(0).m11(0).m22(0).m33(0);
        BoneWeight weight = weights.get(i);
        for (int w = 0; w < weight.jointCount(); w++) {
            Matrix4f jointMat = new Matrix4f(boneTransforms.get(weight.getJoint(w)));
            jointMat.scale(weight.getBias(w));
            skinMat.add(jointMat);
        }
        norm.mulTransposePosition(skinMat);
        results.add(norm);
    }
    return results;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f)

Example 18 with Matrix4f

use of org.joml.Matrix4f in project Terasology by MovingBlocks.

the class SkeletalMeshData method getVertexPositions.

/**
 * Provides the positions of all vertices of the mesh, transformed based on the transformation matrices of all
 * bones
 *
 * @param boneTransforms A transformation matrix for each bone in the skeletal mesh
 * @return The positions of each vertex
 */
public List<Vector3f> getVertexPositions(List<Matrix4f> boneTransforms) {
    List<Vector3f> results = Lists.newArrayListWithCapacity(getVertexCount());
    for (int i = 0; i < vertices.size(); i++) {
        Vector3f pos = new Vector3f(vertices.get(i));
        Matrix4f skinMat = new Matrix4f().m00(0).m11(0).m22(0).m33(0);
        BoneWeight weight = weights.get(i);
        for (int w = 0; w < weight.jointCount(); w++) {
            Matrix4f jointMat = new Matrix4f(boneTransforms.get(weight.getJoint(w)));
            jointMat.scale(weight.getBias(w));
            skinMat.add(jointMat);
        }
        pos.mulTransposePosition(skinMat);
        results.add(pos);
    }
    return results;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f)

Example 19 with Matrix4f

use of org.joml.Matrix4f in project Terasology by MovingBlocks.

the class AABBRenderer method prepare.

private void prepare() {
    defaultMaterial.enable();
    Matrix4f modelView = new Matrix4f();
    Camera camera = worldRenderer.getActiveCamera();
    Vector3f center = aabb.center(new Vector3f());
    Vector3f cameraPosition = CoreRegistry.get(LocalPlayer.class).getViewPosition(new Vector3f());
    modelView.set(camera.getViewMatrix()).mul(new Matrix4f().setTranslation(center.x() - cameraPosition.x, center.y() - cameraPosition.y, center.z() - cameraPosition.z));
    defaultMaterial.setMatrix4("modelViewMatrix", modelView);
    defaultMaterial.setMatrix4("projectionMatrix", camera.getProjectionMatrix());
}
Also used : Matrix4f(org.joml.Matrix4f) LocalPlayer(org.terasology.engine.logic.players.LocalPlayer) Vector3f(org.joml.Vector3f) Camera(org.terasology.engine.rendering.cameras.Camera)

Example 20 with Matrix4f

use of org.joml.Matrix4f in project Terasology by MovingBlocks.

the class SphereBuilder method build.

// refrence: https://github.com/caosdoar/spheres/blob/master/src/spheres.cpp
public StandardMeshData build() {
    StandardMeshData meshData = new StandardMeshData();
    Matrix4f mat = new Matrix4f().setRotationXYZ(0, 0, (float) (Math.PI / 2.0f));
    Vector4f pos = new Vector4f();
    Vector3f normal = new Vector3f();
    Vector2f uv0 = new Vector2f();
    Vector3f loc = new Vector3f();
    float s = 0.0f;
    float t = 1.0f;
    float ds = 1.0f / verticalCuts;
    float dt = 1.0f / horizontalCuts;
    for (int j = 0; j <= horizontalCuts; ++j) {
        double polar = (Math.PI * j) / horizontalCuts;
        double sp = Math.sin(polar);
        double cp = Math.cos(polar);
        s = 0.0f;
        for (int i = 0; i <= verticalCuts; ++i) {
            double azimuth = (2.0 * Math.PI * i) / verticalCuts;
            double sa = Math.sin(azimuth);
            double ca = Math.cos(azimuth);
            if (this.normals) {
                normal.set((float) (sp * ca), (float) cp, (float) (sp * sa));
                meshData.normal.put(normal);
            }
            if (this.textured) {
                uv0.set(s, t);
                meshData.uv0.put(uv0);
            }
            s += ds;
            pos.set((float) ((sp * ca) * radius), (float) (cp * radius), (float) ((sp * sa) * radius), 1.0f);
            mat.transform(pos);
            loc.set(pos.x, pos.y, pos.z);
            meshData.position.put(loc);
        }
        t -= dt;
    }
    for (int j = 0; j < horizontalCuts; ++j) {
        int aStart = (j * (verticalCuts + 1));
        int bStart = (j + 1) * (verticalCuts + 1);
        for (int i = 0; i < verticalCuts; ++i) {
            int a = aStart + i;
            int a1 = aStart + ((i + 1) % (verticalCuts + 1));
            int b = bStart + i;
            int b1 = bStart + ((i + 1) % (verticalCuts + 1));
            meshData.indices.putAll(a, b1, b);
            meshData.indices.putAll(a1, b1, a);
        }
    }
    return meshData;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f)

Aggregations

Matrix4f (org.joml.Matrix4f)62 Vector3f (org.joml.Vector3f)34 Quaternionf (org.joml.Quaternionf)13 LocationComponent (org.terasology.engine.logic.location.LocationComponent)7 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)5 Vector3d (org.joml.Vector3d)5 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)5 Shader (io.xol.chunkstories.api.rendering.shader.Shader)4 AABBf (org.terasology.joml.geom.AABBf)4 com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject (com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject)3 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)3 Location (io.xol.chunkstories.api.Location)3 GameWindow (io.xol.chunkstories.api.rendering.GameWindow)3 ByteBuffer (java.nio.ByteBuffer)3 Matrix3f (org.joml.Matrix3f)3 Vector4f (org.joml.Vector4f)3 PointerBuffer (org.lwjgl.PointerBuffer)3 Camera (org.terasology.engine.rendering.cameras.Camera)3 ClosestRayResultCallback (com.badlogic.gdx.physics.bullet.collision.ClosestRayResultCallback)2 TFloatList (gnu.trove.list.TFloatList)2