Search in sources :

Example 61 with Vector3f

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

the class ServerCharacterPredictionSystem method onImpulse.

@ReceiveEvent(components = { CharacterMovementComponent.class, LocationComponent.class, AliveCharacterComponent.class })
public void onImpulse(CharacterImpulseEvent event, EntityRef entity) {
    Vector3f impulse = event.getDirection();
    CircularBuffer<CharacterStateEvent> stateBuffer = characterStates.get(entity);
    CharacterStateEvent lastState = stateBuffer.getLast();
    CharacterStateEvent newState = new CharacterStateEvent(lastState);
    newState.setVelocity(impulse.add(newState.getVelocity()));
    newState.setTime(time.getGameTimeInMs());
    newState.setGrounded(false);
    stateBuffer.add(newState);
    characterMovementSystemUtility.setToState(entity, newState);
}
Also used : Vector3f(org.joml.Vector3f) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 62 with Vector3f

use of org.joml.Vector3f 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)

Example 63 with Vector3f

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

the class SkeletonRenderer method renderOpaque.

@Override
public void renderOpaque() {
    Vector3fc cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Quaternionf worldRot = new Quaternionf();
    Vector3f worldPos = new Vector3f();
    Quaternionf inverseWorldRot = new Quaternionf();
    FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
    FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
    for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
        SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
        if (skeletalMesh.mesh == null || skeletalMesh.material == null || skeletalMesh.boneEntities == null || !skeletalMesh.material.isRenderable()) {
            continue;
        }
        AABBf aabb;
        MeshAnimation animation = skeletalMesh.animation;
        if (animation != null) {
            aabb = animation.getAabb();
        } else {
            aabb = skeletalMesh.mesh.getStaticAabb();
        }
        LocationComponent location = entity.getComponent(LocationComponent.class);
        location.getWorldRotation(worldRot);
        worldRot.invert(inverseWorldRot);
        location.getWorldPosition(worldPos);
        float worldScale = location.getWorldScale();
        aabb = aabb.transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
        // Scale bounding box for skeletalMesh.
        Vector3f scale = skeletalMesh.scale;
        Vector3f aabbCenter = aabb.center(new Vector3f());
        Vector3f scaledExtents = aabb.extent(new Vector3f()).mul(scale.x(), scale.y(), scale.z());
        aabb = new AABBf(aabbCenter, aabbCenter).expand(scaledExtents);
        if (!worldRenderer.getActiveCamera().hasInSight(aabb)) {
            continue;
        }
        skeletalMesh.material.enable();
        skeletalMesh.material.setFloat("sunlight", 1.0f, true);
        skeletalMesh.material.setFloat("blockLight", 1.0f, true);
        skeletalMesh.material.setFloat3("colorOffset", skeletalMesh.color.rf(), skeletalMesh.color.gf(), skeletalMesh.color.bf(), true);
        skeletalMesh.material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
        skeletalMesh.material.bindTextures();
        Vector3f worldPositionCameraSpace = new Vector3f();
        worldPos.sub(cameraPosition, worldPositionCameraSpace);
        worldPositionCameraSpace.y += skeletalMesh.heightOffset;
        Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, worldRot, worldScale);
        Matrix4f modelViewMatrix = worldRenderer.getActiveCamera().getViewMatrix().mul(matrixCameraSpace, new Matrix4f());
        modelViewMatrix.get(tempMatrixBuffer44);
        skeletalMesh.material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
        modelViewMatrix.normal(new Matrix3f()).get(tempMatrixBuffer33);
        skeletalMesh.material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
        skeletalMesh.material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
        skeletalMesh.material.setFloat("blockLight", worldRenderer.getBlockLightIntensityAt(worldPos), true);
        Matrix4f[] boneTransforms = new Matrix4f[skeletalMesh.mesh.getBones().size()];
        for (Bone bone : skeletalMesh.mesh.getBones()) {
            EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
            if (boneEntity == null) {
                boneEntity = EntityRef.NULL;
            }
            LocationComponent boneLocation = boneEntity.getComponent(LocationComponent.class);
            if (boneLocation != null) {
                Matrix4f boneTransform = new Matrix4f();
                boneLocation.getRelativeTransform(boneTransform, entity);
                boneTransform.mul(bone.getInverseBindMatrix());
                boneTransforms[bone.getIndex()] = boneTransform.transpose();
            } else {
                logger.warn("Unable to resolve bone \"{}\"", bone.getName());
                boneTransforms[bone.getIndex()] = new Matrix4f();
            }
        }
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).render(Arrays.asList(boneTransforms));
    }
}
Also used : Quaternionf(org.joml.Quaternionf) FloatBuffer(java.nio.FloatBuffer) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Vector3fc(org.joml.Vector3fc) Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) Matrix3f(org.joml.Matrix3f) OpenGLSkeletalMesh(org.terasology.engine.rendering.opengl.OpenGLSkeletalMesh) Vector3f(org.joml.Vector3f) MeshAnimation(org.terasology.engine.rendering.assets.animation.MeshAnimation) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 64 with Vector3f

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

