Search in sources :

Example 16 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class HUDScreenLayer method onDraw.

@Override
public void onDraw(Canvas canvas) {
    for (HUDElement element : elementsLookup.values()) {
        int minX = TeraMath.floorToInt(element.region.minX() * canvas.size().x);
        int minY = TeraMath.floorToInt(element.region.minY() * canvas.size().y);
        int sizeX = TeraMath.floorToInt(element.region.getSizeX() * canvas.size().x);
        int sizeY = TeraMath.floorToInt(element.region.getSizeY() * canvas.size().y);
        Rectanglei region = new Rectanglei(minX, minY).setSize(sizeX, sizeY);
        canvas.drawWidget(element.widget, region);
    }
}
Also used : Rectanglei(org.terasology.joml.geom.Rectanglei)

Example 17 with Rectanglei

use of org.terasology.joml.geom.Rectanglei 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 18 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawTexture.

@Override
public void drawTexture(UITextureRegion texture, Colorc color, ScaleMode mode, Rectanglei absoluteRegionRectangle, float ux, float uy, float uw, float uh, float alpha) {
    if (!((TextureRegion) texture).getTexture().isLoaded()) {
        return;
    }
    Rectanglei absoluteRegion = new Rectanglei(absoluteRegionRectangle);
    if (!currentTextureCropRegion.equals(requestedCropRegion) && !(currentTextureCropRegion.containsRectangle(absoluteRegion) && requestedCropRegion.containsRectangle(absoluteRegion))) {
        textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
        currentTextureCropRegion = requestedCropRegion;
    }
    Vector2f scale = mode.scaleForRegion(absoluteRegionRectangle, texture.getWidth(), texture.getHeight());
    Rectanglef textureArea = new Rectanglef(texture.getRegion());
    Mesh mesh = billboard;
    switch(mode) {
        case TILED:
            {
                Vector2i textureSize = texture.size();
                TextureCacheKey key = new TextureCacheKey(textureSize, new Vector2i(absoluteRegion.getSizeX(), absoluteRegion.getSizeY()));
                usedTextures.add(key);
                mesh = cachedTextures.get(key);
                if (mesh == null || mesh.isDisposed()) {
                    MeshBuilder builder = new MeshBuilder();
                    addTiles(builder, absoluteRegion, FULL_REGION, textureSize, FULL_REGION);
                    mesh = builder.build();
                    cachedTextures.put(key, mesh);
                }
                textureMat.setFloat2("scale", scale);
                textureMat.setFloat2("offset", absoluteRegion.minX, absoluteRegion.minY);
                textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.getSizeX(), textureArea.minY + uy * textureArea.getSizeY());
                textureMat.setFloat2("texSize", uw * textureArea.getSizeX(), uh * textureArea.getSizeY());
                break;
            }
        case SCALE_FILL:
            {
                textureMat.setFloat2("offset", absoluteRegion.minX, absoluteRegion.minY);
                textureMat.setFloat2("scale", absoluteRegion.getSizeX(), absoluteRegion.getSizeY());
                float texBorderX = (scale.x - absoluteRegion.getSizeX()) / scale.x * uw;
                float texBorderY = (scale.y - absoluteRegion.getSizeY()) / scale.y * uh;
                textureMat.setFloat2("texOffset", textureArea.minX + (ux + 0.5f * texBorderX) * textureArea.getSizeX(), textureArea.minY + (uy + 0.5f * texBorderY) * textureArea.getSizeY());
                textureMat.setFloat2("texSize", (uw - texBorderX) * textureArea.getSizeX(), (uh - texBorderY) * textureArea.getSizeY());
                break;
            }
        default:
            {
                textureMat.setFloat2("scale", scale);
                textureMat.setFloat2("offset", absoluteRegion.minX + 0.5f * (absoluteRegion.getSizeX() - scale.x), absoluteRegion.minY + 0.5f * (absoluteRegion.getSizeY() - scale.y));
                textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.getSizeX(), textureArea.minY + uy * textureArea.getSizeY());
                textureMat.setFloat2("texSize", uw * textureArea.getSizeX(), uh * textureArea.getSizeY());
                break;
            }
    }
    textureMat.setTexture("tex", ((TextureRegion) texture).getTexture());
    textureMat.setFloat4("color", color.rf(), color.gf(), color.bf(), color.af() * alpha);
    textureMat.setMatrix4("projectionMatrix", projMatrix);
    textureMat.setMatrix4("modelViewMatrix", modelMatrixStack);
    textureMat.bindTextures();
    mesh.render();
}
Also used : UITextureRegion(org.terasology.nui.UITextureRegion) TextureRegion(org.terasology.engine.rendering.assets.texture.TextureRegion) Vector2f(org.joml.Vector2f) Rectanglef(org.terasology.joml.geom.Rectanglef) MeshBuilder(org.terasology.engine.rendering.assets.mesh.MeshBuilder) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Rectanglei(org.terasology.joml.geom.Rectanglei) Vector2i(org.joml.Vector2i)

