Search in sources :

Example 11 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent 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 12 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent 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 13 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent 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)

Example 14 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent 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 15 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class MeshRenderer method renderEntitiesByMaterial.

private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
    Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Quaternionf worldRot = new Quaternionf();
    Vector3f worldPos = new Vector3f();
    Matrix3f normalMatrix = new Matrix3f();
    Matrix4f matrixCameraSpace = new Matrix4f();
    Matrix4f modelViewMatrix = new Matrix4f();
    FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
    FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
    for (Material material : meshByMaterial.keySet()) {
        if (material.isRenderable()) {
            material.enable();
            material.setFloat("sunlight", 1.0f, true);
            material.setFloat("blockLight", 1.0f, true);
            material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
            material.bindTextures();
            Set<EntityRef> entities = meshByMaterial.get(material);
            lastRendered = entities.size();
            for (EntityRef entity : entities) {
                MeshComponent meshComp = entity.getComponent(MeshComponent.class);
                LocationComponent location = entity.getComponent(LocationComponent.class);
                if (isHidden(entity, meshComp) || location == null || meshComp.mesh == null) {
                    continue;
                }
                Vector3f worldPosition = location.getWorldPosition(new Vector3f());
                if (!worldPosition.isFinite() && !isRelevant(entity, worldPosition)) {
                    continue;
                }
                if (meshComp.mesh.isDisposed()) {
                    logger.error("Attempted to render disposed mesh");
                    continue;
                }
                worldRot.set(location.getWorldRotation(new Quaternionf()));
                worldPos.set(location.getWorldPosition(new Vector3f()));
                float worldScale = location.getWorldScale();
                Vector3f offsetFromCamera = worldPos.sub(cameraPosition, new Vector3f());
                matrixCameraSpace.translationRotateScale(offsetFromCamera, worldRot, worldScale);
                AABBf aabb = meshComp.mesh.getAABB().transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
                if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
                    modelViewMatrix.set(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
                    modelViewMatrix.get(tempMatrixBuffer44);
                    modelViewMatrix.normal(normalMatrix).get(tempMatrixBuffer33);
                    material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
                    material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
                    material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
                    material.setFloat3("colorOffset", meshComp.color.rf(), meshComp.color.gf(), meshComp.color.bf(), true);
                    material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
                    material.setFloat("blockLight", Math.max(worldRenderer.getBlockLightIntensityAt(worldPos), meshComp.selfLuminance), true);
                    meshComp.mesh.render();
                }
            }
        }
    }
}
Also used : Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) Matrix3f(org.joml.Matrix3f) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) FloatBuffer(java.nio.FloatBuffer) Material(org.terasology.engine.rendering.assets.material.Material) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Aggregations

LocationComponent (org.terasology.engine.logic.location.LocationComponent)65 Vector3f (org.joml.Vector3f)46 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)40 Quaternionf (org.joml.Quaternionf)18 ClientComponent (org.terasology.engine.network.ClientComponent)17 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)9 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)9 Matrix4f (org.joml.Matrix4f)7 Vector3i (org.joml.Vector3i)6 CharacterTeleportEvent (org.terasology.engine.logic.characters.CharacterTeleportEvent)5 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)5 Vector3fc (org.joml.Vector3fc)4 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)4 CharacterHeldItemComponent (org.terasology.engine.logic.characters.CharacterHeldItemComponent)4 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)4 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)4 DropItemEvent (org.terasology.engine.logic.inventory.events.DropItemEvent)4 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)4 BlockItemFactory (org.terasology.engine.world.block.items.BlockItemFactory)4 Component (org.terasology.gestalt.entitysystem.component.Component)4