Search in sources :

Example 1 with Material

use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.

the class FloatingTextRenderer method render.

private void render(Iterable<EntityRef> floatingTextEntities) {
    Vector3f cameraPosition = camera.getPosition();
    for (EntityRef entity : floatingTextEntities) {
        LocationComponent location = entity.getComponent(LocationComponent.class);
        if (location == null) {
            continue;
        }
        Vector3f worldPos = location.getWorldPosition();
        if (!worldProvider.isBlockRelevant(worldPos)) {
            continue;
        }
        FloatingTextComponent floatingText = entity.getComponent(FloatingTextComponent.class);
        String[] linesOfText = floatingText.text.split("\n");
        Color baseColor = floatingText.textColor;
        Color shadowColor = floatingText.textShadowColor;
        boolean underline = false;
        int textWidth = 0;
        for (String singleLine : linesOfText) {
            if (font.getWidth(singleLine) > textWidth) {
                textWidth = font.getWidth(singleLine);
            }
        }
        FontMeshBuilder meshBuilder = new FontMeshBuilder(underlineMaterial);
        Map<Material, Mesh> meshMap = entityMeshCache.get(entity);
        if (meshMap == null) {
            meshMap = meshBuilder.createTextMesh(font, Arrays.asList(linesOfText), textWidth, HorizontalAlign.CENTER, baseColor, shadowColor, underline);
            entityMeshCache.put(entity, meshMap);
        }
        if (floatingText.isOverlay) {
            glDisable(GL_DEPTH_TEST);
        }
        glPushMatrix();
        float scale = METER_PER_PIXEL * floatingText.scale;
        glTranslated(worldPos.x - cameraPosition.x, worldPos.y - cameraPosition.y, worldPos.z - cameraPosition.z);
        OpenGLUtils.applyBillboardOrientation();
        glScaled(scale, -scale, scale);
        glTranslated(-textWidth / 2.0, 0.0, 0.0);
        for (Map.Entry<Material, Mesh> meshMapEntry : meshMap.entrySet()) {
            Mesh mesh = meshMapEntry.getValue();
            Material material = meshMapEntry.getKey();
            material.enable();
            material.bindTextures();
            material.setFloat4("croppingBoundaries", Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE);
            material.setFloat2("offset", 0.0f, 0.0f);
            material.setFloat("alpha", 1.0f);
            mesh.render();
        }
        glPopMatrix();
        // Revert to default state
        if (floatingText.isOverlay) {
            glEnable(GL_DEPTH_TEST);
        }
    }
}
Also used : Color(org.terasology.rendering.nui.Color) Mesh(org.terasology.rendering.assets.mesh.Mesh) Material(org.terasology.rendering.assets.material.Material) LocationComponent(org.terasology.logic.location.LocationComponent) FontMeshBuilder(org.terasology.rendering.assets.font.FontMeshBuilder) Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 2 with Material

use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.

the class FloatingTextRenderer method diposeMeshMap.

private void diposeMeshMap(Map<Material, Mesh> meshMap) {
    for (Map.Entry<Material, Mesh> meshMapEntry : meshMap.entrySet()) {
        Mesh mesh = meshMapEntry.getValue();
        mesh.dispose();
    // Note: Material belongs to font and must not be disposed
    }
}
Also used : Mesh(org.terasology.rendering.assets.mesh.Mesh) Material(org.terasology.rendering.assets.material.Material) Map(java.util.Map)

Example 3 with Material

use of org.terasology.rendering.assets.material.Material 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 4 with Material

use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.

the class MeshRenderer method renderEntities.

private void renderEntities(Iterable<EntityRef> entityRefs) {
    SetMultimap<Material, EntityRef> entitiesToRender = HashMultimap.create();
    for (EntityRef entity : entityRefs) {
        MeshComponent meshComponent = entity.getComponent(MeshComponent.class);
        if (meshComponent != null && meshComponent.material != null) {
            entitiesToRender.put(meshComponent.material, entity);
        }
    }
    renderEntitiesByMaterial(entitiesToRender);
}
Also used : Material(org.terasology.rendering.assets.material.Material) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 5 with Material

use of org.terasology.rendering.assets.material.Material 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)

Aggregations

Material (org.terasology.rendering.assets.material.Material)15 Mesh (org.terasology.rendering.assets.mesh.Mesh)7 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 Vector3f (org.terasology.math.geom.Vector3f)4 MaterialData (org.terasology.rendering.assets.material.MaterialData)4 Shader (org.terasology.rendering.assets.shader.Shader)4 IOException (java.io.IOException)3 AssetFactory (org.terasology.assets.AssetFactory)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 AssetManager (org.terasology.assets.management.AssetManager)3 ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)3 Context (org.terasology.context.Context)3 LocationComponent (org.terasology.logic.location.LocationComponent)3 MeshAnimation (org.terasology.rendering.assets.animation.MeshAnimation)3 MeshAnimationData (org.terasology.rendering.assets.animation.MeshAnimationData)3 MeshAnimationImpl (org.terasology.rendering.assets.animation.MeshAnimationImpl)3 Atlas (org.terasology.rendering.assets.atlas.Atlas)3 AtlasData (org.terasology.rendering.assets.atlas.AtlasData)3 Font (org.terasology.rendering.assets.font.Font)3 FloatBuffer (java.nio.FloatBuffer)2