Example 19 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawTextureBordered.

@Override
public void drawTextureBordered(UITextureRegion texture, Rectanglei regionRectangle, Border border, boolean tile, float ux, float uy, float uw, float uh, float alpha) {
    if (!((TextureRegion) texture).getTexture().isLoaded()) {
        return;
    }
    Rectanglei region = new Rectanglei(regionRectangle);
    if (!currentTextureCropRegion.equals(requestedCropRegion) && !(currentTextureCropRegion.containsRectangle(region) && requestedCropRegion.containsRectangle(region))) {
        textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
        currentTextureCropRegion = requestedCropRegion;
    }
    Vector2i textureSize = new Vector2i(TeraMath.ceilToInt(texture.getWidth() * uw), TeraMath.ceilToInt(texture.getHeight() * uh));
    TextureCacheKey key = new TextureCacheKey(textureSize, new Vector2i(region.getSizeX(), region.getSizeY()), border, tile);
    usedTextures.add(key);
    Mesh mesh = cachedTextures.get(key);
    if (mesh == null || mesh.isDisposed()) {
        MeshBuilder builder = new MeshBuilder();
        float topTex = (float) border.getTop() / textureSize.y;
        float leftTex = (float) border.getLeft() / textureSize.x;
        float bottomTex = 1f - (float) border.getBottom() / textureSize.y;
        float rightTex = 1f - (float) border.getRight() / textureSize.x;
        int centerHoriz = region.getSizeX() - border.getTotalWidth();
        int centerVert = region.getSizeY() - border.getTotalHeight();
        float top = (float) border.getTop() / region.getSizeY();
        float left = (float) border.getLeft() / region.getSizeX();
        float bottom = 1f - (float) border.getBottom() / region.getSizeY();
        float right = 1f - (float) border.getRight() / region.getSizeX();
        if (border.getTop() != 0) {
            if (border.getLeft() != 0) {
                addRectPoly(builder, 0, 0, left, top, 0, 0, leftTex, topTex);
            }
            if (tile) {
                addTiles(builder, new Rectanglei(border.getLeft(), 0).setSize(centerHoriz, border.getTop()), new Rectanglef(left, 0, right, top), new Vector2i(textureSize.x - border.getTotalWidth(), border.getTop()), new Rectanglef(leftTex, 0, rightTex, topTex));
            } else {
                addRectPoly(builder, left, 0, right, top, leftTex, 0, rightTex, topTex);
            }
            if (border.getRight() != 0) {
                addRectPoly(builder, right, 0, 1, top, rightTex, 0, 1, topTex);
            }
        }
        if (border.getLeft() != 0) {
            if (tile) {
                addTiles(builder, new Rectanglei(0, border.getTop()).setSize(border.getLeft(), centerVert), new Rectanglef(0, top, left, bottom), new Vector2i(border.getLeft(), textureSize.y - border.getTotalHeight()), new Rectanglef(0, topTex, leftTex, bottomTex));
            } else {
                addRectPoly(builder, 0, top, left, bottom, 0, topTex, leftTex, bottomTex);
            }
        }
        if (tile) {
            addTiles(builder, new Rectanglei(border.getLeft(), border.getTop()).setSize(centerHoriz, centerVert), new Rectanglef(left, top, right, bottom), new Vector2i(textureSize.x - border.getTotalWidth(), textureSize.y - border.getTotalHeight()), new Rectanglef(leftTex, topTex, rightTex, bottomTex));
        } else {
            addRectPoly(builder, left, top, right, bottom, leftTex, topTex, rightTex, bottomTex);
        }
        if (border.getRight() != 0) {
            if (tile) {
                addTiles(builder, new Rectanglei(region.getSizeX() - border.getRight(), border.getTop()).setSize(border.getRight(), centerVert), new Rectanglef(right, top, 1, bottom), new Vector2i(border.getRight(), textureSize.y - border.getTotalHeight()), new Rectanglef(rightTex, topTex, 1, bottomTex));
            } else {
                addRectPoly(builder, right, top, 1, bottom, rightTex, topTex, 1, bottomTex);
            }
        }
        if (border.getBottom() != 0) {
            if (border.getLeft() != 0) {
                addRectPoly(builder, 0, bottom, left, 1, 0, bottomTex, leftTex, 1);
            }
            if (tile) {
                addTiles(builder, new Rectanglei(border.getLeft(), region.getSizeY() - border.getBottom()).setSize(centerHoriz, border.getBottom()), new Rectanglef(left, bottom, right, 1), new Vector2i(textureSize.x - border.getTotalWidth(), border.getBottom()), new Rectanglef(leftTex, bottomTex, rightTex, 1));
            } else {
                addRectPoly(builder, left, bottom, right, 1, leftTex, bottomTex, rightTex, 1);
            }
            if (border.getRight() != 0) {
                addRectPoly(builder, right, bottom, 1, 1, rightTex, bottomTex, 1, 1);
            }
        }
        mesh = builder.build();
        cachedTextures.put(key, mesh);
    }
    textureMat.setFloat2("scale", region.getSizeX(), region.getSizeY());
    textureMat.setFloat2("offset", region.minX, region.minY);
    Rectanglef textureArea = texture.getRegion();
    textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.lengthX(), textureArea.minY + uy * textureArea.lengthY());
    textureMat.setFloat2("texSize", uw * textureArea.lengthX(), uh * textureArea.lengthY());
    textureMat.setTexture("tex", ((TextureRegion) texture).getTexture());
    textureMat.setFloat4("color", 1, 1, 1, alpha);
    textureMat.bindTextures();
    mesh.render();
}
Also used : UITextureRegion(org.terasology.nui.UITextureRegion) TextureRegion(org.terasology.engine.rendering.assets.texture.TextureRegion) MeshBuilder(org.terasology.engine.rendering.assets.mesh.MeshBuilder) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) Rectanglef(org.terasology.joml.geom.Rectanglef) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Rectanglei(org.terasology.joml.geom.Rectanglei) Vector2i(org.joml.Vector2i)

