Search in sources :

Example 1 with Matrix4f

use of org.terasology.math.geom.Matrix4f in project Terasology by MovingBlocks.

the class MeshRenderer method renderEntitiesByMaterial.

private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
    Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Quat4f worldRot = new Quat4f();
    Vector3f worldPos = new Vector3f();
    FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
    FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
    for (Material material : meshByMaterial.keySet()) {
        if (material.isRenderable()) {
            OpenGLMesh lastMesh = null;
            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 || !isRelevant(entity, location.getWorldPosition())) {
                    continue;
                }
                if (meshComp.mesh.isDisposed()) {
                    logger.error("Attempted to render disposed mesh");
                    continue;
                }
                location.getWorldRotation(worldRot);
                location.getWorldPosition(worldPos);
                float worldScale = location.getWorldScale();
                Transform toWorldSpace = new Transform(worldPos, worldRot, worldScale);
                Vector3f offsetFromCamera = new Vector3f();
                offsetFromCamera.sub(worldPos, cameraPosition);
                Matrix4f matrixCameraSpace = new Matrix4f(worldRot, offsetFromCamera, worldScale);
                AABB aabb = meshComp.mesh.getAABB().transform(toWorldSpace);
                if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
                    if (meshComp.mesh != lastMesh) {
                        if (lastMesh != null) {
                            lastMesh.postRender();
                        }
                        lastMesh = (OpenGLMesh) meshComp.mesh;
                        lastMesh.preRender();
                    }
                    Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
                    MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
                    MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
                    material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
                    material.setMatrix4("worldViewMatrix", 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);
                    lastMesh.doRender();
                }
            }
            if (lastMesh != null) {
                lastMesh.postRender();
            }
        }
    }
}
Also used : OpenGLMesh(org.terasology.rendering.opengl.OpenGLMesh) FloatBuffer(java.nio.FloatBuffer) Material(org.terasology.rendering.assets.material.Material) LocationComponent(org.terasology.logic.location.LocationComponent) Matrix4f(org.terasology.math.geom.Matrix4f) Vector3f(org.terasology.math.geom.Vector3f) Transform(org.terasology.math.Transform) EntityRef(org.terasology.entitySystem.entity.EntityRef) Quat4f(org.terasology.math.geom.Quat4f) AABB(org.terasology.math.AABB)

Example 2 with Matrix4f

use of org.terasology.math.geom.Matrix4f in project Terasology by MovingBlocks.

the class SkeletonRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (config.getRendering().getDebug().isRenderSkeletons()) {
        glDisable(GL_DEPTH_TEST);
        Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
        Material material = Assets.getMaterial("engine:white").get();
        material.setFloat("sunlight", 1.0f, true);
        material.setFloat("blockLight", 1.0f, true);
        material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
        glLineWidth(2);
        Vector3f worldPos = new Vector3f();
        FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
        FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
        for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
            LocationComponent location = entity.getComponent(LocationComponent.class);
            location.getWorldPosition(worldPos);
            Vector3f worldPositionCameraSpace = new Vector3f();
            worldPositionCameraSpace.sub(worldPos, cameraPosition);
            float worldScale = location.getWorldScale();
            Matrix4f matrixCameraSpace = new Matrix4f(new Quat4f(0, 0, 0, 1), worldPositionCameraSpace, worldScale);
            Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
            MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
            material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true);
            MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
            material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
            SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
            renderBone(skeletalMesh.rootBone, worldPos);
        }
        glEnable(GL_DEPTH_TEST);
    }
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) Vector3f(org.terasology.math.geom.Vector3f) BaseVector3f(org.terasology.math.geom.BaseVector3f) Material(org.terasology.rendering.assets.material.Material) FloatBuffer(java.nio.FloatBuffer) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) BaseQuat4f(org.terasology.math.geom.BaseQuat4f) Quat4f(org.terasology.math.geom.Quat4f)

Example 3 with Matrix4f

use of org.terasology.math.geom.Matrix4f in project Terasology by MovingBlocks.

the class MatrixUtils method createPerspectiveProjectionMatrix.