the class SkeletonRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (config.getRendering().getDebug().isRenderSkeletons()) {
        meshData.reallocate(0, 0);
        meshData.indices.rewind();
        meshData.position.rewind();
        meshData.color0.rewind();
        Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
        Matrix4f relMat = new Matrix4f();
        Matrix4f relFinal = new Matrix4f();
        Matrix4f entityTransform = new Matrix4f();
        Matrix4f result = new Matrix4f();
        Vector3f currentPos = new Vector3f();
        int index = 0;
        for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
            SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
            LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
            if (skeletalMesh.boneEntities == null) {
                continue;
            }
            Vector3f location = locationComponent.getWorldPosition(new Vector3f());
            Quaternionf rotation = locationComponent.getWorldRotation(new Quaternionf());
            // transformation of the entity
            entityTransform.translationRotateScale(location, rotation, 1.0f);
            // position is referenced around (0,0,0) (worldposition - cameraposition)
            Vector3f worldPositionCameraSpace = cameraPosition.negate(new Vector3f());
            // same heightOffset is applied to worldPositionCameraSpace from #renderOpaque()
            // TODO: resolve repeated logic for transformation applied to bones
            worldPositionCameraSpace.y += skeletalMesh.heightOffset;
            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);
            for (Bone bone : skeletalMesh.mesh.getBones()) {
                Bone parentBone = bone.getParent();
                EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
                if (parentBone == null) {
                    continue;
                }
                // TODO: the position of the bone is de-coupled from the actual translation/scale
                EntityRef boneParentEntity = skeletalMesh.boneEntities.get(parentBone.getName());
                LocationComponent locCompA = boneEntity.getComponent(LocationComponent.class);
                LocationComponent locCompB = boneParentEntity.getComponent(LocationComponent.class);
                // need to calculate the relative transformation from the entity to the start of the bone
                locCompA.getRelativeTransform(relMat.identity(), entity);
                // entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
                result.set(entityTransform).mul(relFinal.identity().scale(skeletalMesh.scale).translate(skeletalMesh.translate).mul(relMat)).transformPosition(// get the position of the start of the bone
                currentPos.zero());
                // the start of the bone
                meshData.position.put(currentPos);
                // need to calculate the relative transformation from the entity to the connecting bone
                locCompB.getRelativeTransform(relMat.identity(), entity);
                // entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
                result.set(entityTransform).mul(relFinal.identity().scale(skeletalMesh.scale).translate(skeletalMesh.translate).mul(relMat)).transformPosition(// get the position to the connecting bone
                currentPos.zero());
                // the end of the bone
                meshData.position.put(currentPos);
                meshData.color0.put(Color.white);
                meshData.color0.put(Color.white);
                meshData.indices.putAll(new int[] { index, index + 1 });
                index += 2;
            }
        }
        GL33.glDepthFunc(GL33.GL_ALWAYS);
        material.enable();
        mesh.reload(meshData);
        mesh.render();
        GL33.glDepthFunc(GL33.GL_LEQUAL);
    }
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 65 with Vector3f

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

the class SkeletonRenderer method updateSkeleton.

private void updateSkeleton(SkeletalMeshComponent skeletalMeshComp, MeshAnimationFrame frameA, MeshAnimationFrame frameB, float interpolationVal) {
    for (int i = 0; i < skeletalMeshComp.animation.getBoneCount(); ++i) {
        String boneName = skeletalMeshComp.animation.getBoneName(i);
        Bone bone = skeletalMeshComp.mesh.getBone(boneName);
        EntityRef boneEntity = skeletalMeshComp.boneEntities.get(boneName);
        if (boneEntity == null || bone == null) {
            continue;
        }
        LocationComponent boneLoc = boneEntity.getComponent(LocationComponent.class);
        if (boneLoc != null) {
            Vector3f newPos = frameA.getPosition(i).lerp(frameB.getPosition(i), interpolationVal, new Vector3f());
            boneLoc.setLocalPosition(newPos);
            Quaternionf newRot = frameA.getRotation(i).slerp(frameB.getRotation(i), interpolationVal, new Quaternionf());
            newRot.normalize();
            boneLoc.setLocalRotation(newRot);
            boneLoc.setLocalScale(frameA.getBoneScale(i).lerp(frameB.getBoneScale(i), interpolationVal, new Vector3f()).x);
            boneEntity.saveComponent(boneLoc);
        }
    }
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Aggregations

Vector3f (org.joml.Vector3f)261 LocationComponent (org.terasology.engine.logic.location.LocationComponent)50 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)49 Matrix4f (org.joml.Matrix4f)34 Quaternionf (org.joml.Quaternionf)29 Test (org.junit.jupiter.api.Test)20 Vector3i (org.joml.Vector3i)19 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)18 FloatBuffer (java.nio.FloatBuffer)17 ClientComponent (org.terasology.engine.network.ClientComponent)17 ByteBuffer (java.nio.ByteBuffer)16 Vector3fc (org.joml.Vector3fc)15 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)15 Vector2f (org.joml.Vector2f)13 Vector4f (org.joml.Vector4f)13 ArrayList (java.util.ArrayList)10 HitResult (org.terasology.engine.physics.HitResult)10 IOException (java.io.IOException)8 Test (org.junit.Test)8 VertexResourceBuilder (org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder)8