Search in sources :

Example 1 with Transform

use of org.terasology.math.Transform 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 Transform

use of org.terasology.math.Transform in project Terasology by MovingBlocks.

the class Block method setCollision.

/**
 * Set the collision box for the block
 *
 * @param offset The offset to the block's center
 * @param shape  The shape of collision box
 */
public void setCollision(Vector3f offset, CollisionShape shape) {
    collisionShape = shape;
    collisionOffset = offset;
    bounds = shape.getAABB(new Transform(offset, new Quat4f(0, 0, 0, 1), 1.0f));
}
Also used : Transform(org.terasology.math.Transform) Quat4f(org.terasology.math.geom.Quat4f)

Aggregations

Transform (org.terasology.math.Transform)2 Quat4f (org.terasology.math.geom.Quat4f)2 FloatBuffer (java.nio.FloatBuffer)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 AABB (org.terasology.math.AABB)1 Matrix4f (org.terasology.math.geom.Matrix4f)1 Vector3f (org.terasology.math.geom.Vector3f)1 Material (org.terasology.rendering.assets.material.Material)1 OpenGLMesh (org.terasology.rendering.opengl.OpenGLMesh)1