Search in sources :

Example 11 with Matrix4f

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

the class LwjglCanvasRenderer method drawMesh.

@Override
public void drawMesh(Mesh mesh, Material material, Rect2i drawRegion, Rect2i cropRegion, Quat4f rotation, Vector3f offset, float scale, float alpha) {
    if (!material.isRenderable()) {
        return;
    }
    AABB meshAABB = mesh.getAABB();
    Vector3f meshExtents = meshAABB.getExtents();
    float fitScale = 0.35f * Math.min(drawRegion.width(), drawRegion.height()) / Math.max(meshExtents.x, Math.max(meshExtents.y, meshExtents.z));
    Vector3f centerOffset = meshAABB.getCenter();
    centerOffset.scale(-1.0f);
    Matrix4f centerTransform = new Matrix4f(BaseQuat4f.IDENTITY, centerOffset, 1.0f);
    Matrix4f userTransform = new Matrix4f(rotation, offset, -fitScale * scale);
    Matrix4f translateTransform = new Matrix4f(BaseQuat4f.IDENTITY, new Vector3f(drawRegion.minX() + drawRegion.width() / 2, drawRegion.minY() + drawRegion.height() / 2, 0), 1);
    userTransform.mul(centerTransform);
    translateTransform.mul(userTransform);
    Matrix4f finalMat = new Matrix4f(modelView);
    finalMat.mul(translateTransform);
    MatrixUtils.matrixToFloatBuffer(finalMat, matrixBuffer);
    material.setFloat4(CROPPING_BOUNDARIES_PARAM, cropRegion.minX(), cropRegion.maxX(), cropRegion.minY(), cropRegion.maxY());
    material.setMatrix4("posMatrix", translateTransform);
    glEnable(GL11.GL_DEPTH_TEST);
    glClear(GL11.GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL11.GL_MODELVIEW);
    glPushMatrix();
    glLoadMatrix(matrixBuffer);
    matrixBuffer.rewind();
    boolean matrixStackSupported = material.supportsFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
    if (matrixStackSupported) {
        material.activateFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
    }
    material.setFloat("alpha", alpha);
    material.bindTextures();
    mesh.render();
    if (matrixStackSupported) {
        material.deactivateFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
    }
    glPopMatrix();
    glDisable(GL11.GL_DEPTH_TEST);
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) Vector3f(org.terasology.math.geom.Vector3f) AABB(org.terasology.math.AABB)

Example 12 with Matrix4f

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

the class OpenVRStereoCamera method updateMatrices.

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

Example 13 with Matrix4f

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

the class DeferredPointLightsNode method process.

/**
 * Iterates over all available point lights and renders them as spheres into the light accumulation buffer.
 *
 * Furthermore, lights that are further from the camera than their set rendering distance are ignored,
 * while lights with a rendering distance set to 0.0 are always considered. However, only lights within
 * the camera's field of view (frustrum) are rendered.
 */
