Search in sources :

Example 1 with Vector4f

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

the class TessellatorHelper method addBlockMesh.

public static void addBlockMesh(Tessellator tessellator, Vector4f color, Vector2f[] texOffsets, Vector2f[] texSizes, float size, float light1, float light2, float posX, float posY, float posZ) {
    final float sizeHalf = size / 2;
    tessellator.resetParams();
    tessellator.setColor(new Vector4f(light1 * color.x, light1 * color.y, light1 * color.z, color.w));
    tessellator.setNormal(new Vector3f(0, 1, 0));
    tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[0].x, texOffsets[0].y), new Vector2f(texOffsets[0].x + texSizes[0].x, texOffsets[0].y), new Vector2f(texOffsets[0].x + texSizes[0].x, texOffsets[0].y + texSizes[0].y), new Vector2f(texOffsets[0].x, texOffsets[0].y + texSizes[0].y) });
    tessellator.setNormal(new Vector3f(-1, 0, 0));
    tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[1].x, texOffsets[1].y + texSizes[1].y), new Vector2f(texOffsets[1].x + texSizes[1].x, texOffsets[1].y + texSizes[1].y), new Vector2f(texOffsets[1].x + texSizes[1].x, texOffsets[1].y), new Vector2f(texOffsets[1].x, texOffsets[1].y) });
    tessellator.setNormal(new Vector3f(1, 0, 0));
    tessellator.addPoly(new Vector3f[] { new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[2].x, texOffsets[2].y), new Vector2f(texOffsets[2].x + texSizes[2].x, texOffsets[2].y), new Vector2f(texOffsets[2].x + texSizes[2].x, texOffsets[2].y + texSizes[2].y), new Vector2f(texOffsets[2].x, texOffsets[2].y + texSizes[2].y) });
    tessellator.setColor(new Vector4f(light2 * color.x, light2 * color.y, light2 * color.z, color.w));
    tessellator.setNormal(new Vector3f(0, 0, -1));
    tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[3].x, texOffsets[3].y), new Vector2f(texOffsets[3].x + texSizes[3].x, texOffsets[3].y), new Vector2f(texOffsets[3].x + texSizes[3].x, texOffsets[3].y + texSizes[3].y), new Vector2f(texOffsets[3].x, texOffsets[3].y + texSizes[3].y) });
    tessellator.setNormal(new Vector3f(0, 0, 1));
    tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[4].x, texOffsets[4].y + texSizes[4].y), new Vector2f(texOffsets[4].x + texSizes[4].x, texOffsets[4].y + texSizes[4].y), new Vector2f(texOffsets[4].x + texSizes[4].x, texOffsets[4].y), new Vector2f(texOffsets[4].x, texOffsets[4].y) });
    tessellator.setNormal(new Vector3f(0, -1, 0));
    tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[5].x, texOffsets[5].y), new Vector2f(texOffsets[5].x + texSizes[5].x, texOffsets[5].y), new Vector2f(texOffsets[5].x + texSizes[5].x, texOffsets[5].y + texSizes[5].y), new Vector2f(texOffsets[5].x, texOffsets[5].y + texSizes[5].y) });
}
Also used : Vector4f(org.terasology.math.geom.Vector4f) Vector2f(org.terasology.math.geom.Vector2f) Vector3f(org.terasology.math.geom.Vector3f)

Example 2 with Vector4f

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

the class TessellatorHelper method addGUIQuadMesh.

public static void addGUIQuadMesh(Tessellator tessellator, Vector4f color, float sizeX, float sizeY) {
    tessellator.resetParams();
    tessellator.setColor(new Vector4f(color.x, color.y, color.z, color.w));
    tessellator.setUseLighting(false);
    tessellator.setUseNormals(false);
    tessellator.addPoly(new Vector3f[] { new Vector3f(0, 0, 0), new Vector3f(sizeX, 0, 0), new Vector3f(sizeX, sizeY, 0), new Vector3f(0, sizeY, 0) }, new Vector2f[] { new Vector2f(0, 0), new Vector2f(1, 0), new Vector2f(1, 1), new Vector2f(0, 1) });
    tessellator.setUseLighting(true);
    tessellator.setUseNormals(true);
}
Also used : Vector4f(org.terasology.math.geom.Vector4f) Vector2f(org.terasology.math.geom.Vector2f) Vector3f(org.terasology.math.geom.Vector3f)

Example 3 with Vector4f

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

the class BlockSelectionRenderer method initialize.

