Search in sources :

Example 1 with Colorc

use of org.terasology.nui.Colorc in project Terasology by MovingBlocks.

the class VertexGLAttributeTest method testColorBinding.

@Test
public void testColorBinding() {
    VertexResourceBuilder builder = new VertexResourceBuilder();
    VertexAttributeBinding<Colorc, Color> a1 = builder.add(0, GLAttributes.COLOR_4_F_VERTEX_ATTRIBUTE);
    VertexResource resource = builder.build();
    a1.put(Color.white);
    a1.put(Color.red);
    a1.put(Color.green);
    a1.put(Color.blue);
    assertEquals(4, a1.getPosition());
    int stride = Float.BYTES * 4;
    resource.writeBuffer(buffer -> {
        assertEquals(4 * 4 * Float.BYTES, buffer.limit());
        assertEquals(1.0f, buffer.getFloat(Float.BYTES * 0), 0.001f);
        assertEquals(1.0f, buffer.getFloat(Float.BYTES * 1), 0.001f);
        assertEquals(1.0f, buffer.getFloat(Float.BYTES * 2), 0.001f);
        assertEquals(1.0f, buffer.getFloat(Float.BYTES * 3), 0.001f);
        assertEquals(1.0f, buffer.getFloat(stride + Float.BYTES * 0));
        assertEquals(0, buffer.getFloat(stride + Float.BYTES * 1));
        assertEquals(0, buffer.getFloat(stride + Float.BYTES * 2));
        assertEquals(1.0f, buffer.getFloat(stride + Float.BYTES * 3));
        assertEquals(0, buffer.getFloat((2 * stride) + Float.BYTES * 0));
        assertEquals(1.0f, buffer.getFloat((2 * stride) + Float.BYTES * 1));
        assertEquals(0, buffer.getFloat((2 * stride) + Float.BYTES * 2));
        assertEquals(1.0f, buffer.getFloat((2 * stride) + Float.BYTES * 3));
        assertEquals(0, buffer.getFloat((3 * stride) + Float.BYTES * 0));
        assertEquals(0, buffer.getFloat((3 * stride) + Float.BYTES * 1));
        assertEquals(1.0f, buffer.getFloat((3 * stride) + Float.BYTES * 2));
        assertEquals(1.0f, buffer.getFloat((3 * stride) + Float.BYTES * 3));
    });
}
Also used : VertexResource(org.terasology.engine.rendering.assets.mesh.resource.VertexResource) VertexResourceBuilder(org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder) Colorc(org.terasology.nui.Colorc) Color(org.terasology.nui.Color) Test(org.junit.Test)

Example 2 with Colorc

use of org.terasology.nui.Colorc in project Terasology by MovingBlocks.

the class BlockMeshGeneratorSingleShape method generateChunkMesh.