Example 20 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method preRender.

@Override
public void preRender() {
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // TODO: figure out previous call to viewport scaling is handled
    // changing resolution scaling affect viewport of LWJGLCanvas causing strange scaling artifact.
    glViewport(0, 0, displayDevice.getWidth(), displayDevice.getHeight());
    projMatrix.setOrtho(0, displayDevice.getWidth(), displayDevice.getHeight(), 0, 0, 2048f);
    modelMatrixStack.pushMatrix();
    modelMatrixStack.set(new Matrix4f().setTranslation(0, 0, -1024));
    modelMatrixStack.scale(uiScale, uiScale, uiScale);
    requestedCropRegion = new Rectanglei(0, 0, displayDevice.getWidth(), displayDevice.getHeight());
    currentTextureCropRegion = requestedCropRegion;
    textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
}
Also used : Matrix4f(org.joml.Matrix4f) Rectanglei(org.terasology.joml.geom.Rectanglei)

Aggregations

Rectanglei (org.terasology.joml.geom.Rectanglei)24 Vector2i (org.joml.Vector2i)9 Vector2f (org.joml.Vector2f)6 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.jupiter.api.Test)3 FontMeshBuilder (org.terasology.engine.rendering.assets.font.FontMeshBuilder)3 Mesh (org.terasology.engine.rendering.assets.mesh.Mesh)3 MeshBuilder (org.terasology.engine.rendering.assets.mesh.MeshBuilder)3 TextureRegion (org.terasology.engine.rendering.assets.texture.TextureRegion)3 Rectanglef (org.terasology.joml.geom.Rectanglef)3 UITextureRegion (org.terasology.nui.UITextureRegion)3 BufferedImage (java.awt.image.BufferedImage)2 Matrix4f (org.joml.Matrix4f)2 Vector2ic (org.joml.Vector2ic)2 Color (org.terasology.nui.Color)2 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 TIntList (gnu.trove.list.TIntList)1 Graphics2D (java.awt.Graphics2D)1 DataBufferInt (java.awt.image.DataBufferInt)1