private void initialize() {
    Tessellator tessellator = new Tessellator();
    TessellatorHelper.addBlockMesh(tessellator, new Vector4f(1, 1, 1, 1f), textureRegion.min(), textureRegion.size(), 1.001f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
    overlayMesh = tessellator.generateMesh();
    tessellator = new Tessellator();
    TessellatorHelper.addBlockMesh(tessellator, new Vector4f(1, 1, 1, .2f), textureRegion.min(), textureRegion.size(), 1.001f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
    overlayMesh2 = tessellator.generateMesh();
    defaultTextured = Assets.getMaterial("engine:prog.defaultTextured").get();
}
Also used : Tessellator(org.terasology.rendering.primitives.Tessellator) Vector4f(org.terasology.math.geom.Vector4f)

Example 4 with Vector4f

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

the class IconMeshFactory method generateIconMeshData.

public static MeshData generateIconMeshData(TextureRegion tex, int alphaLimit, boolean withContour, Vector4f colorContour) {
    ByteBuffer buffer = tex.getTexture().getData().getBuffers()[0];
    Rect2i pixelRegion = tex.getPixelRegion();
    int posX = pixelRegion.minX();
    int posY = pixelRegion.minY();
    int stride = tex.getTexture().getWidth() * 4;
    float textureSize = Math.max(tex.getWidth(), tex.getHeight());
    Tessellator tessellator = new Tessellator();
    for (int y = 0; y < tex.getHeight(); y++) {
        for (int x = 0; x < tex.getWidth(); x++) {
            int r = buffer.get((posY + y) * stride + (posX + x) * 4) & 255;
            int g = buffer.get((posY + y) * stride + (posX + x) * 4 + 1) & 255;
            int b = buffer.get((posY + y) * stride + (posX + x) * 4 + 2) & 255;
            int a = buffer.get((posY + y) * stride + (posX + x) * 4 + 3) & 255;
            if (a > alphaLimit) {
                Vector4f color = new Vector4f(r / 255f, g / 255f, b / 255f, a / 255f);
                TessellatorHelper.addBlockMesh(tessellator, color, 2f / textureSize, 1.0f, 0.5f, 2f / textureSize * x - 1f, 2f / textureSize * (tex.getHeight() - y - 1) - 1f, 0f);
                if (withContour) {
                    int newX = 0;
                    int newY = 0;
                    int newA = 0;
                    for (int i = 0; i < 4; i++) {
                        newA = alphaLimit + 1;
                        switch(i) {
                            case 0:
                                // check left
                                if (x > 0) {
                                    newX = x - 1;
                                    newY = y;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 1:
                                // check top
                                if (y > 0) {
                                    newX = x;
                                    newY = y - 1;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 2:
                                // check right
                                if (x < 16) {
                                    newX = x + 1;
                                    newY = y;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 3:
                                // check bottom
                                if (y < 16) {
                                    newX = x;
                                    newY = y + 1;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            default:
                                break;
                        }
                        if (newA < alphaLimit) {
                            Vector4f cColor = new Vector4f(colorContour.x / 255f, colorContour.y / 255f, colorContour.z / 255f, colorContour.w);
                            TessellatorHelper.addBlockMesh(tessellator, cColor, 0.125f, 1.0f, 0.5f, 2f * 0.0625f * newX - 0.5f, 0.125f * (15 - newY) - 1f, 0f);
                        }
                    }
                }
            }
        }
    }
    return tessellator.generateMeshData();
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Tessellator(org.terasology.rendering.primitives.Tessellator) Vector4f(org.terasology.math.geom.Vector4f) ByteBuffer(java.nio.ByteBuffer)

Example 5 with Vector4f

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

the class BlockMeshGeneratorSingleShape method generateChunkMesh.

@Override
public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y, int z) {
    Biome selfBiome = view.getBiome(x, y, z);
    Block selfBlock = view.getBlock(x, y, z);
    // TODO: Needs review - too much hardcoded special cases and corner cases resulting from this.
    ChunkVertexFlag vertexFlag = ChunkVertexFlag.NORMAL;
    if (selfBlock.isWater()) {
        if (view.getBlock(x, y + 1, z).isWater()) {
            vertexFlag = ChunkVertexFlag.WATER;
        } else {
            vertexFlag = ChunkVertexFlag.WATER_SURFACE;
        }
    } else if (selfBlock.isLava()) {
        vertexFlag = ChunkVertexFlag.LAVA;
    } else if (selfBlock.isWaving() && selfBlock.isDoubleSided()) {
        vertexFlag = ChunkVertexFlag.WAVING;
    } else if (selfBlock.isWaving()) {
        vertexFlag = ChunkVertexFlag.WAVING_BLOCK;
    }
    // Gather adjacent blocks
    Map<Side, Block> adjacentBlocks = Maps.newEnumMap(Side.class);
    for (Side side : Side.values()) {
        Vector3i offset = side.getVector3i();
        Block blockToCheck = view.getBlock(x + offset.x, y + offset.y, z + offset.z);
        adjacentBlocks.put(side, blockToCheck);
    }
    BlockAppearance blockAppearance = selfBlock.getAppearance(adjacentBlocks);
    /*
         * Determine the render process.
         */
    ChunkMesh.RenderType renderType = ChunkMesh.RenderType.TRANSLUCENT;
    if (!selfBlock.isTranslucent()) {
        renderType = ChunkMesh.RenderType.OPAQUE;
    }
    // TODO: Review special case, or alternatively compare uris.
    if (selfBlock.isWater() || selfBlock.isIce()) {
        renderType = ChunkMesh.RenderType.WATER_AND_ICE;
    }
    if (selfBlock.isDoubleSided()) {
        renderType = ChunkMesh.RenderType.BILLBOARD;
    }
    if (blockAppearance.getPart(BlockPart.CENTER) != null) {
        Vector4f colorOffset = selfBlock.calcColorOffsetFor(BlockPart.CENTER, selfBiome);
        blockAppearance.getPart(BlockPart.CENTER).appendTo(chunkMesh, x, y, z, colorOffset, renderType, vertexFlag);
    }
    boolean[] drawDir = new boolean[6];
    for (Side side : Side.values()) {
        drawDir[side.ordinal()] = blockAppearance.getPart(BlockPart.fromSide(side)) != null && isSideVisibleForBlockTypes(adjacentBlocks.get(side), selfBlock, side);
    }
    // If the selfBlock is lowered, some more faces may have to be drawn
    if (selfBlock.isLiquid()) {
        Block bottomBlock = adjacentBlocks.get(Side.BOTTOM);
        // Draw horizontal sides if visible from below
        for (Side side : Side.horizontalSides()) {
            Vector3i offset = side.getVector3i();
            Block adjacentBelow = view.getBlock(x + offset.x, y - 1, z + offset.z);
            Block adjacent = adjacentBlocks.get(side);
            boolean visible = (blockAppearance.getPart(BlockPart.fromSide(side)) != null && isSideVisibleForBlockTypes(adjacentBelow, selfBlock, side) && !isSideVisibleForBlockTypes(bottomBlock, adjacent, side.reverse()));
            drawDir[side.ordinal()] |= visible;
        }
        // Draw the top if below a non-lowered selfBlock
        // TODO: Don't need to render the top if each side and the selfBlock above each side are either liquid or opaque solids.
        Block blockToCheck = adjacentBlocks.get(Side.TOP);
        drawDir[Side.TOP.ordinal()] |= !blockToCheck.isLiquid();
        if (bottomBlock.isLiquid() || bottomBlock.getMeshGenerator() == null) {
            for (Side dir : Side.values()) {
                if (drawDir[dir.ordinal()]) {
                    Vector4f colorOffset = selfBlock.calcColorOffsetFor(BlockPart.fromSide(dir), selfBiome);
                    selfBlock.getLoweredLiquidMesh(dir).appendTo(chunkMesh, x, y, z, colorOffset, renderType, vertexFlag);
                }
            }
            return;
        }
    }
    for (Side dir : Side.values()) {
        if (drawDir[dir.ordinal()]) {
            Vector4f colorOffset = selfBlock.calcColorOffsetFor(BlockPart.fromSide(dir), selfBiome);
            // TODO: Needs review since the new per-vertex flags introduce a lot of special scenarios - probably a per-side setting?
            if (selfBlock.isGrass() && dir != Side.TOP && dir != Side.BOTTOM) {
                blockAppearance.getPart(BlockPart.fromSide(dir)).appendTo(chunkMesh, x, y, z, colorOffset, renderType, ChunkVertexFlag.COLOR_MASK);
            } else {
                if (blockAppearance.getPart(BlockPart.fromSide(dir)) == null) {
                    // TODO: This would catch something like water blocks attempting to render with a "fixed" trimmedLoweredCube shape
                    // That shape has its top trimmed down a bit to let water sit slightly lower than land, however, underwater this shouldn't show
                    // Normally we would configure that shape with CENTER instead of TOP, that way the trimmed part wouldn't occlude in a stack
                    // But with that handling you don't get water blocks occluding tops underwater... and there's no TOP to retrieve below -> NPE
                    logger.debug("Cannot render side '{}' for a block - no stored block appearance for it. renderType {}, vertexFlag {}", dir, renderType, vertexFlag);
                } else {
                    blockAppearance.getPart(BlockPart.fromSide(dir)).appendTo(chunkMesh, x, y, z, colorOffset, renderType, vertexFlag);
                }
            }
        }
    }
}
Also used : Side(org.terasology.math.Side) Biome(org.terasology.world.biomes.Biome) BlockAppearance(org.terasology.world.block.BlockAppearance) Vector4f(org.terasology.math.geom.Vector4f) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block)

Aggregations

Vector4f (org.terasology.math.geom.Vector4f)8 Vector3f (org.terasology.math.geom.Vector3f)3 Tessellator (org.terasology.rendering.primitives.Tessellator)3 Vector2f (org.terasology.math.geom.Vector2f)2 ByteBuffer (java.nio.ByteBuffer)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 Side (org.terasology.math.Side)1 Rect2i (org.terasology.math.geom.Rect2i)1 Vector3i (org.terasology.math.geom.Vector3i)1 Name (org.terasology.naming.Name)1 NUIManager (org.terasology.rendering.nui.NUIManager)1 NUIManagerInternal (org.terasology.rendering.nui.internal.NUIManagerInternal)1 Biome (org.terasology.world.biomes.Biome)1 Block (org.terasology.world.block.Block)1 BlockAppearance (org.terasology.world.block.BlockAppearance)1