@Override
public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y, int z) {
    final BlockAppearance blockAppearance = block.getPrimaryAppearance();
    if (!blockAppearance.hasAppearance()) {
        // perf: Skip mesh generation for blocks without appearance, e.g., air blocks.
        return;
    }
    Color colorCache = new Color();
    // Gather adjacent blocks
    Block[] adjacentBlocks = new Block[Side.allSides().size()];
    for (Side side : Side.allSides()) {
        Vector3ic offset = side.direction();
        Block blockToCheck = view.getBlock(x + offset.x(), y + offset.y(), z + offset.z());
        adjacentBlocks[side.ordinal()] = blockToCheck;
    }
    final ChunkMesh.RenderType renderType = getRenderType(block);
    final ChunkVertexFlag vertexFlag = getChunkVertexFlag(view, x, y, z, block);
    boolean isRendered = false;
    for (final Side side : Side.allSides()) {
        if (isSideVisibleForBlockTypes(adjacentBlocks[side.ordinal()], block, side)) {
            isRendered = true;
            BlockMeshPart blockMeshPart = blockAppearance.getPart(BlockPart.fromSide(side));
            // If the selfBlock isn't lowered, some more faces may have to be drawn
            if (block.isLiquid()) {
                final Block topBlock = adjacentBlocks[Side.TOP.ordinal()];
                // Draw horizontal sides if visible from below
                if (topBlock.isLiquid() && Side.horizontalSides().contains(side)) {
                    final Vector3ic offset = side.direction();
                    final Block adjacentAbove = view.getBlock(x + offset.x(), y + 1, z + offset.z());
                    final Block adjacent = adjacentBlocks[side.ordinal()];
                    if (adjacent.isLiquid() && !adjacentAbove.isLiquid()) {
                        blockMeshPart = block.getTopLiquidMesh(side);
                    }
                } else {
                    if (blockMeshPart != null) {
                        blockMeshPart = block.getLowLiquidMesh(side);
                    }
                }
            }
            if (blockMeshPart != null) {
                // TODO: Needs review since the new per-vertex flags introduce a lot of special scenarios - probably a per-side setting?
                ChunkVertexFlag sideVertexFlag = vertexFlag;
                if (block.isGrass() && side != Side.TOP && side != Side.BOTTOM) {
                    sideVertexFlag = ChunkVertexFlag.COLOR_MASK;
                }
                Colorc colorOffset = block.getColorOffset(BlockPart.fromSide(side));
                Colorc colorSource = block.getColorSource(BlockPart.fromSide(side)).calcColor(view, x, y, z);
                colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
                blockMeshPart.appendTo(chunkMesh, view, x, y, z, renderType, colorCache, sideVertexFlag);
            }
        }
    }
    if (isRendered && blockAppearance.getPart(BlockPart.CENTER) != null) {
        Colorc colorOffset = block.getColorOffset(BlockPart.CENTER);
        Colorc colorSource = block.getColorSource(BlockPart.CENTER).calcColor(view, x, y, z);
        colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
        blockAppearance.getPart(BlockPart.CENTER).appendTo(chunkMesh, view, x, y, z, renderType, colorCache, vertexFlag);
    }
}
Also used : Side(org.terasology.engine.math.Side) Colorc(org.terasology.nui.Colorc) BlockAppearance(org.terasology.engine.world.block.BlockAppearance) Vector3ic(org.joml.Vector3ic) Color(org.terasology.nui.Color) Block(org.terasology.engine.world.block.Block) BlockMeshPart(org.terasology.engine.world.block.shapes.BlockMeshPart)

Example 3 with Colorc

use of org.terasology.nui.Colorc in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawText.

@Override
public void drawText(String text, Font font, HorizontalAlign hAlign, VerticalAlign vAlign, Rectanglei absoluteRegionRectangle, Colorc color, Colorc shadowColor, float alpha, boolean underlined) {
    Rectanglei absoluteRegion = new Rectanglei(absoluteRegionRectangle);
    TextCacheKey key = new TextCacheKey(text, font, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
    usedText.add(key);
    Map<Material, Mesh> fontMesh = cachedText.get(key);
    List<String> lines = TextLineBuilder.getLines(font, text, absoluteRegion.getSizeX());
    if (fontMesh != null) {
        for (Mesh mesh : fontMesh.values()) {
            if (mesh.isDisposed()) {
                fontMesh = null;
                break;
            }
        }
    }
    if (fontMesh == null) {
        fontMesh = fontMeshBuilder.createTextMesh((org.terasology.engine.rendering.assets.font.Font) font, lines, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
        cachedText.put(key, fontMesh);
    }
    Vector2i offset = new Vector2i(absoluteRegion.minX, absoluteRegion.minY);
    offset.y += vAlign.getOffset(lines.size() * font.getLineHeight(), absoluteRegion.lengthY());
    fontMesh.entrySet().stream().filter(entry -> entry.getKey().isRenderable()).forEach(entry -> {
        entry.getKey().bindTextures();
        entry.getKey().setMatrix4("projectionMatrix", projMatrix);
        entry.getKey().setMatrix4("modelViewMatrix", modelMatrixStack);
        entry.getKey().setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
        entry.getKey().setFloat2("offset", offset.x, offset.y);
        entry.getKey().setFloat("alpha", alpha);
        entry.getValue().render();
    });
}
Also used : Quaternionf(org.joml.Quaternionf) GL11.glDisable(org.lwjgl.opengl.GL11.glDisable) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) Vector2ic(org.joml.Vector2ic) DisplayDevice(org.terasology.engine.core.subsystem.DisplayDevice) UITextureRegion(org.terasology.nui.UITextureRegion) Map(java.util.Map) Vector3f(org.joml.Vector3f) GL11(org.lwjgl.opengl.GL11) TeraMath(org.terasology.math.TeraMath) VerticalAlign(org.terasology.nui.VerticalAlign) Quaternionfc(org.joml.Quaternionfc) FrameBufferObject(org.terasology.engine.rendering.opengl.FrameBufferObject) GL_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_SRC_ALPHA) RenderingConfig(org.terasology.engine.config.RenderingConfig) GL_DEPTH_TEST(org.lwjgl.opengl.GL11.GL_DEPTH_TEST) TextLineBuilder(org.terasology.nui.TextLineBuilder) Set(java.util.Set) ScaleMode(org.terasology.nui.ScaleMode) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) MeshBuilder(org.terasology.engine.rendering.assets.mesh.MeshBuilder) List(java.util.List) Context(org.terasology.engine.context.Context) TextureRegion(org.terasology.engine.rendering.assets.texture.TextureRegion) Vector2i(org.joml.Vector2i) Border(org.terasology.nui.Border) PropertyChangeListener(java.beans.PropertyChangeListener) Vector2f(org.joml.Vector2f) Matrix3f(org.joml.Matrix3f) LwjglFrameBufferObject(org.terasology.engine.rendering.opengl.LwjglFrameBufferObject) Matrix4fStack(org.joml.Matrix4fStack) Rectanglef(org.terasology.joml.geom.Rectanglef) Rectanglei(org.terasology.joml.geom.Rectanglei) Colorc(org.terasology.nui.Colorc) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) AABBfc(org.terasology.joml.geom.AABBfc) GL11.glClear(org.lwjgl.opengl.GL11.glClear) Font(org.terasology.nui.asset.font.Font) Matrix4f(org.joml.Matrix4f) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) PropertyChangeEvent(java.beans.PropertyChangeEvent) AssetManager(org.terasology.gestalt.assets.management.AssetManager) GL11.glEnable(org.lwjgl.opengl.GL11.glEnable) Assets(org.terasology.engine.utilities.Assets) Iterator(java.util.Iterator) Maps(com.google.common.collect.Maps) GL11.glBlendFunc(org.lwjgl.opengl.GL11.glBlendFunc) GL_ONE_MINUS_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA) Vector3fc(org.joml.Vector3fc) Config(org.terasology.engine.config.Config) Material(org.terasology.engine.rendering.assets.material.Material) GL_BLEND(org.lwjgl.opengl.GL11.GL_BLEND) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) HorizontalAlign(org.terasology.nui.HorizontalAlign) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Material(org.terasology.engine.rendering.assets.material.Material) Rectanglei(org.terasology.joml.geom.Rectanglei) Font(org.terasology.nui.asset.font.Font) Vector2i(org.joml.Vector2i)