public static Matrix4f createPerspectiveProjectionMatrix(float fovY, float aspectRatio, float zNear, float zFar) {
    Matrix4f m = new Matrix4f();
    float f = 1.0f / (float) Math.tan(fovY * 0.5f);
    m.m00 = f / aspectRatio;
    m.m10 = 0;
    m.m20 = 0;
    m.m30 = 0;
    m.m01 = 0;
    m.m11 = f;
    m.m21 = 0;
    m.m31 = 0;
    m.m02 = 0;
    m.m12 = 0;
    m.m22 = (zFar + zNear) / (zNear - zFar);
    m.m32 = (2 * zFar * zNear) / (zNear - zFar);
    m.m03 = 0;
    m.m13 = 0;
    m.m23 = -1;
    m.m33 = 0;
    m.transpose();
    return m;
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f)

Example 4 with Matrix4f

use of org.terasology.math.geom.Matrix4f in project Terasology by MovingBlocks.

the class MatrixUtils method createOrthogonalProjectionMatrix.

public static Matrix4f createOrthogonalProjectionMatrix(float left, float right, float top, float bottom, float near, float far) {
    Matrix4f m = new Matrix4f();
    float lateral = right - left;
    float vertical = top - bottom;
    float forward = far - near;
    float tx = -(right + left) / (right - left);
    float ty = -(top + bottom) / (top - bottom);
    float tz = -(far + near) / (far - near);
    m.m00 = 2.0f / lateral;
    m.m10 = 0.0f;
    m.m20 = 0.0f;
    m.m30 = tx;
    m.m01 = 0.0f;
    m.m11 = 2.0f / vertical;
    m.m21 = 0.0f;
    m.m31 = ty;
    m.m02 = 0.0f;
    m.m12 = 0.0f;
    m.m22 = -2.0f / forward;
    m.m32 = tz;
    m.m03 = 0.0f;
    m.m13 = 0.0f;
    m.m23 = 0.0f;
    m.m33 = 1.0f;
    m.transpose();
    return m;
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f)

Example 5 with Matrix4f

use of org.terasology.math.geom.Matrix4f in project Terasology by MovingBlocks.

the class ColladaLoader method quad4fArrayFromFloat16ArrayData.

private Quat4f[] quad4fArrayFromFloat16ArrayData(float[] inverseBindMatrixArray) {
    Quat4f[] rotationArray = new Quat4f[inverseBindMatrixArray.length / 16];
    for (int i = 0; i < inverseBindMatrixArray.length / 16; ++i) {
        int offset = i * 16;
        Matrix4f matrix4f = new Matrix4f(Arrays.copyOfRange(inverseBindMatrixArray, offset, offset + 16));
        Quat4f rotation = new Quat4f();
        rotation.set(matrix4f);
        rotationArray[i] = rotation;
    }
    return rotationArray;
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) Quat4f(org.terasology.math.geom.Quat4f)

Aggregations

Matrix4f (org.terasology.math.geom.Matrix4f)17 Vector3f (org.terasology.math.geom.Vector3f)12 Quat4f (org.terasology.math.geom.Quat4f)8 FloatBuffer (java.nio.FloatBuffer)4 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 LocationComponent (org.terasology.logic.location.LocationComponent)4 AABB (org.terasology.math.AABB)3 BaseQuat4f (org.terasology.math.geom.BaseQuat4f)2 BaseVector3f (org.terasology.math.geom.BaseVector3f)2 Material (org.terasology.rendering.assets.material.Material)2 CharSequenceIterator (org.terasology.utilities.collection.CharSequenceIterator)2 Quaternionf (org.joml.Quaternionf)1 Vector4f (org.joml.Vector4f)1 LSystemRule (org.terasology.math.LSystemRule)1 Transform (org.terasology.math.Transform)1 MeshAnimation (org.terasology.rendering.assets.animation.MeshAnimation)1 Bone (org.terasology.rendering.assets.skeletalmesh.Bone)1 LightComponent (org.terasology.rendering.logic.LightComponent)1 OpenGLMesh (org.terasology.rendering.opengl.OpenGLMesh)1 OpenGLSkeletalMesh (org.terasology.rendering.opengl.OpenGLSkeletalMesh)1