@Override
public void process() {
    PerformanceMonitor.startActivity("rendering/" + getUri());
    lightGeometryMaterial.activateFeature(ShaderProgramFeature.FEATURE_LIGHT_POINT);
    // Specific Shader Parameters
    cameraPosition = activeCamera.getPosition();
    // TODO: This is necessary right now because activateFeature removes all material parameters.
    // TODO: Remove this explicit binding once we get rid of activateFeature, or find a way to retain parameters through it.
    lightGeometryMaterial.setInt("texSceneOpaqueDepth", 0, true);
    lightGeometryMaterial.setInt("texSceneOpaqueNormals", 1, true);
    if (renderingConfig.isDynamicShadows()) {
        if (renderingConfig.isCloudShadows()) {
            lightGeometryMaterial.setFloat("time", worldProvider.getTime().getDays(), true);
            lightGeometryMaterial.setFloat3("cameraPosition", cameraPosition, true);
        }
    }
    if (renderingConfig.isDynamicShadows()) {
        lightGeometryMaterial.setMatrix4("lightViewProjMatrix", lightCamera.getViewProjectionMatrix(), true);
        lightGeometryMaterial.setMatrix4("invViewProjMatrix", activeCamera.getInverseViewProjectionMatrix(), true);
        activeCameraToLightSpace.sub(cameraPosition, lightCamera.getPosition());
        lightGeometryMaterial.setFloat3("activeCameraToLightSpace", activeCameraToLightSpace.x, activeCameraToLightSpace.y, activeCameraToLightSpace.z, true);
    }
    for (EntityRef entity : entityManager.getEntitiesWith(LightComponent.class, LocationComponent.class)) {
        LightComponent lightComponent = entity.getComponent(LightComponent.class);
        if (lightComponent.lightType == LightComponent.LightType.POINT) {
            LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
            final Vector3f lightPositionInTeraCoords = locationComponent.getWorldPosition();
            Vector3f lightPositionRelativeToCamera = new Vector3f();
            lightPositionRelativeToCamera.sub(lightPositionInTeraCoords, activeCamera.getPosition());
            if (lightIsRenderable(lightComponent, lightPositionRelativeToCamera)) {
                lightGeometryMaterial.setCamera(activeCamera);
                // setting shader parameters regarding the light's properties
                lightGeometryMaterial.setFloat3("lightColorDiffuse", lightComponent.lightColorDiffuse.x, lightComponent.lightColorDiffuse.y, lightComponent.lightColorDiffuse.z, true);
                lightGeometryMaterial.setFloat3("lightColorAmbient", lightComponent.lightColorAmbient.x, lightComponent.lightColorAmbient.y, lightComponent.lightColorAmbient.z, true);
                lightGeometryMaterial.setFloat3("lightProperties", lightComponent.lightAmbientIntensity, lightComponent.lightDiffuseIntensity, lightComponent.lightSpecularPower, true);
                lightGeometryMaterial.setFloat4("lightExtendedProperties", lightComponent.lightAttenuationRange, lightComponent.lightAttenuationFalloff, 0.0f, 0.0f, true);
                // setting shader parameters for the light position in camera space
                Vector3f lightPositionInViewSpace = new Vector3f(lightPositionRelativeToCamera);
                activeCamera.getViewMatrix().transformPoint(lightPositionInViewSpace);
                lightGeometryMaterial.setFloat3("lightViewPos", lightPositionInViewSpace.x, lightPositionInViewSpace.y, lightPositionInViewSpace.z, true);
                // set the size and location of the sphere to be rendered via shader parameters
                Matrix4f modelMatrix = new Matrix4f();
                // scales the modelview matrix, effectively scales the light sphere
                modelMatrix.set(lightComponent.lightAttenuationRange);
                // effectively moves the light sphere in the right position relative to camera
                modelMatrix.setTranslation(lightPositionRelativeToCamera);
                lightGeometryMaterial.setMatrix4("modelMatrix", modelMatrix, true);
                // draws the light sphere
                glCallList(lightSphereDisplayList);
            }
        }
    }
    lightGeometryMaterial.deactivateFeature(ShaderProgramFeature.FEATURE_LIGHT_POINT);
    PerformanceMonitor.endActivity();
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) LightComponent(org.terasology.rendering.logic.LightComponent) Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 14 with Matrix4f

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

the class MatrixUtils method calcModelViewMatrix.

public static Matrix4f calcModelViewMatrix(Matrix4f m, Matrix4f vm) {
    Matrix4f result = new Matrix4f();
    result.mul(m, vm);
    return result;
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f)

Example 15 with Matrix4f

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

the class MatrixUtils method createViewMatrix.

public static Matrix4f createViewMatrix(Vector3f eye, Vector3f center, Vector3f up) {
    Matrix4f m = new Matrix4f();
    Vector3f f = new Vector3f();
    f.sub(center, eye);
    f.normalize();
    up.normalize();
    Vector3f s = new Vector3f();
    s.cross(f, up);
    s.normalize();
    Vector3f u = new Vector3f();
    u.cross(s, f);
    u.normalize();
    m.m00 = s.x;
    m.m10 = s.y;
    m.m20 = s.z;
    m.m30 = 0;
    m.m01 = u.x;
    m.m11 = u.y;
    m.m21 = u.z;
    m.m31 = 0;
    m.m02 = -f.x;
    m.m12 = -f.y;
    m.m22 = -f.z;
    m.m32 = 0;
    m.m03 = 0;
    m.m13 = 0;
    m.m23 = 0;
    m.m33 = 1;
    m.m30 = -eye.x;
    m.m31 = -eye.y;
    m.m32 = -eye.z;
    m.transpose();
    return m;
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) Vector3f(org.terasology.math.geom.Vector3f)

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