Example 4 with Colorc

use of org.terasology.nui.Colorc in project Terasology by MovingBlocks.

the class VertexGLAttributeTest method testColorPackedBinding.

@Test
public void testColorPackedBinding() {
    VertexResourceBuilder builder = new VertexResourceBuilder();
    VertexAttributeBinding<Colorc, Color> a1 = builder.add(0, GLAttributes.COLOR_4_PACKED_VERTEX_ATTRIBUTE);
    VertexResource resource = builder.build();
    a1.put(Color.white);
    a1.put(Color.red);
    a1.put(Color.green);
    a1.put(Color.blue);
    assertEquals(4, a1.getPosition());
    int stride = Byte.BYTES * 4;
    resource.writeBuffer(buffer -> {
        assertEquals(4 * 4 * Byte.BYTES, buffer.limit());
        assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 0)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 1)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 2)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 3)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 0)));
        assertEquals(0, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 1)));
        assertEquals(0, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 2)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get(stride + Byte.BYTES * 3)));
        assertEquals(0, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 0)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 1)));
        assertEquals(0, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 2)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get((2 * stride) + Byte.BYTES * 3)));
        assertEquals(0, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 0)));
        assertEquals(0, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 1)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 2)));
        assertEquals(255, Byte.toUnsignedInt(buffer.get((3 * stride) + Byte.BYTES * 3)));
    });
}
Also used : VertexResource(org.terasology.engine.rendering.assets.mesh.resource.VertexResource) VertexResourceBuilder(org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder) Colorc(org.terasology.nui.Colorc) Color(org.terasology.nui.Color) Test(org.junit.Test)

Aggregations

Colorc (org.terasology.nui.Colorc)4 Color (org.terasology.nui.Color)3 Test (org.junit.Test)2 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Matrix3f (org.joml.Matrix3f)1 Matrix4f (org.joml.Matrix4f)1 Matrix4fStack (org.joml.Matrix4fStack)1 Quaternionf (org.joml.Quaternionf)1 Quaternionfc (org.joml.Quaternionfc)1 Vector2f (org.joml.Vector2f)1 Vector2i (org.joml.Vector2i)1 Vector2ic (org.joml.Vector2ic)1