Search in sources :

Example 26 with Matrix4f

use of org.joml.Matrix4f 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 27 with Matrix4f

use of org.joml.Matrix4f 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 28 with Matrix4f

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

Example 29 with Matrix4f

use of org.joml.Matrix4f in project chunkstories by Hugobros3.

the class AssimpMeshLoader method tomat4.

private Matrix4f tomat4(Mat4 mat4) {
    Matrix4f mat = new Matrix4f();
    mat.m00(mat4.v00());
    mat.m01(mat4.v01());
    mat.m02(mat4.v02());
    mat.m03(mat4.v03());
    mat.m10(mat4.v10());
    mat.m11(mat4.v11());
    mat.m12(mat4.v12());
    mat.m13(mat4.v13());
    mat.m20(mat4.v20());
    mat.m21(mat4.v21());
    mat.m22(mat4.v22());
    mat.m23(mat4.v23());
    mat.m30(mat4.v30());
    mat.m31(mat4.v31());
    mat.m32(mat4.v32());
    mat.m33(mat4.v33());
    mat.transpose();
    return mat;
}
Also used : Matrix4f(org.joml.Matrix4f)

Example 30 with Matrix4f

use of org.joml.Matrix4f in project chunkstories by Hugobros3.

the class NativeAssimpMesh method tomat4.

private Matrix4f tomat4(AIMatrix4x4 m) {
    Matrix4f mat = new Matrix4f();
    mat.m00(m.a1());
    mat.m01(m.a2());
    mat.m02(m.a3());
    mat.m03(m.a4());
    mat.m10(m.b1());
    mat.m11(m.b2());
    mat.m12(m.b3());
    mat.m13(m.b4());
    mat.m20(m.c1());
    mat.m21(m.c2());
    mat.m22(m.c3());
    mat.m23(m.c4());
    mat.m30(m.d1());
    mat.m31(m.d2());
    mat.m32(m.d3());
    mat.m33(m.d4());
    return mat;
}
Also used : Matrix4f(org.joml.Matrix4f)

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