Search in sources :

Example 21 with Matrix4f

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

the class BoundingBoxRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (config.getRendering().getDebug().isRenderEntityBoundingBoxes()) {
        GL33.glDepthFunc(GL33.GL_ALWAYS);
        Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
        Vector3f worldPos = new Vector3f();
        Vector3f worldPositionCameraSpace = new Vector3f();
        worldPos.sub(cameraPosition, worldPositionCameraSpace);
        Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
        Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
        material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
        material.setMatrix4("modelViewMatrix", modelViewMatrix, true);
        int index = 0;
        meshData.reallocate(0, 0);
        meshData.indices.rewind();
        meshData.position.rewind();
        meshData.color0.rewind();
        Vector3f worldPosition = new Vector3f();
        Quaternionf worldRot = new Quaternionf();
        Matrix4f transform = new Matrix4f();
        AABBf bounds = new AABBf(0, 0, 0, 0, 0, 0);
        for (EntityRef entity : entityManager.getEntitiesWith(LocationComponent.class)) {
            LocationComponent location = entity.getComponent(LocationComponent.class);
            location.getWorldPosition(worldPosition);
            location.getWorldRotation(worldRot);
            BoxShapeComponent boxShapeComponent = entity.getComponent(BoxShapeComponent.class);
            if (boxShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(boxShapeComponent.extents).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            CapsuleShapeComponent capsuleComponent = entity.getComponent(CapsuleShapeComponent.class);
            if (capsuleComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(capsuleComponent.radius, capsuleComponent.height / 2.0f, capsuleComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            CylinderShapeComponent cylinderShapeComponent = entity.getComponent(CylinderShapeComponent.class);
            if (cylinderShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(cylinderShapeComponent.radius, cylinderShapeComponent.height / 2.0f, cylinderShapeComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            SphereShapeComponent sphereShapeComponent = entity.getComponent(SphereShapeComponent.class);
            if (sphereShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(sphereShapeComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
        }
        material.enable();
        mesh.reload(meshData);
        mesh.render();
        GL33.glDepthFunc(GL33.GL_LEQUAL);
    }
}
Also used : Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) SphereShapeComponent(org.terasology.engine.physics.components.shapes.SphereShapeComponent) Vector3f(org.joml.Vector3f) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) Quaternionf(org.joml.Quaternionf) CylinderShapeComponent(org.terasology.engine.physics.components.shapes.CylinderShapeComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) CapsuleShapeComponent(org.terasology.engine.physics.components.shapes.CapsuleShapeComponent)

Example 22 with Matrix4f

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

the class GLTFCommonFormat method loadMat4fList.

protected List<Matrix4f> loadMat4fList(int inverseBindMatrices, GLTF gltf, List<byte[]> loadedBuffers) {
    GLTFAccessor accessor = gltf.getAccessors().get(inverseBindMatrices);
    GLTFBufferView bufferView = gltf.getBufferViews().get(accessor.getBufferView());
    byte[] buffer = loadedBuffers.get(bufferView.getBuffer());
    TFloatList values = new TFloatArrayList();
    readBuffer(buffer, accessor, bufferView, values);
    List<Matrix4f> matricies = Lists.newArrayList();
    for (int i = 0; i < values.size(); i += 16) {
        Matrix4f mat = new Matrix4f(values.get(i), values.get(i + 1), values.get(i + 2), values.get(i + 3), values.get(i + 4), values.get(i + 5), values.get(i + 6), values.get(i + 7), values.get(i + 8), values.get(i + 9), values.get(i + 10), values.get(i + 11), values.get(i + 12), values.get(i + 13), values.get(i + 14), values.get(i + 15));
        matricies.add(mat);
    }
    return matricies;
}
Also used : GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) Matrix4f(org.joml.Matrix4f) TFloatList(gnu.trove.list.TFloatList) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor) TFloatArrayList(gnu.trove.list.array.TFloatArrayList)

Example 23 with Matrix4f

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

the class OpenVRStereoCamera method updateFrustum.

@Override
public void updateFrustum() {
    super.updateFrustum();
    Matrix4f dest = new Matrix4f();
    viewFrustumLeftEye.set(projectionMatrixLeftEye.mul(viewMatrixLeftEye, dest), true);
    viewFrustumRightEye.set(projectionMatrixRightEye.mul(viewMatrixRightEye, dest));
    viewFrustumReflectedLeftEye.set(projectionMatrixLeftEye.mul(viewMatrixReflectedLeftEye, dest), true);
    viewFrustumReflectedRightEye.set(projectionMatrixRightEye.mul(viewMatrixReflectedRightEye, dest), true);
}
Also used : Matrix4f(org.joml.Matrix4f)

Example 24 with Matrix4f

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

the class OpenVRStereoCamera method updateMatrices.

@Override
public void updateMatrices(float fov) {
    prevViewProjectionMatrix.set(viewProjectionMatrix);
    Matrix4f leftEyeProjection = vrProvider.getState().getEyeProjectionMatrix(0);
    Matrix4f rightEyeProjection = vrProvider.getState().getEyeProjectionMatrix(1);
    Matrix4f leftEyePose = vrProvider.getState().getEyePose(0);
    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(quaternion);
    // 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;
    }
    projectionMatrixLeftEye.set(leftEyeProjection);
    projectionMatrixRightEye.set(rightEyeProjection);
    projectionMatrix = projectionMatrixLeftEye;
    viewMatrixLeftEye.set(leftEyePose);
    viewMatrixRightEye.set(rightEyePose);
    viewMatrix = viewMatrixLeftEye;
    normViewMatrix = viewMatrixLeftEye;
    reflectionMatrix.setRow(0, new Vector4f(1.0f, 0.0f, 0.0f, 0.0f));
    reflectionMatrix.setRow(1, new Vector4f(0.0f, -1.0f, 0.0f, 2f * (-position.y + 32f)));
    reflectionMatrix.setRow(2, new Vector4f(0.0f, 0.0f, 1.0f, 0.0f));
    reflectionMatrix.setRow(3, new Vector4f(0.0f, 0.0f, 0.0f, 1.0f));
    viewMatrix.mul(reflectionMatrix, viewMatrixReflected);
    reflectionMatrix.setRow(1, new Vector4f(0.0f, -1.0f, 0.0f, 0.0f));
    normViewMatrix.mul(reflectionMatrix, normViewMatrixReflected);
    viewTranslationLeftEye.identity();
    viewTranslationLeftEye.setTranslation(halfIPD, 0.0f, 0.0f);
    viewTranslationRightEye.identity();
    viewTranslationRightEye.setTranslation(-halfIPD, 0.0f, 0.0f);
    viewTranslationLeftEye.mul(viewMatrixReflected, viewMatrixReflectedLeftEye);
    viewTranslationRightEye.mul(viewMatrixReflected, viewMatrixReflectedRightEye);
    projectionMatrixLeftEye.mul(viewMatrixLeftEye, viewProjectionMatrixLeftEye);
    projectionMatrixRightEye.mul(viewMatrixRightEye, viewProjectionMatrixRightEye);
    viewProjectionMatrixLeftEye.invert(inverseViewProjectionMatrixLeftEye);
    viewProjectionMatrixRightEye.invert(inverseViewProjectionMatrixRightEye);
    projectionMatrixLeftEye.invert(inverseProjectionMatrixLeftEye);
    projectionMatrixRightEye.invert(inverseProjectionMatrixRightEye);
    updateFrustum();
}
Also used : Matrix4f(org.joml.Matrix4f) Vector4f(org.joml.Vector4f) Quaternionf(org.joml.Quaternionf)

Example 25 with Matrix4f

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

the class RegionOutlineRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (entityToRegionOutlineMap.isEmpty()) {
        // skip everything if there is nothing to do to avoid possibly costly draw mode changes
        return;
    }
    GL33.glDepthFunc(GL33.GL_ALWAYS);
    Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Vector3f worldPos = new Vector3f();
    Vector3f worldPositionCameraSpace = new Vector3f();
    worldPos.sub(cameraPosition, worldPositionCameraSpace);
    Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
    Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
    material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
    material.setMatrix4("modelViewMatrix", modelViewMatrix, true);
    meshData.reallocate(0, 0);
    meshData.indices.rewind();
    meshData.position.rewind();
    meshData.color0.rewind();
    int index = 0;
    Vector3f pos = new Vector3f();
    for (RegionOutlineComponent regionOutline : entityToRegionOutlineMap.values()) {
        if (regionOutline.corner1 == null || regionOutline.corner2 == null) {
            continue;
        }
        BlockRegion region = new BlockRegion(regionOutline.corner1).union(regionOutline.corner2);
        AABBf bounds = region.getBounds(new AABBf());
        meshData.position.put(pos.set(bounds.minX, bounds.minY, bounds.minZ));
        meshData.position.put(pos.set(bounds.maxX, bounds.minY, bounds.minZ));
        meshData.position.put(pos.set(bounds.maxX, bounds.minY, bounds.maxZ));
        meshData.position.put(pos.set(bounds.minX, bounds.minY, bounds.maxZ));
        meshData.position.put(pos.set(bounds.minX, bounds.maxY, bounds.minZ));
        meshData.position.put(pos.set(bounds.maxX, bounds.maxY, bounds.minZ));
        meshData.position.put(pos.set(bounds.maxX, bounds.maxY, bounds.maxZ));
        meshData.position.put(pos.set(bounds.minX, bounds.maxY, bounds.maxZ));
        meshData.color0.put(regionOutline.color);
        meshData.color0.put(regionOutline.color);
        meshData.color0.put(regionOutline.color);
        meshData.color0.put(regionOutline.color);
        meshData.color0.put(regionOutline.color);
        meshData.color0.put(regionOutline.color);
        meshData.color0.put(regionOutline.color);
        meshData.color0.put(regionOutline.color);
        meshData.indices.putAll(new int[] { // top loop
        index, index + 1, index + 1, index + 2, index + 2, index + 3, index + 3, index, // connecting edges between top and bottom
        index, index + 4, index + 1, index + 5, index + 2, index + 6, index + 3, index + 7, // bottom loop
        index + 4, index + 5, index + 5, index + 6, index + 6, index + 7, index + 7, index + 4 });
        index += 8;
    }
    material.enable();
    mesh.reload(meshData);
    mesh.render();
    GL33.glDepthFunc(GL33.GL_LEQUAL);
}
Also used : Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) BlockRegion(org.terasology.engine.world.block.BlockRegion)

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